diff flys-client/src/main/java/de/intevation/flys/client/client/ui/fixation/FixationPanel.java @ 2884:88bca9f735c0

Implemented filter for fix analysis helper widgets. flys-client/trunk@4567 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 01 Jun 2012 10:27:14 +0000
parents a9cdd62aa73e
children 6ab6025cef20
line wrap: on
line diff
--- a/flys-client/src/main/java/de/intevation/flys/client/client/ui/fixation/FixationPanel.java	Wed May 30 12:02:56 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/client/ui/fixation/FixationPanel.java	Fri Jun 01 10:27:14 2012 +0000
@@ -1,13 +1,23 @@
 package de.intevation.flys.client.client.ui.fixation;
 
 import java.util.HashMap;
+import java.util.Date;
+
 import java.lang.Math;
+import java.io.Serializable;
 
 import java.lang.NumberFormatException;
 
 import com.google.gwt.core.client.GWT;
 import com.google.gwt.user.client.rpc.AsyncCallback;
 
+import com.google.gwt.json.client.JSONArray;
+import com.google.gwt.json.client.JSONObject;
+import com.google.gwt.json.client.JSONNumber;
+import com.google.gwt.json.client.JSONParser;
+import com.google.gwt.json.client.JSONString;
+import com.google.gwt.i18n.client.DateTimeFormat;
+
 import com.smartgwt.client.util.SC;
 
 import com.smartgwt.client.widgets.Canvas;
@@ -38,6 +48,7 @@
 import de.intevation.flys.client.shared.model.Data;
 import de.intevation.flys.client.shared.model.DataList;
 import de.intevation.flys.client.shared.model.FixingsOverviewInfo;
+import de.intevation.flys.client.shared.model.FixAnalysisArtifact;
 
 import de.intevation.flys.client.client.services.FixingsOverviewService;
 import de.intevation.flys.client.client.services.FixingsOverviewServiceAsync;
@@ -63,7 +74,6 @@
 
     protected String   htmlOverview;
     protected FixingsOverviewInfo fixInfo;
-    protected FixFilter fixFilter;
     protected TabSet tabs;
     protected Tab events;
     protected Tab chart;
@@ -71,7 +81,7 @@
     protected Img chartImg;
 
     public FixationPanel() {
-        fixFilter = new FixFilter();
+        GWT.log("new filter");
         chartImg = new Img();
         htmlOverview = "";
     }
@@ -127,18 +137,20 @@
         final HTMLPane eventPane = new HTMLPane();
 
         String river = artifact.getArtifactDescription().getRiver();
-        fixFilter.setRiver(river);
         createCallback();
 /*
 Filter example.
-{"fixings": { "river": { "name": "Elbe"}, "range": {"from": 1, "to": 200}, "filter": {"and": { "column": {"cid": 1}, "column": {"cid": 2} } }}
+{"fixings":  "river": { "name": "Elbe"}, "range": {"from": 1, "to": 200}, "filter": {"and": { "column": {"cid": 1}, "column": {"cid": 2} } }}
 */
         String callBack = "fixationCallback(this.checked, this.name)";
