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; raimund@2276: ingo@4173: import java.util.Collections; raimund@2276: import java.util.List; raimund@2276: import java.util.Date; raimund@2276: raimund@2276: import org.apache.log4j.Logger; raimund@2276: raimund@2276: import org.w3c.dom.Document; raimund@2276: import org.w3c.dom.Element; raimund@2276: teichmann@5831: import org.dive4elements.artifacts.CallMeta; teichmann@5831: import org.dive4elements.artifacts.GlobalContext; raimund@2276: teichmann@5831: import org.dive4elements.artifacts.common.ArtifactNamespaceContext; teichmann@5831: import org.dive4elements.artifacts.common.utils.XMLUtils; raimund@2276: teichmann@5831: import org.dive4elements.river.artifacts.model.DischargeTables; teichmann@5831: import org.dive4elements.river.model.Gauge; teichmann@5831: import org.dive4elements.river.model.DischargeTable; teichmann@5831: import org.dive4elements.river.model.TimeInterval; raimund@2276: raimund@2276: /** raimund@2276: * This service provides information about discharges at a defined gauge. raimund@2276: * raimund@2276: * @author Raimund Renkert raimund@2276: */ teichmann@5868: public class DischargeInfoService extends D4EService { raimund@2276: teichmann@8202: /** The log used in this service. */ teichmann@8202: private static Logger log = Logger.getLogger(DischargeInfoService.class); raimund@2276: raimund@2276: public static final String GAUGE_XPATH = "/art:gauge/text()"; raimund@2276: aheinecke@6839: public static final String RIVER_NAME_XPATH = "/art:gauge/art:river/text()"; aheinecke@6839: raimund@2276: public DischargeInfoService() { raimund@2276: } raimund@2276: raimund@2276: raimund@2276: @Override raimund@2276: public Document doProcess( raimund@2276: Document data, raimund@2276: GlobalContext globalContext, raimund@2276: CallMeta callMeta raimund@2276: ) { teichmann@8202: if (log.isDebugEnabled()) { teichmann@8202: log.debug("DischargeInfoService.process"); teichmann@8202: log.debug(XMLUtils.toString(data)); teichmann@5916: } raimund@2276: raimund@2276: String gaugeNumber = XMLUtils.xpathString( raimund@2276: data, GAUGE_XPATH, ArtifactNamespaceContext.INSTANCE); raimund@2276: aheinecke@6839: String river = XMLUtils.xpathString( aheinecke@6839: data, RIVER_NAME_XPATH, ArtifactNamespaceContext.INSTANCE); aheinecke@6839: teichmann@5916: if (gaugeNumber == null || raimund@2276: (gaugeNumber = gaugeNumber.trim()).length() == 0) { teichmann@8202: log.warn("No gauge specified. Cannot return discharge info."); raimund@2276: return XMLUtils.newDocument(); raimund@2276: } raimund@2276: teichmann@8202: log.debug("Getting discharge for gauge: " + gaugeNumber + " at river: " + river); raimund@2276: raimund@2276: long gn; raimund@2276: try { raimund@2276: gn = Long.parseLong(gaugeNumber); raimund@2276: } raimund@2276: catch (NumberFormatException nfe) { teichmann@8202: log.warn("Invalid gauge number. Cannot return discharge info."); raimund@2276: return XMLUtils.newDocument(); raimund@2276: } raimund@2276: aheinecke@6839: Gauge gauge; aheinecke@6839: if (river == null || river.isEmpty()) { aheinecke@6839: gauge = Gauge.getGaugeByOfficialNumber(gn); aheinecke@6839: } else { aheinecke@6839: gauge = Gauge.getGaugeByOfficialNumber(gn, river); aheinecke@6839: } aheinecke@6839: teichmann@5916: if (gauge == null) { teichmann@8202: log.warn("No such gauge found."); teichmann@5916: return XMLUtils.newDocument(); teichmann@5916: } raimund@2276: teichmann@8202: log.debug("Found gauge: " + gauge.getName() + " id: " + gauge.getId()); raimund@2276: raimund@2276: return buildDocument(gauge); raimund@2276: } raimund@2276: raimund@2276: raimund@2276: protected Document buildDocument(Gauge gauge) { raimund@2276: Document result = XMLUtils.newDocument(); raimund@2276: teichmann@5905: List tables = gauge.getDischargeTables(); felix@4433: Collections.sort(tables); raimund@2276: teichmann@8202: log.debug("# of tables:" + tables.size()); teichmann@5916: raimund@2276: Element all = result.createElement("discharges"); raimund@2276: for (DischargeTable dt: tables) { teichmann@5905: if (dt.getKind() == DischargeTables.MASTER) { teichmann@5905: continue; teichmann@5905: } raimund@2276: Element discharge = result.createElement("discharge"); raimund@2276: discharge.setAttribute("description", dt.getDescription()); raimund@2276: raimund@2276: // Get time interval. raimund@2276: TimeInterval ti = dt.getTimeInterval(); ingo@2598: ingo@2598: if (ti == null) { teichmann@8202: log.warn("DischargeTable has no TimeInterval set!"); ingo@2598: continue; ingo@2598: } ingo@2598: raimund@2276: Date startTime = ti.getStartTime(); raimund@2276: Date stopTime = ti.getStopTime(); raimund@2276: raimund@2276: if (startTime != null) { ingo@4115: discharge.setAttribute("start", String.valueOf(startTime.getTime())); raimund@2276: } raimund@2276: else { ingo@4173: continue; raimund@2276: } ingo@4115: teichmann@5905: if (stopTime != null) { ingo@4115: discharge.setAttribute("end", String.valueOf(stopTime.getTime())); raimund@2276: } teichmann@5905: else { ingo@4173: long now = System.currentTimeMillis(); ingo@4173: discharge.setAttribute("end", String.valueOf(now)); ingo@4173: } raimund@2276: teichmann@5910: String bfgId = dt.getBfgId(); teichmann@5910: if (bfgId != null) { teichmann@5910: discharge.setAttribute("bfg-id", bfgId); teichmann@5910: } teichmann@5910: raimund@2276: all.appendChild(discharge); raimund@2276: } raimund@2276: result.appendChild(all); raimund@2276: return result; raimund@2276: } raimund@2276: } sascha@3083: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :