Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedloadOverview.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedloadOverview.java@bd047b71ab37 |
children | 4897a58c8746 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.artifacts.model.minfo; | |
2 | |
3 import java.io.Serializable; | |
4 import java.text.SimpleDateFormat; | |
5 import java.util.ArrayList; | |
6 import java.util.Date; | |
7 import java.util.List; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 import org.hibernate.SQLQuery; | |
11 import org.hibernate.Session; | |
12 import org.hibernate.type.StandardBasicTypes; | |
13 import org.w3c.dom.Document; | |
14 import org.w3c.dom.Element; | |
15 | |
16 import org.dive4elements.river.utils.KMIndex; | |
17 | |
18 public class BedloadOverview implements Serializable { | |
19 | |
20 private static Logger log = Logger.getLogger(BedloadOverview.class); | |
21 | |
22 /** | |
23 * Serial version UId. | |
24 */ | |
25 private static final long serialVersionUID = -7607668985959407096L; | |
26 | |
27 public interface Filter { | |
28 boolean accept(KMIndex<List<Date>> entry); | |
29 | |
30 } // interface Filter | |
31 | |
32 | |
33 public static final Filter ACCEPT = new Filter() { | |
34 public boolean accept(KMIndex<List<Date>> entry) { | |
35 return true; | |
36 } | |
37 }; | |
38 | |
39 public static class KmFilter implements Filter { | |
40 | |
41 protected double km; | |
42 | |
43 public KmFilter (double km) { | |
44 this.km = km; | |
45 } | |
46 public boolean accept(KMIndex<List<Date>> list) { | |
47 for (KMIndex.Entry<List<Date>> e: list){ | |
48 if (e.getKm() == km) { | |
49 return true; | |
50 } | |
51 } | |
52 return false; | |
53 } | |
54 }; | |
55 | |
56 public static class DateFilter implements Filter { | |
57 | |
58 protected Date date; | |
59 | |
60 public DateFilter (Date date) { | |
61 this.date = date; | |
62 } | |
63 | |
64 public boolean accept(KMIndex<List<Date>> list) { | |
65 for (KMIndex.Entry<List<Date>> e: list){ | |
66 if (e.getValue().equals(this.date)) { | |
67 return true; | |
68 } | |
69 } | |
70 return false; | |
71 } | |
72 }; | |
73 | |
74 public static final double EPSILON = 1e-4; | |
75 | |
76 public static final String DATE_FORMAT = "dd.MM.yyyy"; | |
77 | |
78 public static final String SQL_SQ = | |
79 "SELECT" + | |
80 " m.km AS km," + | |
81 " m.datum AS datum " + | |
82 "FROM messung m " + | |
83 " JOIN station s" + | |
84 " ON m.stationid = s.stationid " + | |
85 " JOIN gewaesser g " + | |
86 " ON s.gewaesserid = g.gewaesserid " + | |
87 "WHERE" + | |
88 " g.name = :name AND " + | |
89 " m.km IS NOT NULL " + | |
90 "ORDER by" + | |
91 " m.km, m.datum"; | |
92 | |
93 protected String riverName; | |
94 | |
95 protected KMIndex<List<Date>> entries; | |
96 | |
97 public BedloadOverview() { | |
98 entries = new KMIndex<List<Date>>(); | |
99 } | |
100 | |
101 public BedloadOverview(String riverName) { | |
102 this(); | |
103 this.riverName = riverName; | |
104 } | |
105 | |
106 private static final boolean epsilonEquals(double a, double b) { | |
107 return Math.abs(a - b) < EPSILON; | |
108 } | |
109 | |
110 protected void loadData(Session session) { | |
111 SQLQuery query = session.createSQLQuery(SQL_SQ) | |
112 .addScalar("km", StandardBasicTypes.DOUBLE) | |
113 .addScalar("datum", StandardBasicTypes.DATE); | |
114 | |
115 query.setString("name", riverName); | |
116 | |
117 List<Object []> list = query.list(); | |
118 | |
119 if (list.isEmpty()) { | |
120 log.warn("No river '" + riverName + "' found."); | |
121 } | |
122 | |
123 Double prevKm = -Double.MAX_VALUE; | |
124 List<Date> dates = new ArrayList<Date>(); | |
125 | |
126 for (Object [] row: list) { | |
127 Double km = (Double)row[0]; | |
128 if (!epsilonEquals(km, prevKm) && !dates.isEmpty()) { | |
129 entries.add(prevKm, dates); | |
130 dates = new ArrayList<Date>(); | |
131 } | |
132 dates.add((Date)row[1]); | |
133 prevKm = km; | |
134 } | |
135 | |
136 if (!dates.isEmpty()) { | |
137 entries.add(prevKm, dates); | |
138 } | |
139 } | |
140 | |
141 public boolean load(Session session) { | |
142 | |
143 loadData(session); | |
144 | |
145 return true; | |
146 } | |
147 | |
148 | |
149 public void generateOverview(Document document) { | |
150 generateOverview(document, ACCEPT); | |
151 } | |
152 | |
153 public KMIndex<List<Date>> filter(Filter f) { | |
154 // TODO: Apply filter | |
155 return entries; | |
156 } | |
157 | |
158 public void generateOverview( | |
159 Document document, | |
160 Filter filter | |
161 ) { | |
162 KMIndex<List<Date>> filtered = filter(ACCEPT); | |
163 | |
164 Element sqElement = document.createElement("bedload"); | |
165 | |
166 Element riverElement = document.createElement("river"); | |
167 | |
168 riverElement.setAttribute("name", riverName); | |
169 | |
170 sqElement.appendChild(riverElement); | |
171 | |
172 SimpleDateFormat df = new SimpleDateFormat(DATE_FORMAT); | |
173 | |
174 Element kmE = document.createElement("km"); | |
175 | |
176 for (KMIndex.Entry<List<Date>> e: filtered) { | |
177 | |
178 List<Date> dates = e.getValue(); | |
179 | |
180 if (!dates.isEmpty()) { | |
181 Element dEs = document.createElement("dates"); | |
182 | |
183 for (Date d: dates) { | |
184 Element dE = document.createElement("date"); | |
185 | |
186 dE.setAttribute("value", df.format(d)); | |
187 | |
188 dEs.appendChild(dE); | |
189 } | |
190 | |
191 kmE.appendChild(dEs); | |
192 } | |
193 } | |
194 | |
195 sqElement.appendChild(kmE); | |
196 | |
197 document.appendChild(sqElement); | |
198 } | |
199 } |