view artifacts/src/main/java/org/dive4elements/river/artifacts/services/RiverInfoService.java @ 8870:c26fb37899ca

Introduced groups for modules. Modules marked with the same group-id, will be put together in the ui. Also using now the localization info from the server instead of localizing the modules again on the client side.
author gernotbelger
date Wed, 07 Feb 2018 11:59:13 +0100
parents c4ce25093953
children 0a5239a1e46e
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.math.BigDecimal;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.dive4elements.artifacts.CallMeta;
import org.dive4elements.artifacts.GlobalContext;
import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
import org.dive4elements.artifacts.common.utils.XMLUtils;

import org.dive4elements.river.artifacts.model.RiverFactory;
import org.dive4elements.river.model.River;

/**
 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
 */
public class RiverInfoService extends D4EService {

    private static final Logger log = Logger.getLogger(
            RiverInfoService.class);

    protected static final String RIVER_XPATH = "/art:river/text()";

    protected XMLUtils.ElementCreator ec;
    protected River river;
    protected Element riverele;

    @Override
    protected Document doProcess(
        Document      data,
        GlobalContext globalContext,
        CallMeta      callMeta
    ) {
        String rivername = XMLUtils.xpathString(
            data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE);

        river = RiverFactory.getRiver(rivername);

        Document result = XMLUtils.newDocument();

        if (river == null) {
            log.warn("No river with name " + rivername + " found.");
            return null;
        }

        ec = new XMLUtils.ElementCreator(
            result,
            ArtifactNamespaceContext.NAMESPACE_URI,
            ArtifactNamespaceContext.NAMESPACE_PREFIX);

        riverele = ec.create("river-info");

        double[] minmax  = river.determineMinMaxDistance();
        double[] minmaxq = river.determineMinMaxQ();
        Long offnumber = river.getOfficialNumber();

        Element r = ec.create("river");
        ec.addAttr(r, "name", river.getName(), true);
        ec.addAttr(r, "start",
            minmax != null ? Double.toString(minmax[0]) : "", true);
        ec.addAttr(r, "end",
            minmax != null ? Double.toString(minmax[1]) : "", true);
        ec.addAttr(r, "wstunit", river.getWstUnit().getName(), true);
        ec.addAttr(r, "kmup", Boolean.toString(river.getKmUp()), true);
        ec.addAttr(r, "minq", Double.toString(minmaxq[0]), true);
        ec.addAttr(r, "maxq", Double.toString(minmaxq[1]), true);
        ec.addAttr(r, "official",
            offnumber != null ? Long.toString(offnumber) : "", true);
        ec.addAttr(r, "model-uuid", river.getModelUuid(), true);

        riverele.appendChild(r);
        result.appendChild(riverele);

        return result;
    }

    /**
     * Returns a Double as String from a BigDecimal value.
     *
     * If value is null an empty String is returned.
     */
    protected static String getStringValue(BigDecimal value) {
        return value != null
            ? Double.toString(value.doubleValue()) : "";
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org