teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * 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; andre@8124: import java.util.Collection; 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; andre@8124: import org.dive4elements.river.artifacts.model.minfo.SedimentLoadDataFactory; andre@8124: import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData; andre@8124: import org.dive4elements.river.artifacts.model.minfo.SedimentLoadData.Load; rrenkert@4297: felix@6726: /** Service delivering info about sediment loads. */ rrenkert@4297: public class SedimentLoadInfoService teichmann@5868: extends D4EService rrenkert@4297: { teichmann@8202: /** The log used in this service. */ teichmann@8202: private static Logger log = 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: felix@6726: /** felix@6726: * Create document with sedimentload infos, felix@6726: * constrained by contents in data. felix@6726: */ 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); felix@6726: double fromD, toD; rrenkert@4297: try { felix@6726: fromD = Double.parseDouble(from); felix@6726: toD = Double.parseDouble(to); rrenkert@4297: } rrenkert@4297: catch (NumberFormatException nfe) { teichmann@8202: log.warn("Invalid locations. Cannot return sediment loads."); rrenkert@4297: return XMLUtils.newDocument(); rrenkert@4297: } rrenkert@4297: andre@8124: /* This call initializes the sedimentloaddata for the river. Might be andre@8124: * expensive but has to be done anyway for the calculation later on. */ andre@8124: SedimentLoadData allLoadData = SedimentLoadDataFactory.INSTANCE.getSedimentLoadData( andre@8124: river); andre@8124: andre@8124: Collection loads = allLoadData.findLoads(fromD, toD); andre@8124: rrenkert@4297: return buildDocument(loads); rrenkert@4297: } rrenkert@4297: andre@8124: protected Document buildDocument(Collection loads) { rrenkert@4297: Document result = XMLUtils.newDocument(); rrenkert@4297: Element all = result.createElement("sedimentloads"); andre@8124: for (Load load : loads) { andre@8124: Element ele = result.createElement("sedimentload"); andre@8124: ele.setAttribute("description", load.getDescription()); andre@8124: if (load.isEpoch()) { rrenkert@4297: Calendar calendarS = Calendar.getInstance(); andre@8124: calendarS.setTime(load.getStartTime()); rrenkert@4297: Calendar calendarE = Calendar.getInstance(); andre@8124: calendarE.setTime(load.getStopTime()); andre@8124: ele.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(); andre@8124: calendar.setTime(load.getStartTime()); andre@8124: ele.setAttribute( teichmann@4736: "date", rrenkert@4297: String.valueOf(calendar.get(Calendar.YEAR))); rrenkert@4297: } andre@8124: all.appendChild(ele); rrenkert@4297: } rrenkert@4297: result.appendChild(all); rrenkert@4297: return result; rrenkert@4297: } rrenkert@4297: } felix@6726: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :