raimund@2276: package de.intevation.flys.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:
raimund@2276: import de.intevation.artifacts.CallMeta;
raimund@2276: import de.intevation.artifacts.GlobalContext;
raimund@2276:
raimund@2276: import de.intevation.artifacts.common.ArtifactNamespaceContext;
raimund@2276: import de.intevation.artifacts.common.utils.XMLUtils;
raimund@2276:
ingo@4173: import de.intevation.flys.artifacts.model.DischargeTables;
raimund@2276: import de.intevation.flys.model.Gauge;
raimund@2276: import de.intevation.flys.model.DischargeTable;
raimund@2276: import de.intevation.flys.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: */
raimund@2276: public class DischargeInfoService extends FLYSService {
raimund@2276:
raimund@2276: /** The logger used in this service. */
raimund@2276: private static Logger logger = Logger.getLogger(DischargeInfoService.class);
raimund@2276:
raimund@2276: public static final String GAUGE_XPATH = "/art:gauge/text()";
raimund@2276:
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: ) {
raimund@2276: logger.debug("DischargeInfoService.process");
ingo@4115: logger.debug(XMLUtils.toString(data));
raimund@2276:
raimund@2276: String gaugeNumber = XMLUtils.xpathString(
raimund@2276: data, GAUGE_XPATH, ArtifactNamespaceContext.INSTANCE);
raimund@2276:
raimund@2276: if(gaugeNumber == null ||
raimund@2276: (gaugeNumber = gaugeNumber.trim()).length() == 0) {
raimund@2276: logger.warn("No gauge specified. Cannot return discharge info.");
raimund@2276: return XMLUtils.newDocument();
raimund@2276: }
raimund@2276:
raimund@2276: logger.debug("Getting discharge for gauge: " + gaugeNumber);
raimund@2276:
raimund@2276: long gn;
raimund@2276: try {
raimund@2276: gn = Long.parseLong(gaugeNumber);
raimund@2276: }
raimund@2276: catch (NumberFormatException nfe) {
raimund@2276: logger.warn("Invalid gauge number. Cannot return discharg info.");
raimund@2276: return XMLUtils.newDocument();
raimund@2276: }
raimund@2276:
raimund@2276: Gauge gauge = Gauge.getGaugeByOfficialNumber(gn);
raimund@2276:
raimund@2276: logger.debug("Found gauge: " + gauge.getName());
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:
raimund@2276: List tables =gauge.getDischargeTables();
ingo@4173: Collections.sort(tables);
raimund@2276:
raimund@2276: Element all = result.createElement("discharges");
raimund@2276: for (DischargeTable dt: tables) {
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) {
ingo@2598: logger.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:
ingo@4173: if (stopTime != null && dt.getKind() != DischargeTables.MASTER) {
ingo@4115: discharge.setAttribute("end", String.valueOf(stopTime.getTime()));
raimund@2276: }
ingo@4173: else if (dt.getKind() == DischargeTables.MASTER) {
ingo@4173: long now = System.currentTimeMillis();
ingo@4173: discharge.setAttribute("end", String.valueOf(now));
ingo@4173: }
raimund@2276: else {
ingo@4173: continue;
raimund@2276: }
raimund@2276:
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 :