Mercurial > dive4elements > river
diff artifacts/src/main/java/org/dive4elements/river/artifacts/services/AbstractDynamicMainValuesService.java @ 9404:bc9a45d2b1fa
common time range for gauges incl. error messages
author | gernotbelger |
---|---|
date | Wed, 15 Aug 2018 13:59:09 +0200 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/services/AbstractDynamicMainValuesService.java Wed Aug 15 13:59:09 2018 +0200 @@ -0,0 +1,96 @@ +/* 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; + +} \ No newline at end of file