view gwt-client/src/test/java/test/SimpleRecommendation.java @ 9227:84397da33d17

Allow to control specific behaviour in TwinDatacagePanel Implemented client logic of 'intelligent datacage filtering' for SINFO
author gernotbelger
date Wed, 04 Jul 2018 18:28:08 +0200
parents 83aee0942eae
children
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(null, "xxxx"));
        final String rec2String = RecommandationUtils.createDataString(artifacts[1].getUuid(), recom2, new NilDatacageTwinPanelInfo(null, "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