view gwt-client/src/test/java/test/SimpleRecommendation.java @ 9157:f9bb5d0a6ff3

Added the S-Info collision calculation and chart output
author mschaefer
date Tue, 19 Jun 2018 14:19:32 +0200
parents 48d87af1243e
children 83aee0942eae
line wrap: on
line source
/** 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 test;

import java.util.ArrayList;
import java.util.HashMap;

import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException;
import org.dive4elements.river.client.client.ui.NilDatacageTwinPanelInfo;
import org.dive4elements.river.client.client.ui.RecommandationUtils;
import org.dive4elements.river.client.server.ArtifactHelper;
import org.dive4elements.river.client.server.CollectionHelper;
import org.dive4elements.river.client.server.LoadArtifactServiceImpl;
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.Recommendation;

/**
 * @author Domenico Nardi Tironi
 *
 */
public class SimpleRecommendation {

    // entspricht den Bezeichnungen im Datacage-Export
    private final String factory;
    private final String ids;
    private final String target;
    private String displayName;

    public SimpleRecommendation(final String factory, final String ids, final String target) {
        this.factory = factory;
        this.ids = ids;
        this.target = target;
    }

    public SimpleRecommendation(final String factory, final String ids, final String target, final String displayName) {
        this(factory, ids, target);
        this.displayName = displayName;
    }

    public String getIds() {
        return this.ids;
    }

    public String getTarget() {
        return this.target;
    }

    public String getFactory() {
        return this.factory;
    }

    private String getDisplayName() {
        return this.displayName;
    }

    public final String getRecommendationPairString(final SimpleRecommendation rec2, final Collection collection, final String serverUrl, final String locale)
            throws ConnectionException, ServerException {
        final Recommendation recom1 = new Recommendation(this.getFactory(), this.getIds(), this.getTarget());
        recom1.setDisplayName(this.displayName);
        final Recommendation recom2 = new Recommendation(rec2.getFactory(), rec2.getIds(), rec2.getTarget());
        recom2.setDisplayName(rec2.getDisplayName());
        final Artifact[] artifacts = loadMany(new Recommendation[] { recom1, recom2 }, null, collection, serverUrl, locale);
        final String rec1String = RecommandationUtils.createDataString(artifacts[0].getUuid(), recom1, new NilDatacageTwinPanelInfo("xxxx"));
        final String rec2String = RecommandationUtils.createDataString(artifacts[1].getUuid(), recom2, new NilDatacageTwinPanelInfo("xxxx"));
        final String combinedIdNeu = rec1String + "#" + rec2String;
        return combinedIdNeu;

    }

    private final Artifact[] loadMany(final Recommendation[] recoms, final String factory, final Collection collection, final String serverUrl,
            final String locale) throws ServerException, ConnectionException {
        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(collection, 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()]);

    }

}

http://dive4elements.wald.intevation.org