comparison flys-client/src/main/java/org/dive4elements/river/client/server/StepForwardServiceImpl.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/StepForwardServiceImpl.java@d0a9acddbea2
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
5 import org.apache.log4j.Logger;
6
7 import de.intevation.artifacts.common.ArtifactNamespaceContext;
8 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
9 import de.intevation.artifacts.common.utils.XMLUtils;
10
11 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
12 import de.intevation.artifacts.httpclient.http.HttpClient;
13 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
14 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
15
16 import de.intevation.flys.client.shared.exceptions.ServerException;
17 import de.intevation.flys.client.shared.model.Artifact;
18 import de.intevation.flys.client.shared.model.ArtifactDescription;
19 import de.intevation.flys.client.shared.model.Data;
20 import de.intevation.flys.client.shared.model.DataItem;
21 import de.intevation.flys.client.client.services.StepForwardService;
22
23
24 /**
25 * This interface provides a method that bundles the artifact specific
26 * operations FEED and ADVANCE.
27 *
28 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
29 */
30 public class StepForwardServiceImpl
31 extends AdvanceServiceImpl
32 implements StepForwardService
33 {
34 private static final Logger logger =
35 Logger.getLogger(StepForwardServiceImpl.class);
36
37
38 /** XPath that points to the result type of a feed or advance operation.*/
39 public static final String XPATH_RESULT = "/art:result/@art:type";
40
41 /** XPath that points to the result type of a feed or advance operation.*/
42 public static final String XPATH_RESULT_MSG = "/art:result/text()";
43
44 /** A constant that marks errors.*/
45 public static final String OPERATION_FAILURE = "FAILURE";
46
47 /** The error message key that is thrown if an error occured while feeding
48 * new data.*/
49 public static final String ERROR_FEED_DATA = "error_feed_data";
50
51
52 /**
53 * This method wraps the artifact operations FEED and ADVANCE. FEED is
54 * always triggerd, ADVANCE only, if there is at least one reachable state.
55 *
56 * @param locale The locale used for the request.
57 * @param artifact The artifact that needs to be fed.
58 * @param data An array of Data objects that contain the information that
59 *
60 * @return the modified artifact.
61 */
62 public Artifact go(String locale, Artifact artifact, Data[] data)
63 throws ServerException
64 {
65 logger.info("StepForwardServiceImpl.go");
66
67 String url = getServletContext().getInitParameter("server-url");
68
69 Artifact afterFeed = feed(url, locale, artifact, data);
70
71 if (afterFeed == null) {
72 logger.warn("StepForwardService.feed() - FAILED");
73 throw new ServerException(ERROR_FEED_DATA);
74 }
75
76 ArtifactDescription desc = afterFeed.getArtifactDescription();
77 String[] reachable = desc.getReachableStates();
78
79 if (reachable == null || reachable.length == 0) {
80 logger.debug("Did not find any reachable state.");
81 return afterFeed;
82 }
83
84 // We use the first reachable state as default target, maybe we need to
85 // change this later.
86 return advance(locale, afterFeed, reachable[0]);
87 }
88
89
90 /**
91 * This method triggers the FEED operation.
92 *
93 * @param url The url of the artifact server.
94 * @param artifact The artifact that needs to be fed.
95 * @param data An array of Data objects that contain the information that
96 * are used for the FEED operation.
97 *
98 * @return a new artifact parsed from the description of FEED.
99 */
100 protected Artifact feed(
101 String url,
102 String locale,
103 Artifact artifact,
104 Data[] data)
105 throws ServerException
106 {
107 logger.info("StepForwardServiceImpl.feed");
108
109 Document feed = ClientProtocolUtils.newFeedDocument(
110 artifact.getUuid(),
111 artifact.getHash(),
112 createKVP(data));
113
114 HttpClient client = new HttpClientImpl(url, locale);
115
116 try {
117 Document description = (Document) client.feed(
118 new de.intevation.artifacts.httpclient.objects.Artifact(
119 artifact.getUuid(),
120 artifact.getHash()),
121 feed,
122 new DocumentResponseHandler());
123
124 if (description == null) {
125 logger.warn("StepForwardService.feed() - FAILED");
126 throw new ServerException(ERROR_FEED_DATA);
127 }
128
129 String result = XMLUtils.xpathString(
130 description,
131 XPATH_RESULT,
132 ArtifactNamespaceContext.INSTANCE);
133
134 if (result == null || !result.equals(OPERATION_FAILURE)) {
135 logger.debug("StepForwardService.feed() - SUCCESS");
136 return (Artifact) new FLYSArtifactCreator().create(description);
137 }
138 else if (result != null && result.equals(OPERATION_FAILURE)) {
139 String msg = XMLUtils.xpathString(
140 description,
141 XPATH_RESULT_MSG,
142 ArtifactNamespaceContext.INSTANCE);
143 throw new ServerException(msg);
144 }
145 }
146 catch (ConnectionException ce) {
147 logger.error(ce, ce);
148 }
149
150 logger.warn("StepForwardService.feed() - FAILED");
151 throw new ServerException(ERROR_FEED_DATA);
152 }
153
154
155 /**
156 * This method creates an array of key/value pairs from an array of Data
157 * objects. The string array is used as parameter for the feed() operation.
158 *
159 * @param data The data that should be transformed into the string array.
160 *
161 * @return a string array that contains key/value pairs.
162 */
163 protected String[][] createKVP(Data[] data) {
164 String[][] kvp = new String[data.length][];
165
166 int i = 0;
167
168 for (Data d: data) {
169 DataItem[] items = d.getItems();
170 String key = d.getLabel();
171 String value = d.getStringValue();
172
173 kvp[i++] = new String[] { key, value };
174 }
175
176 return kvp;
177 }
178 }
179 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org