Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java @ 2424:092e519ff461
merged flys-artifacts/2.6.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:26 +0200 |
parents | e92bc9b0ca1d |
children | df4d6b286af8 |
comparison
equal
deleted
inserted
replaced
2392:8112ec686a9a | 2424:092e519ff461 |
---|---|
1 package de.intevation.flys.artifacts.services; | |
2 | |
3 import java.util.Iterator; | |
4 | |
5 import org.apache.log4j.Logger; | |
6 | |
7 import org.w3c.dom.Document; | |
8 import org.w3c.dom.Element; | |
9 | |
10 import de.intevation.artifacts.CallMeta; | |
11 import de.intevation.artifacts.GlobalContext; | |
12 | |
13 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
14 import de.intevation.artifacts.common.utils.XMLUtils; | |
15 | |
16 import de.intevation.flys.model.FastAnnotations; | |
17 | |
18 import de.intevation.flys.artifacts.model.LocationProvider; | |
19 | |
20 | |
21 /** | |
22 * This service provides information about distances of a specified river. | |
23 * | |
24 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
25 */ | |
26 public class DistanceInfoService extends FLYSService { | |
27 | |
28 /** The logger used in this service. */ | |
29 private static Logger logger = Logger.getLogger(DistanceInfoService.class); | |
30 | |
31 public static final String RIVER_XPATH = "/art:river/text()"; | |
32 | |
33 public static final String FILTER_XPATH = "/art:river/art:filter/text()"; | |
34 | |
35 | |
36 /** | |
37 * The default constructor. | |
38 */ | |
39 public DistanceInfoService() { | |
40 } | |
41 | |
42 | |
43 @Override | |
44 public Document doProcess( | |
45 Document data, | |
46 GlobalContext globalContext, | |
47 CallMeta callMeta | |
48 ) { | |
49 logger.debug("DistanceInfoService.process"); | |
50 | |
51 String river = XMLUtils.xpathString( | |
52 data, RIVER_XPATH, ArtifactNamespaceContext.INSTANCE); | |
53 | |
54 String filterName = XMLUtils.xpathString( | |
55 data, FILTER_XPATH, ArtifactNamespaceContext.INSTANCE); | |
56 | |
57 if (river == null || (river = river.trim()).length() == 0) { | |
58 logger.warn("No river specified. Cannot return distance info!"); | |
59 return XMLUtils.newDocument(); | |
60 } | |
61 | |
62 logger.debug("Search distances for river: " + river); | |
63 | |
64 FastAnnotations fas = LocationProvider.getAnnotations(river); | |
65 | |
66 FastAnnotations.Filter filter = selectFilter(filterName); | |
67 | |
68 return buildDocument(fas.filter(filter)); | |
69 } | |
70 | |
71 protected Document buildDocument( | |
72 Iterator<FastAnnotations.Annotation> iter | |
73 ) { | |
74 Document result = XMLUtils.newDocument(); | |
75 | |
76 Element all = result.createElement("distances"); | |
77 | |
78 while (iter.hasNext()) { | |
79 all.appendChild(buildNode(result, iter.next())); | |
80 } | |
81 | |
82 result.appendChild(all); | |
83 | |
84 return result; | |
85 } | |
86 | |
87 protected static FastAnnotations.Filter selectFilter(String name) { | |
88 | |
89 if (name != null) { | |
90 if ("locations".equals(name)) return FastAnnotations.IS_POINT; | |
91 if ("distances".equals(name)) return FastAnnotations.IS_RANGE; | |
92 } | |
93 | |
94 return FastAnnotations.ALL; | |
95 } | |
96 | |
97 /** | |
98 * Builds an Element for a distance info. | |
99 * | |
100 * @param an The Annotation that provides information about the distance. | |
101 * | |
102 * @return an Element that contains information about a distance. | |
103 */ | |
104 protected static Element buildNode( | |
105 Document document, | |
106 FastAnnotations.Annotation an | |
107 ) { | |
108 Element distance = document.createElement("distance"); | |
109 | |
110 distance.setAttribute("description", an.getPosition()); | |
111 | |
112 String riverSide = an.getAttribute(); | |
113 | |
114 if (riverSide != null && riverSide.length() > 0) { | |
115 distance.setAttribute("riverside", riverSide); | |
116 } | |
117 | |
118 distance.setAttribute("from", String.valueOf(an.getA())); | |
119 | |
120 double b = an.getB(); | |
121 double bottom = an.getBottom(); | |
122 double top = an.getTop(); | |
123 | |
124 if (!Double.isNaN(b)) { | |
125 distance.setAttribute("to", String.valueOf(b)); | |
126 } | |
127 | |
128 if (!Double.isNaN(bottom)) { | |
129 distance.setAttribute("bottom", String.valueOf(bottom)); | |
130 } | |
131 | |
132 if (!Double.isNaN(top)) { | |
133 distance.setAttribute("top", String.valueOf(top)); | |
134 } | |
135 | |
136 return distance; | |
137 } | |
138 } | |
139 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |