view artifacts/src/main/java/org/dive4elements/river/artifacts/services/AbstractDynamicMainValuesService.java @ 9415:9744ce3c3853

Rework of fixanalysis computation and dWt and WQ facets. Got rid of strange remapping and bitshifting code by explicitely saving the column information and using it in the facets. The facets also put the valid station range into their xml-metadata
author gernotbelger
date Thu, 16 Aug 2018 16:27:53 +0200
parents bc9a45d2b1fa
children
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.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.River;
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>
 */
abstract public class AbstractDynamicMainValuesService extends AbstractMainValuesService {

    private static final long serialVersionUID = 1L;

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

            final River river = getRequestedRiver(data, "/art:mainvalues/art:river/text()");
            final Gauge gauge = getRequestedGauge(data, river);
            final Date startTime = getRequestedStartDate(data, "/art:mainvalues/art:startDate/text()");
            final Date endTime = getRequestedEndDate(data, "/art:mainvalues/art:endDate/text()");

            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());
        }
        catch (final Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }

    public static final Date getRequestedStartDate(final Document data, final String XPATH_END_YEAR) throws MainValuesServiceException {

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

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

        try {
            return new Date(Long.parseLong(startStr));
            // FIXME: timezone? probably must match timezone of database
        }
        catch (final NumberFormatException e) {
            e.printStackTrace();
            throw new MainValuesServiceException("invalid start date");
        }
    }

    public static final Date getRequestedEndDate(final Document data, final String XPATH_END_DATE) throws MainValuesServiceException {

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

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

        try {
            return new Date(Long.parseLong(endStr));
            // FIXME: timezone? probably must match timezone of database
        }
        catch (final NumberFormatException e) {
            e.printStackTrace();
            throw new MainValuesServiceException("invalid end date");
        }
    }

    /**
     * Computes a gauge's main values for a period of time based on its daily discharges stored in the database
     */
    protected abstract List<MainValue> getMainValues(final River river, final Gauge gauge, final Date startTime, final Date endTime)
            throws MainValuesServiceException;

}

http://dive4elements.wald.intevation.org