comparison gwt-client/src/main/java/org/dive4elements/river/client/test/SinfoProof.java @ 9026:679b1442c886

local backup
author gernotbelger
date Wed, 25 Apr 2018 11:58:49 +0200
parents
children 274ddafb719b
comparison
equal deleted inserted replaced
9025:4de5bdd027ff 9026:679b1442c886
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;
12 import java.io.IOException;
13 import java.net.MalformedURLException;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.apache.log4j.Logger;
18 import org.apache.log4j.PropertyConfigurator;
19 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
20 import org.dive4elements.artifacts.common.utils.ClientProtocolUtils;
21 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException;
22 import org.dive4elements.artifacts.httpclient.http.HttpClient;
23 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl;
24 import org.dive4elements.artifacts.httpclient.http.response.DocumentResponseHandler;
25 import org.dive4elements.artifacts.httpclient.utils.Configuration;
26 import org.dive4elements.artifacts.httpclient.utils.XMLUtils;
27 import org.dive4elements.river.client.server.AdvanceServiceImpl;
28 import org.dive4elements.river.client.server.ArtifactHelper;
29 import org.dive4elements.river.client.server.CollectionHelper;
30 import org.dive4elements.river.client.server.CreateCollectionServiceImpl;
31 import org.dive4elements.river.client.server.FLYSArtifactCreator;
32 import org.dive4elements.river.client.server.FeedServiceImpl;
33 import org.dive4elements.river.client.server.auth.DefaultUser;
34 import org.dive4elements.river.client.server.auth.User;
35 import org.dive4elements.river.client.server.auth.UserClient;
36 import org.dive4elements.river.client.shared.exceptions.ServerException;
37 import org.dive4elements.river.client.shared.model.Artifact;
38 import org.dive4elements.river.client.shared.model.Collection;
39 import org.dive4elements.river.client.shared.model.Data;
40 import org.dive4elements.river.client.shared.model.DataItem;
41 import org.dive4elements.river.client.shared.model.DataList;
42 import org.dive4elements.river.client.shared.model.DefaultCollection;
43 import org.dive4elements.river.client.shared.model.DefaultDataItem;
44 import org.dive4elements.river.client.shared.model.OutputMode;
45 import org.dive4elements.river.client.shared.model.Recommendation;
46 import org.dive4elements.river.client.shared.model.StringOptionsData;
47 import org.w3c.dom.Document;
48 import org.w3c.dom.Element;
49
50 /**
51 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
52 */
53 public class SinfoProof {
54 /**
55 * The logging is done via Log4j. To configure the logging
56 * a file 'log4j.properties' is search in the configuration directory.
57 */
58 public static final String LOG4J_PROPERTIES = "log4j.properties";
59
60 /**
61 * The path of the configuration directory.
62 */
63 public static final String CONFIG_PATH = System.getProperty("config.dir", "conf");
64
65 public static final String CONFIG = System.getProperty("config.file", "use_case1.conf");
66
67 public static final String XPATH_DYNAMIC = "/art:result/art:ui/art:dynamic";
68 private static final String serverUrl = "http://localhost:8181";
69 private static final String locale = "de";
70 /**
71 * The logger used in this class.
72 */
73 private static Logger logger;
74
75 static {
76 configureLogging();
77
78 logger = Logger.getLogger(SinfoProof.class);
79 }
80
81 /**
82 * Trys to load the Log4j configuration from ${config.dir}/log4j.properties.
83 */
84 public static final void configureLogging() {
85 final File configDir = new File(CONFIG_PATH);
86 final File propFile = new File(configDir, LOG4J_PROPERTIES);
87
88 if (propFile.isFile() && propFile.canRead()) {
89 try {
90 PropertyConfigurator.configure(propFile.toURI().toURL());
91 }
92 catch (final MalformedURLException mue) {
93 mue.printStackTrace(System.err);
94 }
95 }
96 }
97
98 public static final Configuration readConfiguration() {
99 final File configDir = new File(CONFIG_PATH);
100 final File configFile = new File(configDir, CONFIG);
101
102 logger.debug("Configuration file: " + configFile.getAbsolutePath());
103
104 if (configFile.isFile() && configFile.canRead()) {
105 try {
106 final Configuration conf = new Configuration(configFile);
107 conf.initialize();
108
109 return conf;
110 }
111 catch (final IOException ioe) {
112 logger.error("Error while reading configuration.");
113 }
114 }
115
116 return null;
117 }
118
119 public SinfoProof() {
120 // empty constructor
121 super();
122 }
123
124 public static void main(final String[] args) throws ServerException, IOException {
125 logger.info("Starting console client.");
126
127 final User user = new DefaultUser("belger", "belger", null, false, new ArrayList<String>(), new ArrayList<String>());
128 final UserClient userClient = new UserClient(serverUrl);
129 final Element userElement = userClient.findUser(user);
130 final String userUuid = userElement.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "uuid");
131
132 /* Init Collection */
133 final Collection collection = createCollection(serverUrl, locale, userUuid);
134 final org.dive4elements.river.client.shared.model.Artifact sinfoArtifact = ArtifactHelper.createArtifact(serverUrl, locale, "sinfo", null);
135 final Collection newColl = CollectionHelper.addArtifact(collection, sinfoArtifact, serverUrl, locale); // wichtig; sorgt für Persistenz
136
137 /* Select River */
138 final Data data = new StringOptionsData("river", "river", new DataItem[] { new DefaultDataItem("Beispielfluss", "Beispielfluss", "Beispielfluss") });
139 final Artifact calcModeArtifact = feedAndGo(sinfoArtifact, new Data[] { data }, 0);
140
141 /* Select CalcMode */
142 final DataList calcModes = calcModeArtifact.getArtifactDescription().getCurrentData(); // AUSWAHL-Möglichkeiten
143 final DataItem minMaxFlowdepth = calcModes.get(0).getItems()[2];
144 final Data dataCalcMode = new StringOptionsData("calculation_mode", "calculation_mode", new DataItem[] { minMaxFlowdepth });
145 final Artifact rangeArtifact = feedAndGo(calcModeArtifact, new Data[] { dataCalcMode }, 0);
146
147 /* Select Distance */
148
149 // entweder eine bestimmte Range
150 final Data dataFrom = new StringOptionsData("ld_from", "ld_from", new DataItem[] { new DefaultDataItem("0", "0", "0") });
151 final Data dataTo = new StringOptionsData("ld_to", "ld_to", new DataItem[] { new DefaultDataItem("100", "100", "100") });
152 final Data[] rangeFromToDetermined = new Data[] { dataFrom, dataTo };
153
154 // oder die maxRange
155 final DataList list = rangeArtifact.getArtifactDescription().getCurrentData();
156 final Data[] rangeMax = new Data[] { list.get(0), list.get(1) };
157 final Artifact dataChoiceArtifact = feedAndGo(rangeArtifact, rangeMax, 0);
158
159 /* Select Fixpunkte */
160 final List<Recommendation> recs = collection.getRecommendations();
161 if (recs != null) {
162
163 }
164 final String combinedId = "[c23bcc26-2282-47b7-b262-5a328a372926;staticwqkms;0;ELBE_W-MNQ1890-2006_Fixierungsdaten_150-280.csv]#[d2e2e1da-81cd-40e6-8857-dddc22252c29;bedheight;0;FP-2015_0-502]"; // SCHWACHSTELLE...
165 final Data pair = new StringOptionsData("Ausgewählte Differenzen", "diffids", new DataItem[] { new DefaultDataItem(null, null, combinedId) });
166 final Artifact export = feedAndGo(dataChoiceArtifact, new Data[] { pair }, 0);
167 final Recommendation r;
168
169 /* Export calculation */
170 final OutputMode[] modes = export.getArtifactDescription().getOutputModes();
171 if (modes != null) {
172
173 }
174 }
175
176 private static Artifact feedAndGo(final Artifact inputArtifact, final Data[] data, final int reachableStateIndex)
177 throws ConnectionException, ServerException {
178 final Artifact artifact = feed(serverUrl, locale, inputArtifact, data);
179 return advance(serverUrl, locale, artifact, getReachableStateByIndex(artifact, reachableStateIndex));
180 }
181
182 private static String getReachableStateByIndex(final Artifact artifact, final int index) {
183
184 final String[] states = artifact.getArtifactDescription().getReachableStates();
185 if (states != null) {
186 if (states.length > index) {
187 return states[index];
188 } else {
189 return states[0];
190 }
191 } else {
192 return "";
193 }
194 }
195
196 public static Collection createCollection(final String url, final String locale, final String ownerId) throws ConnectionException {
197 final Document create = ClientProtocolUtils.newCreateCollectionDocument(null);
198 final HttpClient client = new HttpClientImpl(url, locale);
199 final Document doc = (Document) client.createCollection(create, ownerId, new DocumentResponseHandler());
200 final String uuid = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_UUID, ArtifactNamespaceContext.INSTANCE);
201 final String ttlStr = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_TTL, ArtifactNamespaceContext.INSTANCE);
202
203 return new DefaultCollection(uuid, Long.valueOf(ttlStr), uuid);
204 }
205
206 public static Artifact feed(final String url, final String locale, final Artifact artifact, final Data[] data) throws ServerException, ConnectionException {
207 final Document feed = ClientProtocolUtils.newFeedDocument(artifact.getUuid(), artifact.getHash(), createKVP(data));
208
209 final HttpClient client = new HttpClientImpl(url, locale);
210
211 final Document description = (Document) client.feed(new org.dive4elements.artifacts.httpclient.objects.Artifact(artifact.getUuid(), artifact.getHash()),
212 feed, new DocumentResponseHandler());
213
214 final String result = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE);
215
216 if (result == null || !result.equals(FeedServiceImpl.OPERATION_FAILURE)) {
217 return (Artifact) new FLYSArtifactCreator().create(description);
218 } else if (result != null && result.equals(FeedServiceImpl.OPERATION_FAILURE)) {
219 final String msg = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT_MSG, ArtifactNamespaceContext.INSTANCE);
220 throw new ServerException(msg);
221 }
222
223 throw new ServerException(FeedServiceImpl.ERROR_FEED_DATA);
224 }
225
226 protected static String[][] createKVP(final Data[] data) {
227 if (data != null) {
228 final String[][] kvp = new String[data.length][];
229
230 int i = 0;
231
232 for (final Data d : data) {
233 final DataItem[] items = d.getItems();
234 final String key = d.getLabel();
235 final String value = d.getStringValue();
236
237 kvp[i++] = new String[] { key, value };
238 }
239
240 return kvp;
241 }
242 return null;
243 }
244
245 public static Artifact advance(final String url, final String locale, final Artifact artifact, final String target)
246 throws ConnectionException, ServerException {
247 final Document advance = ClientProtocolUtils.newAdvanceDocument(artifact.getUuid(), artifact.getHash(), target);
248 final HttpClient client = new HttpClientImpl(url, locale);
249
250 final Document description = (Document) client.advance(
251 new org.dive4elements.artifacts.httpclient.objects.Artifact(artifact.getUuid(), artifact.getHash()), advance, new DocumentResponseHandler());
252
253 if (description == null) {
254 throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT);
255 }
256
257 final String result = XMLUtils.xpathString(description, AdvanceServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE);
258
259 if (result == null || !result.equals(AdvanceServiceImpl.OPERATION_FAILURE)) {
260 return (Artifact) new FLYSArtifactCreator().create(description);
261 }
262
263 throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT);
264 }
265
266 // FIXME
267 }
268 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org