teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5863: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5863: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.services; rrenkert@4297: rrenkert@4297: import java.util.Calendar; rrenkert@4297: rrenkert@4297: import org.apache.log4j.Logger; rrenkert@4297: import org.w3c.dom.Document; rrenkert@4297: import org.w3c.dom.Element; rrenkert@4297: teichmann@5831: import org.dive4elements.artifacts.ArtifactNamespaceContext; teichmann@5831: import org.dive4elements.artifacts.CallMeta; teichmann@5831: import org.dive4elements.artifacts.GlobalContext; teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.SedimentLoad; teichmann@5831: import org.dive4elements.river.artifacts.model.minfo.SedimentLoadFactory; rrenkert@4297: rrenkert@4297: rrenkert@4297: public class SedimentLoadInfoService rrenkert@4297: extends FLYSService rrenkert@4297: { rrenkert@4297: /** The logger used in this service. */ rrenkert@4297: private static Logger logger = Logger.getLogger(SedimentLoadInfoService.class); rrenkert@4297: rrenkert@4297: public static final String RIVER_XPATH = "/art:river/text()"; rrenkert@4297: public static final String TYPE_XPATH = "/art:river/art:type/text()"; rrenkert@4297: public static final String FROM_XPATH = "/art:river/art:location/art:from/text()"; rrenkert@4297: public static final String TO_XPATH = "/art:river/art:location/art:to/text()"; rrenkert@4297: rrenkert@4297: @Override rrenkert@4297: protected Document doProcess( rrenkert@4297: Document data, rrenkert@4297: GlobalContext globalContext, rrenkert@4297: CallMeta callMeta) { rrenkert@4297: String river = XMLUtils.xpathString( rrenkert@4297: data, rrenkert@4297: RIVER_XPATH, rrenkert@4297: ArtifactNamespaceContext.INSTANCE); rrenkert@4297: String type = XMLUtils.xpathString( rrenkert@4297: data, rrenkert@4297: TYPE_XPATH, rrenkert@4297: ArtifactNamespaceContext.INSTANCE); rrenkert@4297: String from = XMLUtils.xpathString( rrenkert@4297: data, rrenkert@4297: FROM_XPATH, rrenkert@4297: ArtifactNamespaceContext.INSTANCE); rrenkert@4297: String to = XMLUtils.xpathString( rrenkert@4297: data, rrenkert@4297: TO_XPATH, rrenkert@4297: ArtifactNamespaceContext.INSTANCE); rrenkert@4297: double f, t; rrenkert@4297: try { rrenkert@4297: f = Double.parseDouble(from); rrenkert@4297: t = Double.parseDouble(to); rrenkert@4297: } rrenkert@4297: catch (NumberFormatException nfe) { rrenkert@4297: logger.warn("Invalid locations. Cannot return sediment loads."); rrenkert@4297: return XMLUtils.newDocument(); rrenkert@4297: } rrenkert@4297: rrenkert@4297: SedimentLoad[] loads = SedimentLoadFactory.getLoads(river, type, f, t); rrenkert@4297: return buildDocument(loads); rrenkert@4297: } rrenkert@4297: rrenkert@4297: protected Document buildDocument(SedimentLoad[] loads) { rrenkert@4297: Document result = XMLUtils.newDocument(); rrenkert@4297: Element all = result.createElement("sedimentloads"); rrenkert@4297: for (SedimentLoad sl : loads) { rrenkert@4297: Element load = result.createElement("sedimentload"); rrenkert@4297: load.setAttribute("description", sl.getDescription()); rrenkert@4297: if (sl.isEpoch()) { rrenkert@4297: Calendar calendarS = Calendar.getInstance(); rrenkert@4297: calendarS.setTime(sl.getStart()); rrenkert@4297: Calendar calendarE = Calendar.getInstance(); rrenkert@4297: calendarE.setTime(sl.getEnd()); rrenkert@4297: load.setAttribute( rrenkert@4297: "date", rrenkert@4297: calendarS.get(Calendar.YEAR) + rrenkert@4297: " - " + rrenkert@4297: calendarE.get(Calendar.YEAR)); rrenkert@4297: } rrenkert@4297: else { rrenkert@4297: Calendar calendar = Calendar.getInstance(); rrenkert@4297: calendar.setTime(sl.getStart()); rrenkert@4297: load.setAttribute( teichmann@4736: "date", rrenkert@4297: String.valueOf(calendar.get(Calendar.YEAR))); rrenkert@4297: } rrenkert@4297: all.appendChild(load); rrenkert@4297: } rrenkert@4297: result.appendChild(all); rrenkert@4297: return result; rrenkert@4297: } rrenkert@4297: }