changeset 8597:ba2a34a4e440

(issue1051) Validate epochs before adding them to the list
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 19 Mar 2015 16:28:40 +0100
parents b486812f4f14
children af840bf7f05a
files gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/SedLoadEpochPanel.java
diffstat 1 files changed, 63 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/SedLoadEpochPanel.java	Thu Mar 19 15:15:55 2015 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/client/ui/minfo/SedLoadEpochPanel.java	Thu Mar 19 16:28:40 2015 +0100
@@ -60,6 +60,8 @@
     private TextItem end;
     private ListGrid sedLoadTable;
 
+    protected List<String> validYears;
+
     public Canvas createWidget(DataList data) {
         HLayout input = new HLayout();
         VLayout root = new VLayout();
@@ -85,15 +87,10 @@
             public void onClick(ClickEvent ce) {
                 String v1 = start.getValueAsString();
                 String v2 = end.getValueAsString();
-                //TODO: better validation.
                 if (v1 == null || v2 == null) {
                     return;
                 }
-                try {
-                    int v1i = Integer.parseInt(v1);
-                    int v2i = Integer.parseInt(v2);
-                }
-                catch(NumberFormatException nfe) {
+                if (!isValidEpoch(v1, v2)) {
                     return;
                 }
                 ListGridRecord r = new ListGridRecord();
@@ -286,6 +283,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) {
@@ -325,7 +323,66 @@
         for(SedimentLoadInfoObject sl: sedLoad) {
             SedimentLoadInfoRecord rec = new SedimentLoadInfoRecord(sl);
             sedLoadTable.addData(rec);
+            validYears.add(rec.getDate());
         }
     }
 
+    /* Validate the epoch input. We do this here and not in an overridden
+     * validate method as we want to validate before an epoch is added
+     * to the list of epochs. */
+    protected boolean isValidEpoch(String y1, String y2) {
+        // First check that both are integer
+        int iY1;
+        int iY2;
+        List<String> errors = new ArrayList<String>();
+        try {
+            iY1 = Integer.parseInt(y1);
+        } catch (NumberFormatException e) {
+            errors.add(MESSAGES.wrongFormat() + ": " + y1);
+        }
+        try {
+            iY2 = Integer.parseInt(y2);
+        } catch (NumberFormatException e) {
+            errors.add(MESSAGES.wrongFormat() + ": " + y2);
+        }
+        if (!errors.isEmpty()) {
+            showErrors(errors);
+            return false;
+        }
+        boolean startIsGood = false;
+        boolean endIsGood = false;
+        for (String validYear: validYears) {
+            if (startIsGood || y1.equals(validYear)) {
+                startIsGood = true;
+            }
+            if (endIsGood || y2.equals(validYear)) {
+                endIsGood = true;
+            }
+            if (startIsGood && endIsGood) {
+                break;
+            }
+            /* alternative check if data lies in between
+            int aYear = Integer.parseInt(validYear);
+            if (aYear >= iY1 && aYear <= iY2) {
+                isGood = true;
+                break;
+            }
+         */
+        }
+        if (!startIsGood) {
+            String tmp = MESSAGES.no_data_for_year();
+            tmp = tmp.replace("$1", y1);
+            errors.add(tmp);
+        }
+        if (!endIsGood) {
+            String tmp = MESSAGES.no_data_for_year();
+            tmp = tmp.replace("$1", y2);
+            errors.add(tmp);
+        }
+        if (!errors.isEmpty()) {
+            showErrors(errors);
+            return false;
+        }
+        return true;
+    }
 }

http://dive4elements.wald.intevation.org