Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.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 | 26e19cdaed5e |
children | e92bc9b0ca1d |
comparison
equal
deleted
inserted
replaced
1491:2a00f4849738 | 3814:8083f6384023 |
---|---|
1 package de.intevation.flys.artifacts.services; | |
2 | |
3 import java.math.BigDecimal; | |
4 import java.util.Iterator; | |
5 | |
6 import org.apache.log4j.Logger; | |
7 | |
8 import org.w3c.dom.Document; | |
9 import org.w3c.dom.Element; | |
10 | |
11 import de.intevation.artifacts.CallMeta; | |
12 import de.intevation.artifacts.GlobalContext; | |
13 | |
14 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
15 import de.intevation.artifacts.common.utils.XMLUtils; | |
16 | |
17 import de.intevation.flys.model.Annotation; | |
18 import de.intevation.flys.model.Attribute; | |
19 import de.intevation.flys.model.Position; | |
20 import de.intevation.flys.model.Range; | |
21 import de.intevation.flys.model.Edge; | |
22 | |
23 import de.intevation.flys.artifacts.model.AnnotationsFactory; | |
24 | |
25 import de.intevation.flys.artifacts.cache.CacheFactory; | |
26 | |
27 import net.sf.ehcache.Cache; | |
28 | |
29 /** | |
30 * This service provides information about distances of a specified river. | |
31 * | |
32 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
33 */ | |
34 public class DistanceInfoService extends FLYSService { | |
35 | |
36 private static enum DistanceFilter { | |
37 NONE, LOCATIONS, DISTANCES | |
38 } | |
39 | |
40 /** The logger used in this service. */ | |
41 private static Logger logger = Logger.getLogger(DistanceInfoService.class); | |
42 | |
43 public static final String CACHE_NAME = "service-distanceinfo"; | |
44 | |
45 public static final String RIVER_XPATH = "/art:river/text()"; | |
46 | |
47 public static final String FILTER_XPATH = "/art:river/art:filter/text()"; | |
48 | |
49 | |
50 /** | |
51 * The default constructor. | |
52 */ | |
53 public DistanceInfoService() { | |
54 } | |
55 | |
56 | |
57 @Override | |
58 public Document doProcess( | |
59 Document data, | |
60 GlobalContext globalContext, | |
61 CallMeta callMeta | |
62 ) { | |
63 logger.debug("DistanceInfoService.process"); | |
64 | |
65 String river = XMLUtils.xpathString( | |
66 data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE); | |
67 | |
68 String filter = XMLUtils.xpathString( | |
69 data, FILTER_XPATH, ArtifactNamespaceContext.INSTANCE); | |
70 | |
71 if (river == null || (river = river.trim()).length() == 0) { | |
72 logger.warn("No river specified. Cannot return distance info!"); | |
73 return XMLUtils.newDocument(); | |
74 } | |
75 | |
76 logger.debug("Search distances for river: " + river); | |
77 | |
78 Cache cache = CacheFactory.getCache(CACHE_NAME); | |
79 | |
80 if (cache == null) { | |
81 logger.debug("no cache configured for distance info"); | |
82 return getUncached(river, filter); | |
83 } | |
84 | |
85 | |
86 String key = getCacheKey(river, filter); | |
87 | |
88 net.sf.ehcache.Element element = cache.get(key); | |
89 | |
90 if (element != null) { | |
91 logger.debug("distance info found in cache"); | |
92 return (Document)element.getValue(); | |
93 } | |
94 | |
95 Document result = getUncached(river, filter); | |
96 | |
97 element = new net.sf.ehcache.Element(key, result); | |
98 | |
99 logger.debug("store distance info found into cache"); | |
100 | |
101 cache.put(element); | |
102 | |
103 return result; | |
104 } | |
105 | |
106 | |
107 protected String getCacheKey(String river, String filtertype) { | |
108 return filtertype != null && filtertype.length() > 0 | |
109 ? river + "_" + filtertype | |
110 : river; | |
111 } | |
112 | |
113 | |
114 protected Document getUncached(String river, String filtertype) { | |
115 | |
116 Document result = XMLUtils.newDocument(); | |
117 | |
118 Iterator<Annotation> iter = | |
119 AnnotationsFactory.getAnnotationsIterator(river); | |
120 | |
121 Element all = result.createElement("distances"); | |
122 | |
123 DistanceFilter filter = getDistanceFilter(filtertype); | |
124 | |
125 while (iter.hasNext()) { | |
126 Annotation a = iter.next(); | |
127 Element distance = buildDistanceNode(result, a, filter); | |
128 | |
129 if (distance != null) { | |
130 all.appendChild(distance); | |
131 } | |
132 } | |
133 | |
134 result.appendChild(all); | |
135 | |
136 return result; | |
137 } | |
138 | |
139 | |
140 protected static DistanceFilter getDistanceFilter(String type) { | |
141 if (type.equals("locations")) { | |
142 logger.debug("Found 'location' filter."); | |
143 return DistanceFilter.LOCATIONS; | |
144 } | |
145 else if (type.equals("distances")) { | |
146 logger.debug("Found 'distances' filter."); | |
147 return DistanceFilter.DISTANCES; | |
148 } | |
149 | |
150 logger.debug("Do not use any filter at all."); | |
151 | |
152 return DistanceFilter.NONE; | |
153 } | |
154 | |
155 | |
156 /** | |
157 * Builds an Element for a distance info. | |
158 * | |
159 * @param anno The Annotation that provides information about the distance. | |
160 * | |
161 * @return an Element that contains information about a distance. | |
162 */ | |
163 protected static Element buildDistanceNode( | |
164 Document document, | |
165 Annotation anno, | |
166 DistanceFilter filter | |
167 ) { | |
168 Position pos = anno.getPosition(); | |
169 Range range = anno.getRange(); | |
170 Attribute attr = anno.getAttribute(); | |
171 Edge edge = anno.getEdge(); | |
172 BigDecimal a = range.getA(); | |
173 BigDecimal b = range.getB(); | |
174 | |
175 if (b == null && filter == DistanceFilter.DISTANCES) { | |
176 return null; | |
177 } | |
178 | |
179 if (b != null && filter == DistanceFilter.LOCATIONS) { | |
180 return null; | |
181 } | |
182 | |
183 Element distance = document.createElement("distance"); | |
184 | |
185 distance.setAttribute("description", pos.getValue()); | |
186 | |
187 String riverSide = attr.getValue(); | |
188 | |
189 if (riverSide != null && riverSide.length() > 0) { | |
190 distance.setAttribute("riverside", riverSide); | |
191 } | |
192 | |
193 if (a != null) { | |
194 distance.setAttribute("from", a.toString()); | |
195 } | |
196 if (b != null) { | |
197 distance.setAttribute("to", b.toString()); | |
198 } | |
199 if (edge != null) { | |
200 BigDecimal bottom = edge.getBottom(); | |
201 BigDecimal top = edge.getTop(); | |
202 if (bottom != null) { | |
203 distance.setAttribute("bottom", bottom.toString()); | |
204 } | |
205 if (top != null) { | |
206 distance.setAttribute("top", top.toString()); | |
207 } | |
208 } | |
209 | |
210 return distance; | |
211 } | |
212 } | |
213 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |