Author: michiel
Date: 2010-05-17 14:02:34 +0200 (Mon, 17 May 2010)
New Revision: 42181

Modified:
   
mmbase/branches/MMBase-1_9/core/src/main/java/org/mmbase/bridge/util/Queries.java
Log:
copying sortorders in steps after element steps fucks up distinct restrictions, 
so don't do that

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-05-17 12:00:31 UTC (rev 42180)
+++ 
mmbase/branches/MMBase-1_9/core/src/main/java/org/mmbase/bridge/util/Queries.java
   2010-05-17 12:02:34 UTC (rev 42181)
@@ -630,11 +630,15 @@
 
      */
     public static void copySortOrders(List<SortOrder> sortOrders, Step 
sourceStep,  Query query, Step step) {
-        log.info("Copying sortorders " + sortOrders);
+        if (log.isDebugEnabled()) {
+            log.debug("Copying sortorders " + sortOrders);
+        }
         for (SortOrder sortOrder : sortOrders) {
             StepField sourceField = sortOrder.getField();
             if (! sourceField.getStep().equals(sourceStep)) {
-                log.info("Different step " + sourceField + " " + sourceStep);
+                if (log.isDebugEnabled()) {
+                    log.debug("Different step " + sourceField + " " + 
sourceStep);
+                }
                 continue; // for another step
             }
             if (sortOrder instanceof DateSortOrder) {
@@ -644,7 +648,9 @@
             } else {
                 SortOrder so = query.addSortOrder(query.createStepField(step, 
sourceField.getFieldName()), sortOrder.getDirection(),
                                                   sortOrder.isCaseSensitive());
-                log.info("Copied " + so);
+                if (log.isDebugEnabled()) {
+                    log.debug("Copied " + so);
+                }
 
             }
         }
@@ -2052,30 +2058,37 @@
 
     }
 
-
     /**
+     * Defaulting version {...@link #getSubQuery(Query, Node, int, int)}. The 
last argument is replaced with q.getSteps().size();
+     * @since MMBase-1.9.3
+     */
+    public static NodeQuery getSubQuery(final Query q, final Node node, int 
step) {
+        return getSubQuery(q, node, step, q.getSteps().size());
+    }
+    /**
      * 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.
+     * constraints, nodes, and aliases on the remainings steps are copied. 
Sortorders are only preserved in steps <= elementStep.
      *
      * 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
+     * @param elementStep The element step. The first non-relation step after 
the first step where the node is fixed.
+     * @param lastStep On default the remainder of the query is copied. It you 
need to truncate it from the back to, use this argument.
+     * @exception ClassCastException if step + 1 is not a relationstep. (This 
restriction may perhaps be removed).
+     * @since MMBase-1.9.4
      */
-    public static NodeQuery getSubQuery(final Query q, final Node node, int 
step) {
-
+    public static NodeQuery getSubQuery(final Query q, final Node node, final 
int elementStep, final int lastStep) {
         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 (elementStep % 2 != 0) {
+            throw new UnsupportedOperationException("Only non-relation steps 
are supported, so element step must be even (now " + elementStep + ")");
         }
+        int step = elementStep;
         if (node != null) {
             Step firstStep = q.getSteps().get(step - 2);
             Step copyFirstStep = 
subQuery.addStep(cloud.getNodeManager(firstStep.getTableName()));
@@ -2094,7 +2107,7 @@
                 throw new IllegalArgumentException();
             }
         }
-        for (int i = step - 1; i < q.getSteps().size(); i+=2) {
+        for (int i = step - 1; i < lastStep; i+=2) {
             RelationStep sourceRelStep = (RelationStep) q.getSteps().get(i);
 
             // Seems a bit cumbersome...
@@ -2125,8 +2138,11 @@
                 }
             }
             subQuery.setAlias(destStep, sourceStep.getAlias());
-            copySortOrders(q.getSortOrders(), sourceRelStep, subQuery, 
destRelStep);
-            copySortOrders(q.getSortOrders(), sourceStep, subQuery, destStep);
+            if (i < elementStep) {
+                // don't copy sortorder other, that will **** up distinct 
restrictions in previous, actually shown steps
+                copySortOrders(q.getSortOrders(), sourceRelStep, subQuery, 
destRelStep);
+                copySortOrders(q.getSortOrders(), sourceStep, subQuery, 
destStep);
+            }
         }
         if (log.isDebugEnabled()) {
             log.debug("Setting nodeStep to " + subQuery.getSteps().get(2));

_______________________________________________
Cvs mailing list
Cvs@lists.mmbase.org
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to