diff gwt-client/src/main/java/org/dive4elements/river/client/test/SuperProof.java @ 9028:7f3818ec6eb6

work on proof 2
author gernotbelger
date Thu, 26 Apr 2018 13:12:33 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/test/SuperProof.java	Thu Apr 26 13:12:33 2018 +0200
@@ -0,0 +1,310 @@
+/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
+ * Software engineering by
+ *  Björnsen Beratende Ingenieure GmbH
+ *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
+ *
+ * This file is Free Software under the GNU AGPL (>=v3)
+ * and comes with ABSOLUTELY NO WARRANTY! Check out the
+ * documentation coming with Dive4Elements River for details.
+ */
+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.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.DefaultCollection;
+import org.dive4elements.river.client.shared.model.Recommendation;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * @author Domenico Nardi Tironi
+ *
+ */
+public abstract class SuperProof {
+
+    private final String serverUrl = "http://localhost:8181";
+    private final String locale = "de";
+    private final HttpClient client;
+
+    private final String username;
+    private final String password;
+    private final String infotype;
+    private final String userUuid;
+    private Collection collection;
+    private Artifact artifact;
+
+    public SuperProof(final String username, final String password, final String infotype) {
+        this.username = username;
+        this.password = password;
+        this.infotype = infotype;
+
+        // init
+        this.client = new HttpClientImpl(this.serverUrl, this.locale);
+        this.userUuid = makeUserUuid();
+        this.collection = getCollection();
+        this.artifact = getArtifact();
+    }
+
+    private String makeUserUuid() {
+        final User user = new DefaultUser(this.username, this.password, null, false, new ArrayList<String>(), new ArrayList<String>());
+        final UserClient userClient = new UserClient(this.serverUrl);
+        Element userElement;
+        try {
+            userElement = userClient.findUser(user);
+            return userElement.getAttributeNS(ArtifactNamespaceContext.NAMESPACE_URI, "uuid");
+        }
+        catch (final ConnectionException e) {
+            e.printStackTrace();
+        }
+        return "";
+    }
+
+    protected final Artifact getArtifact() {
+
+        /* Init Collection */
+        if (this.artifact == null)
+            try {
+
+                this.artifact = ArtifactHelper.createArtifact(this.serverUrl, this.locale, this.infotype, null);
+                setCollection(CollectionHelper.addArtifact(getCollection(), this.artifact, this.serverUrl, this.locale)); // wichtig; sorgt für Persistenz
+            }
+            catch (final ServerException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+        return this.artifact;
+    }
+
+    private Collection getCollection() {
+
+        if (this.collection == null) {
+            try {
+                // lazy-Loading
+                final Document create = ClientProtocolUtils.newCreateCollectionDocument(null);
+                final Document doc = (Document) this.client.createCollection(create, this.userUuid, 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);
+                this.collection = new DefaultCollection(uuid, Long.valueOf(ttlStr), uuid);
+            }
+            catch (final ConnectionException e) {
+                // TODO Auto-generated catch block
+                e.printStackTrace();
+            }
+        }
+        return this.collection;
+    }
+
+    private void setCollection(final Collection collection) {
+        this.collection = collection;
+    }
+
+    private void setArtifact(final Artifact artifact) {
+        this.artifact = artifact;
+    }
+
+    // TODO: MAKE THIS CLASS ABSTRACT AND OVERRIDE runTest in children
+    public abstract void runTest();
+
+    protected final void describeCollection() {
+        try {
+            final String uuid = getCollection().identifier();
+            final Document describe = ClientProtocolUtils.newDescribeCollectionDocument(uuid);
+            final Document response = (Document) this.client.doCollectionAction(describe, uuid, new DocumentResponseHandler());
+            final Collection c = CollectionHelper.parseCollection(response);
+            setCollection(c);
+        }
+        catch (final ConnectionException e) {
+            e.printStackTrace();
+        }
+    }
+
+    protected final void feedAndGo(final Data[] data, final int reachableStateIndex) {
+        try {
+            feed(data);
+            advance(getReachableStateByIndex(getArtifact(), reachableStateIndex)); // reachablestate könnte auch String sein... TODO: feedAndgo(data,string)
+                                                                                   // bauen
+        }
+        catch (final ConnectionException e) {
+            e.printStackTrace();
+        }
+        catch (final ServerException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private 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 "";
+        }
+    }
+
+    private void feed(final Data[] data) throws ServerException, ConnectionException {
+        final Document feed = ClientProtocolUtils.newFeedDocument(getArtifact().getUuid(), getArtifact().getHash(), createKVP(data));
+
+        final Document description = (Document) this.client.feed(
+                new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().getHash()), feed,
+                new DocumentResponseHandler());
+
+        final String result = XMLUtils.xpathString(description, FeedServiceImpl.XPATH_RESULT, ArtifactNamespaceContext.INSTANCE);
+
+        if (result == null || !result.equals(FeedServiceImpl.OPERATION_FAILURE)) {
+            setArtifact((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);
+    }
+
+    private 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;
+    }
+
+    private void advance(final String target) throws ConnectionException, ServerException {
+        final Document advance = ClientProtocolUtils.newAdvanceDocument(getArtifact().getUuid(), getArtifact().getHash(), target);
+        // final HttpClient client = new HttpClientImpl(url, locale);
+
+        final Document description = (Document) this.client.advance(
+                new org.dive4elements.artifacts.httpclient.objects.Artifact(getArtifact().getUuid(), getArtifact().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)) {
+            setArtifact((Artifact) new FLYSArtifactCreator().create(description));
+        }
+
+        // throw new ServerException(AdvanceServiceImpl.ERROR_ADVANCE_ARTIFACT);
+    }
+
+    protected final Artifact[] loadMany(final Recommendation[] recoms, final String factory) {
+        try {
+            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(this.serverUrl, this.locale, realFactory, recom);
+
+                    if (clone != null) {
+                        final Collection c = CollectionHelper.addArtifact(getCollection(), clone, this.serverUrl, this.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()]);
+        }
+        catch (final ServerException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    /// ExportServiceImpl
+    public void doGet(final String mode) {
+        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(getCollection().identifier(), mode, type, attr);
+            // final HttpClient client = new HttpClientImpl(serverUrl, locale);
+
+            if (enc != null) {
+                final InputStreamReader in = new InputStreamReader(this.client.collectionOut(request, getCollection().identifier(), 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();
+        }
+    }
+
+}

http://dive4elements.wald.intevation.org