comparison flys-client/src/main/java/org/dive4elements/river/client/server/ArtifactHelper.java @ 5834:f507086aa94b

Repaired internal references.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:31:32 +0200
parents flys-client/src/main/java/de/intevation/flys/client/server/ArtifactHelper.java@b660090b417d
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.server;
2
3 import org.w3c.dom.Document;
4 import org.w3c.dom.Element;
5
6 import org.apache.log4j.Logger;
7
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Map;
11
12 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
13 import de.intevation.artifacts.common.utils.CreationFilter;
14
15 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
16 import de.intevation.artifacts.httpclient.http.HttpClient;
17 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
18 import de.intevation.artifacts.httpclient.utils.ArtifactNamespaceContext;
19 import de.intevation.artifacts.httpclient.utils.XMLUtils;
20
21 import de.intevation.flys.client.shared.exceptions.ServerException;
22 import de.intevation.flys.client.shared.model.Artifact;
23
24 import de.intevation.flys.client.shared.model.Recommendation;
25
26 /**
27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
28 */
29 public class ArtifactHelper {
30
31 private static final Logger logger = Logger.getLogger(ArtifactHelper.class);
32
33
34 /** The error message key that is thrown if an error occured while artifact
35 * creation.*/
36 public static final String ERROR_CREATE_ARTIFACT = "error_create_artifact";
37
38 /**
39 * Name of the factory to generate a GaugeDischargeCurveArtifact
40 */
41 private static final String GAUGE_DISCHARGE_CURVE_ARTIFACT = "gaugedischargecurve";
42 private static final String SQ_RELATION_ARTIFACT = "staticsqrelation";
43
44 private ArtifactHelper() {
45 }
46
47
48 /**
49 * @param factory ArtifactFactory to use.
50 */
51 public static Artifact createArtifact(
52 String serverUrl,
53 String locale,
54 String factory,
55 Recommendation recommendation)
56 throws ServerException
57 {
58 logger.debug("ArtifactHelper.create");
59
60 String uuid;
61 String ids;
62 CreationFilter filter;
63
64 if (recommendation != null) {
65 uuid = recommendation.getMasterArtifact();
66 ids = recommendation.getIDs();
67 filter = convertFilter(recommendation.getFilter());
68 }
69 else {
70 uuid = null;
71 ids = null;
72 filter = null;
73 }
74
75 Document create = ClientProtocolUtils.newCreateDocument(
76 factory, uuid, ids, filter);
77
78 return sendCreate(serverUrl, locale, create);
79
80 }
81
82 /**
83 * Creates a new GaugeDischargeCurverArtifact
84 *
85 * @param river the name of the river
86 * @param reference the reference id of the gauge (official number)
87 */
88 public static Artifact createGaugeDischargeCurveArtifact(
89 String serverUrl,
90 String locale,
91 String river,
92 Long reference)
93 throws ServerException
94 {
95 Document create = ClientProtocolUtils.newCreateDocument(
96 GAUGE_DISCHARGE_CURVE_ARTIFACT);
97
98 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
99 create,
100 ArtifactNamespaceContext.NAMESPACE_URI,
101 ArtifactNamespaceContext.NAMESPACE_PREFIX);
102
103 Element root = create.getDocumentElement();
104
105 Element eriver = ec.create("river");
106 ec.addAttr(eriver, "name", river);
107
108 Element egauge = ec.create("gauge");
109 ec.addAttr(egauge, "reference", reference.toString());
110
111 root.appendChild(eriver);
112 root.appendChild(egauge);
113
114 return sendCreate(serverUrl, locale, create);
115 }
116
117 /**
118 * Sends a create document to the artifact server
119 */
120 private static Artifact sendCreate(
121 String serverUrl,
122 String locale,
123 Document doc)
124 throws ServerException
125 {
126 HttpClient client = new HttpClientImpl(serverUrl, locale);
127
128 try {
129 return (Artifact) client.create(doc, new FLYSArtifactCreator());
130 }
131 catch (ConnectionException ce) {
132 logger.error(ce, ce);
133 }
134
135 throw new ServerException(ERROR_CREATE_ARTIFACT);
136 }
137
138
139 /**
140 * Create CreationFilter from Recommendation.Filter.
141 */
142 public static CreationFilter convertFilter(Recommendation.Filter filter) {
143
144 if (filter == null) {
145 return null;
146 }
147
148 CreationFilter cf = new CreationFilter();
149
150 Map<String, List<Recommendation.Facet>> outs = filter.getOuts();
151
152 for (Map.Entry<String, List<Recommendation.Facet>> entry:
153 outs.entrySet()) {
154 List<Recommendation.Facet> rfs = entry.getValue();
155 List<CreationFilter.Facet> cfs =
156 new ArrayList<CreationFilter.Facet>(rfs.size());
157 for (Recommendation.Facet rf: rfs) {
158 cfs.add(new CreationFilter.Facet(rf.getName(), rf.getIndex()));
159 }
160 cf.add(entry.getKey(), cfs);
161 }
162
163 return cf;
164 }
165
166
167 public static Artifact createSQRelationArtifact(
168 String serverUrl,
169 String locale,
170 String river,
171 int measurementStation)
172 throws ServerException
173 {
174 Document create = ClientProtocolUtils.newCreateDocument(
175 SQ_RELATION_ARTIFACT);
176
177 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
178 create,
179 ArtifactNamespaceContext.NAMESPACE_URI,
180 ArtifactNamespaceContext.NAMESPACE_PREFIX);
181
182 Element root = create.getDocumentElement();
183
184 Element eriver = ec.create("river");
185 ec.addAttr(eriver, "name", river);
186
187 Element estation = ec.create("measurement_station");
188 ec.addAttr(estation, "number", String.valueOf(measurementStation));
189
190 root.appendChild(eriver);
191 root.appendChild(estation);
192
193 return sendCreate(serverUrl, locale, create);
194 }
195 }
196 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org