comparison flys-client/src/main/java/org/dive4elements/river/client/server/FeedServiceImpl.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/FeedServiceImpl.java@d0a9acddbea2
children 821a02bbfb4e
comparison
equal deleted inserted replaced
5833:a2bdc0f524e8 5834:f507086aa94b
1 package de.intevation.flys.client.server;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.w3c.dom.Document;
7
8 import org.apache.log4j.Logger;
9
10 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
11
12 import de.intevation.artifacts.common.ArtifactNamespaceContext;
13 import de.intevation.artifacts.common.utils.ClientProtocolUtils;
14 import de.intevation.artifacts.common.utils.XMLUtils;
15
16 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
17 import de.intevation.artifacts.httpclient.http.HttpClient;
18 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
19 import de.intevation.artifacts.httpclient.http.response.DocumentResponseHandler;
20
21 import de.intevation.flys.client.shared.exceptions.ServerException;
22 import de.intevation.flys.client.shared.model.Artifact;
23 import de.intevation.flys.client.shared.model.Data;
24 import de.intevation.flys.client.shared.model.DataItem;
25 import de.intevation.flys.client.client.services.FeedService;
26
27 /**
28 * This interface provides a method that bundles the artifact specific
29 * operation FEED.
30 */
31 public class FeedServiceImpl
32 extends RemoteServiceServlet
33 implements FeedService
34 {
35 private static final Logger logger = Logger.getLogger(FeedServiceImpl.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 triggers the FEED operation.
54 *
55 * @param artifact The artifact that needs to be fed.
56 * @param data An array of Data objects that contain the information that
57 * are used for the FEED operation.
58 *
59 * @return a new artifact parsed from the description of FEED.
60 */
61 public Artifact feed(
62 String locale,
63 Artifact artifact,
64 Data[] data)
65 throws ServerException
66 {
67 logger.info("StepForwardServiceImpl.feed");
68
69 String url = getServletContext().getInitParameter("server-url");
70
71 Document feed = ClientProtocolUtils.newFeedDocument(
72 artifact.getUuid(),
73 artifact.getHash(),
74 createKVP(data));
75
76 HttpClient client = new HttpClientImpl(url, locale);
77
78 try {
79 Document description = (Document) client.feed(
80 new de.intevation.artifacts.httpclient.objects.Artifact(
81 artifact.getUuid(),
82 artifact.getHash()),
83 feed,
84 new DocumentResponseHandler());
85
86 if (description == null) {
87 logger.warn("StepForwardService.feed() - FAILED");
88 throw new ServerException(ERROR_FEED_DATA);
89 }
90
91 String result = XMLUtils.xpathString(
92 description,
93 XPATH_RESULT,
94 ArtifactNamespaceContext.INSTANCE);
95
96 if (result == null || !result.equals(OPERATION_FAILURE)) {
97 logger.debug("StepForwardService.feed() - SUCCESS");
98 return (Artifact) new FLYSArtifactCreator().create(description);
99 }
100 else if (result != null && result.equals(OPERATION_FAILURE)) {
101 String msg = XMLUtils.xpathString(
102 description,
103 XPATH_RESULT_MSG,
104 ArtifactNamespaceContext.INSTANCE);
105 throw new ServerException(msg);
106 }
107 }
108 catch (ConnectionException ce) {
109 logger.error(ce, ce);
110 }
111
112 logger.warn("StepForwardService.feed() - FAILED");
113 throw new ServerException(ERROR_FEED_DATA);
114 }
115
116
117 /**
118 * Triggers FEED operation, many artifacts, same data item(s).
119 *
120 * @param artifacts Artifacts that shall be fed.
121 * @param data An array of Data objects that contain the information that
122 * are used for the FEED operation.
123 *
124 * @return a new artifact parsed from the description of FEED.
125 */
126 public List<Artifact> feedMany(
127 String locale,
128 List<Artifact> artifacts,
129 Data[] data)
130 throws ServerException
131 {
132 logger.info("StepForwardServiceImpl.feedMany");
133
134 String url = getServletContext().getInitParameter("server-url");
135
136 List<Artifact> resultArtifacts = new ArrayList<Artifact>();
137
138 for (Artifact artifact: artifacts) {
139 logger.info("feedMany: Relay to StepForwardServiceImpl.feed");
140 Artifact fedArtifact = feed(locale, artifact, data);
141 resultArtifacts.add(fedArtifact);
142 }
143
144 return resultArtifacts;
145 }
146
147
148 /**
149 * This method creates an array of key/value pairs from an array of Data
150 * objects. The string array is used as parameter for the feed() operation.
151 *
152 * @param data The data that should be transformed into the string array.
153 *
154 * @return a string array that contains key/value pairs.
155 */
156 protected String[][] createKVP(Data[] data) {
157 String[][] kvp = new String[data.length][];
158
159 int i = 0;
160
161 for (Data d: data) {
162 DataItem[] items = d.getItems();
163 String key = d.getLabel();
164 String value = d.getStringValue();
165
166 kvp[i++] = new String[] { key, value };
167 }
168
169 return kvp;
170 }
171 }
172 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org