[PATCH 02 of 45] (issue1051) Use a list of single years in SedimentLoadYear mode

Wald Commits scm-commit at wald.intevation.org
Tue Mar 10 17:05:30 CET 2015


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1423674048 -3600
# Node ID 73a4c3c202e5af849f0f2bd11fd4b617beb806ff
# Parent  3e6d4bd1c06e8887c175b36c1e7a1c7dbc491563
(issue1051) Use a list of single years in SedimentLoadYear mode

    Instead of a range you can now select multiple distinct years
    in the sedimentload calculation.

diff -r 3e6d4bd1c06e -r 73a4c3c202e5 artifacts/doc/conf/artifacts/minfo.xml
--- a/artifacts/doc/conf/artifacts/minfo.xml	Wed Feb 11 12:41:51 2015 +0100
+++ b/artifacts/doc/conf/artifacts/minfo.xml	Wed Feb 11 18:00:48 2015 +0100
@@ -545,8 +545,7 @@
 
         <state id="state.minfo.sediment.load.period" description="state.minfo.bed.period" state="org.dive4elements.river.artifacts.states.minfo.SedimentLoadYearSelect"
 				helpText="help.state.minfo.sediment.load.period">
-            <data name="start" type="Integer"/>
-            <data name="end" type="Integer"/>
+            <data name="years" type="String"/>
         </state>
 
         <state id="state.minfo.sediment.load.epochs" description="state.minfo.bed.epochs" state="org.dive4elements.river.artifacts.states.minfo.SedimentLoadEpochSelect"
diff -r 3e6d4bd1c06e -r 73a4c3c202e5 artifacts/src/main/java/org/dive4elements/river/artifacts/access/SedimentLoadAccess.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/access/SedimentLoadAccess.java	Wed Feb 11 12:41:51 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/access/SedimentLoadAccess.java	Wed Feb 11 18:00:48 2015 +0100
@@ -25,12 +25,13 @@
 
     private int [][] epochs;
 
-    private int [] period;
+    private int [] years;
 
     private Integer sqTiId;
 
     public SedimentLoadAccess(D4EArtifact artifact) {
         super(artifact);
+        years = null;
     }
 
     public Double getLowerKM() {
@@ -50,21 +51,33 @@
         return time;
     }
 
