Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugesFactory.java @ 1178:1b432c260e97
Enabled the FloodMapState to fetch waterlevel data from external Artifact.
flys-artifacts/trunk@2764 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Thu, 15 Sep 2011 15:36:05 +0000 |
parents | 94c176fdcb89 |
children | 7e19449d7826 |
rev | line source |
---|---|
151
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.artifacts.model; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import java.util.List; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 import java.util.ArrayList; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 |
315
94c176fdcb89
Removed the SessionHolder and adapted imports.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
311
diff
changeset
|
6 import de.intevation.flys.backend.SessionHolder; |
151
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 import de.intevation.flys.model.River; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
8 import de.intevation.flys.model.Gauge; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
9 import de.intevation.flys.model.Range; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
10 |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
11 import org.hibernate.Session; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 import org.hibernate.Query; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 public class GaugesFactory |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 { |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 public static List<Gauge> getGauges(River river) { |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 return getGauges(river.getName()); |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
18 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
19 |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
20 public static List<Gauge> getGauges(String river) { |
311
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
151
diff
changeset
|
21 Session session = SessionHolder.HOLDER.get(); |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
151
diff
changeset
|
22 Query query = session.createQuery( |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
151
diff
changeset
|
23 "from Gauge where river.name=:name"); |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
151
diff
changeset
|
24 query.setParameter("name", river); |
ea3a438a7021
Backend access: Static methods are using the SessionHolder, too.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
151
diff
changeset
|
25 return query.list(); |
151
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
26 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
27 |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
28 public static List<Gauge> filterRanges( |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 List<Gauge> gauges, |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 List<double []> ranges |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 ) { |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 // XXX: Inefficent! |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 ArrayList<Range> rs = new ArrayList<Range>(); |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
34 for (double [] range: ranges) { |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
35 double a = range[0]; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
36 double b = range[1]; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
37 rs.add(new Range(Math.min(a, b), Math.max(a, b), null)); |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
38 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
39 return filter(gauges, rs); |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
40 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
41 |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
42 public static List<Gauge> filter(List<Gauge> gauges, List<Range> ranges) { |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 // TODO: Make it an HQL filter! |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 ArrayList<Gauge> out = new ArrayList<Gauge>(); |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 for (Gauge gauge: gauges) { |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 Range range = gauge.getRange(); |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
47 for (Range cmp: ranges) { |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
48 if (range.intersects(cmp)) { |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
49 out.add(gauge); |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 break; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
53 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 return out; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
56 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |