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