-    /** [startyear, endyear] if its about years. */
-    public int[] getPeriod() {
-        if (period != null) {
-            return period;
+    /** [year1, years2,..] if its about years. */
+    public int[] getYears() {
+        if (years != null) {
+            return years;
         }
         if (getYearEpoch().equals("year") ) {
-            Integer start = getInteger("start");
-            Integer end = getInteger("end");
-            if (start == null || end == null) {
-                log.warn("No 'start' or 'end' parameter specified!");
+            TIntArrayList ints = new TIntArrayList();
+            String yearsData = getString("years");
+            if (yearsData == null || yearsData.isEmpty()) {
+                log.warn("No years provided");
                 return null;
             }
+            for (String sValue :yearsData.split(" ")) {
+                try {
+                    ints.add(Integer.parseInt(sValue));
+                } catch (NumberFormatException e) {
+                    /* Client should prevent this */
+                    log.warn("Invalid year value: " + sValue);
+                    continue;
+                }
+            }
 
-            period = new int[]{start.intValue(), end.intValue()};
-            return period;
+            if (!ints.isEmpty()) {
+                ints.sort();
+                years = ints.toNativeArray();
+            }
+            return years;
         }
         return null;
     }
diff -r 3e6d4bd1c06e -r 73a4c3c202e5 artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataCalculation.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataCalculation.java	Wed Feb 11 12:41:51 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadDataCalculation.java	Wed Feb 11 18:00:48 2015 +0100
@@ -176,7 +176,7 @@
         Integer sqTiId = access.getSQTiId();
 
         if (yearEpoch.equals("year")) {
-            years = access.getPeriod();
+            years = access.getYears();
         }
         else if (yearEpoch.equals("epoch") || yearEpoch.equals("off_epoch")) {
             epochs = access.getEpochs();
@@ -240,10 +240,8 @@
 
         SedimentDensity sd = getSedimentDensity();
 
-        int min = Math.min(years[0], years[1]);
-        int max = Math.max(years[0], years[1]);
-
-        for (int year = min; year <= max; ++year) {
+        for (int i = 0; i < years.length; i++) {
+            int year = years[i];
             Value.Filter filter = new And(notEpochs)
                 .add(new TimeRangeIntersects(year)).add(sqTiFilter);
             String period = Integer.toString(year);
diff -r 3e6d4bd1c06e -r 73a4c3c202e5 artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/SedimentLoadYearSelect.java
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/SedimentLoadYearSelect.java	Wed Feb 11 12:41:51 2015 +0100
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/states/minfo/SedimentLoadYearSelect.java	Wed Feb 11 18:00:48 2015 +0100
@@ -47,7 +47,7 @@
         SedimentLoadAccess access = new SedimentLoadAccess((D4EArtifact) artifact);
 
         // Second year should be later than first.
-        if (access.getPeriod() != null && access.getPeriod()[1] < access.getPeriod()[0])
+        if (access.getYears() != null && access.getYears()[1] < access.getYears()[0])
            throw new IllegalArgumentException("error_years_wrong");
 
         return true;
diff -r 3e6d4bd1c06e -r 73a4c3c202e5 gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.java
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.java	Wed Feb 11 12:41:51 2015 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.java	Wed Feb 11 18:00:48 2015 +0100
@@ -1411,5 +1411,7 @@
     String lower_time();
 
     String upper_time();
+
+    String no_data_for_year();
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r 3e6d4bd1c06e -r 73a4c3c202e5 gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.properties
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.properties	Wed Feb 11 12:41:51 2015 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants.properties	Wed Feb 11 18:00:48 2015 +0100
@@ -750,3 +750,5 @@
 
 lower_time = from
 upper_time = to
+
+no_data_for_year = No data available for: $1
diff -r 3e6d4bd1c06e -r 73a4c3c202e5 gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_de.properties
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_de.properties	Wed Feb 11 12:41:51 2015 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_de.properties	Wed Feb 11 18:00:48 2015 +0100
@@ -742,3 +742,5 @@
 
 lower_time = von
 upper_time = bis
+
+no_data_for_year = F\u00fcr das Jahr $1 liegen keine Daten vor.
diff -r 3e6d4bd1c06e -r 73a4c3c202e5 gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_en.properties
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_en.properties	Wed Feb 11 12:41:51 2015 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/FLYSConstants_en.properties	Wed Feb 11 18:00:48 2015 +0100
@@ -779,3 +779,5 @@
 
 lower_time = from
 upper_time = to
+
+no_data_for_year = No data available for: $1
diff -r 3e6d4bd1c06e -r 73a4c3c202e5 gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/SedLoadEpochPanel.java
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/SedLoadEpochPanel.java	Wed Feb 11 12:41:51 2015 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/SedLoadEpochPanel.java	Wed Feb 11 18:00:48 2015 +0100
@@ -228,6 +228,7 @@
                 start.setValue(r.getAttribute("date"));
             }
         });
+
         pinTo.addRecordClickHandler (new RecordClickHandler () {
             @Override
             public void onRecordClick (RecordClickEvent e) {
diff -r 3e6d4bd1c06e -r 73a4c3c202e5 gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/SedLoadPeriodPanel.java
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/SedLoadPeriodPanel.java	Wed Feb 11 12:41:51 2015 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/SedLoadPeriodPanel.java	Wed Feb 11 18:00:48 2015 +0100
@@ -12,7 +12,9 @@
 import java.util.List;
 
 import com.google.gwt.core.client.GWT;
+import com.google.gwt.i18n.client.NumberFormat;
 import com.google.gwt.user.client.rpc.AsyncCallback;
+import com.smartgwt.client.data.Record;
 import com.smartgwt.client.types.ListGridFieldType;
 import com.smartgwt.client.util.SC;
 import com.smartgwt.client.widgets.Canvas;
@@ -24,8 +26,11 @@
 import com.smartgwt.client.widgets.grid.ListGridField;
 import com.smartgwt.client.widgets.layout.HLayout;
 import com.smartgwt.client.widgets.layout.VLayout;
+import com.smartgwt.client.widgets.grid.events.RecordClickHandler;
+import com.smartgwt.client.widgets.grid.events.RecordClickEvent;
 
 import org.dive4elements.river.client.client.Config;
+import org.dive4elements.river.client.client.FLYSConstants;
 import org.dive4elements.river.client.client.services.SedimentLoadInfoService;
 import org.dive4elements.river.client.client.services.SedimentLoadInfoServiceAsync;
 import org.dive4elements.river.client.client.ui.AbstractUIProvider;
@@ -45,35 +50,26 @@
     protected SedimentLoadInfoServiceAsync sedLoadInfoService =
         GWT.create(SedimentLoadInfoService.class);
 
-    private TextItem start;
-    private TextItem end;
+    protected FLYSConstants MESSAGES = GWT.create(FLYSConstants.class);
+
+    private TextItem yearsItem;
 
     private ListGrid sedLoadTable;
 
+    protected List<String> validYears;
+
     public SedLoadPeriodPanel () {
     }
 
     @Override
     public Canvas createOld(DataList dataList) {
         List<Data> items = dataList.getAll();
-        Data start = getData(items, "start");
-        Data end = getData(items, "end");
-        DataItem[] startItem = start.getItems();
-        DataItem[] endItem = end.getItems();
+        Data years = getData(items, "years");
+        DataItem[] yearsItems = years.getItems();
 
-        String v1 = startItem[0].getStringValue();
-        String v2 = endItem[0].getStringValue();
+        String v1 = yearsItems[0].getStringValue().replace(" ", ", ");
 
-        int v1i = 0;
-        int v2i = 0;
-        try {
-            v1i = Integer.parseInt(v1);
-            v2i = Integer.parseInt(v2);
-        }
-        catch (NumberFormatException nfe) {
-            GWT.log(nfe.toString());
-        }
-        Label old = new Label(v1i + " - " + v2i);
+        Label old = new Label(v1);
         HLayout layout = new HLayout();
         layout.setWidth("400px");
 
@@ -90,6 +86,11 @@
     }
 
     @Override
+    public List<String> validate() {
+        return validateYears();
+    }
+
+    @Override
     public Canvas create(DataList data) {
         VLayout layout = new VLayout();
 
@@ -126,7 +127,21 @@
         descr.setType(ListGridFieldType.TEXT);
         descr.setWidth("*");
 
-        sedLoadTable.setFields(date, descr);
+        String baseUrl = GWT.getHostPageBaseURL();
+        ListGridField pinFrom = new ListGridField ("fromIcon",  MESSAGES.selection());
+        pinFrom.setWidth (60);
+        pinFrom.setType (ListGridFieldType.ICON);
+        pinFrom.setCellIcon(baseUrl + MESSAGES.markerGreen());
+
+        pinFrom.addRecordClickHandler (new RecordClickHandler () {
+            @Override
+            public void onRecordClick (RecordClickEvent e) {
+                Record r = e.getRecord();
+                appendYear(r.getAttribute("date"));
+            }
+        });
+
+        sedLoadTable.setFields(pinFrom, date, descr);
         return sedLoadTable;
     }
 
@@ -138,14 +153,9 @@
 
         DynamicForm form = new DynamicForm();
         form.setNumCols(4);
-        start = new TextItem(MSG.from());
-        start.setValidators(new IsIntegerValidator());
-        end = new TextItem(MSG.to());
-        end.setValidators(new IsIntegerValidator());
-        form.setFields(start, end);
-//        inputPanel.setToTitle(MSG.to());
-//        inputPanel.setFromTitle(MSG.from());
-//        inputPanel.setShowTitle(false);
+        yearsItem = new TextItem(MSG.years());
+        yearsItem.setValidators(new IsIntegerValidator());
+        form.setFields(yearsItem);
 
         layout.addMember(title);
         layout.addMember(form);
@@ -155,45 +165,66 @@
 
     @Override
     protected Data[] getData() {
-        int v1;
-        int v2;
-        try {
-            v1 = Integer.parseInt(start.getValueAsString());
-            v2 = Integer.parseInt(end.getValueAsString());
-        }
-        catch(NumberFormatException nfe) {
-            // warn the user...
-            return new Data[0];
-        }
-        if (validateRange(v1, v2)) {
+        validateYears();
+        if (yearsItem != null && !yearsItem.getValueAsString().isEmpty()) {
             List<Data> data = new ArrayList<Data>();
 
-            DataItem startItem = new DefaultDataItem("start", "start", start.getValueAsString());
-            DataItem endItem   = new DefaultDataItem("end", "end", end.getValueAsString());
+            DataItem yearsdata = new DefaultDataItem("years", "years", yearsItem.getValueAsString().trim());
             data.add(new DefaultData(
-                "start",
+                "years",
                 null,
                 null,
-                new DataItem[] { startItem }));
-            data.add(new DefaultData(
-                "end",
-                null,
-                null,
-                new DataItem[] { endItem }));
+                new DataItem[] { yearsdata }));
 
             return data.toArray(new Data[data.size()]);
         }
         return new Data[0];
     }
 
-    protected boolean validateRange(int v1, int v2) {
-        // TODO: Set useful years for validation. Current range is between start
-        // of gregorian calendar and a year in the future...
-        if ((v1 > 1582 && v1 < 2100)
-            && (v2 > 1582 && v2 < 2100)) {
-            return true;
+    protected List<String> validateYears() {
+        List<String> errors = new ArrayList<String>();
+        NumberFormat nf     = NumberFormat.getDecimalFormat();
+
+        if (yearsItem.getValueAsString() == null ||
+                yearsItem.getValueAsString().trim().isEmpty()) {
+            errors.add(MESSAGES.empty_filter());
+            return errors;
         }
-        return false;
+
+        String [] sValues = yearsItem.getValueAsString().trim().split(" ");
+        String filtered = "";
+        int goodValues = 0;
+        for (String sValue: sValues) {
+            int value;
+            try {
+                value = Integer.parseInt(sValue);
+            } catch (NumberFormatException e) {
+                errors.add(MESSAGES.wrongFormat());
+                continue;
+            }
+            boolean isGood = false;
+            for (String validYear: validYears) {
+                /* No list contains for strings? */
+                if (sValue.equals(validYear)) {
+                    isGood = true;
+                    break;
+                }
+            }
+            if (!isGood) {
+                String tmp = MESSAGES.no_data_for_year();
+                tmp = tmp.replace("$1", sValue);
+                errors.add(tmp);
+                continue;
+            }
+            goodValues++;
+            if (goodValues > 1) {
+                filtered += " " + Integer.toString(value);
+            } else {
+                filtered = Integer.toString(value);
+            }
+        }
+
+        return errors;
     }
 
    protected void fetchSedimentLoadData() {
@@ -207,6 +238,7 @@
         String river = artifact.getArtifactDescription().getRiver();
 
         String sq_ti_id = "";
+        validYears = new ArrayList<String>(data.length);
         for (int i = 0; i < data.length; i++) {
             Data str = getData(data[i].getAll(), "sq_ti_id");
             if (str != null) {
@@ -246,7 +278,16 @@
         for(SedimentLoadInfoObject sl: sedLoad) {
             SedimentLoadInfoRecord rec = new SedimentLoadInfoRecord(sl);
             sedLoadTable.addData(rec);
+            validYears.add(rec.getDate());
         }
     }
 
+    protected void appendYear (String year) {
+        String oldYears = yearsItem.getValueAsString();
+        if (oldYears != null && !oldYears.isEmpty()) {
+            yearsItem.setValue(oldYears.trim() + " " + year);
+        } else {
+            yearsItem.setValue(year);
+        }
+    }
 }


More information about the Dive4Elements-commits mailing list