comparison flys-client/src/main/java/de/intevation/flys/client/server/StepForwardServiceImpl.java @ 32:88c530c25968

Added a service for the ADVANCE operation and a service that bundles FEED and ADVANCE in a single service. flys-client/trunk@1451 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 10 Mar 2011 10:53:41 +0000
parents
children e02f50a3ad59
comparison
equal deleted inserted replaced
31:e8745ca02f2b 32:88c530c25968
1 package de.intevation.flys.client.server;
2
3 import org.w3c.dom.Document;
4
5 import de.intevation.artifacts.common.ArtifactNamespaceContext;
6 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
7 import de.intevation.artifacts.common.utils.XMLUtils;
8
9 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
10 import de.intevation.artifacts.httpclient.http.HttpClient;
11 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
12 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
13
14 import de.intevation.flys.client.shared.model.Artifact;
15 import de.intevation.flys.client.shared.model.ArtifactDescription;
16 import de.intevation.flys.client.shared.model.Data;
17 import de.intevation.flys.client.shared.model.DataItem;
18 import de.intevation.flys.client.client.services.StepForwardService;
19
20
21 /**
22 * This interface provides a method that bundles the artifact specific
23 * operations FEED and ADVANCE.
24 *
25 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
26 */
27 public class StepForwardServiceImpl
28 extends AdvanceServiceImpl
29 implements StepForwardService
30 {
31 /** XPath that points to the result text of a feed or advance operation.*/
32 public static final String XPATH_RESULT = "/art:result/text()";
33
34 /** A constant that marks errors.*/
35 public static final String OPERATION_FAILURE = "FAILED";
36
37
38 /**
39 * This method wraps the artifact operations FEED and ADVANCE. FEED is
40 * always triggerd, ADVANCE only, if there is at least one reachable state.
41 *
42 * @param serverUrl The url of the artifact server.
43 * @param artifact The artifact that needs to be fed.
44 * @param data An array of Data objects that contain the information that
45 *
46 * @return the modified artifact.
47 */
48 public Artifact go(String serverUrl, Artifact artifact, Data[] data) {
49 Artifact afterFeed = feed(serverUrl, artifact, data);
50
51 if (afterFeed == null) {
52 System.err.println("StepForwardService.feed() - FAILED");
53 // XXX Better handling here!
54 return null;
55 }
56
57 ArtifactDescription desc = afterFeed.getArtifactDescription();
58 String[] reachable = desc.getReachableStates();
59
60 if (reachable == null || reachable.length == 0) {
61 System.out.println("Did not find any reachable state.");
62 return afterFeed;
63 }
64
65 // We use the first reachable state as default target, maybe we need to
66 // change this later.
67 return advance(serverUrl, afterFeed, reachable[0]);
68 }
69
70
71 /**
72 * This method triggers the FEED operation.
73 *
74 * @param serverUrl The url of the artifact server.
75 * @param artifact The artifact that needs to be fed.
76 * @param data An array of Data objects that contain the information that
77 * are used for the FEED operation.
78 *
79 * @return a new artifact parsed from the description of FEED.
80 */
81 protected Artifact feed(String serverUrl, Artifact artifact, Data[] data) {
82 Document feed = ClientProtocolUtils.newFeedDocument(
83 artifact.getUuid(),
84 artifact.getHash(),
85 createKVP(data));
86
87 HttpClient client = new HttpClientImpl(serverUrl);
88
89 try {
90 Document description = (Document) client.feed(
91 new de.intevation.artifacts.httpclient.objects.Artifact(
92 artifact.getUuid(),
93 artifact.getHash()),
94 feed,
95 new DocumentResponseHandler());
96
97 if (description == null) {
98 System.err.println("StepForwardService.feed() - FAILED");
99 return artifact;
100 }
101
102 String result = XMLUtils.xpathString(
103 description,
104 XPATH_RESULT,
105 ArtifactNamespaceContext.INSTANCE);
106
107 if (result == null || !result.equals(OPERATION_FAILURE)) {
108 System.out.println("StepForwardService.feed() - SUCCESS");
109 return (Artifact) new FLYSArtifactCreator().create(description);
110 }
111 }
112 catch (ConnectionException ce) {
113 System.err.println(ce.getLocalizedMessage());
114 }
115
116 System.err.println("StepForwardService.feed() - FAILED");
117
118 return artifact;
119 }
120
121
122 /**
123 * This method creates an array of key/value pairs from an array of Data
124 * objects. The string array is used as parameter for the feed() operation.
125 *
126 * @param data The data that should be transformed into the string array.
127 *
128 * @return a string array that contains key/value pairs.
129 */
130 protected String[][] createKVP(Data[] data) {
131 String[][] kvp = new String[data.length][];
132
133 int i = 0;
134
135 for (Data d: data) {
136 DataItem[] items = d.getItems();
137 String key = d.getLabel();
138 String value = items[0].getStringValue();
139
140 kvp[i++] = new String[] { key, value };
141 }
142
143 return kvp;
144 }
145 }
146 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org