changeset 2535:d0a9acddbea2

Added an int data item used in int array data and added getter that returns values as colon separated string to all data types. flys-client/trunk@4445 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Mon, 21 May 2012 08:48:16 +0000
parents 05143379059e
children 256b458cc7ea
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/server/DataFactory.java flys-client/src/main/java/de/intevation/flys/client/server/FeedServiceImpl.java flys-client/src/main/java/de/intevation/flys/client/server/StepForwardServiceImpl.java flys-client/src/main/java/de/intevation/flys/client/shared/model/Data.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultData.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DoubleArrayData.java flys-client/src/main/java/de/intevation/flys/client/shared/model/DoubleRangeData.java flys-client/src/main/java/de/intevation/flys/client/shared/model/IntDataItem.java flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerArrayData.java flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerOptionsData.java flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerRangeData.java flys-client/src/main/java/de/intevation/flys/client/shared/model/LongRangeData.java flys-client/src/main/java/de/intevation/flys/client/shared/model/StringOptionsData.java
diffstat 14 files changed, 279 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/ChangeLog	Mon May 21 08:48:16 2012 +0000
@@ -1,3 +1,29 @@
+2012-05-21  Raimund Renkert <raimund.renkert@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/shared/model/Data.java,
+	  src/main/java/de/intevation/flys/client/shared/model/IntegerOptionsData.java,
+	  src/main/java/de/intevation/flys/client/shared/model/DefaultData.java,
+	  src/main/java/de/intevation/flys/client/shared/model/DoubleArrayData.java,
+	  src/main/java/de/intevation/flys/client/shared/model/DoubleRangeData.java,
+	  src/main/java/de/intevation/flys/client/shared/model/IntegerRangeData.java,
+	  src/main/java/de/intevation/flys/client/shared/model/StringOptionsData.java,
+	  src/main/java/de/intevation/flys/client/shared/model/LongRangeData.java:
+	  Added getter that returns the value as string.
+
+	* src/main/java/de/intevation/flys/client/shared/model/IntegerArrayData.java:
+	  Refactored data type. Now stores data items of type IntDataItem
+	  instead of int[].
+
+	* src/main/java/de/intevation/flys/client/shared/model/IntDataItem.java:
+	  New. Data type for int array data.
+
+	* src/main/java/de/intevation/flys/client/server/FeedServiceImpl.java,
+	  src/main/java/de/intevation/flys/client/server/StepForwardServiceImpl.java:
+	  Use new getter in data.
+
+	* src/main/java/de/intevation/flys/client/server/DataFactory.java:
+	  Create int array data using int data items.
+
 2012-05-18  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/FLYSConstants_en.properties,
--- a/flys-client/src/main/java/de/intevation/flys/client/server/DataFactory.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/DataFactory.java	Mon May 21 08:48:16 2012 +0000
@@ -23,6 +23,7 @@
 import de.intevation.flys.client.shared.model.StringOptionsData;
 import de.intevation.flys.client.shared.model.LongRangeData;
 
+import de.intevation.flys.client.shared.model.IntDataItem;
 
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
@@ -198,22 +199,8 @@
      * @return an instance of IntegerArrayData.
      */
     protected static Data createIntegerArrayData(Element ele, String name, String label) {
-        DataItem[] items    = extractDataItems(ele);
-        String     rawValue = items[0].getStringValue();
-
-        String[] values   = rawValue.split(";");
-        int[]    integers = new int[values.length];
-
-        for (int i = 0; i < values.length; i++) {
-            try {
-                integers[i] = Integer.valueOf(values[i]);
-            }
-            catch (NumberFormatException nfe) {
-                logger.warn("Error while parsing IntegerArrayData: " + nfe);
-            }
-        }
-
-        return new IntegerArrayData(name, label, integers);
+        IntDataItem[] items    = extractIntDataItems(ele);
+        return new IntegerArrayData(name, label, items);
     }
 
 
@@ -289,6 +276,46 @@
 
 
     /**
+     * This method extracts the art:item elements placed under <i>elements</i>.
+     *
+     * @param element A data node that contains items.
+     *
+     * @return a list of DataItems.
+     */
+    protected static IntDataItem[] extractIntDataItems(Element element) {
+        NodeList itemList = (NodeList) XMLUtils.xpath(
+            element,
+            "art:item",
+            XPathConstants.NODESET,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (itemList == null || itemList.getLength() == 0) {
+            logger.debug("No old data items found.");
+            return null;
+        }
+
+        int count = itemList.getLength();
+
+        IntDataItem[] items = new IntDataItem[count];
+
+         for (int i = 0; i < count; i++) {
+             Element tmp = (Element) itemList.item(i);
+
+             String value = tmp.getAttributeNS(NS_URI, "value");
+             String label = tmp.getAttributeNS(NS_URI, "label");
+
+             try {
+                 int data = Integer.parseInt(value);
+                 items[i] = new IntDataItem(label, label, data);
+             }
+             catch(NumberFormatException nfe) {
+                 logger.debug(nfe, nfe);
+             }
+         }
+         return items;
+    }
+
+    /**
      * This method creates a new instance of LongRangeData which has a type
      * "longrange" set.
      *
--- a/flys-client/src/main/java/de/intevation/flys/client/server/FeedServiceImpl.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/FeedServiceImpl.java	Mon May 21 08:48:16 2012 +0000
@@ -161,7 +161,7 @@
         for (Data d: data) {
             DataItem[] items = d.getItems();
             String key       = d.getLabel();
-            String value     = items[0].getStringValue();
+            String value     = d.getStringValue();
 
             kvp[i++] = new String[] { key, value };
         }
--- a/flys-client/src/main/java/de/intevation/flys/client/server/StepForwardServiceImpl.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/StepForwardServiceImpl.java	Mon May 21 08:48:16 2012 +0000
@@ -168,7 +168,7 @@
         for (Data d: data) {
             DataItem[] items = d.getItems();
             String key       = d.getLabel();
-            String value     = items[0].getStringValue();
+            String value     = d.getStringValue();
 
             kvp[i++] = new String[] { key, value };
         }
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/Data.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/Data.java	Mon May 21 08:48:16 2012 +0000
@@ -50,5 +50,13 @@
      * @return the default value.
      */
     public DataItem getDefault();
+
+
+    /**
+     * Returns the values as colon separated string.
+     *
+     * @return colon separated string.
+     */
+    public String getStringValue();
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultData.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DefaultData.java	Mon May 21 08:48:16 2012 +0000
@@ -115,5 +115,23 @@
         return new Data[]
             { new DefaultData(name, null, null, new DataItem[] {d})};
     }
+
+    /**
+     * Returns the values as colon separated string.
+     *
+     * @return colon separated string.
+     */
+    public String getStringValue() {
+        String data = "";
+        boolean first = true;
+        for (int i = 0; i < items.length; i++) {
+            if (!first) {
+                data += ";";
+            }
+            data += items[i].getStringValue();
+            first = false;
+        }
+        return data;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/DoubleArrayData.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DoubleArrayData.java	Mon May 21 08:48:16 2012 +0000
@@ -102,5 +102,25 @@
     public DataItem getDefault() {
         return null;
     }
+
+
+    /**
+     * Returns the values as colon separated string.
+     *
+     * @return colon separated string.
+     */
+    public String getStringValue() {
+        String data = "";
+        boolean first = true;
+        for (int i = 0; i < values.length; i++) {
+            if (!first) {
+                data += ";";
+            }
+            data += String.valueOf(values[i]);
+            first = false;
+        }
+        return data;
+    }
+
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/DoubleRangeData.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/DoubleRangeData.java	Mon May 21 08:48:16 2012 +0000
@@ -117,5 +117,17 @@
     public Object getDefaultUpper() {
         return defUpper;
     }
+
+
+    /**
+     * Returns the values as colon separated string.
+     *
+     * @return colon separated string.
+     */
+    public String getStringValue() {
+        String data = lower + ";" + upper;
+        return data;
+    }
+
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/IntDataItem.java	Mon May 21 08:48:16 2012 +0000
@@ -0,0 +1,57 @@
+package de.intevation.flys.client.shared.model;
+
+
+/**
+ * The integer implementation of a {@link DataItem}.
+ *
+ * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
+ */
+public class IntDataItem implements DataItem {
+
+    /** The label. */
+    protected String label;
+
+    /** The description. */
+    protected String description;
+
+    /** The value. */
+    protected int value;
+
+
+    public IntDataItem() {
+    }
+
+
+    /**
+     * The default constructor to create new instances.
+     *
+     * @param label The label.
+     * @param description The description.
+     * @param value The value.
+     */
+    public IntDataItem(String label, String description, int value) {
+        this.label       = label;
+        this.description = description;
+        this.value       = value;
+    }
+
+
+    public String getLabel() {
+        return label;
+    }
+
+
+    public String getDescription() {
+        return description;
+    }
+
+
+    public String getStringValue() {
+        return String.valueOf(value);
+    }
+
+    public int getValue() {
+        return value;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerArrayData.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerArrayData.java	Mon May 21 08:48:16 2012 +0000
@@ -9,14 +9,18 @@
     protected String label;
     protected String description;
 
-    protected int[] values;
+    protected IntDataItem[] values;
 
 
     public IntegerArrayData() {
     }
 
 
-    public IntegerArrayData(String label, String description, int[] values) {
+    public IntegerArrayData(
+        String label,
+        String description,
+        IntDataItem[] values
+    ) {
         this.label       = label;
         this.description = description;
         this.values      = values;
@@ -60,26 +64,7 @@
      * @return the DataItem.
      */
     public DataItem[] getItems() {
-        if (values == null || values.length == 0) {
-            return new DataItem[0];
-        }
-
-        StringBuilder sb    = new StringBuilder();
-        boolean       first = true;
-
-        for (int value: values) {
-            if (first) {
-                sb.append(String.valueOf(value));
-            }
-            else {
-                sb.append(";" + String.valueOf(value));
-            }
-        }
-
-        String  value = sb.toString();
-        DataItem item = new DefaultDataItem(value, value, value);
-
-        return new DataItem[] { item };
+        return values;
     }
 
 
@@ -89,11 +74,33 @@
      * @return the values as array.
      */
     public int[] getValues() {
-        return values;
+        int[] data = new int[values.length];
+        for (int i = 0; i < values.length; i++) {
+            data[i] = values[i].getValue();
+        }
+        return data;
     }
 
 
     /**
+     * Returns the values as colon separated string.
+     *
+     * @return colon separated string.
+     */
+    public String getStringValue() {
+        String data = "";
+        boolean first = true;
+        for (int i = 0; i < values.length; i++) {
+            if (!first) {
+                data += ";";
+            }
+            data += values[i].getStringValue();
+            first = false;
+        }
+        return data;
+    }
+
+    /**
      * @return always null.
      */
     public DataItem getDefault() {
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerOptionsData.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerOptionsData.java	Mon May 21 08:48:16 2012 +0000
@@ -68,4 +68,24 @@
     public DataItem getDefault() {
         return null;
     }
+
+
+    /**
+     * Returns the values as colon separated string.
+     *
+     * @return colon separated string.
+     */
+    public String getStringValue() {
+        String data = "";
+        boolean first = true;
+        for (int i = 0; i < opts.length; i++) {
+            if (!first) {
+                data += ";";
+            }
+            data += opts[i].getStringValue();
+            first = false;
+        }
+        return data;
+    }
+
 }
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerRangeData.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/IntegerRangeData.java	Mon May 21 08:48:16 2012 +0000
@@ -128,5 +128,17 @@
     public Object getDefaultUpper() {
         return defUpper;
     }
+
+
+    /**
+     * Returns the values as colon separated string.
+     *
+     * @return colon separated string.
+     */
+    public String getStringValue() {
+        String data = lower + ";" + upper;
+        return data;
+    }
+
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/LongRangeData.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/LongRangeData.java	Mon May 21 08:48:16 2012 +0000
@@ -127,5 +127,17 @@
     public Object getDefaultUpper() {
         return defUpper;
     }
+
+
+    /**
+     * Returns the values as colon separated string.
+     *
+     * @return colon separated string.
+     */
+    public String getStringValue() {
+        String data = lower + ";" + upper;
+        return data;
+    }
+
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-client/src/main/java/de/intevation/flys/client/shared/model/StringOptionsData.java	Fri May 18 12:42:57 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/shared/model/StringOptionsData.java	Mon May 21 08:48:16 2012 +0000
@@ -68,5 +68,24 @@
     public DataItem getDefault() {
         return null;
     }
+
+
+    /**
+     * Returns the values as colon separated string.
+     *
+     * @return colon separated string.
+     */
+    public String getStringValue() {
+        String data = "";
+        boolean first = true;
+        for (int i = 0; i < opts.length; i++) {
+            if (!first) {
+                data += ";";
+            }
+            data += opts[i].getStringValue();
+            first = false;
+        }
+        return data;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org