incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r517394 - in /incubator/graffito/trunk/jcr/jcr-mapping/src: dtd/ java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ test-config/ test/org/apache/portals/graffito/jcr/persistence/uuid/ test/org/apache/portals/graffito...
Date Mon, 12 Mar 2007 21:06:14 GMT
Author: clombart
Date: Mon Mar 12 14:06:13 2007
New Revision: 517394

URL: http://svn.apache.org/viewvc?view=rev&rev=517394
Log:
continue to work GRFT-40 :
* add new collection converters to manage collection of uuid/references

Added:
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/BeanReferenceCollectionConverterImpl.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ReferenceCollectionConverterImpl.java
Modified:
    incubator/graffito/trunk/jcr/jcr-mapping/src/dtd/graffito-jcr-mapping.dtd
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-uuid.xml
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/uuid/PersistenceManagerUuidTest.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/uuid/B.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/uuid/B2.java

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/dtd/graffito-jcr-mapping.dtd
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/dtd/graffito-jcr-mapping.dtd?view=diff&rev=517394&r1=517393&r2=517394
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/dtd/graffito-jcr-mapping.dtd (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/dtd/graffito-jcr-mapping.dtd Mon Mar 12 14:06:13
2007
@@ -151,7 +151,7 @@
 	autoRetrieve (true|false) "true"
 	autoUpdate (true|false) "true"
 	autoInsert (true|false) "true"	
-	elementClassName CDATA #REQUIRED
+	elementClassName CDATA #IMPLIED
 	collectionClassName CDATA #IMPLIED
 	collectionConverter CDATA #IMPLIED
 	jcrNodeType CDATA #IMPLIED

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java?view=diff&rev=517394&r1=517393&r2=517394
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java
Mon Mar 12 14:06:13 2007
@@ -34,7 +34,6 @@
 import org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter;
 import org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection;
 import org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter;
-import org.apache.portals.graffito.jcr.reflection.ReflectionUtils;
 
 /** 
  * Abstract class used for all CollectionConverter
@@ -45,7 +44,6 @@
 public abstract class AbstractCollectionConverterImpl implements CollectionConverter {
 	protected Map atomicTypeConverters;
 	protected ObjectConverter objectConverter;
-	// NOT USED
 	protected Mapper mapper;
 
 	/**

Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/BeanReferenceCollectionConverterImpl.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/BeanReferenceCollectionConverterImpl.java?view=auto&rev=517394
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/BeanReferenceCollectionConverterImpl.java
(added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/BeanReferenceCollectionConverterImpl.java
Mon Mar 12 14:06:13 2007
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.collectionconverter.impl;
+
+
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.UnsupportedRepositoryOperationException;
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+import javax.jcr.ValueFormatException;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.version.VersionException;
+
+import org.apache.portals.graffito.jcr.exception.JcrMappingException;
+import org.apache.portals.graffito.jcr.exception.PersistenceException;
+import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.FieldDescriptor;
+import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollectionUtil;
+import org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter;
+import org.apache.portals.graffito.jcr.reflection.ReflectionUtils;
+
+/**
+ * Collection Mapping used to map a reference/uuid property list into a java bean collection
(readonly).
+ * Only the uuid can be modified, not the associated object
+ *
+ * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
+ * 
+ */
+public class BeanReferenceCollectionConverterImpl extends AbstractCollectionConverterImpl
{
+
+    /**
+     * Constructor
+     *
+     * @param atomicTypeConverters
+     * @param objectConverter
+     * @param mapper
+     */
+    public BeanReferenceCollectionConverterImpl(Map atomicTypeConverters,
+                                             ObjectConverter objectConverter,
+                                             Mapper mapper) {
+        super(atomicTypeConverters, objectConverter, mapper);
+    }
+
+    /**
+     *
+     * @see AbstractCollectionConverterImpl#doInsertCollection(Session, Node, CollectionDescriptor,
ManageableCollection)
+     */
+    protected void doInsertCollection(Session session,
+                                      Node parentNode,
+                                      CollectionDescriptor collectionDescriptor,
+                                      ManageableCollection collection) throws RepositoryException
{
+        addUuidProperties(session, parentNode, collectionDescriptor, collection);
+    }
+
+
+    /**
+     *
+     * @see AbstractCollectionConverterImpl#doUpdateCollection(Session, Node, CollectionDescriptor,
ManageableCollection)
+     */
+    protected void doUpdateCollection(Session session,
+                                 Node parentNode,
+                                 CollectionDescriptor collectionDescriptor,
+                                 ManageableCollection collection) throws RepositoryException

+    {
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+
+        // Delete existing values
+        if (parentNode.hasProperty(jcrName)) {
+            parentNode.setProperty(jcrName, (Value[]) null);
+        }
+
+        if (collection == null) {
+            return;
+        }
+        
+        addUuidProperties(session, parentNode, collectionDescriptor, collection);
+
+    }
+
+    /**
+     * @see AbstractCollectionConverterImpl#doGetCollection(Session, Node, CollectionDescriptor,
Class)
+     */
+    protected ManageableCollection doGetCollection(Session session,
+                                                   Node parentNode,
+                                                   CollectionDescriptor collectionDescriptor,
+                                                   Class collectionFieldClass) throws RepositoryException
{
+        try {
+            String jcrName = getCollectionJcrName(collectionDescriptor);
+            if (!parentNode.hasProperty(jcrName)) {
+                return null;
+            }
+            Property property = parentNode.getProperty(jcrName);
+            Value[] values = property.getValues();
+
+            ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass);
+           
+            for (int i = 0; i < values.length; i++) {
+                String uuid = values[i].getString();
+                String path = session.getNodeByUUID(uuid).getPath();    			
+    			Object object = objectConverter.getObject(session, path);
+                collection.addObject(object);
+            }
+
+            return collection;
+        }
+        catch(Exception e) {
+          throw new PersistenceException("Cannot get the collection field : "
+                  + collectionDescriptor.getFieldName()
+                  + "for class " + collectionDescriptor.getClassDescriptor().getClassName(),
e);
+        }
+    }
+    
+    /**
+     * @see AbstractCollectionConverterImpl#doIsNull(Session, Node, CollectionDescriptor,
Class)
+     */
+    protected boolean doIsNull(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
Class collectionFieldClass) throws RepositoryException 
+    {
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+        return ! parentNode.hasProperty(jcrName);
+    }
+    
+	private void addUuidProperties(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
ManageableCollection collection) throws UnsupportedRepositoryOperationException, RepositoryException,
VersionException, LockException, ConstraintViolationException {
+		try {
+            if (collection == null) {
+                return;
+            }
+
+            String jcrName = getCollectionJcrName(collectionDescriptor);
+            Value[] values = new Value[collection.getSize()];
+            ValueFactory valueFactory = session.getValueFactory();
+            Iterator collectionIterator = collection.getIterator();
+            for (int i = 0; i < collection.getSize(); i++) {
+                Object object = collectionIterator.next();
+				if (object != null) 
+				{
+					ClassDescriptor classDescriptor = mapper.getClassDescriptorByClass(object.getClass());
+
+					FieldDescriptor fieldDescriptor = classDescriptor.getUuidFieldDescriptor();
+					if (fieldDescriptor == null) 
+					{
+						throw new JcrMappingException("The bean doesn't have an uuid - classdescriptor : "
+										              + classDescriptor.getClassName());
+					}
+
+					String uuid = (String) ReflectionUtils.getNestedProperty(object, fieldDescriptor.getFieldName());
+					values[i] = valueFactory.createValue(uuid, PropertyType.REFERENCE);
+				}
+            }
+
+            parentNode.setProperty(jcrName, values);
+        }
+        catch(Exception e) {
+            throw new PersistenceException("Cannot insert collection field : " 
+                    + collectionDescriptor.getFieldName()
+                    + " of class "
+                    + collectionDescriptor.getClassDescriptor().getClassName(), e);
+        }
+	}
+    
+}
\ No newline at end of file

Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ReferenceCollectionConverterImpl.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ReferenceCollectionConverterImpl.java?view=auto&rev=517394
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ReferenceCollectionConverterImpl.java
(added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ReferenceCollectionConverterImpl.java
Mon Mar 12 14:06:13 2007
@@ -0,0 +1,170 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.collectionconverter.impl;
+
+
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.PropertyType;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.UnsupportedRepositoryOperationException;
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+import javax.jcr.ValueFormatException;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.version.VersionException;
+
+import org.apache.portals.graffito.jcr.exception.PersistenceException;
+import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollectionUtil;
+import org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter;
+
+/**
+ *
+ * This collection mapping strategy maps a collection of String into a JCR multi value property
based on the reference type 
+ * 
+ * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
+ */
+public class ReferenceCollectionConverterImpl extends AbstractCollectionConverterImpl {
+
+    /**
+     * Constructor
+     *
+     * @param atomicTypeConverters
+     * @param objectConverter
+     * @param mapper
+     */
+    public ReferenceCollectionConverterImpl(Map atomicTypeConverters,
+                                             ObjectConverter objectConverter,
+                                             Mapper mapper) {
+        super(atomicTypeConverters, objectConverter, mapper);
+    }
+
+    /**
+     *
+     * @see AbstractCollectionConverterImpl#doInsertCollection(Session, Node, CollectionDescriptor,
ManageableCollection)
+     */
+    protected void doInsertCollection(Session session,
+                                      Node parentNode,
+                                      CollectionDescriptor collectionDescriptor,
+                                      ManageableCollection collection) throws RepositoryException
{
+        addUuidProperties(session, parentNode, collectionDescriptor, collection);
+    }
+
+    /**
+     *
+     * @see AbstractCollectionConverterImpl#doUpdateCollection(Session, Node, CollectionDescriptor,
ManageableCollection)
+     */
+    protected void doUpdateCollection(Session session,
+                                 Node parentNode,
+                                 CollectionDescriptor collectionDescriptor,
+                                 ManageableCollection collection) throws RepositoryException
{
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+
+        // Delete existing values
+        if (parentNode.hasProperty(jcrName)) {
+            parentNode.setProperty(jcrName, (Value[]) null);
+        }
+
+        if (collection == null) {
+            return;
+        }
+
+
+        addUuidProperties(session, parentNode, collectionDescriptor, collection);    }
+
+    /**
+     * @see AbstractCollectionConverterImpl#doGetCollection(Session, Node, CollectionDescriptor,
Class)
+     */
+    protected ManageableCollection doGetCollection(Session session,
+                                                   Node parentNode,
+                                                   CollectionDescriptor collectionDescriptor,
+                                                   Class collectionFieldClass) throws RepositoryException
{
+        try {
+            String jcrName = getCollectionJcrName(collectionDescriptor);
+            if (!parentNode.hasProperty(jcrName)) {
+                return null;
+            }
+            Property property = parentNode.getProperty(jcrName);
+            Value[] values = property.getValues();
+
+            ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass);
+            for (int i = 0; i < values.length; i++) {
+                
+                collection.addObject(values[i].getString());
+            }
+
+            return collection;
+        }
+        catch(ValueFormatException vfe) {
+          throw new PersistenceException("Cannot get the collection field : "
+                  + collectionDescriptor.getFieldName()
+                  + "for class " + collectionDescriptor.getClassDescriptor().getClassName(),
+                  vfe);
+        }
+    }
+    
+    /**
+     * @see AbstractCollectionConverterImpl#doIsNull(Session, Node, CollectionDescriptor,
Class)
+     */
+    protected boolean doIsNull(Session session,
+                                              Node parentNode,
+                                              CollectionDescriptor collectionDescriptor,
+                                              Class collectionFieldClass) throws RepositoryException
{
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+
+         if (!parentNode.hasProperty(jcrName)) {
+            return true;
+        }
+        return false;
+    }
+    
+	private void addUuidProperties(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
ManageableCollection collection) throws UnsupportedRepositoryOperationException, RepositoryException,
VersionException, LockException, ConstraintViolationException {
+		try {
+            if (collection == null) {
+                return;
+            }
+
+            String jcrName = getCollectionJcrName(collectionDescriptor);
+            Value[] values = new Value[collection.getSize()];
+            ValueFactory valueFactory = session.getValueFactory();
+            Iterator collectionIterator = collection.getIterator();
+            for (int i = 0; i < collection.getSize(); i++) {
+                String uuid = (String) collectionIterator.next();
+                values[i] = valueFactory.createValue(uuid, PropertyType.REFERENCE);
+                
+            }
+
+            parentNode.setProperty(jcrName, values);
+        }
+        catch(ValueFormatException vfe) {
+            throw new PersistenceException("Cannot insert collection field : " 
+                    + collectionDescriptor.getFieldName()
+                    + " of class "
+                    + collectionDescriptor.getClassDescriptor().getClassName(), vfe);
+        }
+	}
+    
+}
\ No newline at end of file

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-uuid.xml
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-uuid.xml?view=diff&rev=517394&r1=517393&r2=517394
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-uuid.xml (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-uuid.xml Mon Mar 12
14:06:13 2007
@@ -13,12 +13,18 @@
 		className="org.apache.portals.graffito.jcr.testmodel.uuid.B" jcrNodeType="nt:unstructured"
 >
 		<field-descriptor fieldName="path" path="true" />
 		<field-descriptor fieldName="reference2A" jcrName="reference2A" converter="org.apache.portals.graffito.jcr.persistence.atomictypeconverter.impl.ReferenceTypeConverterImpl"
/> 
+		<collection-descriptor fieldName="multiReferences" jcrName="multiReferences" proxy="false"
		                      
+		                       collectionConverter="org.apache.portals.graffito.jcr.persistence.collectionconverter.impl.ReferenceCollectionConverterImpl"
/>
+		
 	</class-descriptor>
 
 	<class-descriptor
 		className="org.apache.portals.graffito.jcr.testmodel.uuid.B2" jcrNodeType="nt:unstructured"
 >
 		<field-descriptor fieldName="path" path="true" />
 		<bean-descriptor fieldName="a" jcrName="a" converter="org.apache.portals.graffito.jcr.persistence.beanconverter.impl.ReferenceBeanConverterImpl"
/>
+		<collection-descriptor fieldName="multiReferences" jcrName="multiReferences" proxy="false"
		                      
+		                       collectionConverter="org.apache.portals.graffito.jcr.persistence.collectionconverter.impl.BeanReferenceCollectionConverterImpl"
/>
+		
 	</class-descriptor>	
 			
 	

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/uuid/PersistenceManagerUuidTest.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/uuid/PersistenceManagerUuidTest.java?view=diff&rev=517394&r1=517393&r2=517394
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/uuid/PersistenceManagerUuidTest.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/uuid/PersistenceManagerUuidTest.java
Mon Mar 12 14:06:13 2007
@@ -16,6 +16,9 @@
  */
 package org.apache.portals.graffito.jcr.persistence.uuid;
 
+import java.util.ArrayList;
+import java.util.Collection;
+
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
@@ -88,6 +91,11 @@
         super.tearDown();
     }
     
