comparison artifacts/src/main/java/org/dive4elements/river/artifacts/services/DistanceInfoService.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/services/DistanceInfoService.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.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 org.dive4elements.artifacts.CallMeta;
11 import org.dive4elements.artifacts.GlobalContext;
12
13 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
14 import org.dive4elements.artifacts.common.utils.XMLUtils;
15
16 import org.dive4elements.river.model.FastAnnotations;
17
18 import org.dive4elements.river.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 if ("measuringpoint".equals(name))
93 return new FastAnnotations.NameFilter("Messstelle");
94 }
95
96 return FastAnnotations.ALL;
97 }
98
99 /**
100 * Builds an Element for a distance info.
101 *
102 * @param an The Annotation that provides information about the distance.
103 *
104 * @return an Element that contains information about a distance.
105 */
106 protected static Element buildNode(
107 Document document,
108 FastAnnotations.Annotation an
109 ) {
110 Element distance = document.createElement("distance");
111
112 distance.setAttribute("description", an.getPosition());
113
114 String riverSide = an.getAttribute();
115
116 if (riverSide != null && riverSide.length() > 0) {
117 distance.setAttribute("riverside", riverSide);
118 }
119
120 distance.setAttribute("from", String.valueOf(an.getA()));
121
122 double b = an.getB();
123 double bottom = an.getBottom();
124 double top = an.getTop();
125
126 if (!Double.isNaN(b)) {
127 distance.setAttribute("to", String.valueOf(b));
128 }
129
130 if (!Double.isNaN(bottom)) {
131 distance.setAttribute("bottom", String.valueOf(bottom));
132 }
133
134 if (!Double.isNaN(top)) {
135 distance.setAttribute("top", String.valueOf(top));
136 }
137
138 return distance;
139 }
140 }
141 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org