[PATCH 4 of 5] Datacage: Remove tree branches vom datacage tree on client side which do not contain loadable data

Wald Commits scm-commit at wald.intevation.org
Wed Oct 30 11:59:20 CET 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1383130737 -3600
# Node ID 4b29bb2c785f542bebb207b2445a3806edc94a74
# Parent  f16dce7a24075fdaca5d4dacdec85620a46bf66b
Datacage: Remove tree branches vom datacage tree on client side which do not contain loadable data.

diff -r f16dce7a2407 -r 4b29bb2c785f gwt-client/src/main/java/org/dive4elements/river/client/server/MetaDataServiceImpl.java
--- a/gwt-client/src/main/java/org/dive4elements/river/client/server/MetaDataServiceImpl.java	Wed Oct 30 11:57:04 2013 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/server/MetaDataServiceImpl.java	Wed Oct 30 11:58:57 2013 +0100
@@ -28,6 +28,8 @@
 import org.dive4elements.artifacts.httpclient.http.HttpClient;
 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl;
 
+import org.dive4elements.river.client.shared.model.AttrList;
+import org.dive4elements.river.client.shared.model.DataCageNode;
 import org.dive4elements.river.client.shared.model.DataCageTree;
 
 import org.dive4elements.river.client.server.meta.Converter;
@@ -38,7 +40,7 @@
  */
 public class MetaDataServiceImpl
 extends      RemoteServiceServlet
-implements   MetaDataService
+implements   MetaDataService, DataCageTree.Visitor
 {
     /** Our very own logger. */
     private static final Logger logger =
@@ -48,6 +50,12 @@
         "error_no_meta_data_found";
 
 
+    @Override
+    public boolean accept(DataCageNode node) {
+        AttrList al = node.getAttributes();
+        return al != null && al.hasAttribute("factory");
+    }
+
     /**
      * @param locale needed for i18n.
      * @param artifactId ID of masterartifact (can be null)
@@ -109,7 +117,9 @@
 
         try {
             Converter converter = new Converter();
-            return converter.convert(client.callService(url, "metadata", doc));
+            DataCageTree tree = converter.convert(client.callService(url, "metadata", doc));
+            tree.prune(this);
+            return tree;
         }
         catch (ConnectionException ce) {
             ce.printStackTrace();
diff -r f16dce7a2407 -r 4b29bb2c785f gwt-client/src/main/java/org/dive4elements/river/client/shared/model/AttrList.java
--- a/gwt-client/src/main/java/org/dive4elements/river/client/shared/model/AttrList.java	Wed Oct 30 11:57:04 2013 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/shared/model/AttrList.java	Wed Oct 30 11:58:57 2013 +0100
@@ -41,5 +41,14 @@
         keyValues.add(key);
         keyValues.add(value);
     }
+
+    public boolean hasAttribute(String key) {
+        for (int i = 0, N = keyValues.size(); i < N; i += 2) {
+            if (keyValues.get(i).equals(key)) {
+                return true;
+            }
+        }
+        return false;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r f16dce7a2407 -r 4b29bb2c785f gwt-client/src/main/java/org/dive4elements/river/client/shared/model/DataCageNode.java
--- a/gwt-client/src/main/java/org/dive4elements/river/client/shared/model/DataCageNode.java	Wed Oct 30 11:57:04 2013 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/shared/model/DataCageNode.java	Wed Oct 30 11:58:57 2013 +0100
@@ -63,5 +63,9 @@
     public AttrList getAttributes() {
         return attrs;
     }
+
+    public boolean hasChildren() {
+        return children != null && !children.isEmpty();
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
diff -r f16dce7a2407 -r 4b29bb2c785f gwt-client/src/main/java/org/dive4elements/river/client/shared/model/DataCageTree.java
--- a/gwt-client/src/main/java/org/dive4elements/river/client/shared/model/DataCageTree.java	Wed Oct 30 11:57:04 2013 +0100
+++ b/gwt-client/src/main/java/org/dive4elements/river/client/shared/model/DataCageTree.java	Wed Oct 30 11:58:57 2013 +0100
@@ -9,9 +9,15 @@
 package org.dive4elements.river.client.shared.model;
 
 import java.io.Serializable;
+import java.util.List;
 
 public class DataCageTree implements Serializable
 {
+
+    public interface Visitor {
+        boolean accept(DataCageNode node);
+    } // interface
+
     protected DataCageNode root;
 
     public DataCageTree() {
@@ -28,5 +34,28 @@
     public DataCageNode getRoot() {
         return root;
     }
+
+
+    protected boolean recursivePrune(DataCageNode node, Visitor visitor) {
+        if (!node.hasChildren()) {
+            return visitor.accept(node);
+        }
+
+        List<DataCageNode> children = node.getChildren();
+
+        for (int i = children.size()-1; i >= 0; --i) {
+            if (!recursivePrune(children.get(i), visitor)) {
+                children.remove(i);
+            }
+        }
+
+        return !children.isEmpty();
+    }
+
+    public boolean prune(Visitor visitor) {
+        return root == null || !root.hasChildren()
+            ? true
+            : recursivePrune(root, visitor);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4elements-commits mailing list