comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/CrossSectionKMService.java @ 3814:8083f6384023

merged flys-artifacts/pre2.6-2012-01-04
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:56 +0200
parents e0b081105a82
children f021080cb409
comparison
equal deleted inserted replaced
1491:2a00f4849738 3814:8083f6384023
1 package de.intevation.flys.artifacts.services;
2
3 import de.intevation.artifacts.CallMeta;
4 import de.intevation.artifacts.GlobalContext;
5
6 import de.intevation.artifacts.common.utils.XMLUtils;
7
8 import de.intevation.flys.artifacts.cache.CacheFactory;
9
10 import de.intevation.flys.backend.SessionHolder;
11
12 import de.intevation.flys.model.CrossSection;
13 import de.intevation.flys.model.CrossSectionLine;
14
15 import java.util.AbstractMap;
16 import java.util.ArrayDeque;
17 import java.util.Deque;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.NavigableMap;
21
22 import java.util.concurrent.ConcurrentSkipListMap;
23
24 import net.sf.ehcache.Cache;
25
26 import org.apache.log4j.Logger;
27
28 import org.hibernate.Query;
29 import org.hibernate.Session;
30
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33 import org.w3c.dom.NodeList;
34
35
36 /**
37 * Service to find the next/previous km (measurement) of cross sections.
38 * Looking at the query for a single cross-section id at a single km, the
39 * service does the following:
40 *
41 * It returns the km itself if a measurement at that km was found and
42 * the N nearest other measurement points in both directions.
43 *
44 * That means, you can pass N=0 to find out whether a measurement at given km
45 * exists.
46 *
47 * If less than N neighbours exist in one direction, less are delivered
48 * (e.g. given measurements at [0,2,3,4,5,7,8,9] a query for km=8, N=3 will
49 * result in [4,5,7,8,9]).
50 */
51 public class CrossSectionKMService
52 extends FLYSService
53 {
54 private static Logger logger =
55 Logger.getLogger(CrossSectionKMService.class);
56
57 public static final String CACHE_NAME = "cross-section-kms";
58
59
60 /** Trivial constructor. */
61 public CrossSectionKMService() {
62 }
63
64
65 /**
66 * @param data
67 */
68 @Override
69 public Document doProcess(
70 Document data,
71 GlobalContext globalContext,
72 CallMeta callMeta
73 ) {
74 logger.debug("CrossSectionKMService.doProcess");
75
76 NodeList crossSectionNodes =
77 data.getElementsByTagName("art:cross-section");
78
79 Cache cache = CacheFactory.getCache(CACHE_NAME);
80
81 Document document = XMLUtils.newDocument();
82
83 Element all = document.createElement("cross-sections");
84
85 for (int i = 0, CS = crossSectionNodes.getLength(); i < CS; ++i) {
86 Element crossSectionElement = (Element)crossSectionNodes.item(i);
87
88 String idString = crossSectionElement.getAttribute("id");
89 String kmString = crossSectionElement.getAttribute("km");
90 String neighborsString = crossSectionElement.getAttribute("n");
91
92 if (idString.length() == 0 || kmString.length() == 0) {
93 logger.debug("missing attributes in cross-section element");
94 continue;
95 }
96
97 double km;
98 Integer crossSectionId;
99 int N = 2;
100
101 try {
102 km = Double.parseDouble(kmString);
103 crossSectionId = Integer.valueOf(idString);
104
105 if (neighborsString.length() > 0) {
106 N = Integer.parseInt(neighborsString);
107 }
108 }
109 catch (NumberFormatException nfe) {
110 logger.debug("converting number failed", nfe);
111 continue;
112 }
113
114 NavigableMap<Double, Integer> map;
115
116 if (cache == null) {
117 map = getUncached(crossSectionId);
118 }
119 else {
120 net.sf.ehcache.Element element = cache.get(crossSectionId);
121 if (element == null) {
122 map = getUncached(crossSectionId);
123 if (map != null) {
124 element = new net.sf.ehcache.Element(
125 crossSectionId, map);
126 cache.put(element);
127 }
128 }
129 else {
130 map = (NavigableMap<Double, Integer>)element.getValue();
131 }
132 }
133
134 if (map == null) {
135 logger.debug("cannot find cross section " + crossSectionId);
136 continue;
137 }
138
139 Deque<Map.Entry<Double, Integer>> result =
140 nearestNeighbors(map, km, N);
141
142 if (!result.isEmpty()) {
143 Element csE = document.createElement("cross-section");
144 csE.setAttribute("id", idString);
145 for (Map.Entry<Double, Integer> entry: result) {
146 Element lineE = document.createElement("line");
147 lineE.setAttribute(
148 "line-id", String.valueOf(entry.getValue()));
149 lineE.setAttribute(
150 "km", String.valueOf(entry.getKey()));
151 csE.appendChild(lineE);
152 }
153 all.appendChild(csE);
154 }
155 }
156
157 document.appendChild(all);
158
159 return document;
160 }
161
162
163 /**
164 * @param km the kilometer from which to start searching for other
165 * measurements
166 * @param N number of neighboring measurements to find.
167 */
168 public static Deque<Map.Entry<Double, Integer>> nearestNeighbors(
169 NavigableMap<Double, Integer> map,
170 double km,
171 int N
172 ) {
173 Deque<Map.Entry<Double, Integer>> result =
174 new ArrayDeque<Map.Entry<Double, Integer>>(2*N);
175
176 if(map.get(km) != null) {
177 result.add(new AbstractMap.SimpleEntry<Double, Integer>(km,map.get(km)));
178
179 }
180
181 int i = 0;
182 for (Map.Entry<Double, Integer> entry:
183 map.headMap(km, false).descendingMap().entrySet()) {
184 if (i++ >= N) {
185 break;
186 }
187 result.addFirst(entry);
188 }
189
190 i = 0;
191 for (Map.Entry<Double, Integer> entry:
192 map.tailMap(km, false).entrySet()) {
193 if (i++ >= N) {
194 break;
195 }
196 result.addLast(entry);
197 }
198
199 return result;
200 }
201
202
203 /**
204 * @param crossSectionId id of queried cross-section (in db).
205 * @return Mapping from kilometer to db-id.
206 */
207 public static NavigableMap<Double, Integer> getUncached(
208 Integer crossSectionId
209 ) {
210 NavigableMap<Double, Integer> result =
211 new ConcurrentSkipListMap<Double, Integer>();
212
213 Session session = SessionHolder.HOLDER.get();
214 Query query = session.createQuery(
215 "from CrossSection where id=:id");
216 query.setParameter("id", crossSectionId);
217
218 List<CrossSection> crossSections = query.list();
219 if (crossSections.isEmpty()) {
220 return null;
221 }
222
223 CrossSection crossSection = crossSections.get(0);
224 List<CrossSectionLine> lines = crossSection.getLines();
225
226 for (CrossSectionLine line: lines) {
227 Double km = line.getKm().doubleValue();
228 Integer id = line.getId();
229 result.put(km, id);
230 }
231
232 return result;
233 }
234 }
235 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org