comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DistanceInfoService.java @ 3318:dbe2f85bf160

merged flys-artifacts/2.8
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:35 +0200
parents 9136fc8f35f0
children
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
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 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