view artifacts/src/main/java/org/dive4elements/river/artifacts/services/DynamicMainValuesService.java @ 9288:82c67b859aa7

bundu.bezugswst worklflow incl. service impl for mainValues to be calculated
author gernotbelger
date Tue, 24 Jul 2018 10:39:03 +0200
parents
children e4a6679b868f
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 java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

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.model.Gauge;
import org.dive4elements.river.model.MainValue;
import org.dive4elements.river.model.MainValueType;
import org.dive4elements.river.model.MainValueType.MainValueTypeKey;
import org.dive4elements.river.model.NamedMainValue;
import org.dive4elements.river.model.OfficialLine;
import org.dive4elements.river.model.River;
import org.dive4elements.river.model.TimeInterval;
import org.w3c.dom.Document;

/**
 * This service returns the main values of a river's gauge based on the start
 * and end point of the river.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class DynamicMainValuesService extends AbstractMainValuesService {

    private static final long serialVersionUID = 1L;

    private static final String XPATH_START_YEAR = "/art:mainvalues/art:startYear/text()";

    private static final String XPATH_END_YEAR = "/art:mainvalues/art:endYear/text()";

    @Override
    public Document doProcess(final Document data, final GlobalContext context, final CallMeta callMeta) {
        try {

            final River river = getRequestedRiver(data);
            final Gauge gauge = getRequestedGauge(data, river);
            final Date startTime = getRequestedStartYear(data);
            final Date endTime = getRequestedEndYear(data);

            final List<MainValue> mainValues = getMainValues(river, gauge, startTime, endTime);

            return buildDocument(river, gauge, mainValues, context);
        }
        catch (final MainValuesServiceException e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    private Date getRequestedStartYear(final Document data) throws MainValuesServiceException {

        final String startStr = XMLUtils.xpathString(data, XPATH_START_YEAR, ArtifactNamespaceContext.INSTANCE);

        if (startStr == null)
            throw new MainValuesServiceException("no start year");

        try {
            final int year = Integer.parseInt(startStr);

            // FIXME: timezone? probably must match timezone of database
            final Calendar cal = Calendar.getInstance();
            cal.clear();
            cal.set(year, 0, 1);
            return cal.getTime();
        }
        catch (final NumberFormatException e) {
            e.printStackTrace();
            throw new MainValuesServiceException("invalid start year");
        }
    }

    private Date getRequestedEndYear(final Document data) throws MainValuesServiceException {

        final String endStr = XMLUtils.xpathString(data, XPATH_END_YEAR, ArtifactNamespaceContext.INSTANCE);

        if (endStr == null)
            throw new MainValuesServiceException("no end year");

        try {
            final int year = Integer.parseInt(endStr);

            // FIXME: timezone? probably must match timezone of database
            final Calendar cal = Calendar.getInstance();
            cal.clear();
            cal.set(year, 11, 31);
            return cal.getTime();
        }
        catch (final NumberFormatException e) {
            e.printStackTrace();
            throw new MainValuesServiceException("invalid end year");
        }
    }

    /**
     * This method creates the result document that includes the main values of
     * the specified <i>gauge</i>.
     *
     * @param river
     *            The river.
     * @param gauge
     *            The gauge.
     * @param endYear
     * @param startYear
     *
     * @return a document that includes the main values of the specified river
     *         at the specified gauge.
     */
    protected List<MainValue> getMainValues(final River river, final Gauge gauge, final Date startTime, final Date endTime) {

        // TODO: compute our own main values from the discharge timeseries.

        // final List<MainValue> mainValues = gauge.getMainValues();
        final List<MainValue> mainValues = new ArrayList<>();

        final MainValue myMain = new MainValue();

        // TODO: fetch real NamedMainValue from database: GLQ20, MNQ, MQ, MHQ, HQ5 + Dauerzahlen
        final NamedMainValue mainValue = new NamedMainValue("Testname", new MainValueType(MainValueTypeKey.Q.getName()));
        mainValue.setOfficialLines(new ArrayList<OfficialLine>());

        myMain.setMainValue(mainValue);
        // FIXME: compute value
        myMain.setValue(new BigDecimal("1234.567"));

        final TimeInterval timeInterval = new TimeInterval(startTime, endTime);
        myMain.setTimeInterval(timeInterval);

        mainValues.add(myMain);

        return mainValues;
    }
}

http://dive4elements.wald.intevation.org