9404
|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde |
|
2 * Software engineering by Intevation GmbH |
|
3 * |
|
4 * This file is Free Software under the GNU AGPL (>=v3) |
|
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
|
6 * documentation coming with Dive4Elements River for details. |
|
7 */ |
|
8 |
|
9 package org.dive4elements.river.client.server; |
|
10 |
|
11 import java.util.ArrayList; |
|
12 import java.util.Date; |
|
13 import java.util.List; |
|
14 |
|
15 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; |
|
16 import org.dive4elements.artifacts.common.utils.XMLUtils; |
|
17 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; |
|
18 import org.dive4elements.artifacts.httpclient.http.HttpClient; |
|
19 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; |
|
20 import org.dive4elements.river.client.client.services.DynamicMainValuesTimeRangeDeterminationService; |
|
21 import org.dive4elements.river.client.shared.exceptions.ServerException; |
|
22 import org.dive4elements.river.client.shared.model.GaugeInfoObject; |
|
23 import org.dive4elements.river.client.shared.model.GaugeInfoObjectImpl; |
|
24 import org.w3c.dom.Document; |
|
25 import org.w3c.dom.Element; |
|
26 import org.w3c.dom.NamedNodeMap; |
|
27 import org.w3c.dom.Node; |
|
28 import org.w3c.dom.NodeList; |
|
29 |
|
30 /** |
|
31 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
|
32 */ |
|
33 public class DynamicMainValuesTimeRangeDeterminationServiceImpl extends RemoteServiceServlet implements DynamicMainValuesTimeRangeDeterminationService { |
|
34 private static final long serialVersionUID = 1L; |
|
35 |
|
36 private static final String XPATH_ROOT = "dynamic-mainvalues-input"; |
|
37 private String globalErrorMsg = null; |
|
38 |
|
39 private GaugeInfoObject[] callService(final String serviceName, final Document input, final String locale) throws ServerException { |
|
40 final String url = getServletContext().getInitParameter("server-url"); |
|
41 |
|
42 final HttpClient client = new HttpClientImpl(url, locale); |
|
43 |
|
44 try { |
|
45 final Document result = client.callService(url, serviceName, input); |
|
46 final GaugeInfoObject[] objects = extractDates(result); |
|
47 if (!this.globalErrorMsg.equals("")) |
|
48 throw new ServerException(this.globalErrorMsg); |
|
49 // log.debug("Extract wq info objects now."); |
|
50 return objects; |
|
51 |
|
52 } |
|
53 catch (final ConnectionException ce) { |
|
54 // log.error(ce, ce); |
|
55 } |
|
56 |
|
57 throw new ServerException("error_no_wqinput_found"); |
|
58 } |
|
59 |
|
60 private GaugeInfoObject[] extractDates(final Document result) throws ServerException { |
|
61 final List<GaugeInfoObject> results = new ArrayList<GaugeInfoObject>(); |
|
62 final NodeList list = result.getChildNodes(); |
|
63 |
|
64 if (list != null && list.getLength() == 1) { |
|
65 // log.warn("No wq info found."); |
|
66 final Node nodes = list.item(0); |
|
67 final NodeList childNodes = nodes.getChildNodes(); |
|
68 for (int i = 0; i < childNodes.getLength(); i++) { |
|
69 final Node node = childNodes.item(i); |
|
70 if (node.getLocalName().equals("global-error-msg")) { |
|
71 this.globalErrorMsg = node.getTextContent(); |
|
72 } else { // name equals gauge |
|
73 |
|
74 final NamedNodeMap map = node.getAttributes(); |
|
75 final String gaugeName = getStringFromMap("name", map); |
|
76 final String error = getStringFromMap("error-message", map); |
|
77 final GaugeInfoObject gi = new GaugeInfoObjectImpl(gaugeName, error, getDateFromMap("date-from", map), getDateFromMap("date-to", map)); |
|
78 results.add(gi); |
|
79 } |
|
80 } |
|
81 |
|
82 } |
|
83 return results.toArray(new GaugeInfoObject[results.size()]); |
|
84 } |
|
85 |
|
86 private Date getDateFromMap(final String attrName, final NamedNodeMap map) { |
|
87 final Node dateNode = map.getNamedItem(attrName); |
|
88 if (dateNode != null) |
|
89 return new Date(Long.parseLong(dateNode.getNodeValue())); // errorHandling? Dürfte eigentlich keine Parse-Probleme bekommen! |
|
90 return null; |
|
91 } |
|
92 |
|
93 private String getStringFromMap(final String attrName, final NamedNodeMap map) { |
|
94 final Node node = map.getNamedItem(attrName); |
|
95 if (node != null) |
|
96 return node.getNodeValue(); |
|
97 return null; |
|
98 } |
|
99 |
|
100 @Override |
|
101 public GaugeInfoObject[] getGaugeInfo(final String locale, final String river, final String[] gauges, final int startYear, final int endYear) |
|
102 throws ServerException { |
|
103 |
|
104 final Document input = createInput(river, gauges, startYear, endYear); |
|
105 |
|
106 return callService("dynamicMainvaluesTimeRangeDetermination", input, locale); |
|
107 |
|
108 } |
|
109 |
|
110 public static final Document createInput(final String river, final String[] gauges, final int startYear, final int endYear) { |
|
111 |
|
112 final Document input = XMLUtils.newDocument(); |
|
113 |
|
114 final XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(input, ArtifactNamespaceContext.NAMESPACE_URI, |
|
115 ArtifactNamespaceContext.NAMESPACE_PREFIX); |
|
116 |
|
117 final Element inputElement = ec.create("dynamic-mainvalues-input"); |
|
118 |
|
119 final Element riverEl = ec.create("river"); |
|
120 riverEl.setTextContent(river); |
|
121 inputElement.appendChild(riverEl); |
|
122 |
|
123 for (final String gauge : gauges) { |
|
124 final Element gaugeEl = ec.create("gauge"); |
|
125 gaugeEl.setTextContent(gauge); |
|
126 inputElement.appendChild(gaugeEl); |
|
127 } |
|
128 |
|
129 final Element startYearElement = ec.create("startYear"); |
|
130 startYearElement.setTextContent(String.valueOf(startYear)); |
|
131 inputElement.appendChild(startYearElement); |
|
132 |
|
133 final Element endYearElement = ec.create("endYear"); |
|
134 endYearElement.setTextContent(String.valueOf(endYear)); |
|
135 inputElement.appendChild(endYearElement); |
|
136 |
|
137 input.appendChild(inputElement); |
|
138 |
|
139 return input; |
|
140 } |
|
141 |
|
142 } |