changeset 2608:938e2f6c0c9a

Refactored the code of the fixings filter building to be useful in more cases. flys-artifacts/trunk@4190 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 03 Apr 2012 13:46:16 +0000
parents 247f3e98a14b
children ed550e325248
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsFilterBuilder.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/FixingsOverviewService.java
diffstat 4 files changed, 236 insertions(+), 163 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Apr 03 11:15:13 2012 +0000
+++ b/flys-artifacts/ChangeLog	Tue Apr 03 13:46:16 2012 +0000
@@ -1,3 +1,18 @@
+2012-04-03	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/services/FixingsOverviewService.java:
+	  Removed the code for filter building.
+
+	* src/main/java/de/intevation/flys/artifacts/model/FixingsFilterBuilder.java:
+	  New. Now contains the code for filter building. This useful because other
+	  services like the "Fixierungs-Rohdaten am KM" service need this filtering, too.
+
+	* src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java:
+	  Refactored the code to obtain a filtered list of fixings columns without
+	  the XML serialization, too. The "Fixierungs-Rohdaten am KM" service
+	  does not need the overview data but the ids of the fixing columns to be
+	  displayed.
+
 2012-04-03	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/services/CacheInvalidationService.java,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsFilterBuilder.java	Tue Apr 03 13:46:16 2012 +0000
