comparison gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java @ 36:ad381cc47217

Format Code to max 80 Chars per Row gnv/trunk@172 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Fri, 02 Oct 2009 08:54:13 +0000
parents 4405f31bbc30
children fccf90761825
comparison
equal deleted inserted replaced
35:4405f31bbc30 36:ad381cc47217
41 import de.intevation.gnv.propertiesreader.PropertiesReaderFactory; 41 import de.intevation.gnv.propertiesreader.PropertiesReaderFactory;
42 import de.intevation.gnv.util.XMLUtils; 42 import de.intevation.gnv.util.XMLUtils;
43 43
44 /** 44 /**
45 * @author Tim Englich <tim.englich@intevation.de> 45 * @author Tim Englich <tim.englich@intevation.de>
46 * 46 *
47 */ 47 */
48 public class DefaultArtifactDatabaseClient implements ArtifactDatabaseClient { 48 public class DefaultArtifactDatabaseClient implements ArtifactDatabaseClient {
49 /** 49 /**
50 * The URI of the namespace of the artifacts. 50 * The URI of the namespace of the artifacts.
51 */ 51 */
52 public final static String NAMESPACE_URI = "http://www.intevation.de/2009/artifacts"; 52 public final static String NAMESPACE_URI = "http://www.intevation.de/2009/artifacts";
53 53
54 /** 54 /**
55 * The XML prefix for the artifacts namespace. 55 * The XML prefix for the artifacts namespace.
56 */ 56 */
57 public final static String NAMESPACE_PREFIX = "art"; 57 public final static String NAMESPACE_PREFIX = "art";
58 58
59 /** 59 /**
60 * the logger, used to log exceptions and additonaly information 60 * the logger, used to log exceptions and additonaly information
61 */ 61 */
62 private static Logger log = Logger.getLogger(DefaultArtifactDatabaseClient.class); 62 private static Logger log = Logger
63 63 .getLogger(DefaultArtifactDatabaseClient.class);
64
64 /** 65 /**
65 * The Databases which could be used 66 * The Databases which could be used
66 */ 67 */
67 private static Collection<String> artifactDatabases = null; 68 private static Collection<String> artifactDatabases = null;
68 69
69 /** 70 /**
70 * Is the Class initialized? 71 * Is the Class initialized?
71 */ 72 */
72 private static boolean initialized = false; 73 private static boolean initialized = false;
73 74
74 /** 75 /**
75 * Constructor 76 * Constructor
76 */ 77 */
77 public DefaultArtifactDatabaseClient() { 78 public DefaultArtifactDatabaseClient() {
78 super(); 79 super();
79 } 80 }
80 81
81 /** 82 /**
82 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getArtifactFactories() 83 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getArtifactFactories()
83 */ 84 */
84 public Collection<ArtifactObject> getArtifactFactories() throws ArtifactDatabaseClientException { 85 public Collection<ArtifactObject> getArtifactFactories()
86 throws ArtifactDatabaseClientException {
85 Collection<ArtifactObject> resultValues = null; 87 Collection<ArtifactObject> resultValues = null;
86 if (!initialized){ 88 if (!initialized) {
87 this.initialize(); 89 this.initialize();
88 } 90 }
89 try { 91 try {
90 log.debug("DefaultArtifactDatabaseClient.getArtifactFactories"); 92 log.debug("DefaultArtifactDatabaseClient.getArtifactFactories");
91 Iterator<String> it = artifactDatabases.iterator(); 93 Iterator<String> it = artifactDatabases.iterator();
92 while (it.hasNext()){ 94 while (it.hasNext()) {
93 String server = it.next(); 95 String server = it.next();
94 String url = server+ "/factories"; 96 String url = server + "/factories";
95 Document document = this.doGetRequest(url); 97 Document document = this.doGetRequest(url);
96 if (resultValues == null){ 98 if (resultValues == null) {
97 resultValues = this.getArtifactFactories(document,server); 99 resultValues = this.getArtifactFactories(document, server);
98 }else{ 100 } else {
99 resultValues.addAll(this.getArtifactFactories(document,server)); 101 resultValues.addAll(this.getArtifactFactories(document,
102 server));
100 } 103 }
101 } 104 }
102 } catch (IOException e) { 105 } catch (IOException e) {
103 log.error(e,e); 106 log.error(e, e);
104 } 107 }
105 return resultValues; 108 return resultValues;
106 } 109 }
107 110
108 private Collection<ArtifactObject> getArtifactFactories(Document document, String server){ 111 private Collection<ArtifactObject> getArtifactFactories(Document document,
112 String server) {
109 XMLUtils xmlUtils = new XMLUtils(); 113 XMLUtils xmlUtils = new XMLUtils();
110 NodeList artifactFactories = xmlUtils.getNodeSetXPath(document, "/result/factories/factory"); 114 NodeList artifactFactories = xmlUtils.getNodeSetXPath(document,
111 Collection<ArtifactObject> resultValues = new ArrayList<ArtifactObject>(artifactFactories.getLength()); 115 "/result/factories/factory");
112 if (artifactFactories != null){ 116 Collection<ArtifactObject> resultValues = new ArrayList<ArtifactObject>(
113 for (int i = 0; i < artifactFactories.getLength(); i++){ 117 artifactFactories.getLength());
114 Node artifactFactoryNode = artifactFactories.item(i); 118 if (artifactFactories != null) {
115 String name = xmlUtils.getStringXPath(artifactFactoryNode, "@name"); 119 for (int i = 0; i < artifactFactories.getLength(); i++) {
116 String description = xmlUtils.getStringXPath(artifactFactoryNode, "@description"); 120 Node artifactFactoryNode = artifactFactories.item(i);
117 ArtifactFactory artifactFactory = new ArtifactFactory(name, description, server); 121 String name = xmlUtils.getStringXPath(artifactFactoryNode,
122 "@name");
123 String description = xmlUtils.getStringXPath(
124 artifactFactoryNode, "@description");
125 ArtifactFactory artifactFactory = new ArtifactFactory(name,
126 description, server);
118 resultValues.add(artifactFactory); 127 resultValues.add(artifactFactory);
119 } 128 }
120 } 129 }
121 return resultValues; 130 return resultValues;
122 } 131 }
123 132
124 /** 133 /**
125 * @throws IOException 134 * @throws IOException
126 */ 135 */
127 private Document doGetRequest(String requestUrl) throws IOException, ArtifactDatabaseClientException { 136 private Document doGetRequest(String requestUrl) throws IOException,
137 ArtifactDatabaseClientException {
128 return this.doGetRequest(requestUrl, null); 138 return this.doGetRequest(requestUrl, null);
129 } 139 }
130 140
131 /** 141 /**
132 * @throws IOException 142 * @throws IOException
133 */ 143 */
134 private Document doGetRequest(String requestUrl, Document requestBody) throws IOException, ArtifactDatabaseClientException { 144 private Document doGetRequest(String requestUrl, Document requestBody)
145 throws IOException, ArtifactDatabaseClientException {
135 XMLUtils xmlUtils = new XMLUtils(); 146 XMLUtils xmlUtils = new XMLUtils();
136 Representation output = doGetRequestInternal(requestUrl, requestBody); 147 Representation output = doGetRequestInternal(requestUrl, requestBody);
137 Document document = xmlUtils.readDocument(output.getStream()); 148 Document document = xmlUtils.readDocument(output.getStream());
138 this.check4ExceptionReport(document); 149 this.check4ExceptionReport(document);
139 return document; 150 return document;
146 */ 157 */
147 private Representation doGetRequestInternal(String requestUrl, 158 private Representation doGetRequestInternal(String requestUrl,
148 Document requestBody) { 159 Document requestBody) {
149 Client client = new Client(Protocol.HTTP); 160 Client client = new Client(Protocol.HTTP);
150 Request request = new Request(Method.GET, requestUrl); 161 Request request = new Request(Method.GET, requestUrl);
151 if (requestBody != null){ 162 if (requestBody != null) {
152 String documentBody = new XMLUtils().writeDocument2String(requestBody); 163 String documentBody = new XMLUtils()
153 Representation representation = new StringRepresentation(documentBody); 164 .writeDocument2String(requestBody);
165 Representation representation = new StringRepresentation(
166 documentBody);
154 request.setEntity(representation); 167 request.setEntity(representation);
155 } 168 }
156 Response response = client.handle(request); 169 Response response = client.handle(request);
157 // TODO RESPONSESTATUS AUSWERTEN. 170 // TODO RESPONSESTATUS AUSWERTEN.
158 Representation output = response.getEntity(); 171 Representation output = response.getEntity();
159 return output; 172 return output;
160 } 173 }
161 174
162 /** 175 /**
163 * @throws IOException 176 * @throws IOException
164 */ 177 */
165 private InputStream doPostRequest(String requestUrl, Document requestBody) throws IOException { 178 private InputStream doPostRequest(String requestUrl, Document requestBody)
179 throws IOException {
166 log.debug("##################################################"); 180 log.debug("##################################################");
167 log.debug(new XMLUtils().writeDocument2String(requestBody)); 181 log.debug(new XMLUtils().writeDocument2String(requestBody));
168 log.debug("##################################################"); 182 log.debug("##################################################");
169 Client client = new Client(Protocol.HTTP); 183 Client client = new Client(Protocol.HTTP);
170 Request request = new Request(Method.POST, requestUrl); 184 Request request = new Request(Method.POST, requestUrl);
171 String documentBody = new XMLUtils().writeDocument2String(requestBody); 185 String documentBody = new XMLUtils().writeDocument2String(requestBody);
172 Representation representation = new StringRepresentation(documentBody); 186 Representation representation = new StringRepresentation(documentBody);
173 request.setEntity(representation); 187 request.setEntity(representation);
174 Response response = client.handle(request); 188 Response response = client.handle(request);
175 // TODO RESPONSESTATUS AUSWERTEN. 189 // TODO RESPONSESTATUS AUSWERTEN.
176 Representation output = response.getEntity(); 190 Representation output = response.getEntity();
177 return output.getStream(); 191 return output.getStream();
178 } 192 }
179 193
180 private synchronized void initialize(){ 194 private synchronized void initialize() {
181 if (!initialized){ 195 if (!initialized) {
182 PropertiesReader pr = PropertiesReaderFactory.getInstance().getPropertiesReader(); 196 PropertiesReader pr = PropertiesReaderFactory.getInstance()
183 int count = Integer.parseInt(pr.getPropertieValue(ARTIFACTDATABASE_COUNT_ID, "0")); 197 .getPropertiesReader();
198 int count = Integer.parseInt(pr.getPropertieValue(
199 ARTIFACTDATABASE_COUNT_ID, "0"));
184 artifactDatabases = new ArrayList<String>(count); 200 artifactDatabases = new ArrayList<String>(count);
185 for (int i = 0; i < count ; i++){ 201 for (int i = 0; i < count; i++) {
186 artifactDatabases.add(pr.getPropertieValue(ARTIFACTDATABASE_URL_ID+"."+(i+1), "N/N")); 202 artifactDatabases.add(pr.getPropertieValue(
203 ARTIFACTDATABASE_URL_ID + "." + (i + 1), "N/N"));
187 } 204 }
188 initialized = true; 205 initialized = true;
189 } 206 }
190 207
191 } 208 }
192 209
193 /** 210 /**
194 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#createNewArtifact(de.intevation.gnv.artifactdatabase.objects.ArtifactObject) 211 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#createNewArtifact(de.intevation.gnv.artifactdatabase.objects.ArtifactObject)
195 */ 212 */
196 public ArtifactObject createNewArtifact(ArtifactObject artifactFactory) 213 public ArtifactObject createNewArtifact(ArtifactObject artifactFactory)
197 throws ArtifactDatabaseClientException { 214 throws ArtifactDatabaseClientException {
198 215
199 try { 216 try {
200 Document request = this.createCreateRequestBody(artifactFactory.getId()); 217 Document request = this.createCreateRequestBody(artifactFactory
218 .getId());
201 Document result = doPostRequest(artifactFactory, request, "create"); 219 Document result = doPostRequest(artifactFactory, request, "create");
202 return this.getArtifact(result); 220 return this.getArtifact(result);
203 } catch (IOException e) { 221 } catch (IOException e) {
204 log.error(e,e); 222 log.error(e, e);
205 throw new ArtifactDatabaseClientException(e); 223 throw new ArtifactDatabaseClientException(e);
206 } 224 }
207 } 225 }
208 226
209 227 private ArtifactObject getArtifact(Document document) {
210 private ArtifactObject getArtifact(Document document){
211 XMLUtils xmlUtils = new XMLUtils(); 228 XMLUtils xmlUtils = new XMLUtils();
212 String uuid = xmlUtils.getStringXPath(document, "/result/uuid/@value"); 229 String uuid = xmlUtils.getStringXPath(document, "/result/uuid/@value");
213 String hash = xmlUtils.getStringXPath(document, "/result/hash/@value"); 230 String hash = xmlUtils.getStringXPath(document, "/result/hash/@value");
214 log.info("NEW Artifact: "+uuid+" / "+hash); 231 log.info("NEW Artifact: " + uuid + " / " + hash);
215 return new Artifact(uuid, hash); 232 return new Artifact(uuid, hash);
216 } 233 }
217 234
218 235 private Document createCreateRequestBody(String artifactFactoryName) {
219
220
221 private Document createCreateRequestBody(String artifactFactoryName){
222 Document document = new XMLUtils().newDocument(); 236 Document document = new XMLUtils().newDocument();
223 Node rootNode = this.createRootNode(document); 237 Node rootNode = this.createRootNode(document);
224 Element typeNode = this.createArtifactElement(document, "type"); 238 Element typeNode = this.createArtifactElement(document, "type");
225 typeNode.setAttribute("name", "create"); 239 typeNode.setAttribute("name", "create");
226 rootNode.appendChild(typeNode); 240 rootNode.appendChild(typeNode);
227 241
228 Element factoyNode = this.createArtifactElement(document, "factory"); 242 Element factoyNode = this.createArtifactElement(document, "factory");
229 factoyNode.setAttribute("name", artifactFactoryName); 243 factoyNode.setAttribute("name", artifactFactoryName);
230 rootNode.appendChild(factoyNode); 244 rootNode.appendChild(factoyNode);
231 245
232 return document; 246 return document;
233 } 247 }
234 248
235 private Element createRootNode(Document document){ 249 private Element createRootNode(Document document) {
236 Element rootNode = this.createArtifactElement(document,"action"); 250 Element rootNode = this.createArtifactElement(document, "action");
237 document.appendChild(rootNode); 251 document.appendChild(rootNode);
238 return rootNode; 252 return rootNode;
239 } 253 }
240 254
241 /** 255 /**
242 * @param document 256 * @param document
243 * @return 257 * @return
244 */ 258 */
245 private Element createArtifactElement(Document document, String name) { 259 private Element createArtifactElement(Document document, String name) {
253 * @param xmlUtils 267 * @param xmlUtils
254 * @param request 268 * @param request
255 * @throws IOException 269 * @throws IOException
256 */ 270 */
257 private Document doPostRequest(ArtifactObject artifactFactory, 271 private Document doPostRequest(ArtifactObject artifactFactory,
258 Document request, String suburl) throws IOException, ArtifactDatabaseClientException { 272 Document request, String suburl) throws IOException,
273 ArtifactDatabaseClientException {
259 XMLUtils xmlUtils = new XMLUtils(); 274 XMLUtils xmlUtils = new XMLUtils();
260 String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl(); 275 String url = ((ArtifactFactory) artifactFactory).getDataBaseUrl();
261 InputStream is = this.doPostRequest(url+"/"+suburl, request); 276 InputStream is = this.doPostRequest(url + "/" + suburl, request);
262 Document result = xmlUtils.readDocument(is); 277 Document result = xmlUtils.readDocument(is);
263 this.check4ExceptionReport(result); 278 this.check4ExceptionReport(result);
264 return result; 279 return result;
265 } 280 }
266 281
267 282 /**
268 /** 283 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getCurrentStepDescription(de.intevation.gnv.artifactdatabase.objects.ArtifactFactory,
269 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#getCurrentStepDescription(de.intevation.gnv.artifactdatabase.objects.ArtifactFactory, de.intevation.gnv.artifactdatabase.objects.ArtifactObject) 284 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject)
270 */ 285 */
271 public ArtifactDescription getCurrentStepDescription(ArtifactObject artifactFactory, 286 public ArtifactDescription getCurrentStepDescription(
272 ArtifactObject currentArtifact) 287 ArtifactObject artifactFactory, ArtifactObject currentArtifact)
273 throws ArtifactDatabaseClientException { 288 throws ArtifactDatabaseClientException {
274 try { 289 try {
275 String url = this.getArtifactUrl(artifactFactory, currentArtifact); 290 String url = this.getArtifactUrl(artifactFactory, currentArtifact);
276 Document result = this.doGetRequest(url); 291 Document result = this.doGetRequest(url);
277 return this.readDescription(result,currentArtifact); 292 return this.readDescription(result, currentArtifact);
278 } catch (IOException e) { 293 } catch (IOException e) {
279 log.error(e,e); 294 log.error(e, e);
280 throw new ArtifactDatabaseClientException(e); 295 throw new ArtifactDatabaseClientException(e);
281 } 296 }
282 } 297 }
283 298
284 /** 299 /**
286 * @param currentArtifact 301 * @param currentArtifact
287 * @return 302 * @return
288 */ 303 */
289 private String getArtifactUrl(ArtifactObject artifactFactory, 304 private String getArtifactUrl(ArtifactObject artifactFactory,
290 ArtifactObject currentArtifact) { 305 ArtifactObject currentArtifact) {
291 String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl()+"/artifact/"+ currentArtifact.getId(); 306 String url = ((ArtifactFactory) artifactFactory).getDataBaseUrl()
307 + "/artifact/" + currentArtifact.getId();
292 return url; 308 return url;
293 } 309 }
294 310
295 private ArtifactDescription readDescription(Document document, ArtifactObject artifact) throws ArtifactDatabaseClientException{ 311 private ArtifactDescription readDescription(Document document,
296 // Check if there was an Error or Exception reported from the ArtifactDatabase 312 ArtifactObject artifact) throws ArtifactDatabaseClientException {
313 // Check if there was an Error or Exception reported from the
314 // ArtifactDatabase
297 this.check4ExceptionReport(document); 315 this.check4ExceptionReport(document);
298 XMLUtils xmlUtils = new XMLUtils(); 316 XMLUtils xmlUtils = new XMLUtils();
299 if (artifact instanceof ArtifactDescription){ 317 if (artifact instanceof ArtifactDescription) {
300 ArtifactDescription ad = (ArtifactDescription)artifact; 318 ArtifactDescription ad = (ArtifactDescription) artifact;
301 Node uiNode = xmlUtils.getNodeXPath(document, "/result/ui"); 319 Node uiNode = xmlUtils.getNodeXPath(document, "/result/ui");
302 Node outputNode = xmlUtils.getNodeXPath(document, "/result/outputs"); 320 Node outputNode = xmlUtils
303 321 .getNodeXPath(document, "/result/outputs");
322
304 Map<String, OutputMode> outputModes = null; 323 Map<String, OutputMode> outputModes = null;
305 if (outputNode != null){ 324 if (outputNode != null) {
306 NodeList outputModesNodes = xmlUtils.getNodeSetXPath(outputNode, "output"); 325 NodeList outputModesNodes = xmlUtils.getNodeSetXPath(
307 if (outputModesNodes != null){ 326 outputNode, "output");
308 outputModes = new HashMap<String, OutputMode>(outputModesNodes.getLength()); 327 if (outputModesNodes != null) {
309 for (int i = 0; i < outputModesNodes.getLength(); i++){ 328 outputModes = new HashMap<String, OutputMode>(
329 outputModesNodes.getLength());
330 for (int i = 0; i < outputModesNodes.getLength(); i++) {
310 Node outputModeNode = outputModesNodes.item(i); 331 Node outputModeNode = outputModesNodes.item(i);
311 String name = xmlUtils.getStringXPath(outputModeNode, "@name"); 332 String name = xmlUtils.getStringXPath(outputModeNode,
312 String mimeType = xmlUtils.getStringXPath(outputModeNode, "@mime-type"); 333 "@name");
313 334 String mimeType = xmlUtils.getStringXPath(
314 NodeList parameterNodes = xmlUtils.getNodeSetXPath(outputModeNode, "parameter/parameter"); 335 outputModeNode, "@mime-type");
336
337 NodeList parameterNodes = xmlUtils.getNodeSetXPath(
338 outputModeNode, "parameter/parameter");
315 Collection<OutputParameter> parameter = null; 339 Collection<OutputParameter> parameter = null;
316 if (parameterNodes != null){ 340 if (parameterNodes != null) {
317 parameter = new ArrayList<OutputParameter>(parameterNodes.getLength()); 341 parameter = new ArrayList<OutputParameter>(
318 for (int j = 0; j < parameterNodes.getLength(); j++){ 342 parameterNodes.getLength());
319 Node outputParameterNode = parameterNodes.item(j); 343 for (int j = 0; j < parameterNodes.getLength(); j++) {
320 parameter.add(new DefaultOutputParameter(xmlUtils.getStringXPath(outputParameterNode,"@name"),xmlUtils.getStringXPath(outputParameterNode,"@value"),xmlUtils.getStringXPath(outputParameterNode,"@name"),xmlUtils.getStringXPath(outputParameterNode,"@type"))); 344 Node outputParameterNode = parameterNodes
345 .item(j);
346 parameter.add(new DefaultOutputParameter(
347 xmlUtils.getStringXPath(
348 outputParameterNode, "@name"),
349 xmlUtils.getStringXPath(
350 outputParameterNode, "@value"),
351 xmlUtils.getStringXPath(
352 outputParameterNode, "@name"),
353 xmlUtils.getStringXPath(
354 outputParameterNode, "@type")));
321 } 355 }
322 } 356 }
323 outputModes.put(name,new DefaultOutputMode(name, mimeType, parameter)); 357 outputModes.put(name, new DefaultOutputMode(name,
358 mimeType, parameter));
324 } 359 }
325 360
326 } 361 }
327 } 362 }
328 363
329 String currentState = xmlUtils.getStringXPath(document, "/result/state/@name"); 364 String currentState = xmlUtils.getStringXPath(document,
330 NodeList statesList = xmlUtils.getNodeSetXPath(document, "/result/reachable-states/state/@name"); 365 "/result/state/@name");
331 Collection<String> reachableStates = new ArrayList<String>(statesList.getLength()); 366 NodeList statesList = xmlUtils.getNodeSetXPath(document,
332 for (int i = 0; i < statesList.getLength(); i++){ 367 "/result/reachable-states/state/@name");
368 Collection<String> reachableStates = new ArrayList<String>(
369 statesList.getLength());
370 for (int i = 0; i < statesList.getLength(); i++) {
333 reachableStates.add(statesList.item(i).getNodeValue()); 371 reachableStates.add(statesList.item(i).getNodeValue());
334 } 372 }
335 373
336 NodeList inputNodes = xmlUtils.getNodeSetXPath(document, "/result/model/input"); 374 NodeList inputNodes = xmlUtils.getNodeSetXPath(document,
337 if (inputNodes != null){ 375 "/result/model/input");
338 Collection<String> inputParameter = new ArrayList<String>(inputNodes.getLength()); 376 if (inputNodes != null) {
339 for (int i = 0; i < inputNodes.getLength(); i++){ 377 Collection<String> inputParameter = new ArrayList<String>(
378 inputNodes.getLength());
379 for (int i = 0; i < inputNodes.getLength(); i++) {
340 Node inputNode = inputNodes.item(i); 380 Node inputNode = inputNodes.item(i);
341 String name = xmlUtils.getStringXPath(inputNode, "@name"); 381 String name = xmlUtils.getStringXPath(inputNode, "@name");
342 inputParameter.add(name); 382 inputParameter.add(name);
343 } 383 }
344 ad.setInputParameter(inputParameter); 384 ad.setInputParameter(inputParameter);
345 } 385 }
346 386
347 ad.setOutputModes(outputModes); 387 ad.setOutputModes(outputModes);
348 ad.setCurrentOut(outputNode); 388 ad.setCurrentOut(outputNode);
349 ad.setCurrentUI(uiNode); 389 ad.setCurrentUI(uiNode);
350 ad.setCurrentState(currentState); 390 ad.setCurrentState(currentState);
351 ad.setReachableStates(reachableStates); 391 ad.setReachableStates(reachableStates);
352 return ad; 392 return ad;
353 }else{ 393 } else {
354 log.error("Artifact must be Instance of ArtifactDescription"); 394 log.error("Artifact must be Instance of ArtifactDescription");
355 throw new ArtifactDatabaseClientException("Artifact must be Instance of ArtifactDescription"); 395 throw new ArtifactDatabaseClientException(
356 } 396 "Artifact must be Instance of ArtifactDescription");
357 397 }
358 398
359 } 399 }
360 400
361 /** 401 /**
362 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doNextStep(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, de.intevation.gnv.artifactdatabase.objects.ArtifactObject, java.lang.String, java.util.Collection) 402 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doNextStep(de.intevation.gnv.artifactdatabase.objects.ArtifactObject,
403 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject,
404 * java.lang.String, java.util.Collection)
363 */ 405 */
364 public ArtifactDescription doNextStep(ArtifactObject artifactFactory, 406 public ArtifactDescription doNextStep(ArtifactObject artifactFactory,
365 ArtifactObject currentArtifact, String target, 407 ArtifactObject currentArtifact, String target,
366 Collection<InputParameter> inputParameter) 408 Collection<InputParameter> inputParameter)
367 throws ArtifactDatabaseClientException { 409 throws ArtifactDatabaseClientException {
368 410
369 try { 411 try {
370 // 1 Feed 412 // 1 Feed
371 this.doFeed(artifactFactory, currentArtifact, inputParameter); 413 this.doFeed(artifactFactory, currentArtifact, inputParameter);
372 // 2 Advance 414 // 2 Advance
373 String url = this.getArtifactUrl(artifactFactory, currentArtifact); 415 String url = this.getArtifactUrl(artifactFactory, currentArtifact);
374 Document advanceDocument = this.createAdvanceRequestBody(currentArtifact, target); 416 Document advanceDocument = this.createAdvanceRequestBody(
375 InputStream advanceResult = this.doPostRequest(url, advanceDocument); 417 currentArtifact, target);
376 this.check4ExceptionReport(new XMLUtils().readDocument(advanceResult)); 418 InputStream advanceResult = this
419 .doPostRequest(url, advanceDocument);
420 this.check4ExceptionReport(new XMLUtils()
421 .readDocument(advanceResult));
377 // 3 Describe 422 // 3 Describe
378 return this.getCurrentStepDescription(artifactFactory, currentArtifact); 423 return this.getCurrentStepDescription(artifactFactory,
424 currentArtifact);
379 } catch (IOException e) { 425 } catch (IOException e) {
380 log.error(e,e); 426 log.error(e, e);
381 throw new ArtifactDatabaseClientException(e); 427 throw new ArtifactDatabaseClientException(e);
382 } 428 }
383 } 429 }
384 430
385 private Document createFeedRequestBody(ArtifactObject currentArtifact, 431 private Document createFeedRequestBody(ArtifactObject currentArtifact,
386 Collection<InputParameter> inputParameter){ 432 Collection<InputParameter> inputParameter) {
387 Document document = new XMLUtils().newDocument(); 433 Document document = new XMLUtils().newDocument();
388 Node rootNode = this.createRootNode(document); 434 Node rootNode = this.createRootNode(document);
389 435
390 Element typeNode = this.createArtifactElement(document, "type"); 436 Element typeNode = this.createArtifactElement(document, "type");
391 typeNode.setAttribute("name", "feed"); 437 typeNode.setAttribute("name", "feed");
392 rootNode.appendChild(typeNode); 438 rootNode.appendChild(typeNode);
393 439
394 Element uuidNode = this.createArtifactElement(document, "uuid"); 440 Element uuidNode = this.createArtifactElement(document, "uuid");
395 uuidNode.setAttribute("value", currentArtifact.getId()); 441 uuidNode.setAttribute("value", currentArtifact.getId());
396 rootNode.appendChild(uuidNode); 442 rootNode.appendChild(uuidNode);
397 443
398 Element hashNode = this.createArtifactElement(document, "hash"); 444 Element hashNode = this.createArtifactElement(document, "hash");
399 hashNode.setAttribute("value", currentArtifact.getHash()); 445 hashNode.setAttribute("value", currentArtifact.getHash());
400 rootNode.appendChild(hashNode); 446 rootNode.appendChild(hashNode);
401 447
402 Node dataNode = this.createParameterNodes(inputParameter, document, "data"); 448 Node dataNode = this.createParameterNodes(inputParameter, document,
449 "data");
403 rootNode.appendChild(dataNode); 450 rootNode.appendChild(dataNode);
404 451
405 return document; 452 return document;
406 } 453 }
407 454
408 /** 455 /**
409 * @param inputParameter 456 * @param inputParameter
410 * @param document 457 * @param document
411 * @param rootNode 458 * @param rootNode
412 */ 459 */
413 private Node createParameterNodes(Collection<InputParameter> inputParameter, Document document, String nodeName) { 460 private Node createParameterNodes(
461 Collection<InputParameter> inputParameter, Document document,
462 String nodeName) {
414 Element dataNode = this.createArtifactElement(document, nodeName); 463 Element dataNode = this.createArtifactElement(document, nodeName);
415 464
416 if (inputParameter != null){ 465 if (inputParameter != null) {
417 Iterator<InputParameter> it = inputParameter.iterator(); 466 Iterator<InputParameter> it = inputParameter.iterator();
418 while(it.hasNext()){ 467 while (it.hasNext()) {
419 InputParameter ip = it.next(); 468 InputParameter ip = it.next();
420 String name = ip.getName(); 469 String name = ip.getName();
421 String[] values = ip.getValues(); 470 String[] values = ip.getValues();
422 if (values != null){ 471 if (values != null) {
423 for (int i = 0; i < values.length; i++){ 472 for (int i = 0; i < values.length; i++) {
424 String value = values[i]; 473 String value = values[i];
425 Element inputNode = this.createArtifactElement(document, "input"); 474 Element inputNode = this.createArtifactElement(
475 document, "input");
426 inputNode.setAttribute("name", name); 476 inputNode.setAttribute("name", name);
427 inputNode.setAttribute("value", value); 477 inputNode.setAttribute("value", value);
428 dataNode.appendChild(inputNode); 478 dataNode.appendChild(inputNode);
429 } 479 }
430 } 480 }
431 } 481 }
432 } 482 }
433 return dataNode; 483 return dataNode;
434 } 484 }
435 485
436 private Document createAdvanceRequestBody(ArtifactObject currentArtifact, String target){ 486 private Document createAdvanceRequestBody(ArtifactObject currentArtifact,
437 Document document = new XMLUtils().newDocument(); 487 String target) {
438 Node rootNode = this.createRootNode(document); 488 Document document = new XMLUtils().newDocument();
439 489 Node rootNode = this.createRootNode(document);
440 Element typeNode = this.createArtifactElement(document, "type"); 490
441 typeNode.setAttribute("name", "advance"); 491 Element typeNode = this.createArtifactElement(document, "type");
442 rootNode.appendChild(typeNode); 492 typeNode.setAttribute("name", "advance");
443 493 rootNode.appendChild(typeNode);
444 Element uuidNode = this.createArtifactElement(document, "uuid"); 494
445 uuidNode.setAttribute("value", currentArtifact.getId()); 495 Element uuidNode = this.createArtifactElement(document, "uuid");
446 rootNode.appendChild(uuidNode); 496 uuidNode.setAttribute("value", currentArtifact.getId());
447 497 rootNode.appendChild(uuidNode);
448 Element hashNode = this.createArtifactElement(document, "hash"); 498
449 hashNode.setAttribute("value", currentArtifact.getHash()); 499 Element hashNode = this.createArtifactElement(document, "hash");
450 rootNode.appendChild(hashNode); 500 hashNode.setAttribute("value", currentArtifact.getHash());
451 Element targetNode = this.createArtifactElement(document, "target"); 501 rootNode.appendChild(hashNode);
452 targetNode.setAttribute("name", target); 502 Element targetNode = this.createArtifactElement(document, "target");
453 rootNode.appendChild(targetNode); 503 targetNode.setAttribute("name", target);
454 return document; 504 rootNode.appendChild(targetNode);
455 } 505 return document;
456 506 }
457 /** 507
458 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doOutput(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, de.intevation.gnv.artifactdatabase.objects.ArtifactObject, java.io.OutputStream, java.lang.String, java.lang.String, java.util.Collection) 508 /**
509 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doOutput(de.intevation.gnv.artifactdatabase.objects.ArtifactObject,
510 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject,
511 * java.io.OutputStream, java.lang.String, java.lang.String,
512 * java.util.Collection)
459 */ 513 */
460 public void doOutput(ArtifactObject artifactFactory, 514 public void doOutput(ArtifactObject artifactFactory,
461 ArtifactObject currentArtifact, OutputStream stream, 515 ArtifactObject currentArtifact, OutputStream stream,
462 String targetName, String mimeType, 516 String targetName, String mimeType,
463 Collection<InputParameter> inputParameter) 517 Collection<InputParameter> inputParameter)
464 throws ArtifactDatabaseClientException { 518 throws ArtifactDatabaseClientException {
465 try { 519 try {
466 XMLUtils xmlUtils = new XMLUtils(); 520 XMLUtils xmlUtils = new XMLUtils();
467 Document requestBody = this.createOutRequestBody(currentArtifact, targetName, mimeType, inputParameter); 521 Document requestBody = this.createOutRequestBody(currentArtifact,
468 522 targetName, mimeType, inputParameter);
469 String requestUrl = this.getArtifactUrl(artifactFactory, currentArtifact)+"/"+targetName; 523
470 InputStream is = this.doPostRequest( requestUrl, requestBody); 524 String requestUrl = this.getArtifactUrl(artifactFactory,
471 525 currentArtifact)
526 + "/" + targetName;
527 InputStream is = this.doPostRequest(requestUrl, requestBody);
528
472 byte[] b = new byte[4096]; 529 byte[] b = new byte[4096];
473 int i = -1 ; 530 int i = -1;
474 while ((i = is.read(b)) > 0) 531 while ((i = is.read(b)) > 0) {
475 {
476 stream.write(b, 0, i); 532 stream.write(b, 0, i);
477 } 533 }
478 } catch (IOException e) { 534 } catch (IOException e) {
479 log.error(e,e); 535 log.error(e, e);
480 throw new ArtifactDatabaseClientException(e); 536 throw new ArtifactDatabaseClientException(e);
481 } 537 }
482 } 538 }
483 539
484 private Document createOutRequestBody(ArtifactObject currentArtifact, String target, String mimeType, Collection<InputParameter> inputParameter){ 540 private Document createOutRequestBody(ArtifactObject currentArtifact,
541 String target, String mimeType,
542 Collection<InputParameter> inputParameter) {
485 Document document = new XMLUtils().newDocument(); 543 Document document = new XMLUtils().newDocument();
486 Node rootNode = this.createRootNode(document); 544 Node rootNode = this.createRootNode(document);
487 545
488 Element typeNode = this.createArtifactElement(document, "type"); 546 Element typeNode = this.createArtifactElement(document, "type");
489 typeNode.setAttribute("name", "out"); 547 typeNode.setAttribute("name", "out");
490 rootNode.appendChild(typeNode); 548 rootNode.appendChild(typeNode);
491 549
492 Element uuidNode = this.createArtifactElement(document, "uuid"); 550 Element uuidNode = this.createArtifactElement(document, "uuid");
493 uuidNode.setAttribute("value", currentArtifact.getId()); 551 uuidNode.setAttribute("value", currentArtifact.getId());
494 rootNode.appendChild(uuidNode); 552 rootNode.appendChild(uuidNode);
495 553
496 Element hashNode = this.createArtifactElement(document, "hash"); 554 Element hashNode = this.createArtifactElement(document, "hash");
497 hashNode.setAttribute("value", currentArtifact.getHash()); 555 hashNode.setAttribute("value", currentArtifact.getHash());
498 rootNode.appendChild(hashNode); 556 rootNode.appendChild(hashNode);
499 557
500 Element outNode = this.createArtifactElement(document, "out"); 558 Element outNode = this.createArtifactElement(document, "out");
501 outNode.setAttribute("name", target); 559 outNode.setAttribute("name", target);
502 rootNode.appendChild(outNode); 560 rootNode.appendChild(outNode);
503 561
504 Element mimeTypeNode = this.createArtifactElement(document, "out"); 562 Element mimeTypeNode = this.createArtifactElement(document, "out");
505 mimeTypeNode.setAttribute("value", mimeType); 563 mimeTypeNode.setAttribute("value", mimeType);
506 outNode.appendChild(mimeTypeNode); 564 outNode.appendChild(mimeTypeNode);
507 565
508 Node parameterNode = this.createParameterNodes(inputParameter, document, "params"); 566 Node parameterNode = this.createParameterNodes(inputParameter,
567 document, "params");
509 outNode.appendChild(parameterNode); 568 outNode.appendChild(parameterNode);
510 569
511 return document; 570 return document;
512 } 571 }
513 572
514 /** 573 /**
515 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doFeed(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, de.intevation.gnv.artifactdatabase.objects.ArtifactObject, java.util.Collection) 574 * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doFeed(de.intevation.gnv.artifactdatabase.objects.ArtifactObject,
575 * de.intevation.gnv.artifactdatabase.objects.ArtifactObject,
576 * java.util.Collection)
516 */ 577 */
517 public void doFeed(ArtifactObject artifactFactory, 578 public void doFeed(ArtifactObject artifactFactory,
518 ArtifactObject currentArtifact, 579 ArtifactObject currentArtifact,
519 Collection<InputParameter> inputParameter) 580 Collection<InputParameter> inputParameter)
520 throws ArtifactDatabaseClientException { 581 throws ArtifactDatabaseClientException {
521 582
522 try { 583 try {
523 Document feedDocument = this.createFeedRequestBody(currentArtifact, inputParameter); 584 Document feedDocument = this.createFeedRequestBody(currentArtifact,
585 inputParameter);
524 String url = this.getArtifactUrl(artifactFactory, currentArtifact); 586 String url = this.getArtifactUrl(artifactFactory, currentArtifact);
525 InputStream feedResult = this.doPostRequest(url, feedDocument); 587 InputStream feedResult = this.doPostRequest(url, feedDocument);
526 Document feedResultDocument = new XMLUtils().readDocument(feedResult); 588 Document feedResultDocument = new XMLUtils()
589 .readDocument(feedResult);
527 this.check4ExceptionReport(feedResultDocument); 590 this.check4ExceptionReport(feedResultDocument);
528 } catch (IOException e) { 591 } catch (IOException e) {
529 log.error(e,e); 592 log.error(e, e);
530 throw new ArtifactDatabaseClientException(e); 593 throw new ArtifactDatabaseClientException(e);
531 } 594 }
532 } 595 }
533 596
534 private void check4ExceptionReport(Document document) throws ArtifactDatabaseClientException{ 597 private void check4ExceptionReport(Document document)
535 598 throws ArtifactDatabaseClientException {
536 String message = new XMLUtils().getStringXPath(document,"/exceptionreport/exception"); 599
537 if (message != null){ 600 String message = new XMLUtils().getStringXPath(document,
601 "/exceptionreport/exception");
602 if (message != null) {
538 throw new ArtifactDatabaseClientException(message); 603 throw new ArtifactDatabaseClientException(message);
539 } 604 }
540 605
541 } 606 }
542 607
543 public Collection<ArtifactStatisticValue> calculateStatistics( 608 public Collection<ArtifactStatisticValue> calculateStatistics(
544 ArtifactObject artifactFactory, ArtifactObject currentArtifact) throws ArtifactDatabaseClientException{ 609 ArtifactObject artifactFactory, ArtifactObject currentArtifact)
545 log.debug("DefaultArtifactDatabaseClient.calculateStatistics") ; 610 throws ArtifactDatabaseClientException {
611 log.debug("DefaultArtifactDatabaseClient.calculateStatistics");
546 Collection<ArtifactStatisticValue> result; 612 Collection<ArtifactStatisticValue> result;
547 try { 613 try {
548 result = null; 614 result = null;
549 String targetName = "statistics"; 615 String targetName = "statistics";
550 String requestUrl = this.getArtifactUrl(artifactFactory, currentArtifact)+"/"+targetName; 616 String requestUrl = this.getArtifactUrl(artifactFactory,
551 Document requestBody = this.createOutRequestBody(currentArtifact, targetName, "text/xml", null); 617 currentArtifact)
618 + "/" + targetName;
619 Document requestBody = this.createOutRequestBody(currentArtifact,
620 targetName, "text/xml", null);
552 XMLUtils xmlUtils = new XMLUtils(); 621 XMLUtils xmlUtils = new XMLUtils();
553 InputStream is = this.doPostRequest(requestUrl, requestBody); 622 InputStream is = this.doPostRequest(requestUrl, requestBody);
554 Document resultDocument = xmlUtils.readDocument(is); 623 Document resultDocument = xmlUtils.readDocument(is);
555 if (resultDocument != null){ 624 if (resultDocument != null) {
556 NodeList resultNodes = xmlUtils.getNodeSetXPath(resultDocument, "/statistic-values/statistic"); 625 NodeList resultNodes = xmlUtils.getNodeSetXPath(resultDocument,
557 if (resultNodes != null){ 626 "/statistic-values/statistic");
558 result = new ArrayList<ArtifactStatisticValue>(resultNodes.getLength()); 627 if (resultNodes != null) {
559 for (int i = 0; i < resultNodes.getLength(); i++){ 628 result = new ArrayList<ArtifactStatisticValue>(resultNodes
629 .getLength());
630 for (int i = 0; i < resultNodes.getLength(); i++) {
560 Node statisticNode = resultNodes.item(i); 631 Node statisticNode = resultNodes.item(i);
561 String name = xmlUtils.getStringXPath(statisticNode, "@name"); 632 String name = xmlUtils.getStringXPath(statisticNode,
562 String value = xmlUtils.getStringXPath(statisticNode, "@value"); 633 "@name");
563 result.add(new DefaultArtifactStatisticValue(name, value)); 634 String value = xmlUtils.getStringXPath(statisticNode,
635 "@value");
636 result.add(new DefaultArtifactStatisticValue(name,
637 value));
564 } 638 }
565 } 639 }
566 } 640 }
567 } catch (IOException e) { 641 } catch (IOException e) {
568 log.error(e,e); 642 log.error(e, e);
569 throw new ArtifactDatabaseClientException(e); 643 throw new ArtifactDatabaseClientException(e);
570 } 644 }
571 645
572 return result; 646 return result;
573 } 647 }
574 } 648 }

http://dive4elements.wald.intevation.org