annotate flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/HYKFactory.java @ 2131:e50a928187cd

Added stubby hyk infrastructure. flys-artifacts/trunk@3706 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 18 Jan 2012 13:39:16 +0000
parents
children 04b6b6a4564d
rev   line source
2131
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.artifacts.model;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
2
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
3 import java.util.ArrayList;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
4 import java.util.List;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
5
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
6 import java.math.BigDecimal;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
7
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
8 import net.sf.ehcache.Cache;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
9 import net.sf.ehcache.Element;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
10
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
11 import org.apache.log4j.Logger;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
12
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
13 import org.hibernate.Query;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
14 import org.hibernate.Session;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
15
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
16 import de.intevation.flys.model.HYKFormation;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
17 import de.intevation.flys.model.HYKFlowZone;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
18
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
19 import de.intevation.flys.artifacts.cache.CacheFactory;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
20
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
21 import de.intevation.flys.backend.SessionHolder;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
22
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
23
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
24 /**
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
25 * Factory to access HYKs (hydrographic values).
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
26 */
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
27 public class HYKFactory
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
28 {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
29 private static Logger logger = Logger.getLogger(HYKFactory.class);
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
30
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
31 public static String HYK_CACHE_NAME = "hykache";
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
32
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
33
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
34 /** Do not instantiate a HYKFactory. */
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
35 private HYKFactory() {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
36 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
37
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
38
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
39 /**
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
40 * Get List of Zones for given river and km.
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
41 */
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
42 public static Object getHYKs(int riverId, double km) {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
43 logger.debug("HYKFactory.getHYKs");
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
44
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
45 Cache cache = CacheFactory.getCache(HYK_CACHE_NAME);
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
46
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
47 String cacheKey;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
48
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
49 if (cache != null) {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
50 cacheKey = "" + riverId + "_" + km;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
51 Element element = cache.get(cacheKey);
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
52 if (element != null) {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
53 logger.debug("Got hyk from cache");
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
54 return (List<Zone>)element.getValue();
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
55 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
56 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
57 else {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
58 cacheKey = null;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
59 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
60
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
61 List<Zone> zones = getZonesUncached(riverId, km);
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
62
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
63 if (zones != null && cacheKey != null) {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
64 logger.debug("Store hykzones in cache.");
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
65 Element element = new Element(cacheKey, zones);
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
66 cache.put(element);
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
67 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
68
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
69 return zones;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
70 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
71
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
72
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
73 /**
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
74 *
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
75 * @param column the position columns value
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
76 * @param wst_id database id of the wst
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
77 * @return according WKms.
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
78 */
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
79 public static List<Zone> getZonesUncached(int riverId, double km) {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
80
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
81 if (logger.isDebugEnabled()) {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
82 logger.debug("HYKFactory.getZoneUncached " + riverId + " km " + km);
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
83 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
84
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
85 Session session = SessionHolder.HOLDER.get();
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
86 Query query = session.createQuery(
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
87 "from HYKFormation where entry.HYK.river.id = :riverid " +
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
88 " and entry.km between " +
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
89 ":km - cast(distance_vl/1000.0 - 0.001 as big_decimal) and " +
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
90 ":km + cast(distance_vl/1000.0 + 0.001 as big_decimal)");
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
91 query.setParameter("riverid", riverId);
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
92 query.setParameter("km", new BigDecimal(km));
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
93 logger.debug("Big km " + new BigDecimal(km));
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
94 List<HYKFormation> forms = query.list();
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
95
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
96 List<Zone> zones = new ArrayList<Zone>();
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
97 for (HYKFormation form : forms) {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
98 logger.debug("One HYKFormation found (entry: "
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
99 + form.getEntry().getId()
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
100 + ") /km " + form.getEntry().getKm() + ".");
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
101
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
102 // Create respective zones.
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
103 for (HYKFlowZone flowZone: form.getZones()) {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
104 Zone z = new Zone(flowZone.getA().doubleValue(),
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
105 flowZone.getB().doubleValue(),
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
106 flowZone.getType().getName());
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
107 zones.add(z);
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
108 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
109 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
110
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
111 return zones;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
112 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
113
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
114 /** Labelled section. */
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
115 public static class Zone {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
116 /** Lower end of segment. */
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
117 protected double from;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
118 /** Upper end of segment. */
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
119 protected double to;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
120 /** The label. */
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
121 protected String name;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
122
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
123 /** Constructor for labelled section. */
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
124 public Zone (double from, double to, String name) {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
125 this.from = from;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
126 this.to = to;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
127 this.name = name;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
128 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
129
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
130 public double getFrom() {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
131 return from;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
132 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
133
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
134 public String getName() {
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
135 return name;
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
136 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
137 } // public static class Zone
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
138 }
e50a928187cd Added stubby hyk infrastructure.
Felix Wolfsteller <felix.wolfsteller@intevation.de>
parents:
diff changeset
139 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org