Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/GaugesFactory.java @ 1839:c3b44da22a3a
Minor refactoring in preparation of generation of interpolators (WstValueTable) for static WQKms.
flys-artifacts/trunk@3175 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Mon, 07 Nov 2011 12:37:41 +0000 |
parents | 7e19449d7826 |
children |
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 |
1712
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
20 |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
21 public static Gauge getGauge(String gaugeName) { |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
22 Session session = SessionHolder.HOLDER.get(); |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
23 Query query = session.createQuery( |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
24 "from Gauge where name=:name"); |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
25 query.setParameter("name", gaugeName); |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
26 |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
27 List<Gauge> res = query.list(); |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
28 |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
29 return res.isEmpty() ? null : res.get(0); |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
30 } |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
31 |
7e19449d7826
#253 Modified the title creation of chart curves - titles will now equal the Facet's description.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
315
diff
changeset
|
32 |
151
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 "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
|
37 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
|
38 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
|
39 } |
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 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
|
42 List<Gauge> gauges, |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
43 List<double []> ranges |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
44 ) { |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
45 // XXX: Inefficent! |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
46 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
|
47 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
|
48 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
|
49 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
|
50 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
|
51 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
52 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
|
53 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
54 |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
55 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
|
56 // 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 out.add(gauge); |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 break; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
65 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
66 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
67 return out; |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 } |
4eddbb219866
Add factory to fetch gauges from database and filter them
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |