changeset 3208:90c4ce100ce1

Added new base class for *Access flys-artifacts/trunk@4827 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Jun 2012 14:55:58 +0000
parents babb0798d548
children 03fc05655258
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/Access.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/FixationArtifactAccess.java
diffstat 3 files changed, 183 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu Jun 28 14:50:14 2012 +0000
+++ b/flys-artifacts/ChangeLog	Thu Jun 28 14:55:58 2012 +0000
@@ -1,3 +1,11 @@
+2012-06-28	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/access/Access.java: New.
+	  Base class for *Access of FLYSArtifacts.
+
+	* src/main/java/de/intevation/flys/artifacts/access/FixationArtifactAccess.java:
+	  Extends Access now.
+
 2012-06-28	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/access/FixationArtifactAccess.java:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/Access.java	Thu Jun 28 14:55:58 2012 +0000
@@ -0,0 +1,175 @@
+package de.intevation.flys.artifacts.access;
+
+import de.intevation.artifactdatabase.data.StateData;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+
+import de.intevation.flys.artifacts.model.fixings.DateRange;
+
+import de.intevation.flys.utils.FLYSUtils;
+
+import gnu.trove.TDoubleArrayList;
+
+import java.util.ArrayList;
+import java.util.Date;
+
+import org.apache.log4j.Logger;
+
+public class Access 
+{
+    private static Logger log = Logger.getLogger(Access.class);
+
+    protected FLYSArtifact artifact;
+
+    public Access() {
+    }
+
+    public Access(FLYSArtifact artifact) {
+        this.artifact = artifact;
+    }
+
+    public FLYSArtifact getArtifact() {
+        return artifact;
+    }
+
+    public void setArtifact(FLYSArtifact artifact) {
+        this.artifact = artifact;
+    }
+
+    protected String getString(String key) {
+        StateData sd = artifact.getData(key);
+        if (sd == null) {
+            log.warn("missing '" + key + "' value");
+            return null;
+        }
+        return (String)sd.getValue();
+    }
+
+    protected Double getDouble(String key) {
+        StateData sd = artifact.getData(key);
+        if (sd == null) {
+            log.warn("missing '" + key + "' value");
+            return null;
+        }
+        try {
+            return Double.valueOf((String)sd.getValue());
+        }
+        catch (NumberFormatException nfe) {
+            log.warn(key + " '" + sd.getValue() + "' is not numeric.");
+        }
+        return null;
+    }
+
+    protected Long getLong(String key) {
+        StateData sd = artifact.getData(key);
+        if (sd == null) {
+            log.warn("missing '" + key + "' value");
+            return null;
+        }
+        try {
+            return Long.valueOf((String)sd.getValue());
+        }
+        catch (NumberFormatException nfe) {
+            log.warn(key + " '" + sd.getValue() + "' is not a long integer.");
+        }
+        return null;
+    }
+
+    protected Integer getInteger(String key) {
+        StateData sd = artifact.getData(key);
+        if (sd == null) {
+            log.warn("missing '" + key + "' value");
+            return null;
+        }
+        try {
+            return Integer.valueOf((String)sd.getValue());
+        }
+        catch (NumberFormatException nfe) {
+            log.warn(key + " '" + sd.getValue() + "' is not a integer.");
+        }
+        return null;
+    }
+
+    protected int [] getIntArray(String key) {
+        StateData sd = artifact.getData(key);
+        if (sd == null) {
+            log.warn("missing '" + key +"' value");
+            return null;
+        }
+        return FLYSUtils.intArrayFromString((String)sd.getValue());
+    }
+
+    protected DateRange [] getDateRange(String key) {
+
+        StateData sd = artifact.getData(key);
+
+        if (sd == null) {
+            log.warn("missing '" + key + "'");
+            return null;
+        }
+
+        String data = (String)sd.getValue();
+        String[] pairs = data.split("\\s*;\\s*");
+
+        ArrayList<DateRange> aPs = new ArrayList<DateRange>(pairs.length);
+
+        for (int i = 0; i < pairs.length; i++) {
+            String[] fromTo = pairs[i].split("\\s*,\\s*");
+            if (fromTo.length >= 2) {
+                try {
+                    Date from = new Date(Long.parseLong(fromTo[0]));
+                    Date to   = new Date(Long.parseLong(fromTo[1]));
+                    DateRange aP = new DateRange(from, to);
+                    if (!aPs.contains(aP)) {
+                        aPs.add(aP);
+                    }
+                }
+                catch (NumberFormatException nfe) {
+                    log.warn(key + " contains no long values.", nfe);
+                }
+            }
+        }
+
+        DateRange [] result = aPs.toArray(new DateRange[aPs.size()]);
+
+        if (log.isDebugEnabled()) {
+            for (int i = 0; i < result.length; ++i) {
+                DateRange ap = result[i];
+                log.debug("period " +
+                    ap.getFrom() + " - " + ap.getTo());
+            }
+        }
+
+        return result;
+    }
+
+    protected Boolean getBoolean(String key) {
+        StateData sd = artifact.getData(key);
+        if (sd == null) {
+            log.warn("missing '" + key + "' value");
+            return null;
+        }
+        return Boolean.valueOf((String)sd.getValue());
+    }
+
+    protected double [] getDoubleArray(String key) {
+        StateData sd = artifact.getData(key);
+        if (sd == null) {
+            log.warn("missing '" + key + "'");
+            return null;
+        }
+        String [] parts = ((String)sd.getValue()).split("[\\s;]+");
+        TDoubleArrayList list = new TDoubleArrayList(parts.length);
+        for (String part: parts) {
+            try {
+                list.add(Double.parseDouble(part));
+            }
+            catch (NumberFormatException nfe) {
+                log.warn("'" + part + "' is not numeric.");
+            }
+        }
+        return list.toNativeArray();
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
+
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/FixationArtifactAccess.java	Thu Jun 28 14:50:14 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/FixationArtifactAccess.java	Thu Jun 28 14:55:58 2012 +0000
@@ -6,11 +6,6 @@
 
 import de.intevation.flys.artifacts.model.fixings.DateRange;
 
-import de.intevation.flys.utils.FLYSUtils;
-
-import gnu.trove.TDoubleArrayList;
-
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Date;
 

http://dive4elements.wald.intevation.org