Author: michiel
Date: 2010-03-30 11:57:03 +0200 (Tue, 30 Mar 2010)
New Revision: 41663

Modified:
   
mmbase/branches/MMBase-1_9/core/src/main/java/org/mmbase/bridge/util/Queries.java
Log:
added a utility funciton 'getSubQuery'

Modified: 
mmbase/branches/MMBase-1_9/core/src/main/java/org/mmbase/bridge/util/Queries.java
===================================================================
--- 
mmbase/branches/MMBase-1_9/core/src/main/java/org/mmbase/bridge/util/Queries.java
   2010-03-30 09:48:43 UTC (rev 41662)
+++ 
mmbase/branches/MMBase-1_9/core/src/main/java/org/mmbase/bridge/util/Queries.java
   2010-03-30 09:57:03 UTC (rev 41663)
@@ -2047,5 +2047,90 @@
     }
 
 
+    /**
+     * Given a Query, and Node, produces a new query, where the first part of 
the query is replaced by the Node.
+     * So, e.g. if you have a query mags,posrel,news,posrel,images and a news 
node, you can feed this query, and
+     * the node into this method (with step is 2), to produce a query 
news,posrel,images, where the start node is the given news node. All
+     * constraints, nodes, and aliases on the remainings steps are copied.
+     *
+     * The query is a NodeQuery, where the NodeStep is the (normal) step after 
the node.
+     *
+     * @param q The query to base the new query
+     * @param node The node to start the query with. If this is 
<code>null</code> then step must be 0, and the original query will be
+     * returned, only converted to a NodeQuery for the first step.
+     * @param The element step. The first non-relation step after the first 
step where the node is fixed.
+     * @exception ClassCastException if step+1 is not a relationstep. (This 
restriction may perhaps be removed).
+     * @since MMBase-1.9.3
+     */
+    public static NodeQuery getSubQuery(final Query q, final Node node, int 
step) {
 
+        Cloud cloud = q.getCloud();
+        NodeQuery subQuery = cloud.createNodeQuery();
+
+        if (step % 2 != 0) {
+            throw new UnsupportedOperationException("Only non-relation steps 
are supported, so step must be even (now " + step + ")");
+        }
+        if (node != null) {
+            Step firstStep = q.getSteps().get(step - 2);
+            Step copyFirstStep = 
subQuery.addStep(cloud.getNodeManager(firstStep.getTableName()));
+            subQuery.addNode(copyFirstStep, node);
+            addConstraint(subQuery, copyConstraint(q.getConstraint(), 
firstStep, subQuery, copyFirstStep));
+        } else {
+            if (step == 0) {
+                Step sourceStep = q.getSteps().get(0);
+                Step destStep = 
subQuery.addStep(cloud.getNodeManager(sourceStep.getTableName()));
+                addConstraint(subQuery, copyConstraint(q.getConstraint(), 
sourceStep, subQuery, destStep));
+                subQuery.setAlias(destStep, sourceStep.getAlias());
+                addConstraint(subQuery, copyConstraint(q.getConstraint(), 
sourceStep, subQuery, destStep));
+                step += 2;
+            } else {
+                throw new IllegalArgumentException();
+            }
+        }
+        for (int i = step - 1; i < q.getSteps().size(); i+=2) {
+            RelationStep sourceRelStep = (RelationStep) q.getSteps().get(i);
+
+            // Seems a bit cumbersome...
+            Integer role = sourceRelStep.getRole();
+            log.debug("Found role " + role);
+            String roleAsString = role == null ? null : 
cloud.getNode(role).getStringValue("sname");
+
+            RelationStep destRelStep = 
subQuery.addRelationStep(cloud.getNodeManager(sourceRelStep.getNext().getTableName()),
+                                                                roleAsString,
+                                                                
RelationStep.DIRECTIONALITY_DESCRIPTIONS[sourceRelStep.getDirectionality()]);
+
+            addConstraint(subQuery, copyConstraint(q.getConstraint(), 
sourceRelStep, subQuery, destRelStep));
+            if (sourceRelStep.getNodes() != null) {
+                for (int n : sourceRelStep.getNodes()) {
+                    subQuery.addNode(destRelStep, n);
+                }
+            }
+            subQuery.setAlias(destRelStep, sourceRelStep.getAlias());
+
+
+            Step sourceStep = sourceRelStep.getNext();
+            Step destStep = destRelStep.getNext();
+            addConstraint(subQuery, copyConstraint(q.getConstraint(), 
sourceStep, subQuery, destStep));
+
+            if (sourceStep.getNodes() != null) {
+                for (int n : sourceStep.getNodes()) {
+                    subQuery.addNode(destStep, n);
+                }
+            }
+            subQuery.setAlias(destStep, sourceStep.getAlias());
+            copySortOrders(q.getSortOrders(), sourceRelStep, subQuery, 
destRelStep);
+            copySortOrders(q.getSortOrders(), sourceStep, subQuery, destStep);
+        }
+        if (log.isDebugEnabled()) {
+            log.debug("Setting nodeStep to " + subQuery.getSteps().get(2));
+        }
+        if (node != null) {
+            subQuery.setNodeStep(subQuery.getSteps().get(2));
+        } else {
+            subQuery.setNodeStep(subQuery.getSteps().get(0));
+        }
+        subQuery.setDistinct(true);
+        return subQuery;
+    }
+
 }

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

Reply via email to