+        FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact;
+
+        GWT.log(art.getFilter().getOverviewFilter());
 
         overviewService.generateOverview(
             locale,
             artifact.getUuid(),
-            fixFilter.getOverviewFilter(),
+            art.getFilter().getOverviewFilter(),
             renderCheckboxes(),
             callBack,
             new AsyncCallback<FixingsOverviewInfo>() {
@@ -150,10 +162,11 @@
                     GWT.log("Successfully loaded overview.");
                     fixInfo = info;
                     htmlOverview = info.getHTML();
-                    fixFilter.setRiver(info.getRiver());
-                    fixFilter.setCurrentKm(1d);
-                    fixFilter.setFromKm(info.getFrom());
-                    fixFilter.setToKm(info.getTo());
+                    FixAnalysisArtifact art = (FixAnalysisArtifact)artifact;
+                    art.getFilter().setRiver(info.getRiver());
+                    art.getFilter().setCurrentKm(1d);
+                    art.getFilter().setFromKm(info.getFrom());
+                    art.getFilter().setToKm(info.getTo());
                     eventPane.setContents(htmlOverview);
                     updateChartTab(fixInfo.getFrom());
                     events.setPane(eventPane);
@@ -186,14 +199,16 @@
         lower.addClickHandler(new ClickHandler() {
             public void onClick(ClickEvent ce) {
                 updateChartTabLow();
-                currentkm.setValue(fixFilter.getCurrentKm());
+                FixAnalysisArtifact art = (FixAnalysisArtifact) artifact;
+                currentkm.setValue(art.getFilter().getCurrentKm());
             }
         });
 
         upper.addClickHandler(new ClickHandler() {
             public void onClick(ClickEvent ce) {
                 updateChartTabUp();
-                currentkm.setValue(fixFilter.getCurrentKm());
+                FixAnalysisArtifact art = (FixAnalysisArtifact) artifact;
+                currentkm.setValue(art.getFilter().getCurrentKm());
             }
         });
 
@@ -226,12 +241,20 @@
         Config config    = Config.getInstance();
         String locale    = config.getLocale ();
 
-        fixFilter.setCurrentKm(km);
-        chartContainer.removeMember(chartImg);
+        FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact;
+        GWT.log("km: " + km);
+        if (km < 0) {
+            km = 0;
+        }
+        GWT.log("km: " + km);
+        art.getFilter().setCurrentKm(km);
+        if (chartContainer.hasMember(chartImg)) {
+            chartContainer.removeMember(chartImg);
+        }
         String imgUrl = GWT.getModuleBaseURL();
         imgUrl += "fixings-km-chart";
         imgUrl += "?locale=" + locale;
-        imgUrl += "&filter=" + fixFilter.getChartFilter(helperContainer.getWidth() - 12, (int)(helperContainer.getWidth()*3/4) - 12);
+        imgUrl += "&filter=" + art.getFilter().getChartFilter(helperContainer.getWidth() - 12, (int)(helperContainer.getWidth()*3/4) - 12);
 
         chartImg = new Img(imgUrl, helperContainer.getWidth() - 12, (int)(helperContainer.getWidth()*3/4) - 12);
         chartContainer.addMember(chartImg);
@@ -240,8 +263,10 @@
 
 
     protected void updateChartTabLow() {
-        double curr = fixFilter.getCurrentKm();
-        if (curr > fixFilter.getFromKm()) {
+        FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact;
+
+        double curr = art.getFilter().getCurrentKm();
+        if (curr > art.getFilter().getFromKm()) {
             double newVal = (curr - 0.1) * 10;
             long round = Math.round(newVal);
             updateChartTab(((double)round) / 10);
@@ -251,8 +276,10 @@
 
 
     protected void updateChartTabUp() {
-        double curr = fixFilter.getCurrentKm();
-        if (curr < fixFilter.getToKm()) {
+        FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact;
+
+        double curr = art.getFilter().getCurrentKm();
+        if (curr < art.getFilter().getToKm()) {
             double newVal = (curr + 0.1) * 10;
             long round = Math.round(newVal);
             updateChartTab(((double)round) / 10);
@@ -262,7 +289,9 @@
 
 
     public void onResized(ResizedEvent re) {
-        updateChartTab(fixFilter.getCurrentKm());
+        FixAnalysisArtifact art = (FixAnalysisArtifact) this.artifact;
+
+        updateChartTab(art.getFilter().getCurrentKm());
     }
 
 
@@ -292,11 +321,9 @@
     public abstract Canvas createWidget(DataList data);
     public abstract void setValues(String cid, boolean checked);
     public abstract boolean renderCheckboxes();
-    public abstract FixFilter getFilter();
-    public abstract void setFilter(FixFilter filter);
     public abstract void success();
 
-    public static class FixFilter {
+    public static class FixFilter implements Serializable{
         protected String river;
         protected double fromKm;
         protected double toKm;
@@ -396,33 +423,100 @@
         }
 
         public String getOverviewFilter() {
-            String filter = "";
             if (river != null && river.length() > 0) {
-                filter = "{\"fixings\": {\"river\": {\"name\": \"" + river + "\"}}";
-                filter += createFilter();
-                filter += "}";
+                JSONObject jfix = new JSONObject();
+                JSONObject jfilter = new JSONObject();
+                JSONObject jrName = new JSONObject();
+                JSONString jrValue = new JSONString(river);
+                jrName.put("name", jrValue);
+                jfilter.put("river", jrName);
+                jfix.put("fixings", createFilter(jfilter));
+                GWT.log("filter: " + jfix.toString());
+                return jfix.toString();
             }
-            return filter;
+            return "";
         }
 
         public String getChartFilter(int width, int height) {
-            String filter = "";
             if (river != null && river.length() > 0 &&
                 currentKm >= fromKm && currentKm <= toKm)
             {
-                filter = "{\"fixings\": "+
-                    "{\"river\": {\"name\": \"" + river + "\"}," +
-                    "\"km\": {\"value\": \"" + currentKm + "\"}," +
-                    "\"extent\": {\"width\": \"" + width + "\", \"height\": \"" + height + "\"}}";
-                filter += createFilter();
-                filter += "}";
+                GWT.log("current: " + currentKm);
+                JSONObject jfix = new JSONObject();
+                JSONObject jfilter = new JSONObject();
+                JSONObject jrName = new JSONObject();
+                JSONString jrValue = new JSONString(river);
+                JSONObject jkm = new JSONObject();
+                JSONNumber jkmValue = new JSONNumber(currentKm);
+                JSONObject jextent = new JSONObject();
+                JSONNumber jwidth = new JSONNumber(width);
+                JSONNumber jheight = new JSONNumber(height);
+
+                jkm.put("value", jkmValue);
+                jrName.put("name", jrValue);
+                jfilter.put("river", jrName);
+                jfilter.put("km", jkm);
+                jextent.put("width", jwidth);
+                jextent.put("height", jheight);
+                jfilter.put("extent", jextent);
+                jfix.put("fixings", createFilter(jfilter));
+                GWT.log("filter: " + jfix.toString());
+                return jfix.toString();
             }
-            return filter;
+            return "";
         }
 
-        protected String createFilter() {
-            //TODO create filter...
-           return "";
+        protected JSONObject createFilter(JSONObject root) {
+            GWT.log("km:" + fromKm + ", " + toKm);
+            if (this.fromKm >= 0 && this.toKm >= 0 && this.fromKm <=this.toKm) {
+                JSONObject range = new JSONObject();
+                JSONObject fromtokm = new JSONObject();
+                JSONNumber f = new JSONNumber(this.fromKm);
+                JSONNumber t = new JSONNumber(this.toKm);
+                fromtokm.put("from", f);
+                fromtokm.put("to", t);
+                root.put("range", fromtokm);
+            }
+            JSONObject and = new JSONObject();
+            if (this.hasDate) {
+                JSONObject daterange = new JSONObject();
+                Date df = new Date(this.fromDate);
+                Date dt = new Date(this.toDate);
+                DateTimeFormat dtf = DateTimeFormat.getFormat("dd.MM.yyyy");
+                JSONString f = new JSONString(dtf.format(df));
+                JSONString t = new JSONString(dtf.format(dt));
+                daterange.put("from", f);
+                daterange.put("to", t);
+                and.put("date-range", daterange);
+            }
+            if (this.fromClass >= 0 &&
+                this.toClass >= 0 &&
+                this.fromClass <= this.toClass) {
+                JSONObject classrange = new JSONObject();
+                JSONNumber f = new JSONNumber(this.fromClass);
+                JSONNumber t = new JSONNumber(this.toClass);
+                classrange.put("from", f);
+                classrange.put("to", t);
+                and.put("sector-range", classrange);
+            }
+            if (this.events.length > 0) {
+                JSONArray col = new JSONArray();
+                for(int i = 0; i < this.events.length; i++) {
+                    JSONObject column = new JSONObject();
+                    JSONObject cid = new JSONObject();
+                    JSONNumber id = new JSONNumber(this.events[i]);
+                    cid.put("cid", id);
+                    column.put("column", cid);
+                    col.set(i, column);
+                }
+                and.put("or", col);
+            }
+            if (and.size() > 0) {
+                JSONObject filter = new JSONObject();
+                filter.put("and", and);
+                root.put("filter", filter);
+            }
+            return root;
         }
     }
 }

http://dive4elements.wald.intevation.org