view gwt-client/src/main/java/org/dive4elements/river/client/shared/model/ToLoad.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 5e38e2924c07
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * 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.shared.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;

public class ToLoad implements Serializable
{
    private static class StringTriple {
        public String first;
        public String second;
        public String third;
        public StringTriple(final String first, final String second, final String third) {
            this.first = first;
            this.second = second;
            this.third = third;
        }
        @Override
        public int hashCode() {
            return this.first.hashCode() + this.second.hashCode() + this.third.hashCode();
        }
        @Override
        public boolean equals(final Object o) {
            if (!(o instanceof StringTriple)) {
                return false;
            }
            final StringTriple other = (StringTriple) o;
            return this.second.equals(other.second)
                    && this.first.equals(other.first)
                    && this.third.equals(other.third);
        }
    }

    private static final String SYNTHETIC_KEY = "key-";

    private final Map<String, Map<StringTriple, ArtifactFilter>> artifacts = new HashMap<String, Map<StringTriple, ArtifactFilter>>();

    public static final String uniqueKey(final Map<?, ?> map) {
        int idx = map.size();

        String key = SYNTHETIC_KEY + idx;
        while (map.containsKey(key)) {
            key = SYNTHETIC_KEY + ++idx;
        }
        return key;
    }

    public void add(
            final String artifactName,
            final String factory,
            final String out,
            final String name,
            final String ids,
            final String displayName
            ) {
        add(artifactName, factory, out, name, ids, displayName, null);
    }

    public void add(
            String artifactName,
            final String factory,
            final String out,
            final String name,
            final String ids,
            final String displayName,
            final String targetOut
            ) {
        GWT.log("Adding artifact: " + artifactName + " Factory: " + factory +
                " Out: " + out + " Name: " + name + " Ids: " + ids +
                " Display Name: " + displayName + " Target Out: " + targetOut);

        if (artifactName == null) {
            artifactName = uniqueKey(this.artifacts);
        }

        Map<StringTriple, ArtifactFilter> artifact = this.artifacts.get(
                artifactName);

        if (artifact == null) {
            artifact = new HashMap<StringTriple, ArtifactFilter>();
            this.artifacts.put(artifactName, artifact);
        }

        ArtifactFilter filter = artifact.get(factory);
        if (filter == null) {
            filter = new ArtifactFilter(factory);
            artifact.put(new StringTriple(
                    factory, displayName, targetOut), filter);
        }

        filter.add(out, name, ids);
    }

    public boolean isEmpty() {
        return this.artifacts.isEmpty();
    }

    public List<Recommendation> toRecommendations() {
        final List<Recommendation> recommendations = new ArrayList<Recommendation>();

        for (final Map.Entry<String, Map<StringTriple, ArtifactFilter>> all:
            this.artifacts.entrySet()
                ) {
            String masterArtifact = all.getKey();

            if (masterArtifact.startsWith(SYNTHETIC_KEY)) { // system data
                masterArtifact = null;
            }

            for (final Map.Entry<StringTriple, ArtifactFilter> entry:
                all.getValue().entrySet()
                    ) {
                final StringTriple triple = entry.getKey();
                final String factory = triple.first;
                final String targetOut = triple.third;
                final ArtifactFilter artifactFilter = entry.getValue();

                String                ids;
                Recommendation.Filter filter;

                if (masterArtifact == null) { // system data
                    ids    = artifactFilter.collectIds();
                    filter = null;
                }
                else { // user specific
                    ids    = null;
                    filter = artifactFilter.toFilter();
                }

                final Recommendation recommendation = new Recommendation(
                        factory, ids, masterArtifact, filter, targetOut);
                recommendation.setDisplayName(triple.second);

                recommendations.add(recommendation);
            }
        }

        return recommendations;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org