9026
|
1 /* |
|
2 * Copyright (c) 2010 by Intevation GmbH |
|
3 * |
|
4 * This program is free software under the LGPL (>=v2.1) |
|
5 * Read the file LGPL.txt coming with the software for details |
|
6 * or visit http://www.gnu.org/licenses/ if it does not exist. |
|
7 */ |
|
8 |
|
9 package org.dive4elements.river.client.test; |
|
10 |
|
11 import java.io.File; |
9027
|
12 import java.io.FileOutputStream; |
9026
|
13 import java.io.IOException; |
9027
|
14 import java.io.InputStreamReader; |
|
15 import java.io.OutputStream; |
|
16 import java.io.OutputStreamWriter; |
9026
|
17 import java.util.ArrayList; |
9027
|
18 import java.util.HashMap; |
9026
|
19 |
|
20 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; |
|
21 import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; |
|
22 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; |
|
23 import org.dive4elements.artifacts.httpclient.http.HttpClient; |
|
24 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; |
|
25 import org.dive4elements.artifacts.httpclient.http.response.DocumentResponseHandler; |
|
26 import org.dive4elements.artifacts.httpclient.utils.XMLUtils; |
9027
|
27 import org.dive4elements.river.client.client.ui.NilDatacageTwinPanelInfo; |
|
28 import org.dive4elements.river.client.client.ui.RecommandationUtils; |
9026
|
29 import org.dive4elements.river.client.server.AdvanceServiceImpl; |
|
30 import org.dive4elements.river.client.server.ArtifactHelper; |
|
31 import org.dive4elements.river.client.server.CollectionHelper; |
|
32 import org.dive4elements.river.client.server.CreateCollectionServiceImpl; |
|
33 import org.dive4elements.river.client.server.FLYSArtifactCreator; |
|
34 import org.dive4elements.river.client.server.FeedServiceImpl; |
9027
|
35 import org.dive4elements.river.client.server.LoadArtifactServiceImpl; |
9026
|
36 import org.dive4elements.river.client.server.auth.DefaultUser; |
|
37 import org.dive4elements.river.client.server.auth.User; |
|
38 import org.dive4elements.river.client.server.auth.UserClient; |
|
39 import org.dive4elements.river.client.shared.exceptions.ServerException; |
|
40 import org.dive4elements.river.client.shared.model.Artifact; |
|
41 import org.dive4elements.river.client.shared.model.Collection; |
|
42 import org.dive4elements.river.client.shared.model.Data; |
|
43 import org.dive4elements.river.client.shared.model.DataItem; |
|
44 import org.dive4elements.river.client.shared.model.DataList; |
|
45 import org.dive4elements.river.client.shared.model.DefaultCollection; |
|
46 import org.dive4elements.river.client.shared.model.DefaultDataItem; |
|
47 import org.dive4elements.river.client.shared.model.OutputMode; |
|
48 import org.dive4elements.river.client.shared.model.Recommendation; |
|
49 import org.dive4elements.river.client.shared.model.StringOptionsData; |
|
50 import org.w3c.dom.Document; |
|
51 import org.w3c.dom.Element; |
|
52 |
|
53 /** |
|
54 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
|
55 */ |
|
56 public class SinfoProof { |
|
57 |
|
58 private static final String serverUrl = "http://localhost:8181"; |
|
59 private static final String locale = "de"; |
|
60 |
|
61 public static void main(final String[] args) throws ServerException, IOException { |
9027
|
62 // logger.info("Starting console client."); |
9026
|
63 |
|
64 final User user = new DefaultUser("belger", "belger", null, false, new ArrayList<String>(), new ArrayList<String>()); |
|
65 final UserClient userClient = new UserClient(serverUrl); |
|
66 final Element userElement = userClient.findUser(user); |
|
67 final String userUuid = userElement.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "uuid"); |
|
68 |
|
69 /* Init Collection */ |
|
70 final Collection collection = createCollection(serverUrl, locale, userUuid); |
|
71 final org.dive4elements.river.client.shared.model.Artifact sinfoArtifact = ArtifactHelper.createArtifact(serverUrl, locale, "sinfo", null); |
|
72 final Collection newColl = CollectionHelper.addArtifact(collection, sinfoArtifact, serverUrl, locale); // wichtig; sorgt für Persistenz |
|
73 |
|
74 /* Select River */ |
|
75 final Data data = new StringOptionsData("river", "river", new DataItem[] { new DefaultDataItem("Beispielfluss", "Beispielfluss", "Beispielfluss") }); |
|
76 final Artifact calcModeArtifact = feedAndGo(sinfoArtifact, new Data[] { data }, 0); |
|
77 |
|
78 /* Select CalcMode */ |
|
79 final DataList calcModes = calcModeArtifact.getArtifactDescription().getCurrentData(); // AUSWAHL-Möglichkeiten |
|
80 final DataItem minMaxFlowdepth = calcModes.get(0).getItems()[2]; |
|
81 final Data dataCalcMode = new StringOptionsData("calculation_mode", "calculation_mode", new DataItem[] { minMaxFlowdepth }); |
|
82 final Artifact rangeArtifact = feedAndGo(calcModeArtifact, new Data[] { dataCalcMode }, 0); |
|
83 |
9027
|
84 /* Select Range */ |
9026
|
85 |
|
86 // entweder eine bestimmte Range |
9027
|
87 final Data dataFrom = new StringOptionsData("ld_from", "ld_from", new DataItem[] { new DefaultDataItem("10", "10", "10") }); |
9026
|
88 final Data dataTo = new StringOptionsData("ld_to", "ld_to", new DataItem[] { new DefaultDataItem("100", "100", "100") }); |
|
89 final Data[] rangeFromToDetermined = new Data[] { dataFrom, dataTo }; |
|
90 |
|
91 // oder die maxRange |
|
92 final DataList list = rangeArtifact.getArtifactDescription().getCurrentData(); |
|
93 final Data[] rangeMax = new Data[] { list.get(0), list.get(1) }; |
9027
|
94 |
|
95 final Artifact dataChoiceArtifact = feedAndGo(rangeArtifact, rangeFromToDetermined, 0); |
9026
|
96 |
|
97 /* Select Fixpunkte */ |
9027
|
98 final Recommendation rec1 = new Recommendation("staticwqkms", "additionals-wstv-0-103", "sinfo_flowdepth_waterlevels"); |
|
99 final Recommendation rec2 = new Recommendation("bedheight", "bedheight-single-36-2015-FP-2015_0-502", "sinfo_flowdepthminmax_heights"); |
9026
|
100 |
9027
|
101 final Artifact[] artifacts = loadMany(collection, new Recommendation[] { rec1, rec2 }, null); |
|
102 |
|
103 // rec1.getDisplayName() TODO: makeDisplayName |
|
104 final String rec1String = RecommandationUtils.createDataString(artifacts[0].getUuid(), rec1, new NilDatacageTwinPanelInfo("xxxx")); |
|
105 final String rec2String = RecommandationUtils.createDataString(artifacts[1].getUuid(), rec2, new NilDatacageTwinPanelInfo("xxxx")); |
|
106 // TODO: check display name |
|
107 final String combinedIdNeu = rec1String + "#" + rec2String; |
|
108 |
|
109 final Data pair = new StringOptionsData("diffids", "diffids", new DataItem[] { new DefaultDataItem(combinedIdNeu, combinedIdNeu, combinedIdNeu) }); |
|
110 final Artifact exportArtifact = feedAndGo(dataChoiceArtifact, new Data[] { pair }, 0); |
|
111 |
|
112 // Describe collection |
|
113 describeCollection(collection); // wichtig, damit die Facets erzeugt werden |
|
114 |
|
115 // /* Export calculation */ |
|
116 final OutputMode[] modes = exportArtifact.getArtifactDescription().getOutputModes(); |
|
117 if (modes != null) { |
|
118 final OutputMode mode = modes[1]; // output.sinfo_flowdepthminmax_export |
|
119 doGet(mode.getName(), collection.identifier()); |
9026
|
120 } |
9027
|
121 } |
9026
|
122 |
9027
|
123 private static Collection describeCollection(final Collection collection) throws ConnectionException { |
9026
|
124 |
9027
|
125 final String uuid = collection.identifier(); |
|
126 |
|
127 final Document describe = ClientProtocolUtils.newDescribeCollectionDocument(uuid); |
|
128 |
|
129 final HttpClient client = new HttpClientImpl(serverUrl, locale); |
|
130 |
|
131 final Document response = (Document) client.doCollectionAction(describe, uuid, new DocumentResponseHandler()); |
|
132 |
|
133 final Collection c = CollectionHelper.parseCollection(response); |
|
134 |
|
135 return c; |
9026
|
136 } |
|
137 |
|
138 private static Artifact feedAndGo(final Artifact inputArtifact, final Data[] data, final int reachableStateIndex) |
|
139 throws ConnectionException, ServerException { |
|
140 final Artifact artifact = feed(serverUrl, locale, inputArtifact, data); |
|
141 return advance(serverUrl, locale, artifact, getReachableStateByIndex(artifact, reachableStateIndex)); |
|
142 } |
|
143 |
|
144 private static String getReachableStateByIndex(final Artifact artifact, final int index) { |
|
145 |
|
146 final String[] states = artifact.getArtifactDescription().getReachableStates(); |
|
147 if (states != null) { |
|
148 if (states.length > index) { |
|
149 return states[index]; |
|
150 } else { |
|
151 return states[0]; |
|
152 } |
|
153 } else { |
|
154 return ""; |
|
155 } |
|
156 } |
|
157 |
|
158 public static Collection createCollection(final String url, final String locale, final String ownerId) throws ConnectionException { |
|
159 final Document create = ClientProtocolUtils.newCreateCollectionDocument(null); |
|
160 final HttpClient client = new HttpClientImpl(url, locale); |
|
161 final Document doc = (Document) client.createCollection(create, ownerId, new DocumentResponseHandler()); |
|
162 final String uuid = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_UUID, ArtifactNamespaceContext.INSTANCE); |
|
163 final String ttlStr = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_TTL, ArtifactNamespaceContext.INSTANCE); |
|
164 |
|
165 return new DefaultCollection(uuid, Long.valueOf(ttlStr), uuid); |
|
166 } |
|
167 |
|
168 public static Artifact feed(final String url, final String locale, final Artifact artifact, final Data[] data) throws ServerException, ConnectionException { |
|
169 final Document feed = ClientProtocolUtils.newFeedDocument(artifact.getUuid(), artifact.getHash(), createKVP(data)); |
|
170 |
|
171 final HttpClient client = new HttpClientImpl(url, locale); |
|
172 |
|
173 final Document description = (Document) client.feed(new org.dive4elements.artifacts.httpclient.objects.Artifact(artifact.getUuid(), artifact.getHash()), |
|
174 feed, new DocumentResponseHandler()); |
|
175 |
|
176 final String result = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); |
|
177 |
|
178 if (result == null || !result.equals(FeedServiceImpl.OPERATION_FAILURE)) { |
|
179 return (Artifact) new FLYSArtifactCreator().create(description); |
|
180 } else if (result != null && result.equals(FeedServiceImpl.OPERATION_FAILURE)) { |
|
181 final String msg = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT_MSG, ArtifactNamespaceContext.INSTANCE); |
|
182 throw new ServerException(msg); |
|
183 } |
|
184 |
|
185 throw new ServerException(FeedServiceImpl.ERROR_FEED_DATA); |
|
186 } |
|
187 |
|
188 protected static String[][] createKVP(final Data[] data) { |
|
189 if (data != null) { |
|
190 final String[][] kvp = new String[data.length][]; |
|
191 |
|
192 int i = 0; |
|
193 |
|
194 for (final Data d : data) { |
|
195 final DataItem[] items = d.getItems(); |
|
196 final String key = d.getLabel(); |
|
197 final String value = d.getStringValue(); |
|
198 |
|
199 kvp[i++] = new String[] { key, value }; |
|
200 } |
|
201 |
|
202 return kvp; |
|
203 } |
|
204 return null; |
|
205 } |
|
206 |
|
207 public static Artifact advance(final String url, final String locale, final Artifact artifact, final String target) |
|
208 throws ConnectionException, ServerException { |
|
209 final Document advance = ClientProtocolUtils.newAdvanceDocument(artifact.getUuid(), artifact.getHash(), target); |
|
210 final HttpClient client = new HttpClientImpl(url, locale); |
|
211 |
|
212 final Document description = (Document) client.advance( |
|
213 new org.dive4elements.artifacts.httpclient.objects.Artifact(artifact.getUuid(), artifact.getHash()), advance, new DocumentResponseHandler()); |
|
214 |
|
215 if (description == null) { |
|
216 throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT); |
|
217 } |
|
218 |
|
219 final String result = XMLUtils.xpathString(description, AdvanceServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); |
|
220 |
|
221 if (result == null || !result.equals(AdvanceServiceImpl.OPERATION_FAILURE)) { |
|
222 return (Artifact) new FLYSArtifactCreator().create(description); |
|
223 } |
|
224 |
|
225 throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT); |
|
226 } |
|
227 |
9027
|
228 public static Artifact[] loadMany(final Collection parent, final Recommendation[] recoms, final String factory) throws ServerException { |
|
229 |
|
230 final ArrayList<Artifact> artifacts = new ArrayList<Artifact>(); |
|
231 final HashMap<Recommendation, Artifact> cloneMap = new HashMap<Recommendation, Artifact>(); |
|
232 |
|
233 for (final Recommendation recom : recoms) { |
|
234 |
|
235 final Artifact prevClone = cloneMap.get(recom); |
|
236 if (prevClone != null) { |
|
237 |
|
238 artifacts.add(prevClone); |
|
239 } else { |
|
240 // Not already cloned. |
|
241 final String realFactory = factory != null ? factory : recom.getFactory(); |
|
242 |
|
243 final Artifact clone = ArtifactHelper.createArtifact(serverUrl, locale, realFactory, recom); |
|
244 |
|
245 if (clone != null) { |
|
246 final Collection c = CollectionHelper.addArtifact(parent, clone, serverUrl, locale); |
|
247 |
|
248 if (c != null) { |
|
249 artifacts.add(clone); |
|
250 // Remember we cloned a recommendation like this. |
|
251 cloneMap.put(recom, clone); |
|
252 } else { |
|
253 throw new ServerException(LoadArtifactServiceImpl.ERROR_LOAD_ARTIFACT); |
|
254 } |
|
255 } |
|
256 } |
|
257 } |
|
258 return artifacts.toArray(new Artifact[artifacts.size()]); |
|
259 } |
|
260 |
|
261 /// ExportServiceImpl |
|
262 |
|
263 public static void doGet(final String mode, final String collectionId) { |
|
264 |
|
265 try { |
|
266 |
|
267 final String name = mode; |
|
268 final String type = "csv"; |
|
269 |
|
270 final String fn = name + "." + type; // TODO: make filename unique |
|
271 final String enc = "windows-1252";// req.getParameter("encoding"); |
|
272 |
|
273 final OutputStream out = new FileOutputStream(new File("D:" + File.separator + fn)); |
|
274 final Document attr = null; |
|
275 final Document request = ClientProtocolUtils.newOutCollectionDocument(collectionId, mode, type, attr); |
|
276 final HttpClient client = new HttpClientImpl(serverUrl, locale); |
|
277 |
|
278 if (enc != null) { |
|
279 final InputStreamReader in = new InputStreamReader(client.collectionOut(request, collectionId, mode), "UTF-8"); |
|
280 try { |
|
281 final OutputStreamWriter encOut = new OutputStreamWriter(out, enc); |
|
282 final char buf[] = new char[4096]; |
|
283 int c; |
|
284 while ((c = in.read(buf, 0, buf.length)) >= 0) { |
|
285 encOut.write(buf, 0, c); |
|
286 } |
|
287 encOut.flush(); |
|
288 encOut.close(); |
|
289 } finally { |
|
290 in.close(); |
|
291 } |
|
292 } |
|
293 } |
|
294 catch (final IOException ioe) { |
|
295 ioe.printStackTrace(); |
|
296 } |
|
297 } |
|
298 |
9026
|
299 } |