+    /**
+     * 
+     *  Map the jcr uuid into a String attribute
+     *  
+     */
     public void testUuid()
     {
         try
@@ -158,7 +166,12 @@
         }
         
     }
-    
+    /**
+     * 
+     * Map a Reference into a String attribute. 
+     * Object B has an attribute containing the object A uuid. 
+     *
+     */
     public void testFieldReference()
     {
         try
@@ -225,6 +238,13 @@
         
     }
 
+    /**
+     * 
+     * Map a Reference into a bean attribute. 
+     * Object B has an attribute containing the object A. 
+     * The jcr node matching to the object B contains a reference (the jcr node matching
to the object B).   
+     *
+     */
     public void testBeanReference()
     {
         try
@@ -280,6 +300,177 @@
             b = (B2) persistenceManager.getObject("/testB2");
             a = b.getA();
             assertNull("a is not null", a);
+            
+            
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+            fail("Exception occurs during the unit test : " + e);
+        }
+        
+    }
+
+    /**
+     * Map a list of uuid  into a collection of String 
+     * The list is defined in a jcr property (Referece type / multi values) 
+     *
+     */
+    public void testCollectionOfUuid()
+    {
+        try
+        {
+        	PersistenceManager persistenceManager = getPersistenceManager();
+
+            // --------------------------------------------------------------------------------
+            // Create and store an object A in the repository
+            // --------------------------------------------------------------------------------
+            A a1 = new A();
+            a1.setPath("/a1");
+            a1.setStringData("testdata1");
+            persistenceManager.insert(a1);
+            
+            A a2 = new A();
+            a2.setPath("/a2");
+            a2.setStringData("testdata2");
+            persistenceManager.insert(a2);            
+            persistenceManager.save();           
+
+            // --------------------------------------------------------------------------------
+            // Get the objects
+            // --------------------------------------------------------------------------------
          
+            a1 = (A) persistenceManager.getObject( "/a1");
+            assertNotNull("a1 is null", a1);
+            a2 = (A) persistenceManager.getObject( "/a2");
+            assertNotNull("a2 is null", a2);
+            ArrayList references = new ArrayList();
+            references.add(a1.getUuid());
+            references.add(a2.getUuid());
+            
+            // --------------------------------------------------------------------------------
+            // Create and store an object B in the repository which has a collection of A
+            // --------------------------------------------------------------------------------
+            B b = new B();
+            b.setPath("/testB");
+            b.setMultiReferences(references);
+            persistenceManager.insert(b);
+            persistenceManager.save();
+            
+            // --------------------------------------------------------------------------------
+            // Retrieve object B
+            // --------------------------------------------------------------------------------
+            b = (B) persistenceManager.getObject("/testB");
+            Collection allref = b.getMultiReferences();
+            assertNotNull("collection is null", allref);
+            assertTrue("Invalid number of items in the collection", allref.size() == 2);
+
+            // --------------------------------------------------------------------------------
+            // Update object B with invalid uuid
+            // --------------------------------------------------------------------------------
+            allref.add("12345");
+            b.setMultiReferences(allref);
+            try
+            {
+            	persistenceManager.update(b);            	
+            	fail("Exception not throw");
+            }
+            catch(Exception e)
+            {
+            	//Throws an exception due to an invalid uuid
+            	System.out.println("Invalid uuid value in the collection : " + e);
+            	
+            }
+            
+            // --------------------------------------------------------------------------------
+            // Update object B with an null value
+            // --------------------------------------------------------------------------------
+            b.setMultiReferences(null);
+            persistenceManager.update(b);
+            persistenceManager.save();
+            
+            // --------------------------------------------------------------------------------
+            // Retrieve object B
+            // --------------------------------------------------------------------------------
+            b = (B) persistenceManager.getObject("/testB");            
+            assertNull("a is not null", b.getMultiReferences());
+            
+            
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+            fail("Exception occurs during the unit test : " + e);
+        }
+        
+    }
+
+    /**
+     * Map a list of uuid  into a collection
+     * The list is defined in a jcr property (multi values) 
+     *
+     */
+    public void testCollectionOfBeanWithUuid()
+    {
+        try
+        {
+        	PersistenceManager persistenceManager = getPersistenceManager();
+
+            // --------------------------------------------------------------------------------
+            // Create and store an object A in the repository
+            // --------------------------------------------------------------------------------
+            A a1 = new A();
+            a1.setPath("/a1");
+            a1.setStringData("testdata1");
+            persistenceManager.insert(a1);
+            
+            A a2 = new A();
+            a2.setPath("/a2");
+            a2.setStringData("testdata2");
+            persistenceManager.insert(a2);            
+            persistenceManager.save();           
+
+            // --------------------------------------------------------------------------------
+            // Get the objects
+            // --------------------------------------------------------------------------------
          
+            a1 = (A) persistenceManager.getObject( "/a1");
+            assertNotNull("a1 is null", a1);
+            a2 = (A) persistenceManager.getObject( "/a2");
+            assertNotNull("a2 is null", a2);
+            ArrayList references = new ArrayList();
+            references.add(a1);
+            references.add(a2);
+            
+            // --------------------------------------------------------------------------------
+            // Create and store an object B in the repository which has a collection of A
+            // --------------------------------------------------------------------------------
+            B2 b = new B2();
+            b.setPath("/testB2");
+            b.setMultiReferences(references);
+            persistenceManager.insert(b);
+            persistenceManager.save();
+            
+            // --------------------------------------------------------------------------------
+            // Retrieve object B
+            // --------------------------------------------------------------------------------
+            b = (B2) persistenceManager.getObject("/testB2");
+            Collection allref = b.getMultiReferences();
+            assertNotNull("collection is null", allref);
+            assertTrue("Invalid number of items in the collection", allref.size() == 2);
+            this.contains(allref, "/a1" , A.class);
+            this.contains(allref, "/a2" , A.class);
+
+            // --------------------------------------------------------------------------------
+            // Update object B with an null value
+            // --------------------------------------------------------------------------------
+            b.setMultiReferences(null);
+            persistenceManager.update(b);
+            persistenceManager.save();
+            
+            // --------------------------------------------------------------------------------
+            // Retrieve object B
+            // --------------------------------------------------------------------------------
+            b = (B2) persistenceManager.getObject("/testB2");            
+            assertNull("a is not null", b.getMultiReferences());
             
             
         }

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/uuid/B.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/uuid/B.java?view=diff&rev=517394&r1=517393&r2=517394
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/uuid/B.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/uuid/B.java
Mon Mar 12 14:06:13 2007
@@ -16,6 +16,8 @@
  */
 package org.apache.portals.graffito.jcr.testmodel.uuid;
 
+import java.util.Collection;
+
 
 /**
  *
@@ -25,8 +27,8 @@
 public class B
 {
 	private String path; 
-    private String reference2A; // This String attribute is mapped to a reference jcr property
-
+    private String reference2A; // This String attribute is mapped to a reference/uuid jcr
property
+    private Collection multiReferences; // a collection of references (uuid)
     
     public String getPath() 
     {
@@ -47,6 +49,15 @@
 	{
 		this.reference2A = reference2A;
 	}
-	
+
+	public Collection getMultiReferences() {
+		return multiReferences;
+	}
+
+	public void setMultiReferences(Collection multiReferences) {
+		this.multiReferences = multiReferences;
+	}
+
+
     
 }

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/uuid/B2.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/uuid/B2.java?view=diff&rev=517394&r1=517393&r2=517394
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/uuid/B2.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/uuid/B2.java
Mon Mar 12 14:06:13 2007
@@ -16,6 +16,8 @@
  */
 package org.apache.portals.graffito.jcr.testmodel.uuid;
 
+import java.util.Collection;
+
 
 /**
  *
@@ -26,7 +28,7 @@
 {
 	private String path; 
     private A a; // This attribute is mapped to a reference jcr property
-
+    private Collection multiReferences; // a collection of bean (with an uuid) mapped into
a list of jcr properties (reference type)
     
     public String getPath() 
     {
@@ -45,7 +47,12 @@
 	public void setA(A a) {
 		this.a = a;
 	}
+	public Collection getMultiReferences() {
+		return multiReferences;
+	}
 
-	
+	public void setMultiReferences(Collection multiReferences) {
+		this.multiReferences = multiReferences;
+	}
     
 }



Mime
View raw message