@@ -0,0 +1,205 @@
+package de.intevation.flys.artifacts.model;
+
+import de.intevation.flys.artifacts.model.FixingsOverview.AndFilter;
+import de.intevation.flys.artifacts.model.FixingsOverview.DateFilter;
+import de.intevation.flys.artifacts.model.FixingsOverview.DateRangeFilter;
+
+import de.intevation.flys.artifacts.model.FixingsOverview.Fixing.Filter;
+
+import de.intevation.flys.artifacts.model.FixingsOverview.IdFilter;
+import de.intevation.flys.artifacts.model.FixingsOverview.NotFilter;
+import de.intevation.flys.artifacts.model.FixingsOverview.OrFilter;
+import de.intevation.flys.artifacts.model.FixingsOverview.Range;
+import de.intevation.flys.artifacts.model.FixingsOverview.SectorRangeFilter;
+
+import java.text.ParsePosition;
+import java.text.SimpleDateFormat;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+
+public class FixingsFilterBuilder
+{
+    private static Logger log = Logger.getLogger(FixingsFilterBuilder.class);
+
+    protected Filter   filter;
+    protected Range    range;
+
+    protected Document document;
+
+    public FixingsFilterBuilder() {
+    }
+
+    public FixingsFilterBuilder(Document document) {
+        this.document = document;
+    }
+
+    public Filter getFilter() {
+        if (filter == null) {
+            filter = buildFilter();
+        }
+        return filter;
+    }
+
+    public Range getRange() {
+        if (range != null) {
+            range = buildRange();
+        }
+        return range;
+    }
+
+    public Document getDocument() {
+        return document;
+    }
+
+    protected Range buildRange() {
+
+        NodeList ranges = document.getElementsByTagName("range");
+
+        if (ranges.getLength() < 1) {
+            return FixingsOverview.FULL_EXTENT;
+        }
+
+        Element range = (Element)ranges.item(0);
+
+        String from = range.getAttribute("from").trim();
+        String to   = range.getAttribute("to"  ).trim();
+
+        double start = -Double.MAX_VALUE;
+        double end   =  Double.MAX_VALUE;
+
+        if (from.length() > 0) {
+            try {
+                start = Double.parseDouble(from);
+            }
+            catch (NumberFormatException nfe) {
+                log.warn("Invalid from value: " + from);
+            }
+        }
+
+        if (to.length() > 0) {
+            try {
+                end = Double.parseDouble(to);
+            }
+            catch (NumberFormatException nfe) {
+                log.warn("Invalid to value: " + to);
+            }
+        }
+
+        if (start > end) {
+            double t = start;
+            start = end;
+            end = t;
+        }
+
+        return new Range(start, end);
+    }
+
+    protected Filter buildFilter() {
+        NodeList filters = document.getElementsByTagName("filter");
+
+        return filters.getLength() < 1
+            ? FixingsOverview.ACCEPT
+            : buildFilter((Element)filters.item(0));
+    }
+
+    protected static Filter buildFilter(Element root) {
+        List<Filter> filters = buildRecursiveFilter(root);
+        switch (filters.size()) {
+            case  0: return FixingsOverview.ACCEPT;
+            case  1: return filters.get(0);
+            default: return new AndFilter(filters);
+        }
+    }
+
+    protected static final Date parseDate(String text) {
+        SimpleDateFormat format =
+            new SimpleDateFormat(FixingsOverview.DATE_FORMAT);
+        return format.parse(text, new ParsePosition(0));
+    }
+
+    protected static List<Filter> buildRecursiveFilter(Element root) {
+        List<Filter> filters = new ArrayList<Filter>();
+
+        NodeList children = root.getChildNodes();
+
+        for (int i = 0, N = children.getLength(); i < N; ++i) {
+            Node child = children.item(i);
+            if (child.getNodeType() != Node.ELEMENT_NODE) {
+                continue;
+            }
+
+            Element element = (Element)child;
+            String name = element.getLocalName();
+
+            if ("and".equals(name)) {
+                filters.add(new AndFilter(buildRecursiveFilter(element)));
+            }
+            else if ("or".equals(name)) {
+                filters.add(new OrFilter(buildRecursiveFilter(element)));
+            }
+            else if ("not".equals(name)) {
+                List<Filter> childrenFilters = buildRecursiveFilter(element);
+                if (!childrenFilters.isEmpty()) {
+                    filters.add(new NotFilter(childrenFilters.get(0)));
+                }
+            }
+            else if ("column".equals(name)) {
+                String cid = element.getAttribute("cid").trim();
+                if (cid.length() > 0) {
+                    try {
+                        filters.add(new IdFilter(Integer.parseInt(cid)));
+                    }
+                    catch (NumberFormatException nfe) {
+                        log.warn(nfe);
+                    }
+                }
+            }
+            else if ("date".equals(name)) {
+                String when = element.getAttribute("when").trim();
+                if (when.length() > 0) {
+                    Date date = parseDate(when);
+                    if (date != null) {
+                        filters.add(new DateFilter(date));
+                    }
+                }
+            }
+            else if ("date-range".equals(name)) {
+                String from = element.getAttribute("from").trim();
+                String to   = element.getAttribute("to"  ).trim();
+                if (from.length() > 0 && to.length() > 0) {
+                    Date start = parseDate(from);
+                    Date end   = parseDate(to);
+                    if (start != null && end != null) {
+                        filters.add(new DateRangeFilter(start, end));
+                    }
+                }
+            }
+            else if ("sector-range".equals(name)) {
+                String from = element.getAttribute("from").trim();
+                String to   = element.getAttribute("to"  ).trim();
+                if (from.length() > 0 && to.length() > 0) {
+                    try {
+                        filters.add(new SectorRangeFilter(
+                            Integer.parseInt(from),
+                            Integer.parseInt(to)));
+                    }
+                    catch (NumberFormatException nfe) {
+                        log.warn(nfe);
+                    }
+                }
+            }
+        }
+
+        return filters;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java	Tue Apr 03 11:15:13 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FixingsOverview.java	Tue Apr 03 13:46:16 2012 +0000
@@ -853,11 +853,7 @@
         generateOverview(document, FULL_EXTENT, ACCEPT);
     }
 
-    public void generateOverview(
-        Document      document,
-        Range         range,
-        Fixing.Filter filter
-    ) {
+    public List<Fixing.Column> filter(Range range, Fixing.Filter filter) {
         List<Fixing.Column> allColumns = new ArrayList<Fixing.Column>();
 
         for (Fixing fixing: fixings) {
@@ -866,6 +862,16 @@
 
         Collections.sort(allColumns, Fixing.DATE_CMP);
 
+        return allColumns;
+    }
+
+    public void generateOverview(
+        Document      document,
+        Range         range,
+        Fixing.Filter filter
+    ) {
+        List<Fixing.Column> allColumns = filter(range, filter);
+
         Element fixingsElement = document.createElement("fixings");
 
         Element riverElement = document.createElement("river");
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/FixingsOverviewService.java	Tue Apr 03 11:15:13 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/FixingsOverviewService.java	Tue Apr 03 13:46:16 2012 +0000
@@ -5,32 +5,19 @@
 
 import de.intevation.artifacts.common.utils.XMLUtils;
 
+import de.intevation.flys.artifacts.model.FixingsFilterBuilder;
+
 import de.intevation.flys.artifacts.model.FixingsOverview.Fixing.Filter;
 
-import de.intevation.flys.artifacts.model.FixingsOverview.AndFilter;
-import de.intevation.flys.artifacts.model.FixingsOverview.DateFilter;
-import de.intevation.flys.artifacts.model.FixingsOverview.DateRangeFilter;
-import de.intevation.flys.artifacts.model.FixingsOverview.IdFilter;
-import de.intevation.flys.artifacts.model.FixingsOverview.NotFilter;
-import de.intevation.flys.artifacts.model.FixingsOverview.OrFilter;
 import de.intevation.flys.artifacts.model.FixingsOverview.Range;
-import de.intevation.flys.artifacts.model.FixingsOverview.SectorRangeFilter;
 
 import de.intevation.flys.artifacts.model.FixingsOverview;
 import de.intevation.flys.artifacts.model.FixingsOverviewFactory;
 
-import java.text.ParsePosition;
-import java.text.SimpleDateFormat;
-
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
-import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
 public class FixingsOverviewService
@@ -61,8 +48,9 @@
         FixingsOverview overview = FixingsOverviewFactory.getOverview(river);
 
         if (overview != null) {
-            Range  range  = getRange(data);
-            Filter filter = getFilter(data);
+            FixingsFilterBuilder ffb = new FixingsFilterBuilder(data);
+            Range  range  = ffb.getRange();
+            Filter filter = ffb.getFilter();
             overview.generateOverview(document, range, filter);
         }
         else {
@@ -71,146 +59,5 @@
 
         return document;
     }
-
-    public static Range getRange(Document data) {
-
-        NodeList ranges = data.getElementsByTagName("range");
-
-        if (ranges.getLength() < 1) {
-            return FixingsOverview.FULL_EXTENT;
-        }
-
-        Element range = (Element)ranges.item(0);
-
-        String from = range.getAttribute("from").trim();
-        String to   = range.getAttribute("to"  ).trim();
-
-        double start = -Double.MAX_VALUE;
-        double end   =  Double.MAX_VALUE;
-
-        if (from.length() > 0) {
-            try {
-                start = Double.parseDouble(from);
-            }
-            catch (NumberFormatException nfe) {
-                log.warn("Invalid from value: " + from);
-            }
-        }
-
-        if (to.length() > 0) {
-            try {
-                end = Double.parseDouble(to);
-            }
-            catch (NumberFormatException nfe) {
-                log.warn("Invalid to value: " + to);
-            }
-        }
-
-        if (start > end) {
-            double t = start;
-            start = end;
-            end = t;
-        }
-
-        return new Range(start, end);
-    }
-
-    public static Filter getFilter(Document data) {
-
-        NodeList filters = data.getElementsByTagName("filter");
-
-        return filters.getLength() < 1
-            ? FixingsOverview.ACCEPT
-            : buildFilter((Element)filters.item(0));
-    }
-
-    public static Filter buildFilter(Element root) {
-        List<Filter> filters = buildRecursiveFilter(root);
-        switch (filters.size()) {
-            case  0: return FixingsOverview.ACCEPT;
-            case  1: return filters.get(0);
-            default: return new AndFilter(filters);
-        }
-    }
-
-    private static final Date parseDate(String text) {
-        SimpleDateFormat format =
-            new SimpleDateFormat(FixingsOverview.DATE_FORMAT);
-        return format.parse(text, new ParsePosition(0));
-    }
-
-    public static List<Filter> buildRecursiveFilter(Element root) {
-        List<Filter> filters = new ArrayList<Filter>();
-
-        NodeList children = root.getChildNodes();
-
-        for (int i = 0, N = children.getLength(); i < N; ++i) {
-            Node child = children.item(i);
-            if (child.getNodeType() != Node.ELEMENT_NODE) {
-                continue;
-            }
-
-            Element element = (Element)child;
-            String name = element.getLocalName();
-
-            if ("and".equals(name)) {
-                filters.add(new AndFilter(buildRecursiveFilter(element)));
-            }
-            else if ("or".equals(name)) {
-                filters.add(new OrFilter(buildRecursiveFilter(element)));
-            }
-            else if ("not".equals(name)) {
-                List<Filter> childrenFilters = buildRecursiveFilter(element);
-                if (!childrenFilters.isEmpty()) {
-                    filters.add(new NotFilter(childrenFilters.get(0)));
-                }
-            }
-            else if ("column".equals(name)) {
-                String cid = element.getAttribute("cid").trim();
-                if (cid.length() > 0) {
-                    try {
-                        filters.add(new IdFilter(Integer.parseInt(cid)));
-                    }
-                    catch (NumberFormatException nfe) {
-                    }
-                }
-            }
-            else if ("date".equals(name)) {
-                String when = element.getAttribute("when").trim();
-                if (when.length() > 0) {
-                    Date date = parseDate(when);
-                    if (date != null) {
-                        filters.add(new DateFilter(date));
-                    }
-                }
-            }
-            else if ("date-range".equals(name)) {
-                String from = element.getAttribute("from").trim();
-                String to   = element.getAttribute("to"  ).trim();
-                if (from.length() > 0 && to.length() > 0) {
-                    Date start = parseDate(from);
-                    Date end   = parseDate(to);
-                    if (start != null && end != null) {
-                        filters.add(new DateRangeFilter(start, end));
-                    }
-                }
-            }
-            else if ("sector-range".equals(name)) {
-                String from = element.getAttribute("from").trim();
-                String to   = element.getAttribute("to"  ).trim();
-                if (from.length() > 0 && to.length() > 0) {
-                    try {
-                        filters.add(new SectorRangeFilter(
-                            Integer.parseInt(from),
-                            Integer.parseInt(to)));
-                    }
-                    catch (NumberFormatException nfe) {
-                    }
-                }
-            }
-        }
-
-        return filters;
-    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org