jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r718371 - in /jackrabbit/trunk/jackrabbit-ocm/src: main/java/org/apache/jackrabbit/ocm/query/impl/QueryImpl.java test/java/org/apache/jackrabbit/ocm/manager/query/DigesterSimpleQueryTest.java
Date Mon, 17 Nov 2008 20:38:18 GMT
Author: clombart
Date: Mon Nov 17 12:38:18 2008
New Revision: 718371

URL: http://svn.apache.org/viewvc?rev=718371&view=rev
Log:
patch for JCR-1853 provided by Shrirang

Modified:
    jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/query/impl/QueryImpl.java
    jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/query/DigesterSimpleQueryTest.java

Modified: jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/query/impl/QueryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/query/impl/QueryImpl.java?rev=718371&r1=718370&r2=718371&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/query/impl/QueryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/query/impl/QueryImpl.java
Mon Nov 17 12:38:18 2008
@@ -38,7 +38,9 @@
 	
     ClassDescriptor classDescriptor;
 
-    private ArrayList orderByExpressions = new ArrayList();
+    private final static String ORDER_BY_STRING =  "order by ";
+    
+    private String jcrExpression = "";
 
 	/**
 	 * Constructor
@@ -70,7 +72,9 @@
 
 	public void addOrderByDescending(String fieldNameAttribute)
 	{
-		orderByExpressions.add("@" + this.getJcrFieldName(fieldNameAttribute) + " descending");
+		//Changes made to maintain the query state updated with every addition
+		//@author Shrirang Edgaonkar
+		addExpression("@" + this.getJcrFieldName(fieldNameAttribute) + " descending");
 	}
 
 	/**
@@ -79,31 +83,39 @@
 	 */
 	public void addOrderByAscending(String fieldNameAttribute)
 	{
-		orderByExpressions.add("@" + this.getJcrFieldName(fieldNameAttribute) + " ascending");
+		addExpression("@" + this.getJcrFieldName(fieldNameAttribute) + " ascending");
 	}
 	
