incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r292016 - in /incubator/graffito/trunk/jcr-mapping/src: java/org/apache/portals/graffito/jcr/persistence/impl/ java/org/apache/portals/graffito/jcr/query/ java/org/apache/portals/graffito/jcr/query/impl/ test/org/apache/portals/graffito/jcr...
Date Tue, 27 Sep 2005 18:43:47 GMT
Author: clombart
Date: Tue Sep 27 11:43:34 2005
New Revision: 292016

URL: http://svn.apache.org/viewcvs?rev=292016&view=rev
Log:
Still working on the query service

Modified:
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/impl/PersistenceManagerImpl.java
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Query.java
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/QueryManager.java
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/FilterImpl.java
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryImpl.java
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryManagerImpl.java
    incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/impl/PersistenceManagerQueryTest.java
    incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/query/impl/QueryManagerTest.java

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/impl/PersistenceManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/impl/PersistenceManagerImpl.java?rev=292016&r1=292015&r2=292016&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/impl/PersistenceManagerImpl.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/impl/PersistenceManagerImpl.java
Tue Sep 27 11:43:34 2005
@@ -33,6 +33,7 @@
 import org.apache.commons.logging.LogFactory;
 import org.apache.portals.graffito.jcr.exception.JcrMappingException;
 import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.impl.DigesterMapperImpl;
 import org.apache.portals.graffito.jcr.persistence.CustomNodeTypeCreator;
 import org.apache.portals.graffito.jcr.persistence.PersistenceManager;
 import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverterFactory;
@@ -73,6 +74,7 @@
 	 */
 	private AtomicTypeConverterFactory atomicTypeConverterFactory;
 
+	 
 	/**
 	 * Object Converter
 	 */
