Author: michiel
Date: 2010-06-21 11:55:39 +0200 (Mon, 21 Jun 2010)
New Revision: 42629

Modified:
   mmbase/trunk/bridge/src/main/java/org/mmbase/bridge/util/Queries.java
Log:
a few constraint related utilies

Modified: mmbase/trunk/bridge/src/main/java/org/mmbase/bridge/util/Queries.java
===================================================================
--- mmbase/trunk/bridge/src/main/java/org/mmbase/bridge/util/Queries.java       
2010-06-21 09:54:46 UTC (rev 42628)
+++ mmbase/trunk/bridge/src/main/java/org/mmbase/bridge/util/Queries.java       
2010-06-21 09:55:39 UTC (rev 42629)
@@ -15,6 +15,7 @@
 import org.mmbase.datatypes.*;
 import org.mmbase.bridge.implementation.BasicQuery;
 import org.mmbase.storage.search.*;
+import org.mmbase.storage.search.implementation.*;
 import org.mmbase.storage.search.legacy.ConstraintParser;
 import org.mmbase.util.*;
 import org.mmbase.util.logging.*;
@@ -196,7 +197,72 @@
         return newConstraint;
     }
 
+
     /**
+     * @since MMBase-1.9.4
+     */
+    public static List<FieldConstraint> getConstraints(Constraint constraint, 
Step step) {
+        List<FieldConstraint> result = new ArrayList<FieldConstraint>();
+        if (constraint instanceof FieldConstraint) {
+            FieldConstraint fvc = (FieldConstraint) constraint;
+            if (fvc.getField().getStep().equals(step)) {
+                result.add(fvc);
+            }
+        } else if (constraint instanceof CompositeConstraint) {
+            CompositeConstraint composite = (CompositeConstraint) constraint;
+            if (composite.getLogicalOperator() == 
CompositeConstraint.LOGICAL_AND) {
+                for (Constraint cons : composite.getChilds()) {
+                    result.addAll(getConstraints(cons, step));
+                }
+            } else if (composite.getChilds().size() > 0) {
+                result.addAll(getConstraints(composite.getChilds().get(0), 
step));
+            }
+        }
+        return result;
+    }
+
+    /**
+     * @since MMBase-1.9.4
+     */
+    private static boolean removeConstraint(BasicCompositeConstraint 
compConstraint, Constraint cons) {
+        if (compConstraint.getChilds().contains(cons)) {
+            compConstraint.removeChild(cons);
+            return true;
+        } else {
+            for (Constraint child : compConstraint.getChilds()) {
+                if (child instanceof CompositeConstraint) {
+                    boolean r = removeConstraint((BasicCompositeConstraint) 
compConstraint, cons);
+                    if (r) return true;
+                } else {
+                }
+            }
+            return false;
+        }
+
+    }
+
+    /**
+     * @since MMBase-1.9.4
+     */
+    public static boolean removeConstraint(Query q, Constraint cons) {
+        if (cons == null) {
+            return false;
+        }
+        Constraint constraint = q.getConstraint();
+        // remove it from clone (by modifying the 'cloned' constraint)
+        if (cons.equals(constraint)) {
+            q.setConstraint(null);
+            return true;
+        } else { // must be part of the composite constraint
+            if (constraint instanceof CompositeConstraint) {
+                return removeConstraint((BasicCompositeConstraint) constraint, 
cons);
+            } else {
+                return false;
+            }
+        }
+    }
+
+    /**
      * Creates a operator constant for use by createConstraint
      * @param s String representation of operator
      * @return FieldCompareConstraint operator constant
@@ -1358,6 +1424,7 @@
     }
 
 
+
     /**
      * Explores a query object, and creates a certain new relation object, 
which would make the
      * given node appear in the query's result.
@@ -1433,7 +1500,40 @@
 
     }
 
+    /**
+     * @since MMBase-1.9.4
+     */
+    public static int applyConstraints(Query q, Step step, Node n) {
+        NodeManager stepManager = 
q.getCloud().getNodeManager(step.getTableName());
+        if (! (n.getNodeManager().equals(stepManager) || 
stepManager.getDescendants().contains(n.getNodeManager()))) {
+            throw new IllegalArgumentException("Node '" + n.getNumber() + "' 
of type " + n.getNodeManager().getName() + " cannot be part of " + step);
+        }
+        for (FieldConstraint constraint : getConstraints(q.getConstraint(), 
step)) {
+            if (constraint instanceof FieldValueConstraint) {
+                FieldValueConstraint fvc = (FieldValueConstraint) constraint;
+                boolean needsSet = true;
+                try {
+                    needsSet = ! fvc.matches(fvc.getValue());
+                } catch (UnsupportedOperationException ue) {
+                    log.warn(ue);
+                }
+                if (needsSet) {
+                    switch (fvc.getOperator()) {
+                    case FieldCompareConstraint.LESS_EQUAL:
+                    case FieldCompareConstraint.EQUAL:
+                    case FieldCompareConstraint.GREATER_EQUAL:
+                        n.setValue(fvc.getField().getFieldName(), 
fvc.getValue());
+                        break;
+                    default:
+                        throw new IllegalArgumentException("Don't know how to 
apply " + fvc);
+                    }
+                }
+            }
+        }
+        return 0;
 
+    }
+
     /**
      * Deletes the relations with a node from a queries resulting relations 
list.
      * If multiple relations to a node exist all get removed.
@@ -1452,7 +1552,42 @@
         return result;
     }
 
+
     /**
+     * @since MMBase-1.9.4
+     */
+    public static int applyConstraints(Query q, Step step, Node n) {
+        NodeManager stepManager = 
q.getCloud().getNodeManager(step.getTableName());
+        if (! (n.getNodeManager().equals(stepManager) || 
stepManager.getDescendants().contains(n.getNodeManager()))) {
+            throw new IllegalArgumentException("Node '" + n.getNumber() + "' 
of type " + n.getNodeManager().getName() + " cannot be part of " + step);
+        }
+        for (FieldConstraint constraint : getConstraints(q.getConstraint(), 
step)) {
+            if (constraint instanceof FieldValueConstraint) {
+                FieldValueConstraint fvc = (FieldValueConstraint) constraint;
+                boolean needsSet = true;
+                try {
+                    needsSet = ! fvc.matches(fvc.getValue());
+                } catch (UnsupportedOperationException ue) {
+                    log.warn(ue);
+                }
+                if (needsSet) {
+                    switch (fvc.getOperator()) {
+                    case FieldCompareConstraint.LESS_EQUAL:
+                    case FieldCompareConstraint.EQUAL:
+                    case FieldCompareConstraint.GREATER_EQUAL:
+                        n.setValue(fvc.getField().getFieldName(), 
fvc.getValue());
+                        break;
+                    default:
+                        throw new IllegalArgumentException("Don't know how to 
apply " + fvc);
+                    }
+                }
+            }
+        }
+        return 0;
+
+    }
+
+    /**
      * Explores a query object, returns the relations the node has within the 
query.
      *
      * @throws UnsupportedOperationException If it cannot be determined how 
the node is related.

_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to