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; bjoern@4246: bjoern@4246: import java.math.BigDecimal; bjoern@4289: import java.text.DateFormat; bjoern@4289: import java.util.Date; bjoern@4246: import java.util.List; bjoern@4289: import java.util.Locale; bjoern@4246: tom@9726: import org.apache.logging.log4j.Logger; tom@9726: import org.apache.logging.log4j.LogManager; bjoern@4246: bjoern@4246: import org.w3c.dom.Document; bjoern@4246: import org.w3c.dom.Element; bjoern@4246: teichmann@5831: import org.dive4elements.artifacts.CallMeta; teichmann@5831: import org.dive4elements.artifacts.GlobalContext; bjoern@4246: teichmann@5831: import org.dive4elements.river.model.MeasurementStation; teichmann@5831: import org.dive4elements.river.model.Range; teichmann@5831: import org.dive4elements.river.model.TimeInterval; bjoern@4246: bjoern@4246: /** bjoern@4246: * @author Björn Ricks bjoern@4246: */ bjoern@4247: public class MeasurementStationInfoService extends RiverInfoService { bjoern@4246: tom@9726: private static final Logger log = LogManager.getLogger( bjoern@4264: MeasurementStationInfoService.class); bjoern@4246: bjoern@4289: public static final DateFormat DATE_FORMAT = DateFormat.getDateInstance( bjoern@4289: DateFormat.SHORT, Locale.GERMANY); bjoern@4289: bjoern@4246: @Override bjoern@4246: public Document doProcess( bjoern@4246: Document data, bjoern@4246: GlobalContext globalContext, bjoern@4246: CallMeta callMeta bjoern@4246: ) { bjoern@4247: Document result = super.doProcess(data, globalContext, callMeta); bjoern@4246: bjoern@4246: Element egs = ec.create("measurement-stations"); bjoern@4246: tom@8412: List mstations = MeasurementStation tom@8412: .getStationsAtRiver(river); bjoern@4246: teichmann@8202: if (log.isDebugEnabled()) { teichmann@8202: log.debug("Loaded stations: " + mstations); bjoern@4246: } bjoern@4246: bjoern@4246: for (MeasurementStation mstation: mstations) { bjoern@4246: Element eg = ec.create("measurement-station"); bjoern@4246: bjoern@4246: String name = mstation.getName(); bjoern@4246: if (name != null) { bjoern@4246: ec.addAttr(eg, "name", name, true); bjoern@4246: } bjoern@4246: bjoern@4263: Integer id = mstation.getId(); bjoern@4263: if (id != null) { bjoern@4263: ec.addAttr(eg, "id", Integer.toString(id), true); bjoern@4263: } bjoern@4263: bjoern@4246: String type = mstation.getMeasurementType(); bjoern@4246: if (type != null) { bjoern@4246: ec.addAttr(eg, "type", type, true); bjoern@4246: } bjoern@4246: bjoern@4246: String riverside = mstation.getRiverside(); bjoern@4246: if (riverside != null) { bjoern@4246: ec.addAttr(eg, "riverside", riverside, true); bjoern@4246: } bjoern@4246: bjoern@4246: Range range = mstation.getRange(); bjoern@4246: if (range != null) { bjoern@4246: BigDecimal a = range.getA(); tom@8412: BigDecimal b = range.getB(); tom@8412: tom@8412: // In case river is km_up, station is at larger value of range tom@8412: if (b != null && river.getKmUp()) { tom@8412: ec.addAttr(eg, "start", getStringValue(b), true); tom@8412: ec.addAttr(eg, "end", getStringValue(a), true); bjoern@4246: } tom@8412: else { tom@8412: ec.addAttr(eg, "start", getStringValue(a), true); tom@8412: if (b != null) { tom@8412: ec.addAttr(eg, "end", getStringValue(b), true); tom@8412: } bjoern@4246: } bjoern@4246: } bjoern@4246: bjoern@4263: String moperator = mstation.getOperator(); bjoern@4263: if (moperator != null) { bjoern@4263: ec.addAttr(eg, "operator", moperator, true); bjoern@4263: } bjoern@4263: bjoern@4289: TimeInterval tinterval = mstation.getObservationTimerange(); bjoern@4289: if (tinterval != null) { bjoern@4289: Date tstart = tinterval.getStartTime(); bjoern@4289: if (tstart != null) { bjoern@4289: ec.addAttr(eg, "starttime", DATE_FORMAT.format(tstart), bjoern@4289: true); bjoern@4289: } bjoern@4289: Date tstop = tinterval.getStopTime(); bjoern@4289: if (tstop != null) { bjoern@4289: ec.addAttr(eg, "stoptime", DATE_FORMAT.format(tstop), bjoern@4289: true); bjoern@4289: } bjoern@4289: } bjoern@4289: tom@8417: String comment = mstation.getComment(); tom@8417: if (comment != null) { tom@8417: ec.addAttr(eg, "comment", comment, true); tom@8417: } tom@8417: tom@8412: String gaugename = mstation.getGaugeName(); bjoern@4324: if (gaugename != null) { bjoern@4324: Element egauge = ec.create("gauge"); bjoern@4324: ec.addAttr(egauge, "name", gaugename, true); bjoern@4324: eg.appendChild(egauge); bjoern@4324: } bjoern@4324: bjoern@4246: egs.appendChild(eg); bjoern@4246: } bjoern@4246: bjoern@4247: this.riverele.appendChild(egs); bjoern@4246: bjoern@4246: return result; bjoern@4246: } bjoern@4246: bjoern@4246: }