@@ -212,6 +214,9 @@
 		{
 
 			String jcrExpression = this.queryManager.buildJCRExpression(query);
+			log.debug("Get Object with expression : " + jcrExpression);
+			
+				
 			javax.jcr.query.Query jcrQuery = session.getWorkspace().getQueryManager().createQuery(jcrExpression,
javax.jcr.query.Query.XPATH);
 			QueryResult queryResult = jcrQuery.execute();
 			NodeIterator nodeIterator = queryResult.getNodes();

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Query.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Query.java?rev=292016&r1=292015&r2=292016&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Query.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/Query.java
Tue Sep 27 11:43:34 2005
@@ -30,5 +30,9 @@
 	public void setFilter(Filter filter);
 	
 	public Filter getFilter();
+	
+	public void addOrderByAscending(String fieldNameAttribute);
+	
+	public void addOrderByDescending(String fieldNameAttribute);
 
 }

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/QueryManager.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/QueryManager.java?rev=292016&r1=292015&r2=292016&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/QueryManager.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/QueryManager.java
Tue Sep 27 11:43:34 2005
@@ -30,7 +30,7 @@
 {
 	 public Filter createFilter(Class classQuery) throws JcrMappingException;
 	 
-     public Query createQuery(Filter filter);
+     public Query createQuery(Filter filter)  throws JcrMappingException;
      
      public String buildJCRExpression(Query query) throws JcrMappingException;
      

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/FilterImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/FilterImpl.java?rev=292016&r1=292015&r2=292016&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/FilterImpl.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/FilterImpl.java
Tue Sep 27 11:43:34 2005
@@ -17,13 +17,20 @@
 
 package org.apache.portals.graffito.jcr.query.impl;
 
+import java.sql.Timestamp;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Calendar;
 import java.util.Collection;
+import java.util.Date;
 import java.util.Iterator;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.portals.graffito.jcr.exception.JcrMappingException;
 import org.apache.portals.graffito.jcr.mapper.Mapper;
 import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.persistence.impl.PersistenceManagerImpl;
 import org.apache.portals.graffito.jcr.query.Filter;
 
 /**
@@ -45,6 +52,7 @@
 	
 	private String orJcrExpression; 
 		
+	private final static Log log = LogFactory.getLog(FilterImpl.class);
 	
 	public FilterImpl(Mapper mapper, Class clazz) throws JcrMappingException
 	{
@@ -95,7 +103,7 @@
 	public void addBetween(String fieldAttributeName, Object value1, Object value2)
 	{
 		String jcrExpression =  "( @" + this.getJcrFieldName(fieldAttributeName) + " >= " +
this.getStringValue(value1) + 
-		                        " and " + this.getJcrFieldName(fieldAttributeName) + " <= "
+ this.getStringValue(value2) +  ")";
+		                        " and @" + this.getJcrFieldName(fieldAttributeName) + " <= "
+ this.getStringValue(value2) +  ")";
 		jcrExpressions.add(jcrExpression) ;		
 
 	}
@@ -199,7 +207,12 @@
 	
 	private String getJcrFieldName(String fieldAttribute)
 	{
-		return classDescriptor.getJcrName(fieldAttribute);
+		String jcrFieldName =  classDescriptor.getJcrName(fieldAttribute);
+		if (jcrFieldName == null)
+		{
+			log.error("Impossible to find the jcrFieldName for the attribute :" + fieldAttribute);
+		}
+		return jcrFieldName;
 	    	
 	}
 	
@@ -211,7 +224,30 @@
 		}
 		else
 		{
-			return value.toString();
+			
+			if (value instanceof Boolean)
+			{
+				
+				return ((Boolean) value).booleanValue() ? "true()" : "false()"; 
+			}
+			else 
+			{
+			    if ((value instanceof Calendar))
+			    {
+			    	//||(value instanceof Date)
+			    	Calendar calendar = (Calendar) value;			        
+			        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T00:00:00.000+01:00'");
+			        //'1976-01-01T00:00:00.000+01:00'
+			        formatter.setCalendar(calendar);
+			         
+			         String formattedString = " TIMESTAMP '" + formatter.format(calendar.getTime())
+ "'";
+			         return formattedString;
+			    }
+			    else
+			    {
+			    	return value.toString();			    	
+			    }
+			}
 		}
 	}
 	
@@ -233,7 +269,7 @@
 		   int count = 1;
 		   String jcrExp = "";
 		   
-		   jcrExp += "["; 
+		    
 		   Iterator criteriaIterator =  jcrExpressions.iterator();
 		   while (criteriaIterator.hasNext())
 		   {
@@ -245,7 +281,7 @@
 			   count++;
 			   
 		   }
-		   jcrExp += "]"; 
+		    
 		   
 		   return jcrExp;
 		

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryImpl.java?rev=292016&r1=292015&r2=292016&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryImpl.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryImpl.java
Tue Sep 27 11:43:34 2005
@@ -17,6 +17,13 @@
 
 package org.apache.portals.graffito.jcr.query.impl;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.portals.graffito.jcr.exception.JcrMappingException;
+import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
 import org.apache.portals.graffito.jcr.query.Filter;
 import org.apache.portals.graffito.jcr.query.Query;
 
@@ -30,11 +37,19 @@
 {
 		
 	private Filter filter;
+	
+    private Mapper mapper;	
+    
+    ClassDescriptor classDescriptor;
+    
+    private ArrayList orderByExpressions = new ArrayList();
 
 	
-	public QueryImpl(Filter filter)
+	public QueryImpl(Filter filter, Mapper mapper) throws JcrMappingException
 	{				
 		this.filter = filter;
+		this.mapper = mapper;
+		classDescriptor = mapper.getClassDescriptor(filter.getFilterClass());
 	}
 
 	/**
@@ -51,6 +66,48 @@
 	public Filter getFilter()
 	{
         return this.filter;
+	}
+
+	public void addOrderByDescending(String fieldNameAttribute)
+	{
+		orderByExpressions.add("@" + this.getJcrFieldName(fieldNameAttribute) + " descending");
+	}
+
+	public void addOrderByAscending(String fieldNameAttribute)
+	{
+		orderByExpressions.add("@" + this.getJcrFieldName(fieldNameAttribute) + " ascending");
+	}
+	
+	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++;
+		}
+		
+		return orderByExpression;
+	}
+	
+	
+	private String getJcrFieldName(String fieldAttribute)
+	{
+		
+		return classDescriptor.getJcrName(fieldAttribute);
+	    	
 	}
 
 }

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryManagerImpl.java?rev=292016&r1=292015&r2=292016&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryManagerImpl.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/query/impl/QueryManagerImpl.java
Tue Sep 27 11:43:34 2005
@@ -25,10 +25,10 @@
 		return new FilterImpl(mapper, classQuery);
 	}
 
-	public Query createQuery(Filter filter)
+	public Query createQuery(Filter filter)  throws JcrMappingException
 	{
 
-		return new QueryImpl(filter);
+		return new QueryImpl(filter, mapper);
 	}
 
 	public String buildJCRExpression(Query query)  throws JcrMappingException 
@@ -45,10 +45,13 @@
 		}
 		
 		// Add node type
-		jcrExp += "//element(*, "  + this.getNodeType(filter) + ")";
+		jcrExp += "//element(*, "  + this.getNodeType(filter) + ") ";
 
         // Add filter criteria
-		jcrExp += ((FilterImpl)filter).getJcrExpression();
+		jcrExp += "[" + ((FilterImpl)filter).getJcrExpression() + "]";
+		
+		// Add order by
+		jcrExp += ((QueryImpl)query).getOrderByExpression();
 		
 		return jcrExp;
 		 

Modified: incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/impl/PersistenceManagerQueryTest.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/impl/PersistenceManagerQueryTest.java?rev=292016&r1=292015&r2=292016&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/impl/PersistenceManagerQueryTest.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/impl/PersistenceManagerQueryTest.java
Tue Sep 27 11:43:34 2005
@@ -60,7 +60,11 @@
     }
 
     
-    public void testGetObjects()
+    /**
+     * Test equalTo
+     *
+     */
+    public void testGetObjectsEqualsTo()
     {
 
     	try
@@ -79,6 +83,8 @@
     	      PersistenceManager persistenceManager = this.getPersistenceManager();
     	      Collection result = persistenceManager.getObjects(query);
     	      assertTrue("Invalid number of objects - should be = 1", result.size() == 1);
+    	      Paragraph paragraph = (Paragraph) result.iterator().next();
+    	      assertTrue("Invalid paragraph found" , paragraph.getText().equals("Para 1"));
             
         }
         catch (Exception e)
@@ -88,6 +94,123 @@
         }
         
     }
