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