You are viewing a plain text version of this content. The canonical link for it is here.
Posted to graffito-commits@incubator.apache.org by cl...@apache.org on 2005/09/09 21:09:57 UTC

svn commit: r279879 - in /incubator/graffito/trunk/jcr-mapping/src: java/org/apache/portals/graffito/jcr/persistence/collectionconverter/ java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ java/org/apache/portals/graffito/jcr/pe...

Author: clombart
Date: Fri Sep  9 14:09:46 2005
New Revision: 279879

URL: http://svn.apache.org/viewcvs?rev=279879&view=rev
Log:
*Manage null value for Collection & bean fields
This is not yet done for the atomic fields (see mail from Martin) - plan to do ASAP
* More unit tests on Atomic fields

Added:
    incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/impl/AtomicTest.java
    incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/Atomic.java
Modified:
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/ManageableCollectionUtil.java
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/ObjectConverter.java
    incubator/graffito/trunk/jcr-mapping/src/test-config/jcrmapping.xml
    incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/A.java

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/ManageableCollectionUtil.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/ManageableCollectionUtil.java?rev=279879&r1=279878&r2=279879&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/ManageableCollectionUtil.java (original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/ManageableCollectionUtil.java Fri Sep  9 14:09:46 2005
@@ -78,7 +78,10 @@
     {
         try
         {
-
+            if (object == null)
+            {
+                return null;
+            }
 
             if (object instanceof ManageableCollection)
             {

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java?rev=279879&r1=279878&r2=279879&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java (original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java Fri Sep  9 14:09:46 2005
@@ -40,7 +40,7 @@
  * to distinguish the collection elements from the other main object fields.
  * 
  * If the collection element class contains an id (see the FieldDescriptor definition), this id value is used to build the collection element node.
- * Otherwise, the name is a simple indexed constant.
+ * Otherwise, the element node name is a simple indexed constant.
  * 
  * Example - without an id attribute:                 
  *   /test (Main object containing the collection field )
@@ -83,6 +83,12 @@
 
         try
         {
+            
+            if (collection == null)
+            {
+                return ;
+            }
+            
             String jcrName = collectionDescriptor.getJcrName();
 
             if (jcrName == null)
@@ -102,6 +108,7 @@
                 Object item = collectionIterator.next();
                 String elementJcrName = null;
 
+                // If the element object has a unique id => the element jcr node name = the id value 
                 if (elementClassDescriptor.hasIdField())
                 {
                       String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
@@ -134,6 +141,7 @@
     {
         try
         {
+            
             String jcrName = collectionDescriptor.getJcrName();
                                        
             if (jcrName == null)
@@ -141,27 +149,53 @@
                 throw new JcrMappingException("The JcrName attribute is not defined for the CollectionDescriptor : " + collectionDescriptor.getCollectionConverterClassName());
             }
             
+            if (collection == null)
+            {
+                if (parentNode.hasNode(jcrName))
+                {
+                    parentNode.getNode(jcrName).remove();
+                    
+                }
+                return;
+            }
+            
             Node collectionNode = parentNode.getNode(jcrName);
             ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor.getElementClassName()));
             Iterator collectionIterator = collection.getIterator();
             int elementCollectionCount = 0;
+            
+            //  If the collection elements have not an id, it is not possible to find the matching JCR nodes => delete the complete collection             
+            if (! elementClassDescriptor.hasIdField())
+            {
+               collectionNode.remove();
+               collectionNode = parentNode.addNode(jcrName);
+            }
+            
             while(collectionIterator.hasNext())
             {
                 Object item = collectionIterator.next();
                 elementCollectionCount++;  
                 String elementJcrName = null;
+                
                 if (elementClassDescriptor.hasIdField())
                 {
+                      // Update existing JCR Node
                       String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
                       elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
+                      objectConverter.updateNode(session, collectionNode, elementJcrName, item);
+                      
+                      // Delete JCR nodes that are not present in the collection
+                      
+                      // Add new collection elements
                 }
                 else
                 {
-
+                    
                     elementCollectionCount++;
                     elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
+                    objectConverter.insertNode(session, collectionNode, elementJcrName, item);
                 }                
-                objectConverter.updateNode(session, collectionNode, elementJcrName, item);    
+    
             }
         }
         catch (Exception e)
@@ -181,6 +215,10 @@
         try
         {
             String jcrName = collectionDescriptor.getJcrName();            
+            if (! parentNode.hasNode(jcrName))
+            {
+                return null;
+            }
             
             Node collectionNode = parentNode.getNode(jcrName);
             NodeIterator children = collectionNode.getNodes();

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/ObjectConverter.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/ObjectConverter.java?rev=279879&r1=279878&r2=279879&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/ObjectConverter.java (original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/ObjectConverter.java Fri Sep  9 14:09:46 2005
@@ -110,6 +110,12 @@
 
         try
         {
+            
+            if (! session.itemExists(path))
+            {
+                return null;
+            }
+            
             ClassDescriptor classDescriptor = mapper.getClassDescriptor(clazz);
             if (classDescriptor == null) 
             {
@@ -285,7 +291,10 @@
                 BeanDescriptor beanDescriptor = (BeanDescriptor) beanDescriptorIterator.next();
                 String jcrName = beanDescriptor.getJcrName();
                 Object bean = PropertyUtils.getNestedProperty(object, beanDescriptor.getFieldName());
-                this.insertNode(session, objectNode, jcrName, bean);
+                if (bean != null)
+                {
+                    this.insertNode(session, objectNode, jcrName, bean);
+                }
 
             }
         }
@@ -306,7 +315,19 @@
                 BeanDescriptor beanDescriptor = (BeanDescriptor) beanDescriptorIterator.next();
                 String jcrName = beanDescriptor.getJcrName();
                 Object bean = PropertyUtils.getNestedProperty(object, beanDescriptor.getFieldName());
-                this.updateNode(session, objectNode, jcrName, bean);
+                
+                // if the bean is null, remove existing node
+                if ((bean == null))
+                {
+                   if (objectNode.hasNode(jcrName))
+                   {
+                       objectNode.getNode(jcrName).remove();
+                   }
+                }
+                else
+                {
+                   this.updateNode(session, objectNode, jcrName, bean);
+                }
 
             }
         }

Modified: incubator/graffito/trunk/jcr-mapping/src/test-config/jcrmapping.xml
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/test-config/jcrmapping.xml?rev=279879&r1=279878&r2=279879&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/test-config/jcrmapping.xml (original)
+++ incubator/graffito/trunk/jcr-mapping/src/test-config/jcrmapping.xml Fri Sep  9 14:09:46 2005
@@ -2,6 +2,21 @@
 
 <!DOCTYPE graffito-jcr PUBLIC "-//The Apache Software Foundation//DTD Repository//EN" "..\dtd\graffito-jcr-mapping.dtd">
 <graffito-jcr>
+  
+    <class-descriptor
+		className="org.apache.portals.graffito.jcr.testmodel.Atomic" jcrNodeType="nt:unstructured">
+
+		<field-descriptor fieldName="string" jcrName="string" />
+		<field-descriptor fieldName="booleanObject" jcrName="booleanObject" />
+		<field-descriptor fieldName="booleanPrimitive" jcrName="booleanPrimitive" />
+		<field-descriptor fieldName="integerObject" jcrName="integerObject" />
+		<field-descriptor fieldName="intPrimitive" jcrName="intPrimitive" />
+		<!-- field-descriptor fieldName="byteArray" jcrName="byteArray" / -->
+		<field-descriptor fieldName="calandar" jcrName="calandar" />
+		<field-descriptor fieldName="date" jcrName="date" />
+				
+	</class-descriptor>
+    
 	<class-descriptor
 		className="org.apache.portals.graffito.jcr.testmodel.Folder" jcrNodeType="nt:unstructured">
 		<!-- Field-descriptor is used to map simple attributes to jcr property -->
@@ -36,22 +51,10 @@
 		<!-- 
 			Use the default collection mapping stategy and the default collection type (can be a Vector, an ArrayList, a Collection or a List) 
 			The default collection mapping strategy map a collection under an extra JCR node (specify by the jcrName).
-			So, in the following case, the result will be : 
-			/test (containing a object A)
-			a1 (propoerty)
-			a2  (property)
-			collection (node)
-			collection-item1 (node)
-			...
-			collection-item2 (node)
-			...
-			
-			If needed, the collectionConverter attribute can be used to specify another collection converter class (mapping strategy)
-			The managableCollection attribute can be also used to support another kind of Collection or Map
 			
 		-->
 		<collection-descriptor fieldName="collection" jcrName="collection" proxy="false" elementClassName="org.apache.portals.graffito.jcr.testmodel.C" />
-
+		<collection-descriptor fieldName="emptyCollection" jcrName="emptyCollection" proxy="false" elementClassName="org.apache.portals.graffito.jcr.testmodel.C" />
 	</class-descriptor>
 
 	<class-descriptor className="org.apache.portals.graffito.jcr.testmodel.B"  jcrNodeType="nt:unstructured">

Added: incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/impl/AtomicTest.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/impl/AtomicTest.java?rev=279879&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/impl/AtomicTest.java (added)
+++ incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/impl/AtomicTest.java Fri Sep  9 14:09:46 2005
@@ -0,0 +1,123 @@
+/* ========================================================================
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ========================================================================
+ */
+package org.apache.portals.graffito.jcr.persistence.impl;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.Session;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.portals.graffito.jcr.TestBase;
+import org.apache.portals.graffito.jcr.testmodel.A;
+import org.apache.portals.graffito.jcr.testmodel.Atomic;
+import org.apache.portals.graffito.jcr.testmodel.B;
+import org.apache.portals.graffito.jcr.testmodel.C;
+import org.apache.portals.graffito.jcr.testmodel.Folder;
+
+/**
+ * Test Atomic perisstence fields
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Christophe Lombart</a>
+ */
+public class AtomicTest extends TestBase
+{
+    private final static Log log = LogFactory.getLog(AtomicTest.class);
+
+    /**
+     * <p>Defines the test case name for junit.</p>
+     * @param testName The test case name.
+     */
+    public AtomicTest(String testName)
+    {
+        super(testName);
+    }
+
+    public static Test suite()
+    {
+        // All methods starting with "test" will be executed in the test suite.
+        return new TestSuite(AtomicTest.class);
+    }
+
+    public void testAtomicFields()
+    {
+        try
+        {
+
+            if (getPersistenceManager().itemExists("/test"))
+            {
+                getPersistenceManager().remove("/test");
+            }
+
+            // --------------------------------------------------------------------------------
+            // Create and store an object graph in the repository
+            // --------------------------------------------------------------------------------
+            Atomic a = new Atomic();
+            a.setBooleanObject(new Boolean(true));
+            a.setBooleanPrimitive(true);
+            a.setIntegerObject(new Integer(100));
+            a.setIntPrimitive(200);
+            a.setString("Test String");
+            a.setDate(new Date());
+            byte[] content = "Test Byte".getBytes();
+            a.setByteArray(content);
+            a.setCalandar(Calendar.getInstance());
+            
+            getPersistenceManager().insert("/test", a);
+
+             
+            // --------------------------------------------------------------------------------
+            // Get the object
+            // --------------------------------------------------------------------------------
+            a = null;
+            a = (Atomic) getPersistenceManager().getObject(Atomic.class, "/test");
+            assertNotNull("a is null", a);
+            assertNotNull("Boolean object is null", a.getBooleanObject());
+            assertTrue("Incorrect boolean object", a.getBooleanObject().booleanValue());
+            assertTrue("Incorrect boolean primitive", a.isBooleanPrimitive());
+            assertNotNull("Integer Object is null", a.getIntegerObject());
+            assertTrue("Incorrect Integer object", a.getIntegerObject().intValue() == 100);
+            assertTrue("Incorrect int primitive", a.getIntPrimitive() == 200);
+            assertNotNull("String object is null", a.getString());
+            assertTrue("Incorrect boolean object", a.getString().equals("Test String"));
+            //assertNotNull("Byte array object is null", a.getByteArray());
+            //assertTrue("Incorrect byte object", a.getByteArray().toString().equals("Test String"));
+            
+            assertNotNull("date object is null", a.getDate());
+            assertNotNull("calendar object is null", a.getCalandar());
+            
+
+            
+            
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+            fail("Exception occurs during the unit test : " + e);
+        }
+        
+    }
+    
+}
\ No newline at end of file

Modified: incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/A.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/A.java?rev=279879&r1=279878&r2=279879&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/A.java (original)
+++ incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/A.java Fri Sep  9 14:09:46 2005
@@ -28,7 +28,9 @@
     private String a1;
     private String a2;
     private B b;
+    private B emptyB;
     private Collection collection;
+    private Collection emptyCollection; 
     
     
     /**
@@ -100,4 +102,22 @@
        
        collection.add(c);   
     }
+    public Collection getEmptyCollection()
+    {
+        return emptyCollection;
+    }
+    public void setEmptyCollection(Collection emptyCollection)
+    {
+        this.emptyCollection = emptyCollection;
+    }
+    public B getEmptyB()
+    {
+        return emptyB;
+    }
+    public void setEmptyB(B emptyB)
+    {
+        this.emptyB = emptyB;
+    }
+    
+    
 }

Added: incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/Atomic.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/Atomic.java?rev=279879&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/Atomic.java (added)
+++ incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/Atomic.java Fri Sep  9 14:09:46 2005
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2000-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.graffito.jcr.testmodel;
+
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+
+/**
+ *
+ * Simple object used to test atomic type
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe </a>
+ * @version $Id: Exp $
+ */
+public class Atomic
+{
+    private String string;
+    private Boolean booleanObject;
+    private boolean booleanPrimitive;
+    private Integer integerObject;
+    private int intPrimitive;
+    private byte[] byteArray;
+    private Calendar calandar;
+    private Date date;
+    
+    public Boolean getBooleanObject()
+    {
+        return booleanObject;
+    }
+    public void setBooleanObject(Boolean booleanObject)
+    {
+        this.booleanObject = booleanObject;
+    }
+    public boolean isBooleanPrimitive()
+    {
+        return booleanPrimitive;
+    }
+    public void setBooleanPrimitive(boolean booleanPrimitive)
+    {
+        this.booleanPrimitive = booleanPrimitive;
+    }
+    public Integer getIntegerObject()
+    {
+        return integerObject;
+    }
+    public void setIntegerObject(Integer integerObject)
+    {
+        this.integerObject = integerObject;
+    }
+    public int getIntPrimitive()
+    {
+        return intPrimitive;
+    }
+    public void setIntPrimitive(int intPrimitive)
+    {
+        this.intPrimitive = intPrimitive;
+    }
+    public String getString()
+    {
+        return string;
+    }
+    public void setString(String string)
+    {
+        this.string = string;
+    }
+    public byte[] getByteArray()
+    {
+        return byteArray;
+    }
+    public void setByteArray(byte[] byteArray)
+    {
+        this.byteArray = byteArray;
+    }
+    public Calendar getCalandar()
+    {
+        return calandar;
+    }
+    public void setCalandar(Calendar calandar)
+    {
+        this.calandar = calandar;
+    }
+    public Date getDate()
+    {
+        return date;
+    }
+    public void setDate(Date date)
+    {
+        this.date = date;
+    }
+    
+    
+}