Mercurial > dive4elements > river
view gwt-client/src/main/java/org/dive4elements/river/client/test/SinfoProof.java @ 9027:274ddafb719b
S-Info-Proof-Init
Testing
author | gernotbelger |
---|---|
date | Thu, 26 Apr 2018 10:46:12 +0200 |
parents | 679b1442c886 |
children |
line wrap: on
line source
/* * Copyright (c) 2010 by Intevation GmbH * * This program is free software under the LGPL (>=v2.1) * Read the file LGPL.txt coming with the software for details * or visit http://www.gnu.org/licenses/ if it does not exist. */ package org.dive4elements.river.client.test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.HashMap; import org.dive4elements.artifacts.common.ArtifactNamespaceContext; import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; import org.dive4elements.artifacts.httpclient.http.HttpClient; import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; import org.dive4elements.artifacts.httpclient.http.response.DocumentResponseHandler; import org.dive4elements.artifacts.httpclient.utils.XMLUtils; import org.dive4elements.river.client.client.ui.NilDatacageTwinPanelInfo; import org.dive4elements.river.client.client.ui.RecommandationUtils; import org.dive4elements.river.client.server.AdvanceServiceImpl; import org.dive4elements.river.client.server.ArtifactHelper; import org.dive4elements.river.client.server.CollectionHelper; import org.dive4elements.river.client.server.CreateCollectionServiceImpl; import org.dive4elements.river.client.server.FLYSArtifactCreator; import org.dive4elements.river.client.server.FeedServiceImpl; import org.dive4elements.river.client.server.LoadArtifactServiceImpl; import org.dive4elements.river.client.server.auth.DefaultUser; import org.dive4elements.river.client.server.auth.User; import org.dive4elements.river.client.server.auth.UserClient; import org.dive4elements.river.client.shared.exceptions.ServerException; import org.dive4elements.river.client.shared.model.Artifact; import org.dive4elements.river.client.shared.model.Collection; import org.dive4elements.river.client.shared.model.Data; import org.dive4elements.river.client.shared.model.DataItem; import org.dive4elements.river.client.shared.model.DataList; import org.dive4elements.river.client.shared.model.DefaultCollection; import org.dive4elements.river.client.shared.model.DefaultDataItem; import org.dive4elements.river.client.shared.model.OutputMode; import org.dive4elements.river.client.shared.model.Recommendation; import org.dive4elements.river.client.shared.model.StringOptionsData; import org.w3c.dom.Document; import org.w3c.dom.Element; /** * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> */ public class SinfoProof { private static final String serverUrl = "http://localhost:8181"; private static final String locale = "de"; public static void main(final String[] args) throws ServerException, IOException { // logger.info("Starting console client."); final User user = new DefaultUser("belger", "belger", null, false, new ArrayList<String>(), new ArrayList<String>()); final UserClient userClient = new UserClient(serverUrl); final Element userElement = userClient.findUser(user); final String userUuid = userElement.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "uuid"); /* Init Collection */ final Collection collection = createCollection(serverUrl, locale, userUuid); final org.dive4elements.river.client.shared.model.Artifact sinfoArtifact = ArtifactHelper.createArtifact(serverUrl, locale, "sinfo", null); final Collection newColl = CollectionHelper.addArtifact(collection, sinfoArtifact, serverUrl, locale); // wichtig; sorgt für Persistenz /* Select River */ final Data data = new StringOptionsData("river", "river", new DataItem[] { new DefaultDataItem("Beispielfluss", "Beispielfluss", "Beispielfluss") }); final Artifact calcModeArtifact = feedAndGo(sinfoArtifact, new Data[] { data }, 0); /* Select CalcMode */ final DataList calcModes = calcModeArtifact.getArtifactDescription().getCurrentData(); // AUSWAHL-Möglichkeiten final DataItem minMaxFlowdepth = calcModes.get(0).getItems()[2]; final Data dataCalcMode = new StringOptionsData("calculation_mode", "calculation_mode", new DataItem[] { minMaxFlowdepth }); final Artifact rangeArtifact = feedAndGo(calcModeArtifact, new Data[] { dataCalcMode }, 0); /* Select Range */ // entweder eine bestimmte Range final Data dataFrom = new StringOptionsData("ld_from", "ld_from", new DataItem[] { new DefaultDataItem("10", "10", "10") }); final Data dataTo = new StringOptionsData("ld_to", "ld_to", new DataItem[] { new DefaultDataItem("100", "100", "100") }); final Data[] rangeFromToDetermined = new Data[] { dataFrom, dataTo }; // oder die maxRange final DataList list = rangeArtifact.getArtifactDescription().getCurrentData(); final Data[] rangeMax = new Data[] { list.get(0), list.get(1) }; final Artifact dataChoiceArtifact = feedAndGo(rangeArtifact, rangeFromToDetermined, 0); /* Select Fixpunkte */ final Recommendation rec1 = new Recommendation("staticwqkms", "additionals-wstv-0-103", "sinfo_flowdepth_waterlevels"); final Recommendation rec2 = new Recommendation("bedheight", "bedheight-single-36-2015-FP-2015_0-502", "sinfo_flowdepthminmax_heights"); final Artifact[] artifacts = loadMany(collection, new Recommendation[] { rec1, rec2 }, null); // rec1.getDisplayName() TODO: makeDisplayName final String rec1String = RecommandationUtils.createDataString(artifacts[0].getUuid(), rec1, new NilDatacageTwinPanelInfo("xxxx")); final String rec2String = RecommandationUtils.createDataString(artifacts[1].getUuid(), rec2, new NilDatacageTwinPanelInfo("xxxx")); // TODO: check display name final String combinedIdNeu = rec1String + "#" + rec2String; final Data pair = new StringOptionsData("diffids", "diffids", new DataItem[] { new DefaultDataItem(combinedIdNeu, combinedIdNeu, combinedIdNeu) }); final Artifact exportArtifact = feedAndGo(dataChoiceArtifact, new Data[] { pair }, 0); // Describe collection describeCollection(collection); // wichtig, damit die Facets erzeugt werden // /* Export calculation */ final OutputMode[] modes = exportArtifact.getArtifactDescription().getOutputModes(); if (modes != null) { final OutputMode mode = modes[1]; // output.sinfo_flowdepthminmax_export doGet(mode.getName(), collection.identifier()); } } private static Collection describeCollection(final Collection collection) throws ConnectionException { final String uuid = collection.identifier(); final Document describe = ClientProtocolUtils.newDescribeCollectionDocument(uuid); final HttpClient client = new HttpClientImpl(serverUrl, locale); final Document response = (Document) client.doCollectionAction(describe, uuid, new DocumentResponseHandler()); final Collection c = CollectionHelper.parseCollection(response); return c; } private static Artifact feedAndGo(final Artifact inputArtifact, final Data[] data, final int reachableStateIndex) throws ConnectionException, ServerException { final Artifact artifact = feed(serverUrl, locale, inputArtifact, data); return advance(serverUrl, locale, artifact, getReachableStateByIndex(artifact, reachableStateIndex)); } private static String getReachableStateByIndex(final Artifact artifact, final int index) { final String[] states = artifact.getArtifactDescription().getReachableStates(); if (states != null) { if (states.length > index) { return states[index]; } else { return states[0]; } } else { return ""; } } public static Collection createCollection(final String url, final String locale, final String ownerId) throws ConnectionException { final Document create = ClientProtocolUtils.newCreateCollectionDocument(null); final HttpClient client = new HttpClientImpl(url, locale); final Document doc = (Document) client.createCollection(create, ownerId, new DocumentResponseHandler()); final String uuid = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_UUID, ArtifactNamespaceContext.INSTANCE); final String ttlStr = XMLUtils.xpathString(doc, CreateCollectionServiceImpl.XPATH_COLLECTION_TTL, ArtifactNamespaceContext.INSTANCE); return new DefaultCollection(uuid, Long.valueOf(ttlStr), uuid); } public static Artifact feed(final String url, final String locale, final Artifact artifact, final Data[] data) throws ServerException, ConnectionException { final Document feed = ClientProtocolUtils.newFeedDocument(artifact.getUuid(), artifact.getHash(), createKVP(data)); final HttpClient client = new HttpClientImpl(url, locale); final Document description = (Document) client.feed(new org.dive4elements.artifacts.httpclient.objects.Artifact(artifact.getUuid(), artifact.getHash()), feed, new DocumentResponseHandler()); final String result = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); if (result == null || !result.equals(FeedServiceImpl.OPERATION_FAILURE)) { return (Artifact) new FLYSArtifactCreator().create(description); } else if (result != null && result.equals(FeedServiceImpl.OPERATION_FAILURE)) { final String msg = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT_MSG, ArtifactNamespaceContext.INSTANCE); throw new ServerException(msg); } throw new ServerException(FeedServiceImpl.ERROR_FEED_DATA); } protected static String[][] createKVP(final Data[] data) { if (data != null) { final String[][] kvp = new String[data.length][]; int i = 0; for (final Data d : data) { final DataItem[] items = d.getItems(); final String key = d.getLabel(); final String value = d.getStringValue(); kvp[i++] = new String[] { key, value }; } return kvp; } return null; } public static Artifact advance(final String url, final String locale, final Artifact artifact, final String target) throws ConnectionException, ServerException { final Document advance = ClientProtocolUtils.newAdvanceDocument(artifact.getUuid(), artifact.getHash(), target); final HttpClient client = new HttpClientImpl(url, locale); final Document description = (Document) client.advance( new org.dive4elements.artifacts.httpclient.objects.Artifact(artifact.getUuid(), artifact.getHash()), advance, new DocumentResponseHandler()); if (description == null) { throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT); } final String result = XMLUtils.xpathString(description, AdvanceServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE); if (result == null || !result.equals(AdvanceServiceImpl.OPERATION_FAILURE)) { return (Artifact) new FLYSArtifactCreator().create(description); } throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT); } public static Artifact[] loadMany(final Collection parent, final Recommendation[] recoms, final String factory) throws ServerException { final ArrayList<Artifact> artifacts = new ArrayList<Artifact>(); final HashMap<Recommendation, Artifact> cloneMap = new HashMap<Recommendation, Artifact>(); for (final Recommendation recom : recoms) { final Artifact prevClone = cloneMap.get(recom); if (prevClone != null) { artifacts.add(prevClone); } else { // Not already cloned. final String realFactory = factory != null ? factory : recom.getFactory(); final Artifact clone = ArtifactHelper.createArtifact(serverUrl, locale, realFactory, recom); if (clone != null) { final Collection c = CollectionHelper.addArtifact(parent, clone, serverUrl, locale); if (c != null) { artifacts.add(clone); // Remember we cloned a recommendation like this. cloneMap.put(recom, clone); } else { throw new ServerException(LoadArtifactServiceImpl.ERROR_LOAD_ARTIFACT); } } } } return artifacts.toArray(new Artifact[artifacts.size()]); } /// ExportServiceImpl public static void doGet(final String mode, final String collectionId) { try { final String name = mode; final String type = "csv"; final String fn = name + "." + type; // TODO: make filename unique final String enc = "windows-1252";// req.getParameter("encoding"); final OutputStream out = new FileOutputStream(new File("D:" + File.separator + fn)); final Document attr = null; final Document request = ClientProtocolUtils.newOutCollectionDocument(collectionId, mode, type, attr); final HttpClient client = new HttpClientImpl(serverUrl, locale); if (enc != null) { final InputStreamReader in = new InputStreamReader(client.collectionOut(request, collectionId, mode), "UTF-8"); try { final OutputStreamWriter encOut = new OutputStreamWriter(out, enc); final char buf[] = new char[4096]; int c; while ((c = in.read(buf, 0, buf.length)) >= 0) { encOut.write(buf, 0, c); } encOut.flush(); encOut.close(); } finally { in.close(); } } } catch (final IOException ioe) { ioe.printStackTrace(); } } }