+
+    /**
+     * Test the like "like" expression
+     */
+    public void testGetObjectsLike()
+    {
+
+    	try
+    	{
+    		
+    		  importData();
+    		  
+    		  // 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);
+    	      
+    	      PersistenceManager persistenceManager = this.getPersistenceManager();
+    	      Collection result = persistenceManager.getObjects(query);
+    	      assertTrue("Invalid number of objects - should be = 3", result.size() == 3);
+
+    	      Paragraph[] paragraphs = (Paragraph[]) result.toArray(new Paragraph[result.size()]);
+    	      assertTrue("Invalid paragraph found", paragraphs[0].getText().equals("Para 1"));
+    	      assertTrue("Invalid paragraph found", paragraphs[1].getText().equals("Para 2"));
+    	      assertTrue("Invalid paragraph found", paragraphs[2].getText().equals("Para 3"));
+    	      
+
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+            fail("Exception occurs during the unit test : " + e);
+        }
+        
+    }
+    
+    
+    /**
+     * Build an or expression between 2 filters
+     *
+     */
+    public void testGetObjectsOr()
+    {
+
+    	try
+    	{
+    		
+    		  importData();
+    		  
+    		  // Build the Query Object
+    	      QueryManager queryManager = this.getQueryManager();
+    	      Filter filter1 = queryManager.createFilter(Paragraph.class);
+    	      filter1.addEqualTo("text", "Para 1");    	     
+    	      filter1.setScope("/test");
+
+    	      Filter filter2 = queryManager.createFilter(Paragraph.class);
+    	      filter2.addEqualTo("text", "Para 2");    	     
+    	      
+              filter1.addOrFilter(filter2);
+    	      
+    	      Query query = queryManager.createQuery(filter1);
+    	      
+    	      PersistenceManager persistenceManager = this.getPersistenceManager();
+    	      Collection result = persistenceManager.getObjects(query);
+    	      assertTrue("Invalid number of objects - should be = 2", result.size() == 2);
+    	      
+    	      Paragraph[] paragraphs = (Paragraph[]) result.toArray(new Paragraph[result.size()]);
+    	      assertTrue("Invalid paragraph found", paragraphs[0].getText().equals("Para 1"));
+    	      assertTrue("Invalid paragraph found", paragraphs[1].getText().equals("Para 2"));
+            
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+            fail("Exception occurs during the unit test : " + e);
+        }
+        
+    }
+    
+    public void testGetObjectOrderBy()
+    {
+
+    	try
+    	{
+    		
+    		  importData();
+    		  
+    		  // 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");
+    	      
+    	      PersistenceManager persistenceManager = this.getPersistenceManager();
+    	      Collection result = persistenceManager.getObjects(query);
+    	      assertTrue("Invalid number of objects - should be = 3", result.size() == 3);
+
+    	      Paragraph[] paragraphs = (Paragraph[]) result.toArray(new Paragraph[result.size()]);
+    	      assertTrue("Invalid paragraph found", paragraphs[0].getText().equals("Para 3"));
+    	      assertTrue("Invalid paragraph found", paragraphs[1].getText().equals("Para 2"));
+    	      assertTrue("Invalid paragraph found", paragraphs[2].getText().equals("Para 1"));
+    	      
+
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+            fail("Exception occurs during the unit test : " + e);
+        }
+        
+    }
     
     private void importData() throws JcrMappingException 
     {
@@ -105,6 +228,7 @@
         paragraphs.add(new Paragraph("Para 1"));
         paragraphs.add(new Paragraph("Para 2"));
         paragraphs.add(new Paragraph("Para 3"));
+        paragraphs.add(new Paragraph("Another Para "));
         page.setParagraphs(paragraphs);
         
         getPersistenceManager().insert("/test", page);            

Modified: incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/query/impl/QueryManagerTest.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/query/impl/QueryManagerTest.java?rev=292016&r1=292015&r2=292016&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/query/impl/QueryManagerTest.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/query/impl/QueryManagerTest.java
Tue Sep 27 11:43:34 2005
@@ -71,7 +71,7 @@
     	      Query query = queryManager.createQuery(filter);
     	      String jcrExpression = queryManager.buildJCRExpression(query);
     	      assertNotNull("jcrExpression is null", jcrExpression);
-    	      assertTrue("Invalid JcrExpression", jcrExpression.equals("/jcr:root/test//element(*,
graffito:C)[@graffito:name = 'a test value' and @graffito:id = 1]"));
+    	      assertTrue("Invalid JcrExpression", jcrExpression.equals("/jcr:root/test//element(*,
graffito:C) [@graffito:name = 'a test value' and @graffito:id = 1]"));
     	      
     	      
             
@@ -97,7 +97,7 @@
     	      Query query = queryManager.createQuery(filter);
     	      String jcrExpression = queryManager.buildJCRExpression(query);
     	      assertNotNull("jcrExpression is null", jcrExpression);
-    	      assertTrue("Invalid JcrExpression", jcrExpression.equals("//element(*, graffito:C)[@graffito:name
= 'a test value' and @graffito:id = 1]"));
+    	      assertTrue("Invalid JcrExpression", jcrExpression.equals("//element(*, graffito:C)
[@graffito:name = 'a test value' and @graffito:id = 1]"));
     	      
     	      
             



Mime
View raw message