[PATCH 1 of 2] (issue1051) Validate epochs before adding them to the list

Wald Commits scm-commit at wald.intevation.org
Thu Mar 19 16:50:28 CET 2015


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1426778920 -3600
# Node ID ba2a34a4e4400a2bf939ea09b538970ad6e6d7d2
# Parent  b486812f4f1428cab1be768fb12cc3b454a00e81
(issue1051) Validate epochs before adding them to the list

diff -r b486812f4f14 -r ba2a34a4e440 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	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;
+    }
 }


More information about the Dive4Elements-commits mailing list