view gwt-client/src/main/java/org/dive4elements/river/client/client/ui/WspDatacagePanel.java @ 9220:e3c2ae1887e8

Allow to filter contents of datacage on client side. Allow to override column label of datacage Some code cleanup
author gernotbelger
date Wed, 04 Jul 2018 12:00:51 +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.client.ui;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dive4elements.river.client.client.Config;
import org.dive4elements.river.client.client.FLYS;
import org.dive4elements.river.client.client.event.StepForwardEvent;
import org.dive4elements.river.client.client.services.LoadArtifactService;
import org.dive4elements.river.client.client.services.LoadArtifactServiceAsync;
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.DefaultData;
import org.dive4elements.river.client.shared.model.DefaultDataItem;
import org.dive4elements.river.client.shared.model.Recommendation;
import org.dive4elements.river.client.shared.model.Recommendation.Facet;
import org.dive4elements.river.client.shared.model.Recommendation.Filter;
import org.dive4elements.river.client.shared.model.ToLoad;
import org.dive4elements.river.client.shared.model.User;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.events.ClickEvent;

public class WspDatacagePanel extends DatacagePanel {

    private static final long serialVersionUID = 2494432743877141135L;

    private static final String WATERLEVEL_OUTS = "waterlevels_panel";

    private final LoadArtifactServiceAsync loadService = GWT.create(LoadArtifactService.class);

    public WspDatacagePanel(final User user) {
        super(user, WATERLEVEL_OUTS, null, false);
    }

    /**
     * We need to override this method (defined in AbstractUIProvider) because
     * we have to create a new Artifact specified by the Datacage selection via
     * Async request.
     *
     * @param e
     *            The ClickEvent.
     */
    @Override
    public void onClick(final ClickEvent e) {
        final List<String> errors = validate();
        if (errors == null || errors.isEmpty()) {
            // 1) Fetch selected recommendation.
            final Config config = Config.getInstance();
            final String locale = config.getLocale();
            final Collection c = this.collection;
            final Recommendation r = getSelectedRecommendation();

            if (r == null) {
                SC.warn(MSG.warning_no_wsp_selected());
                return;
            }

            // TODO: This could eventually be handled server-side.
            // 2) Create, load Artifact and fire event.
            this.loadService.load(c, r, r.getFactory(), locale, new AsyncCallback<Artifact>() {
                @Override
                public void onFailure(final Throwable caught) {
                    GWT.log("WspDatacagePanel", caught);
                    SC.warn(FLYS.getExceptionString(MSG, caught));
                }

                @Override
                public void onSuccess(final Artifact newArtifact) {
                    GWT.log("Created new artifact.");
                    fireStepForwardEvent(new StepForwardEvent(getData(r, newArtifact)));
                }
            });
        } else {
            showErrors(errors);
        }
    }

    protected Recommendation getSelectedRecommendation() {
        final ToLoad toLoad = getSelection();
        final List<Recommendation> recoms = toLoad.toRecommendations();

        return recoms.size() > 0 ? recoms.get(0) : null;
    }

    /**
     * Nothing is done in this method. It returns null, because we serve the
     * Data another way!
     *
     * @return always null!
     */
    @Override
    protected Data[] getData() {
        // do nothing here, the Data is fetched on another way in this panel.
        return null;
    }

    /** Returns a Data Array with one default item. */
    protected final Data[] getData(final Recommendation r, final Artifact newArtifact) {
        final String uuid = newArtifact.getUuid();
        r.setMasterArtifact(uuid);

        final String value = createDataString(uuid, r);

        final String dataName = getMyDataName();

        final DataItem item = new DefaultDataItem(dataName, dataName, value);
        return new Data[] { new DefaultData(dataName, null, null, new DataItem[] { item }) };
    }

    private String createDataString(final String artifactId, final Recommendation recommendation) {

        // The filter will only be available or previous calculation artifacts.
        final Filter filter = recommendation.getFilter();

        if (filter != null) {
            Facet f = null;

            final Map<String, List<Facet>> outs = filter.getOuts();
            final Set<Map.Entry<String, List<Facet>>> entries = outs.entrySet();

            for (final Map.Entry<String, List<Facet>> entry : entries) {
                final List<Facet> fs = entry.getValue();

                f = fs.get(0);
                if (f != null) {
                    break;
                }
            }

            return "[" + artifactId + ";" + f.getName() + ";" + f.getIndex() + "]";
        }

        return "[" + artifactId + ";" + recommendation.getFactory() + ";" + 0 + "]";
    }
}

http://dive4elements.wald.intevation.org