+    public void addJCRExpression(String jcrExpression) {
+        addExpression(jcrExpression);
+     }
+	
+	
+    private void addExpression(String jcrExpression) {
+		 //@author Shrirang Edgaonkar
+    	 // First time comma is not required
+    	 if(this.jcrExpression.equals(""))
+    	 {	
+    		 this.jcrExpression += jcrExpression ;
+    	 }else
+    		 this.jcrExpression += (" , " + jcrExpression) ;
+    }
+
+	
+    
 	public String getOrderByExpression()
 	{
-		
-		if (orderByExpressions.size() == 0)
-		{
-		   return "";	
-		}
-		
-		String orderByExpression = "order by ";
-		Iterator iterator   = orderByExpressions.iterator();
-		int count=1;
-		while (iterator.hasNext())
-		{
-			   if (count > 1)
-			   {
-				   orderByExpression += " , ";
-			   }
-			   orderByExpression+= (String) iterator.next();
-			   count++;
+		if(jcrExpression.equals(""))
+			return "";
+		else
+		{	
+			//@author Shrirang Edgaonkar
+			//Ensure that the OrderBy string is added only once
+			if(this.jcrExpression.contains(ORDER_BY_STRING))
+				return this.jcrExpression;
+			else
+				return (ORDER_BY_STRING + this.jcrExpression);
 		}
-		
-		return orderByExpression;
 	}
 	
 	

Modified: jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/query/DigesterSimpleQueryTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/query/DigesterSimpleQueryTest.java?rev=718371&r1=718370&r2=718371&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/query/DigesterSimpleQueryTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/query/DigesterSimpleQueryTest.java
Mon Nov 17 12:38:18 2008
@@ -18,23 +18,24 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Iterator;
 
 import javax.jcr.Node;
-import javax.jcr.NodeIterator;
-import javax.jcr.query.QueryResult;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.jackrabbit.ocm.RepositoryLifecycleTestSetup;
 import org.apache.jackrabbit.ocm.DigesterTestBase;
+import org.apache.jackrabbit.ocm.RepositoryLifecycleTestSetup;
 import org.apache.jackrabbit.ocm.exception.JcrMappingException;
 import org.apache.jackrabbit.ocm.manager.ObjectContentManager;
 import org.apache.jackrabbit.ocm.query.Filter;
 import org.apache.jackrabbit.ocm.query.Query;
 import org.apache.jackrabbit.ocm.query.QueryManager;
+import org.apache.jackrabbit.ocm.query.impl.QueryImpl;
 import org.apache.jackrabbit.ocm.testmodel.Page;
 import org.apache.jackrabbit.ocm.testmodel.Paragraph;
 
@@ -86,6 +87,7 @@
             filter.addEqualTo("text", "Para 1");
 
             Query query = queryManager.createQuery(filter);
+            
 
             ObjectContentManager ocm = this.getObjectContentManager();
             Paragraph paragraph = (Paragraph) ocm.getObject(query);
@@ -280,11 +282,80 @@
             assertTrue("Invalid paragraph found", this.containsText(paragraphs,"Para 2"));
             assertTrue("Invalid paragraph found", this.containsText(paragraphs,"Para 3"));
 
-        } catch (Exception e) {
-            e.printStackTrace();
-            fail("Exception occurs during the unit test : " + e);
-        }
-
+	        } catch (Exception e) {
+	            e.printStackTrace();
+	            fail("Exception occurs during the unit test : " + e);
+	        }
+
+    	}
+
+        
+        
+        public void testGetObjectOrderByWithUpdatableJCRExpression() {
+
+            try {
+
+                // Build the Query Object
+                QueryManager queryManager = this.getQueryManager();
+                Filter filter = queryManager.createFilter(Paragraph.class);
+                filter.addLike("text", "Para%");
+                filter.setScope("/test/");
+
+                Query query = queryManager.createQuery(filter);
+                query.addOrderByDescending("text");
+
+                String strQueryBuilderStringWithDescending = ((QueryImpl)query).getOrderByExpression();
+                
+                ObjectContentManager ocm = this.getObjectContentManager();
+                Collection result = ocm.getObjects(query);
+                assertEquals("Invalid number of objects - should be = 3", 3, result.size());
+
+                //Text is Descending
+                Paragraph[] paragraphs = (Paragraph[]) result.toArray(new Paragraph[result.size()]);
+                Iterator iterator = result.iterator();
+                Paragraph para = (Paragraph)iterator.next();
+                assertEquals("Para 3",para.getText());
+                para = (Paragraph)iterator.next();
+                assertEquals("Para 2",para.getText());
+                para = (Paragraph)iterator.next();
+                assertEquals("Para 1",para.getText());
+
+                //Text is Ascending
+                query = queryManager.createQuery(filter);
+                query.addOrderByAscending("text");
+
+                ocm = this.getObjectContentManager();
+                result = ocm.getObjects(query);
+                assertEquals("Invalid number of objects - should be = 3", 3, result.size());
+                iterator = result.iterator();
+                para = (Paragraph)iterator.next();
+                assertEquals("Para 1",para.getText());
+                para = (Paragraph)iterator.next();
+                assertEquals("Para 2",para.getText());
+                para = (Paragraph)iterator.next();
+                assertEquals("Para 3",para.getText());
+
+                //Text is Descending
+                query = queryManager.createQuery(filter);
+                ((QueryImpl)query).addJCRExpression(strQueryBuilderStringWithDescending);
+                ocm = this.getObjectContentManager();
+                result = ocm.getObjects(query);
+                assertEquals("Invalid number of objects - should be = 3", 3, result.size());
+                iterator = result.iterator();
+                para = (Paragraph)iterator.next();
+                assertEquals("Para 3",para.getText());
+                para = (Paragraph)iterator.next();
+                assertEquals("Para 2",para.getText());
+                para = (Paragraph)iterator.next();
+                assertEquals("Para 1",para.getText());
+                
+            
+            } catch (Exception e) {
+                e.printStackTrace();
+                fail("Exception occurs during the unit test : " + e);
+            }
+        
+        
     }
 
     public void testGetObjectsByClassNameAndPath() {



Mime
View raw message