comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/GaugeInfoService.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 fb7975828ffa
children f1f798b5f14a
comparison
equal deleted inserted replaced
2987:98c7a46ec5ae 3318:dbe2f85bf160
1 package de.intevation.flys.artifacts.services;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.w3c.dom.Document;
7 import org.w3c.dom.Element;
8
9 import org.apache.log4j.Logger;
10
11 import de.intevation.artifacts.common.ArtifactNamespaceContext;
12 import de.intevation.artifacts.common.utils.XMLUtils;
13 import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator;
14
15 import de.intevation.artifacts.CallMeta;
16 import de.intevation.artifacts.GlobalContext;
17
18 import de.intevation.flys.model.Gauge;
19 import de.intevation.flys.model.Range;
20 import de.intevation.flys.model.River;
21
22 import de.intevation.flys.artifacts.model.RiverFactory;
23
24
25 /**
26 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
27 */
28 public class GaugeInfoService extends FLYSService {
29
30 interface Filter {
31 boolean apply(Gauge gauge);
32 }
33
34
35 protected class ReferenceNumberFilter implements Filter {
36 private long refNr;
37
38 public ReferenceNumberFilter(long refNr) {
39 this.refNr = refNr;
40 }
41
42 @Override
43 public boolean apply(Gauge gauge) {
44 logger.debug("Test gauge '" + gauge.getName() + "'");
45
46 if (gauge != null && gauge.getOfficialNumber() == refNr) {
47 return true;
48 }
49 else {
50 return false;
51 }
52 }
53 } // end of ReferenceNumberFilter class
54
55
56 /** The logger that is used by this service.*/
57 private static Logger logger = Logger.getLogger(GaugeInfoService.class);
58
59
60 public static final String XPATH_RIVERNAME = "/art:river/@name";
61
62 public static final String XPATH_REFERENCE_NR
63 = "/art:river/art:filter/art:gauge/text()";
64
65
66 public GaugeInfoService() {
67 }
68
69
70 @Override
71 public Document doProcess(
72 Document data,
73 GlobalContext context,
74 CallMeta callMeta
75 ) {
76 logger.debug("GaugeInfoService.process");
77
78 // XXX REMOVE THIS
79 logger.debug(XMLUtils.toString(data));
80
81 River river = getRiverFromRequest(data);
82
83 List<Filter> filters = getFilters(data);
84 List<Gauge> allGauges = river.getGauges();
85 List<Gauge> filtered = new ArrayList<Gauge>();
86
87 for (Gauge g: allGauges) {
88 for (Filter f: filters) {
89 if (f.apply(g)) {
90 filtered.add(g);
91 }
92 }
93 }
94
95 return buildInfoDocument(filtered);
96 }
97
98
99 protected River getRiverFromRequest(Document data) {
100 String rivername = XMLUtils.xpathString(
101 data,
102 XPATH_RIVERNAME,
103 ArtifactNamespaceContext.INSTANCE);
104
105 logger.debug("Return Gauge info for River '" + rivername + "'");
106
107 return rivername != null ? RiverFactory.getRiver(rivername) : null;
108 }
109
110
111 protected List<Filter> getFilters(Document data) {
112 List<Filter> filters = new ArrayList<Filter>();
113
114 String refNr = XMLUtils.xpathString(
115 data,
116 XPATH_REFERENCE_NR,
117 ArtifactNamespaceContext.INSTANCE);
118
119 if (refNr != null && refNr.length() > 0) {
120 try {
121 filters.add(new ReferenceNumberFilter(Long.valueOf(refNr)));
122 }
123 catch (NumberFormatException nfe) {
124 logger.warn(nfe, nfe);
125 }
126 }
127
128 return filters;
129 }
130
131
132 protected Document buildInfoDocument(List<Gauge> gauges) {
133 Document doc = XMLUtils.newDocument();
134
135 ElementCreator cr = new ElementCreator(
136 doc,
137 ArtifactNamespaceContext.NAMESPACE_URI,
138 ArtifactNamespaceContext.NAMESPACE_PREFIX);
139
140 Element service = cr.create("service");
141
142 logger.debug("Append " + gauges.size() + " gauges to info doc.");
143
144 for (Gauge g: gauges) {
145 Range r = g.getRange();
146
147 Element el = cr.create("gauge");
148 cr.addAttr(el, "name", g.getName());
149 cr.addAttr(el, "lower", String.valueOf(r.getA().doubleValue()));
150 cr.addAttr(el, "upper", String.valueOf(r.getB().doubleValue()));
151
152 service.appendChild(el);
153 }
154
155 doc.appendChild(service);
156
157 return doc;
158 }
159 }
160 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org