changeset 8719:36f52c80b7ac

Deduplicate W / BedHeight Difference selection panel code
author Andre Heinecke <andre.heinecke@intevation.de>
date Mon, 27 Apr 2015 14:50:38 +0200
parents e689be2a4dd1
children 3d0ef0a66503
files gwt-client/src/main/java/org/dive4elements/river/client/client/ui/DatacageTwinPanel.java gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/BedHeightsDatacagePanel.java
diffstat 2 files changed, 25 insertions(+), 204 deletions(-) [+]
line wrap: on
line diff
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/DatacageTwinPanel.java	Mon Apr 27 12:48:41 2015 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/DatacageTwinPanel.java	Mon Apr 27 14:50:38 2015 +0200
@@ -113,11 +113,10 @@
 
     /**
      * Create a recommendation from a string representation of it.
-     * @TODO describe format of input string
      * @param from string in format as shown above.
      * @return recommendation from input string.
      */
-    public Recommendation createRecommendationFromString(String from) {
+    public Recommendation createRecommendationFromString(String from, String factory) {
         // TODO Construct "real" filter.
         String[] parts = unbracket(from).split(";");
         Recommendation.Filter filter = new Recommendation.Filter();
@@ -129,7 +128,7 @@
             ();
         facets.add(facet);
         filter.add("longitudinal_section", facets);
-        Recommendation r = new Recommendation("waterlevel", parts[0],
+        Recommendation r = new Recommendation(factory, parts[0],
             this.artifact.getUuid(), filter);
         r.setDisplayName(parts[3]);
         return r;
@@ -139,16 +138,16 @@
     /**
      * Add RecomendationPairRecords from input String to the ListGrid.
      */
-    public void populateGridFromString(String from){
+    public void populateGridFromString(String from, String factory){
         // Split this string.
         // Create according recommendations and display strings.
         String[] recs = from.split("#");
         if (recs.length % 2 != 0) return;
         for (int i = 0; i < recs.length; i+=2) {
             Recommendation minuend =
-                createRecommendationFromString(recs[i+0]);
+                createRecommendationFromString(recs[i+0], factory);
             Recommendation subtrahend =
-                createRecommendationFromString(recs[i+1]);
+                createRecommendationFromString(recs[i+1], factory);
 
             RecommendationPairRecord pr = new RecommendationPairRecord(
                 minuend, subtrahend);
@@ -182,19 +181,22 @@
         layout.setMembersMargin(10);
         this.helperContainer.addMember(helperLayout);
 
-        // Find old data, if any, handle "diffids".
+        populateGrid(dataList, "waterlevel");
+        return layout;
+    }
+
+    protected void populateGrid(DataList dataList, String factory) {
         Data data     = dataList.get(0);
         this.dataName = data.getLabel();
         for (int i = 0; i < dataList.size(); i++) {
             if (dataList.get(i) != null && dataList.get(i).getItems() != null) {
                 if (dataList.get(i).getItems() != null) {
                     populateGridFromString(
-                        dataList.get(i).getItems()[0].getStringValue());
+                        dataList.get(i).getItems()[0].getStringValue(),
+                        factory);
                 }
             }
         }
-
-        return layout;
     }
 
 
@@ -339,7 +341,6 @@
 
                 Recommendation secondR = r.getSecond();
                 adjustRecommendation(secondR);
-
                 ar.add(firstR);
                 ar.add(secondR);
             }
@@ -468,14 +469,20 @@
     }
 
 
+    protected String createDataString(String artifact, Recommendation recommendation) {
+        return createDataString(artifact, recommendation, "staticwkms");
+    }
+
     /**
      * Creates part of the String that encodes minuend or subtrahend.
      * @param artifact Artifacts UUID.
      * @param recommendation Recommendation to wrap in string.
+     * @param factory The factory to encode.
      */
     protected String createDataString(
         String artifact,
-        Recommendation recommendation)
+        Recommendation recommendation,
+        String factory)
     {
         Filter filter = recommendation.getFilter();
         Facet  f      = null;
@@ -503,7 +510,7 @@
         else {
             return "["
                 + artifact
-                + ";staticwkms;0;"
+                + ";" + factory + ";0;"
                 + recommendation.getDisplayName() + "]";
         }
     }
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/BedHeightsDatacagePanel.java	Mon Apr 27 12:48:41 2015 +0200
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/BedHeightsDatacagePanel.java	Mon Apr 27 14:50:38 2015 +0200
@@ -56,7 +56,6 @@
 import java.util.Set;
 
 // TODO Probably better to branch off AbstractUIProvider.
-// TODO Merge with other datacage-widget impls.
 public class BedHeightsDatacagePanel
 extends      DatacageTwinPanel {
 
@@ -83,32 +82,6 @@
         super(user);
     }
 
-
-    /**
-     * Create a recommendation from a string representation of it.
-     * @TODO describe format of input string
-     * @param from string in format as shown above.
-     * @return recommendation from input string.
-     */
-    public Recommendation createRecommendationFromString(String from) {
-        // TODO Construct "real" filter.
-        String[] parts = unbracket(from).split(";");
-        Recommendation.Filter filter = new Recommendation.Filter();
-        Recommendation.Facet  facet  = new Recommendation.Facet(
-                parts[1],
-                parts[2]);
-
-        List<Recommendation.Facet> facets = new ArrayList<Recommendation.Facet>
-            ();
-        facets.add(facet);
-        filter.add("longitudinal_section", facets);
-        Recommendation r = new Recommendation("bedheight", parts[0],
-            this.artifact.getUuid(), filter);
-        r.setDisplayName(parts[3]);
-        return r;
-    }
-
-
     /**
      * Creates the graphical representation and interaction widgets for the data.
      * @param dataList the data.
@@ -142,177 +115,18 @@
         layout.setMembersMargin(10);
         this.helperContainer.addMember(helperLayout);
 
-        this.dataName = "diffids";
+        populateGrid(dataList, "bedheight");
 
         return layout;
     }
 
-
-    /**
-     * Add record to list of removed records.
-     */
-    public void trackRemoved(Record r) {
-        RecommendationPairRecord pr = (RecommendationPairRecord) r;
-        this.removedPairs.add(pr);
+    public void adjustRecommendation(Recommendation recommendation) {
+        recommendation.setFactory("bedheight");
     }
 
-
-    /**
-     * Validates data, does nothing if invalid, otherwise clones new selected
-     * waterlevels and add them to collection, forward the artifact.
-     */
     @Override
-    public void onClick(ClickEvent e) {
-        GWT.log("DatacageTwinPanel.onClick");
-
-        List<String> errors = validate();
-        if (errors != null && !errors.isEmpty()) {
-            showErrors(errors);
-            return;
-        }
-
-        Config config = Config.getInstance();
-        String locale = config.getLocale();
-
-        ListGridRecord[] records = differencesList.getRecords();
-
-        List<Recommendation> ar  = new ArrayList<Recommendation>();
-        List<Recommendation> all = new ArrayList<Recommendation>();
-
-        for (ListGridRecord record : records) {
-            RecommendationPairRecord r =
-                (RecommendationPairRecord) record;
-            // Do not add "old" recommendations.
-            if (!r.isAlreadyLoaded()) {
-                // Check whether one of those is a dike or similar.
-                // TODO differentiate and merge: new clones, new, old.
-                Recommendation firstR = r.getFirst();
-                if(firstR.getIDs() != null) {
-                    GWT.log("First IDs: " + firstR.getIDs() + " factory: "
-                            + firstR.getFactory());
-                }
-                firstR.setFactory("bedheight");
-                Recommendation secondR = r.getSecond();
-                if(secondR.getIDs() != null) {
-                    GWT.log("Second IDs: " + secondR.getIDs() + " factory: "
-                            + secondR.getFactory());
-                }
-                secondR.setFactory("bedheight");
-
-                ar.add(firstR);
-                ar.add(secondR);
-            }
-            else {
-                all.add(r.getFirst());
-                all.add(r.getSecond());
-            }
-        }
-
-        final Recommendation[] toClone = ar.toArray(new Recommendation[ar.size()]);
-        final Recommendation[] toUse   = all.toArray(new Recommendation[all.size()]);
-
-        // Find out whether "old" artifacts have to be removed.
-        List<String> artifactIdsToRemove = new ArrayList<String>();
-        for (RecommendationPairRecord rp: this.removedPairs) {
-            Recommendation first  = rp.getFirst();
-            Recommendation second = rp.getSecond();
-
-            for (Recommendation recommendation: toUse) {
-                if (first != null && first.getIDs().equals(recommendation.getIDs())) {
-                    first = null;
-                }
-                if (second != null && second.getIDs().equals(recommendation.getIDs())) {
-                    second = null;
-                }
-
-                if (first == null && second == null) {
-                    break;
-                }
-            }
-            if (first != null) {
-                artifactIdsToRemove.add(first.getIDs());
-            }
-            if (second != null) {
-                artifactIdsToRemove.add(second.getIDs());
-            }
-        }
-
-        // Remove old artifacts, if any. Do this asychronously without much
-        // feedback.
-        for(final String uuid: artifactIdsToRemove) {
-            removeArtifactService.remove(this.collection,
-                uuid,
-                locale,
-                new AsyncCallback<Collection>() {
-                    public void onFailure(Throwable caught) {
-                        GWT.log("RemoveArtifact (" + uuid + ") failed.");
-                    }
-                    public void onSuccess(Collection collection) {
-                        GWT.log("RemoveArtifact succeeded");
-                    }
-                });
-        }
-
-        // Clone new ones (and spawn statics), go forward.
-        loadArtifactService.loadMany(
-            this.collection,
-            toClone,
-            //"staticwkms" and "waterlevel"
-            null,
-            locale,
-            new AsyncCallback<Artifact[]>() {
-                public void onFailure(Throwable caught) {
-                    GWT.log("Failure of cloning with factories!");
-                }
-                public void onSuccess(Artifact[] artifacts) {
-                    GWT.log("Successfully cloned " + toClone.length +
-                        " with factories.");
-
-                    fireStepForwardEvent(new StepForwardEvent(
-                        getData(toClone, artifacts, toUse)));
-                }
-            });
-    }
-
-
-    /**
-     * Creates part of the String that encodes minuend or subtrahend.
-     * @param artifact Artifacts UUID.
-     * @param recommendation Recommendation to wrap in string.
-     */
-    protected String createDataString(
-        String artifact,
-        Recommendation recommendation)
-    {
-        Filter filter = recommendation.getFilter();
-        Facet  f      = null;
-
-        if(filter != null) {
-            Map<String, List<Facet>>               outs = filter.getOuts();
-            Set<Map.Entry<String, List<Facet>>> entries = outs.entrySet();
-
-            for (Map.Entry<String, List<Facet>> entry: entries) {
-                List<Facet> fs = entry.getValue();
-
-                f = fs.get(0);
-                if (f != null) {
-                    break;
-                }
-            }
-
-            return "[" + artifact + ";"
-                + f.getName()
-                + ";"
-                + f.getIndex()
-                + ";"
-                + recommendation.getDisplayName() + "]";
-        }
-        else {
-            return "["
-                + artifact
-                + ";bedheight;0;"
-                + recommendation.getDisplayName() + "]";
-        }
+    protected String createDataString(String artifact, Recommendation recommendation) {
+        return createDataString(artifact, recommendation, "bedheight");
     }
 
     /**

http://dive4elements.wald.intevation.org