view artifacts/src/main/java/org/dive4elements/river/artifacts/services/SedimentLoadInfoService.java @ 8124:2c21fd1ade39

(issue 1448) Use new data model for sedimentloadinfo This probably returns a bit too much of data. So we might need to reduce this later on.
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 25 Aug 2014 12:18:35 +0200
parents 963ede7b32bb
children e4606eae8ea5
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.artifacts.services;

import java.util.Calendar;
import java.util.Collection;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.dive4elements.artifacts.ArtifactNamespaceContext;
import org.dive4elements.artifacts.CallMeta;
import org.dive4elements.artifacts.GlobalContext;
import org.dive4elements.artifacts.common.utils.XMLUtils;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadDataFactory;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData;
import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData.Load;

/** Service delivering info about sediment loads. */
public class SedimentLoadInfoService
extends D4EService
{
    /** The logger used in this service. */
    private static Logger logger = Logger.getLogger(SedimentLoadInfoService.class);

    public static final String RIVER_XPATH = "/art:river/text()";
    public static final String TYPE_XPATH = "/art:river/art:type/text()";
    public static final String FROM_XPATH = "/art:river/art:location/art:from/text()";
    public static final String TO_XPATH = "/art:river/art:location/art:to/text()";

    /**
     * Create document with sedimentload infos,
     * constrained by contents in data.
     */
    @Override
    protected Document doProcess(
        Document data,
        GlobalContext globalContext,
        CallMeta callMeta) {
        String river = XMLUtils.xpathString(
            data,
            RIVER_XPATH,
            ArtifactNamespaceContext.INSTANCE);
        String type = XMLUtils.xpathString(
            data,
            TYPE_XPATH,
            ArtifactNamespaceContext.INSTANCE);
        String from = XMLUtils.xpathString(
            data,
            FROM_XPATH,
            ArtifactNamespaceContext.INSTANCE);
        String to = XMLUtils.xpathString(
            data,
            TO_XPATH,
            ArtifactNamespaceContext.INSTANCE);
        double fromD, toD;
        try {
            fromD = Double.parseDouble(from);
            toD = Double.parseDouble(to);
        }
        catch (NumberFormatException nfe) {
            logger.warn("Invalid locations. Cannot return sediment loads.");
            return XMLUtils.newDocument();
        }

        /* This call initializes the sedimentloaddata for the river. Might be
         * expensive but has to be done anyway for the calculation later on. */
        SedimentLoadData allLoadData = SedimentLoadDataFactory.INSTANCE.getSedimentLoadData(
                river);

        Collection <Load> loads = allLoadData.findLoads(fromD, toD);

        return buildDocument(loads);
    }

    protected Document buildDocument(Collection<Load> loads) {
        Document result = XMLUtils.newDocument();
        Element all = result.createElement("sedimentloads");
        for (Load load : loads) {
            Element ele = result.createElement("sedimentload");
            ele.setAttribute("description", load.getDescription());
            if (load.isEpoch()) {
                Calendar calendarS = Calendar.getInstance();
                calendarS.setTime(load.getStartTime());
                Calendar calendarE = Calendar.getInstance();
                calendarE.setTime(load.getStopTime());
                ele.setAttribute(
                    "date",
                    calendarS.get(Calendar.YEAR) +
                        " - " +
                        calendarE.get(Calendar.YEAR));
            }
            else {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(load.getStartTime());
                ele.setAttribute(
                    "date",
                    String.valueOf(calendar.get(Calendar.YEAR)));
            }
            all.appendChild(ele);
        }
        result.appendChild(all);
        return result;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org