sascha@4079: package de.intevation.aft; sascha@4079: teichmann@4772: import java.util.ArrayList; teichmann@4772: import java.util.Calendar; teichmann@4772: import java.util.Collections; teichmann@4772: import java.util.Comparator; teichmann@4772: import java.util.Date; teichmann@4772: import java.util.List; teichmann@4772: teichmann@4772: import java.util.regex.Matcher; teichmann@4772: import java.util.regex.Pattern; teichmann@4772: teichmann@4772: import org.apache.log4j.Logger; teichmann@4772: sascha@4079: import org.w3c.dom.Element; sascha@4080: import org.w3c.dom.NodeList; sascha@4080: sascha@4079: public class DIPSGauge sascha@4079: { sascha@4080: private static Logger log = Logger.getLogger(DIPSGauge.class); sascha@4080: sascha@4080: public static final Pattern DATE_PATTERN = Pattern.compile( sascha@4080: "(\\d{4})-(\\d{2})-(\\d{2})\\s+(\\d{2}):(\\d{2}):(\\d{2})"); sascha@4080: sascha@4080: public static final Comparator DATE_CMP = new Comparator() { sascha@4080: public int compare(Datum a, Datum b) { sascha@4080: return a.date.compareTo(b.date); sascha@4080: } sascha@4080: }; sascha@4080: sascha@4080: public static class Datum { sascha@4080: sascha@4080: protected double value; sascha@4080: protected Date date; sascha@4080: sascha@4080: public Datum() { sascha@4080: } sascha@4080: sascha@4080: public Datum(Element element) { sascha@4080: value = Double.parseDouble(element.getAttribute("WERT")); sascha@4080: String dateString = element.getAttribute("GUELTIGAB"); sascha@4080: if (dateString.length() == 0) { teichmann@4736: throw sascha@4080: new IllegalArgumentException("missing GUELTIGAB attribute"); sascha@4080: } sascha@4080: Matcher m = DATE_PATTERN.matcher(dateString); sascha@4080: if (!m.matches()) { sascha@4080: throw sascha@4080: new IllegalArgumentException("GUELTIGAB does not match"); sascha@4080: } sascha@4080: sascha@4080: int year = Integer.parseInt(m.group(1)); sascha@4080: int month = Integer.parseInt(m.group(2)); sascha@4080: int day = Integer.parseInt(m.group(3)); sascha@4080: int hours = Integer.parseInt(m.group(4)); sascha@4080: int mins = Integer.parseInt(m.group(5)); sascha@4080: int secs = Integer.parseInt(m.group(6)); sascha@4080: sascha@4080: Calendar cal = Calendar.getInstance(); sascha@4080: cal.set(year, month, day, hours, mins, secs); sascha@4080: sascha@4080: date = cal.getTime(); sascha@4080: } sascha@4087: sascha@4087: public double getValue() { sascha@4087: return value; sascha@4087: } sascha@4087: sascha@4087: public void setValue(double value) { sascha@4087: this.value = value; sascha@4087: } sascha@4087: sascha@4087: public Date getDate() { sascha@4087: return date; sascha@4087: } sascha@4087: sascha@4087: public void setDate(Date date) { sascha@4087: this.date = date; sascha@4087: } sascha@4080: } // class datum sascha@4080: sascha@4079: protected double aeo; sascha@4079: sascha@4087: protected double station; sascha@4087: sascha@4082: protected String name; sascha@4082: sascha@4085: protected String riverName; sascha@4085: sascha@4080: protected List datums; sascha@4080: sascha@4085: protected int flysId; sascha@4085: sascha@4085: protected String aftName; sascha@4085: sascha@4097: protected Long officialNumber; sascha@4097: sascha@4079: public DIPSGauge() { sascha@4079: } sascha@4079: sascha@4079: public DIPSGauge(Element element) { sascha@4082: sascha@4087: name = element.getAttribute("NAME"); sascha@4087: riverName = element.getAttribute("GEWAESSER"); sascha@4082: sascha@4080: String aeoString = element.getAttribute("EINZUGSGEBIET_AEO"); sascha@4080: if (aeoString.length() == 0) { sascha@4082: log.warn("DIPS: Setting AEO of gauge '" + name + "' to zero."); sascha@4080: aeoString = "0"; sascha@4080: } sascha@4080: aeo = Double.parseDouble(aeoString); sascha@4080: sascha@4087: String stationString = element.getAttribute("STATIONIERUNG"); sascha@4087: if (stationString.length() == 0) { sascha@4087: log.warn("DIPS: Setting station of gauge '" + name + "' to zero."); teichmann@5188: stationString = "-99999"; sascha@4087: } sascha@4087: station = Double.parseDouble(stationString); sascha@4087: if (station == 0d) { sascha@4087: log.warn("DIPS: Station of gauge '" + name + "' is zero."); sascha@4087: } sascha@4087: sascha@4080: datums = new ArrayList(); sascha@4080: NodeList nodes = element.getElementsByTagName("PNP"); sascha@4080: for (int i = 0, N = nodes.getLength(); i < N; ++i) { sascha@4080: Element e = (Element)nodes.item(i); sascha@4080: Datum datum = new Datum(e); sascha@4080: datums.add(datum); sascha@4080: } sascha@4080: Collections.sort(datums, DATE_CMP); sascha@4080: } sascha@4080: sascha@4080: public List getDatums() { sascha@4080: return datums; sascha@4079: } sascha@4082: sascha@4082: public String getName() { sascha@4082: return name; sascha@4082: } sascha@4085: sascha@4085: public String getRiverName() { sascha@4085: return riverName; sascha@4085: } sascha@4085: sascha@4085: public int getFlysId() { sascha@4085: return flysId; sascha@4085: } sascha@4085: sascha@4085: public void setFlysId(int flysId) { sascha@4085: this.flysId = flysId; sascha@4085: } sascha@4085: sascha@4085: public String getAftName() { sascha@4085: return aftName != null ? aftName : name; sascha@4085: } sascha@4085: sascha@4085: public void setAftName(String aftName) { sascha@4085: this.aftName = aftName; sascha@4085: } sascha@4085: sascha@4087: public double getStation() { sascha@4087: return station; sascha@4087: } sascha@4085: sascha@4087: public double getAeo() { sascha@4087: return aeo; sascha@4087: } sascha@4087: sascha@4087: public void setAeo(double aeo) { sascha@4087: this.aeo = aeo; sascha@4087: } sascha@4087: sascha@4087: public void setStation(double station) { sascha@4087: this.station = station; sascha@4087: } sascha@4087: sascha@4087: public boolean hasDatums() { sascha@4087: return !datums.isEmpty(); sascha@4087: } sascha@4087: sascha@4087: public Datum getLatestDatum() { sascha@4087: return datums.get(datums.size()-1); sascha@4087: } sascha@4097: sascha@4097: public Long getOfficialNumber() { sascha@4097: return officialNumber; sascha@4097: } sascha@4097: sascha@4097: public void setOfficialNumber(Long officialNumber) { sascha@4097: this.officialNumber = officialNumber; sascha@4097: } sascha@4079: } sascha@4079: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :