incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From apope...@apache.org
Subject svn commit: r376131 - in /incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter: ./ impl/
Date Thu, 09 Feb 2006 00:44:27 GMT
Author: apopescu
Date: Wed Feb  8 16:44:25 2006
New Revision: 376131

URL: http://svn.apache.org/viewcvs?rev=376131&view=rev
Log:
code cleanup
exception handling

Modified:
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/ManageableCollectionUtil.java
    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/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/MultiValueCollectionConverterImpl.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/ManageableCollectionUtil.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/ManageableCollectionUtil.java?rev=376131&r1=376130&r2=376131&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/ManageableCollectionUtil.java (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/ManageableCollectionUtil.java Wed Feb  8 16:44:25 2006
@@ -22,76 +22,68 @@
 import java.util.List;
 import java.util.Vector;
 
-import org.apache.portals.graffito.jcr.exception.PersistenceException;
+import org.apache.portals.graffito.jcr.exception.JcrMappingException;
 import org.apache.portals.graffito.jcr.persistence.collectionconverter.impl.ManageableArrayList;
 import org.apache.portals.graffito.jcr.persistence.collectionconverter.impl.ManageableVector;
 
 /**
- * Utility class used to instantiate {@link ManageableCollection} 
+ * Utility class used to instantiate {@link ManageableCollection}
  *
  * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
  *
  */
-public class ManageableCollectionUtil
-{
-	
-	/**
-	 * Instantiate a new {@link ManageableCollection}
-	 * @param manageableCollectionClassName The manageable collection class name
-	 * @return an emtpy created {@link ManageableCollection}
-	 */
-    public static ManageableCollection getManageableCollection(String manageableCollectionClassName)
-    {
-        try
-        {
+public class ManageableCollectionUtil {
+
+    /**
+     * Instantiate a new {@link ManageableCollection}
+     * @param manageableCollectionClassName The manageable collection class name
+     * @return an emtpy created {@link ManageableCollection}
+     */
+    public static ManageableCollection getManageableCollection(String manageableCollectionClassName) {
+        try {
             Class collectionClass = Class.forName(manageableCollectionClassName);
+
             return (ManageableCollection) collectionClass.newInstance();
         }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to create the manageable collection : " + manageableCollectionClassName, e);
+        catch (Exception e) {
+            throw new JcrMappingException("Cannot create manageable collection : "
+                                           + manageableCollectionClassName,
+                                           e);
         }
     }
 
-	/**
-	 * Instantiate a new {@link ManageableCollection}
-	 * @param collectionClass the collection class name 
-	 * @return an emtpy created {@link ManageableCollection}
-	 */
-    
-    public static ManageableCollection getManageableCollection(Class collectionClass)
-    {
-        try
-        {
+    /**
+     * Instantiate a new {@link ManageableCollection}
+     * @param collectionClass the collection class name
+     * @return an emtpy created {@link ManageableCollection}
+     */
+
+    public static ManageableCollection getManageableCollection(Class collectionClass) {
+        try {
 
-            if (collectionClass.equals(ArrayList.class))
-            {
+            if (collectionClass.equals(ArrayList.class)) {
                 return new ManageableArrayList();
             }
 
-            if (collectionClass.equals(Vector.class))
-            {
+            if (collectionClass.equals(Vector.class)) {
                 return new ManageableVector();
             }
 
-            if (collectionClass.equals(Collection.class) || collectionClass.equals(List.class))
-            {
+            if (collectionClass.equals(Collection.class) || collectionClass.equals(List.class)) {
                 return new ManageableArrayList();
             }
-            
-            Object collection  = collectionClass.newInstance();
-            if (! (collection instanceof ManageableCollection))
-            {
-               throw new PersistenceException("Unsupported collection type :" + collectionClass.getName());
+
+            Object collection = collectionClass.newInstance();
+            if (!(collection instanceof ManageableCollection)) {
+                throw new JcrMappingException("Unsupported collection type :"
+                                               + collectionClass.getName());
             }
-            else
-            {
+            else {
                 return (ManageableCollection) collection;
             }
         }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to create the manageable collection", e);
+        catch (Exception e) {
+            throw new JcrMappingException("Cannot create manageable collection", e);
         }
     }
 
@@ -99,52 +91,48 @@
      * Convert a java Collection object into a {@link ManageableCollection}.
      * Until now, only the following class are supported :
      * Collection, List, ArrayList, Vector
-     * 
+     *
      * If you need a Map, you have to write your own {@link ManageableCollection}.
      * @param object the java collection or Map
      * @return The converted {@link ManageableCollection}
      *
      */
-    public static ManageableCollection getManageableCollection(Object object)
-    {
-        try
-        {
-            if (object == null)
-            {
+    public static ManageableCollection getManageableCollection(Object object) {
+        try {
+            if (object == null) {
                 return null;
             }
 
-            if (object instanceof ManageableCollection)
-            {
+            if (object instanceof ManageableCollection) {
                 return (ManageableCollection) object;
-                
+
             }
-            if (object.getClass().equals(ArrayList.class))
-            {
-                ManageableArrayList manageableArrayList =  new ManageableArrayList();
+            if (object.getClass().equals(ArrayList.class)) {
+                ManageableArrayList manageableArrayList = new ManageableArrayList();
                 manageableArrayList.addAll((Collection) object);
+
                 return manageableArrayList;
             }
 
-            if (object.getClass().equals(Vector.class))
-            {
+            if (object.getClass().equals(Vector.class)) {
                 ManageableVector manageableVector = new ManageableVector();
                 manageableVector.addAll((Collection) object);
+
                 return manageableVector;
             }
 
-            if (object.getClass().equals(Collection.class) || object.getClass().equals(List.class))
-            {
-                ManageableArrayList manageableArrayList =  new ManageableArrayList();
+            if (object.getClass().equals(Collection.class)
+                || object.getClass().equals(List.class)) {
+                ManageableArrayList manageableArrayList = new ManageableArrayList();
                 manageableArrayList.addAll((Collection) object);
+
                 return manageableArrayList;
             }
-
-            throw new PersistenceException("Unsupported collection type :" + object.getClass().getName());
         }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to create the manageable collection", e);
+        catch (Exception e) {
+            throw new JcrMappingException("Impossible to create the manageable collection", e);
         }
+        
+        throw new JcrMappingException("Unsupported collection type :" + object.getClass().getName());
     }
 }

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java?rev=376131&r1=376130&r2=376131&view=diff
==============================================================================
--- 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 Wed Feb  8 16:44:25 2006
@@ -18,20 +18,33 @@
 
 import java.util.Map;
 
+import javax.jcr.ItemExistsException;
+import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+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.CollectionDescriptor;
 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;
 
 /** 
  * Abstract class used for all CollectionConverter
  * 
  * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
- * 
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
  */
-public abstract class AbstractCollectionConverterImpl implements CollectionConverter
-{
+public abstract class AbstractCollectionConverterImpl implements CollectionConverter {
     protected Map atomicTypeConverters;
 	protected ObjectConverter objectConverter;    
+    // NOT USED
     protected Mapper mapper;
     
     /**
@@ -41,12 +54,150 @@
      * @param objectConverter The object converter to used
      * @param mapper The mapper to used
      */
-    public AbstractCollectionConverterImpl(Map atomicTypeConverters, ObjectConverter objectConverter, Mapper mapper)
-    {
+    public AbstractCollectionConverterImpl(Map atomicTypeConverters, 
+                                           ObjectConverter objectConverter, 
+                                           Mapper mapper) {
     	this.atomicTypeConverters = atomicTypeConverters;
         this.objectConverter = objectConverter;
         this.mapper = mapper;
     }
 
+    protected abstract void doInsertCollection(Session session,
+                                               Node parentNode,
+                                               CollectionDescriptor descriptor,
+                                               ManageableCollection collection) throws RepositoryException;
+    
+    protected abstract void doUpdateCollection(Session session,
+                                               Node parentNode,
+                                               CollectionDescriptor descriptor,
+                                               ManageableCollection collection) throws RepositoryException;
+    
+    protected abstract ManageableCollection  doGetCollection(Session session,
+                                                             Node parentNode,
+                                                             CollectionDescriptor collectionDescriptor,
+                                                             Class collectionFieldClass) throws RepositoryException;
+    
+   /**
+    * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#insertCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection)
+    */
+   public void insertCollection(Session session,
+                                Node parentNode,
+                                CollectionDescriptor collectionDescriptor,
+                                ManageableCollection collection) {
+       try {
+           doInsertCollection(session, parentNode, collectionDescriptor, collection);
+       }
+       catch(ItemExistsException iee) {
+           throw new PersistenceException("Cannot insert collection field : " 
+                   + collectionDescriptor.getFieldName() + " of class " 
+                   + collectionDescriptor.getClassDescriptor().getClassName()
+                   + ". An item already exists.", iee);
+       }
+       catch(PathNotFoundException pnfe) {
+           throw new PersistenceException("Cannot insert collection field : " 
+                   + collectionDescriptor.getFieldName()
+                   + " of class "
+                   + collectionDescriptor.getClassDescriptor().getClassName(), pnfe);
+       }
+       catch(VersionException ve) {
+           throw new PersistenceException("Cannot insert collection field : " 
+                   + collectionDescriptor.getFieldName()
+                   + " of class "
+                   + collectionDescriptor.getClassDescriptor().getClassName(), ve);
+       }
+       catch(ConstraintViolationException cve) {
+           throw new PersistenceException("Cannot insert collection field : " 
+                   + collectionDescriptor.getFieldName() 
+                   + " of class "
+                   + collectionDescriptor.getClassDescriptor().getClassName()
+                   + ". Constraint violation.", cve);
+       }
+       catch(LockException le) {
+           throw new PersistenceException("Cannot insert collection field : " 
+                   + collectionDescriptor.getFieldName() 
+                   + " of class "
+                   + collectionDescriptor.getClassDescriptor().getClassName() 
+                   + " on locked parent.", le);
+       }
+       catch(RepositoryException re) {
+           throw new org.apache.portals.graffito.jcr.exception.RepositoryException(
+                   "Cannot insert collection field : " 
+                   + collectionDescriptor.getFieldName()
+                   + " of class "
+                   + collectionDescriptor.getClassDescriptor().getClassName(), re);
+       }
+   }
+   
+    /**
+    *
+    * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#updateCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection)
+    */
+   public void updateCollection(Session session,
+                                Node parentNode,
+                                CollectionDescriptor collectionDescriptor,
+                                ManageableCollection collection) {
+       try {
+           doUpdateCollection(session, parentNode, collectionDescriptor, collection);
+       }
+       catch(VersionException ve) {
+           throw new PersistenceException("Cannot insert collection field : " 
+                   + collectionDescriptor.getFieldName() 
+                   + " of class " 
+                   + collectionDescriptor.getClassDescriptor().getClassName(), 
+                   ve);
+       }
+       catch(LockException le) {
+           throw new PersistenceException("Cannot insert collection field : " 
+                   + collectionDescriptor.getFieldName() 
+                   + " of class " 
+                   + collectionDescriptor.getClassDescriptor().getClassName()
+                   + " on locked node", 
+                   le);
+       }
+       catch(ConstraintViolationException cve) {
+           throw new PersistenceException("Cannot insert collection field : " 
+                   + collectionDescriptor.getFieldName() 
+                   + " of class " 
+                   + collectionDescriptor.getClassDescriptor().getClassName()
+                   + " Constraint violation.", 
+                   cve);
+       }
+       catch(RepositoryException re) {
+           throw new org.apache.portals.graffito.jcr.exception.RepositoryException(
+                   "Cannot insert collection field : " 
+                   + collectionDescriptor.getFieldName() 
+                   + " of class " 
+                   + collectionDescriptor.getClassDescriptor().getClassName(), 
+                   re);
+       }
+   }
+   
+   /**
+    * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#getCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, java.lang.Class)
+    */
+   public ManageableCollection getCollection(Session session,
+                                             Node parentNode,
+                                             CollectionDescriptor collectionDescriptor,
+                                             Class collectionFieldClass) {
+       try {
+           return doGetCollection(session, parentNode, collectionDescriptor, collectionFieldClass);
+       }
+       catch(RepositoryException re) {
+           throw new org.apache.portals.graffito.jcr.exception.RepositoryException(
+                   "Cannot get collection field : " + collectionDescriptor.getFieldName()
+                   + "for " + collectionDescriptor.getClassDescriptor().getClassName(),
+                   re);
+       }
+   }
 
+   protected String getCollectionJcrName(CollectionDescriptor descriptor) {
+        String jcrName = descriptor.getJcrName();
+        
+        if (null == jcrName) {
+            throw new JcrMappingException("The JcrName attribute is not defined for the CollectionDescriptor : "
+                    + descriptor.getFieldName());
+        }
+        
+        return jcrName;
+    }
 }

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java?rev=376131&r1=376130&r2=376131&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java Wed Feb  8 16:44:25 2006
@@ -1,12 +1,12 @@
 /*
  * Copyright 2000-2005 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.
@@ -16,262 +16,223 @@
 
 package org.apache.portals.graffito.jcr.persistence.collectionconverter.impl;
 
+
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
-import org.apache.commons.beanutils.PropertyUtils;
-import org.apache.portals.graffito.jcr.exception.PersistenceException;
+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.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;
+import org.apache.portals.graffito.jcr.reflection.ReflectionUtils;
 
-/** 
+/**
  * Default Collection Mapping/convertion implementation.
- * 
+ *
  * This collection mapping strategy maps a collection under an extra JCR node (specify by the jcrName in the CollectionDescriptor).
- * It is usefull when the node type "nt:unstructured" is applied to the collection elements. By this way, it is possible 
+ * It is usefull when the node type "nt:unstructured" is applied to the collection elements. By this way, it is possible
  * 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 element node name is a simple indexed constant.
- * 
- * Example - without an id attribute:                 
+ *
+ * Example - without an id attribute:
  *   /test (Main object containing the collection field )
  *     /mycollection (extra node used to store the entire collection)
  *          /collection-element1 (node used to store the first collection element)
- *                /item-prop       
+ *                /item-prop
  *                ....
- *          /collection-element2 (node used to store the second collection element) 
+ *          /collection-element2 (node used to store the second collection element)
  *          ...
- *          
- * Example - with an id attribute:                 
+ *
+ * Example - with an id attribute:
  *   /test (Main object containing the collection field )
  *     /mycollection (extra node used to store the entire collection)
  *          /aValue (id value assigned to the first element)
- *                /item-prop       
+ *                /item-prop
  *                ....
- *          /anotherValue (id value assigned to the first element) 
+ *          /anotherValue (id value assigned to the first element)
  *          ...
 
  * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
- * 
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
  */
-public class DefaultCollectionConverterImpl extends AbstractCollectionConverterImpl 
-{
+public class DefaultCollectionConverterImpl extends AbstractCollectionConverterImpl {
 
     private static final String COLLECTION_ELEMENT_NAME = "collection-element";
 
     /**
-     * Constructor 
+     * Constructor
      * @param atomicTypeConverters
      * @param objectConverter
      * @param mapper
      */
-    public DefaultCollectionConverterImpl(Map atomicTypeConverters, ObjectConverter objectConverter, Mapper mapper)
-    {
+    public DefaultCollectionConverterImpl(Map atomicTypeConverters,
+                                          ObjectConverter objectConverter,
+                                          Mapper mapper) {
         super(atomicTypeConverters, objectConverter, mapper);
     }
 
     /**
-     * 
-     * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#insertCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection)
+     * @see AbstractCollectionConverterImpl#doInsertCollection(Session, Node, CollectionDescriptor, ManageableCollection)
      */
-    public void insertCollection(Session session, Node parentNode,  CollectionDescriptor collectionDescriptor, ManageableCollection collection)  
-    {
-
-        try
-        {
-
-            if (collection == null)
-            {
-                return;
-            }
+    protected void doInsertCollection(Session session,
+                                      Node parentNode,
+                                      CollectionDescriptor collectionDescriptor,
+                                      ManageableCollection collection) throws RepositoryException {
+        if (collection == null) {
+            return;
+        }
 
-            String jcrName = collectionDescriptor.getJcrName();
+        String jcrName = collectionDescriptor.getJcrName();
 
-            if (jcrName == null)
-            {
-                throw new PersistenceException("The JcrName attribute is not defined for the CollectionDescriptor : "
-                        + collectionDescriptor.getCollectionConverterClassName());
-            }
+        if (jcrName == null) {
+            throw new JcrMappingException(
+                    "The JcrName attribute is not defined for the CollectionDescriptor : "
+                    + collectionDescriptor.getFieldName());
+        }
 
-            Node collectionNode = parentNode.addNode(jcrName);
+        Node collectionNode = parentNode.addNode(jcrName);
 
-            Iterator collectionIterator = collection.getIterator();
-            ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor
-                    .getElementClassName()));
-
-            int elementCollectionCount = 0;
-            while (collectionIterator.hasNext())
-            {
-                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();
-                    elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
-                }
-                else
-                {
+        Mapper mapper = collectionDescriptor.getClassDescriptor().getMappingDescriptor().getMapper();
+        ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(
+                ReflectionUtils.forName(collectionDescriptor.getElementClassName()));
 
-                    elementCollectionCount++;
-                    elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
-                }
+        Iterator collectionIterator = collection.getIterator();
+        int elementCollectionCount = 0;
+        while (collectionIterator.hasNext()) {
+            Object item = collectionIterator.next();
+            String elementJcrName = null;
 
-                objectConverter.insert(session, collectionNode, elementJcrName, item);
+            // If the element object has a unique id => the element jcr node name = the id value
+            if (elementClassDescriptor.hasIdField()) {
+                String idFieldName = elementClassDescriptor.getIdFieldDescriptor()
+                                                           .getFieldName();
+                elementJcrName = ReflectionUtils.getNestedProperty(item, idFieldName).toString();
+            }
+            else {
+                elementCollectionCount++;
+                elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
             }
-        }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to insert the collection field : " + collectionDescriptor.getFieldName()
-                    + "for " + collectionDescriptor.getElementClassName(), e);
-        }
 
+            objectConverter.insert(session, collectionNode, elementJcrName, item);
+        }
     }
 
     /**
-     * 
-     * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#updateCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection)
+     *
+     * @see AbstractCollectionConverterImpl#doUpdateCollection(Session, Node, CollectionDescriptor, ManageableCollection)
      */
-    public void updateCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,  ManageableCollection collection)
-    {
-        try
-        {
-
-            String jcrName = collectionDescriptor.getJcrName();
-
-            if (jcrName == null)
-            {
-                throw new PersistenceException("The JcrName attribute is not defined for the CollectionDescriptor : "
-                        + collectionDescriptor.getCollectionConverterClassName());
+    protected void doUpdateCollection(Session session,
+                                 Node parentNode,
+                                 CollectionDescriptor collectionDescriptor,
+                                 ManageableCollection collection) throws RepositoryException {
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+
+        if (collection == null) {
+            if (parentNode.hasNode(jcrName)) {
+                parentNode.getNode(jcrName).remove();
             }
 
-            if (collection == null)
-            {
-                if (parentNode.hasNode(jcrName))
-                {
-                    parentNode.getNode(jcrName).remove();
+            return;
+        }
 
-                }
-                return;
-            }
+        
+        Mapper mapper = collectionDescriptor.getClassDescriptor().getMappingDescriptor().getMapper();
+        ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(
+                ReflectionUtils.forName(collectionDescriptor.getElementClassName()));
+        
+        Node collectionNode = parentNode.getNode(jcrName);
+        //  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);
+        }
 
-            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);
-            }
+        Iterator collectionIterator = collection.getIterator();
+        int elementCollectionCount = 0;
 
-            HashMap updatedItems = new HashMap();
-            while (collectionIterator.hasNext())
-            {
-                Object item = collectionIterator.next();
-                
-                elementCollectionCount++;
-                String elementJcrName = null;
+        Map updatedItems = new HashMap();
+        while (collectionIterator.hasNext()) {
+            Object item = collectionIterator.next();
 
-                if (elementClassDescriptor.hasIdField())
-                {
+            elementCollectionCount++;
+            String elementJcrName = null;
 
-                    String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
-                    elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
+            if (elementClassDescriptor.hasIdField()) {
 
-                    // Update existing JCR Nodes
-                    if (collectionNode.hasNode(elementJcrName))
-                    {
-                        objectConverter.update(session, collectionNode, elementJcrName, item);
-                    }
-                    else
-                    {
-                        // Add new collection elements
-                        objectConverter.insert(session, collectionNode, elementJcrName, item);
-                    }
-                    
-                    updatedItems.put(elementJcrName, item);
+                String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
+                elementJcrName = ReflectionUtils.getNestedProperty(item, idFieldName).toString();
 
+                // Update existing JCR Nodes
+                if (collectionNode.hasNode(elementJcrName)) {
+                    objectConverter.update(session, collectionNode, elementJcrName, item);
                 }
-                else
-                {
-
-                    elementCollectionCount++;
-                    elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
+                else {
+                    // Add new collection elements
                     objectConverter.insert(session, collectionNode, elementJcrName, item);
                 }
 
+                updatedItems.put(elementJcrName, item);
+            }
+            else {
+                elementCollectionCount++;
+                elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
+                objectConverter.insert(session, collectionNode, elementJcrName, item);
             }
+        }
 
-            // Delete JCR nodes that are not present in the collection
-            if (elementClassDescriptor.hasIdField())
-            {
-                NodeIterator nodeIterator = collectionNode.getNodes();
-                while (nodeIterator.hasNext())
-                {
-                    Node child = nodeIterator.nextNode();
-                    if (! updatedItems.containsKey(child.getName()))
-                    {
-                        child.remove();
-                    }
+        // Delete JCR nodes that are not present in the collection
+        if (elementClassDescriptor.hasIdField()) {
+            NodeIterator nodeIterator = collectionNode.getNodes();
+            List removeNodes = new ArrayList();
+            while (nodeIterator.hasNext()) {
+                Node child = nodeIterator.nextNode();
+                if (!updatedItems.containsKey(child.getName())) {
+                    removeNodes.add(child);
                 }
             }
+            for(int i = 0; i < removeNodes.size(); i++) {
+                ((Node) removeNodes.get(i)).remove();
+            }
         }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to update the collection field : " + collectionDescriptor.getFieldName()
-                    + "for " + collectionDescriptor.getElementClassName(), e);
-        }
-
     }
 
     /**
-     * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#getCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, java.lang.Class)
+     * @see AbstractCollectionConverterImpl#doGetCollection(Session, Node, CollectionDescriptor, Class)
      */
-    public ManageableCollection getCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor, Class collectionFieldClass)
-    {
-        try
-        {
-            String jcrName = collectionDescriptor.getJcrName();
-            if (!parentNode.hasNode(jcrName))
-            {
-                return null;
-            }
-
-            Node collectionNode = parentNode.getNode(jcrName);
-            NodeIterator children = collectionNode.getNodes();
-
-            ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass);
-
-            while (children.hasNext())
-            {
-                Node itemNode = children.nextNode();
-                Object item = objectConverter.getObject(session, Class.forName(collectionDescriptor.getElementClassName()),
-                        itemNode.getPath());
-                collection.addObject(item);
-            }
-
-            return collection;
-        }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to get the collection field : " + collectionDescriptor.getFieldName()
-                    + "for " + collectionDescriptor.getElementClassName(), e);
+    protected ManageableCollection doGetCollection(Session session,
+                                              Node parentNode,
+                                              CollectionDescriptor collectionDescriptor,
+                                              Class collectionFieldClass) throws RepositoryException {
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+
+        if (!parentNode.hasNode(jcrName)) {
+            return null;
+        }
+
+        ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass);
+        Node collectionNode = parentNode.getNode(jcrName);
+        NodeIterator children = collectionNode.getNodes();
+        Class elementClass = ReflectionUtils.forName(collectionDescriptor.getElementClassName());
+        
+        while (children.hasNext()) {
+            Node itemNode = children.nextNode();
+            Object item = objectConverter.getObject(session, elementClass, itemNode.getPath());
+            collection.addObject(item);
         }
-    }
 
-}
+        return collection;
+    }
+}
\ No newline at end of file

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/MultiValueCollectionConverterImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/MultiValueCollectionConverterImpl.java?rev=376131&r1=376130&r2=376131&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/MultiValueCollectionConverterImpl.java (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/MultiValueCollectionConverterImpl.java Wed Feb  8 16:44:25 2006
@@ -1,12 +1,12 @@
 /*
  * Copyright 2000-2005 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.
@@ -16,13 +16,17 @@
 
 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.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+import javax.jcr.ValueFormatException;
 
 import org.apache.portals.graffito.jcr.exception.PersistenceException;
 import org.apache.portals.graffito.jcr.mapper.Mapper;
@@ -31,148 +35,130 @@
 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/convertion implementation used for multi values properties
- * 
+ *
  * This collection mapping strategy maps a collection into a JCR multi value property
  *
  * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
- * 
+ * @author <a href='mailto:the_mindstorm[at]evolva[dot]ro'>Alexandru Popescu</a>
  */
-public class MultiValueCollectionConverterImpl extends AbstractCollectionConverterImpl
-{
+public class MultiValueCollectionConverterImpl extends AbstractCollectionConverterImpl {
+
     /**
-     * Constructor 
-     * 
+     * Constructor
+     *
      * @param atomicTypeConverters
      * @param objectConverter
      * @param mapper
      */
-    public MultiValueCollectionConverterImpl(Map atomicTypeConverters, ObjectConverter objectConverter, Mapper mapper)
-    {
+    public MultiValueCollectionConverterImpl(Map atomicTypeConverters,
+                                             ObjectConverter objectConverter,
+                                             Mapper mapper) {
         super(atomicTypeConverters, objectConverter, mapper);
     }
 
     /**
-     * 
-     * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#insertCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection)
+     *
+     * @see AbstractCollectionConverterImpl#doInsertCollection(Session, Node, CollectionDescriptor, ManageableCollection)
      */
-    public void insertCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor, ManageableCollection collection) 
-    {
-        try
-        {
-            if (collection == null)
-            {
+    protected void doInsertCollection(Session session,
+                                      Node parentNode,
+                                      CollectionDescriptor collectionDescriptor,
+                                      ManageableCollection collection) throws RepositoryException {
+        try {
+            if (collection == null) {
                 return;
             }
 
-            String jcrName = collectionDescriptor.getJcrName();
-
-            if (jcrName == null)
-            {
-                throw new PersistenceException("The JcrName attribute is not defined for the CollectionDescriptor : "
-                        + collectionDescriptor.getCollectionConverterClassName());
-            }
-    
+            String jcrName = getCollectionJcrName(collectionDescriptor);
             Value[] values = new Value[collection.getSize()];
-            Iterator collectionIterator = collection.getIterator();    
-            for (int i=0; i<collection.getSize();i++)
-            {
-                Object fieldValue  = collectionIterator.next();
-                AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters.get(fieldValue.getClass());
-                values[i] = atomicTypeConverter.getValue(session.getValueFactory(), fieldValue);     
+            ValueFactory valueFactory = session.getValueFactory();
+            Iterator collectionIterator = collection.getIterator();
+            for (int i = 0; i < collection.getSize(); i++) {
+                Object fieldValue = collectionIterator.next();
+                AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters
+                    .get(fieldValue.getClass());
+                values[i] = atomicTypeConverter.getValue(valueFactory, fieldValue);
             }
-            
+
             parentNode.setProperty(jcrName, values);
         }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to insert the collection field : " + collectionDescriptor.getFieldName()
-                    + "for " + collectionDescriptor.getElementClassName(), e);
+        catch(ValueFormatException vfe) {
+            throw new PersistenceException("Cannot insert collection field : " 
+                    + collectionDescriptor.getFieldName()
+                    + " of class "
+                    + collectionDescriptor.getClassDescriptor().getClassName(), vfe);
         }
-
     }
 
     /**
-     * 
-     * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#updateCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection)
+     *
+     * @see AbstractCollectionConverterImpl#doUpdateCollection(Session, Node, CollectionDescriptor, ManageableCollection)
      */
-    public void updateCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor, ManageableCollection collection)
-    {
-        try
-        {
-        	String jcrName = collectionDescriptor.getJcrName();
-
-            if (collection == null)
-            {
-                if (parentNode.hasProperty(jcrName))
-                {
-                	parentNode.setProperty(jcrName, (Value[]) null);
-                }
-                return;
-            }
+    protected void doUpdateCollection(Session session,
+                                 Node parentNode,
+                                 CollectionDescriptor collectionDescriptor,
+                                 ManageableCollection collection) throws RepositoryException {
+        String jcrName = getCollectionJcrName(collectionDescriptor);
 
-            if (jcrName == null)
-            {
-                throw new PersistenceException("The JcrName attribute is not defined for the CollectionDescriptor : "
-                        + collectionDescriptor.getCollectionConverterClassName());
-            }
-    
-            // Delete existing values
+        // Delete existing values
+        if (parentNode.hasProperty(jcrName)) {
             parentNode.setProperty(jcrName, (Value[]) null);
-            
-            // Add all collection element into an Value array
-            Value[] values = new Value[collection.getSize()];
-            Iterator collectionIterator = collection.getIterator();    
-            for (int i=0; i<collection.getSize();i++)
-            {
-                Object fieldValue  = collectionIterator.next();
-                AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters.get(fieldValue.getClass());
-                values[i] = atomicTypeConverter.getValue(session.getValueFactory(), fieldValue);     
-            }
-            
-            parentNode.setProperty(jcrName, values);
         }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to update the collection field : " + collectionDescriptor.getFieldName()
-                    + "for " + collectionDescriptor.getElementClassName(), e);
+
+        if (collection == null) {
+            return;
         }
 
+
+        // Add all collection element into an Value array
+        Value[] values = new Value[collection.getSize()];
+        ValueFactory valueFactory = session.getValueFactory();
+        int i = 0; 
+        for (Iterator collectionIterator = collection.getIterator(); collectionIterator.hasNext(); i++) {
+            Object fieldValue = collectionIterator.next();
+            AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters
+                .get(fieldValue.getClass());
+            values[i] = atomicTypeConverter.getValue(valueFactory, fieldValue);
+        }
+
+        parentNode.setProperty(jcrName, values);
     }
 
     /**
-     * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#getCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, java.lang.Class)
+     * @see AbstractCollectionConverterImpl#doGetCollection(Session, Node, CollectionDescriptor, Class)
      */
-    public ManageableCollection getCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor, Class collectionFieldClass) 
-    {
-        try
-        {
-            String jcrName = collectionDescriptor.getJcrName();
-            if( ! parentNode.hasProperty(jcrName))
-            {
-            	return null;
+    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 elementClassName = collectionDescriptor.getElementClassName();
-            	AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters.get(Class.forName(elementClassName));
-                collection.addObject(atomicTypeConverter.getObject(values[i]));	
+            String elementClassName = collectionDescriptor.getElementClassName();
+            Class elementClass = ReflectionUtils.forName(elementClassName);
+            for (int i = 0; i < values.length; i++) {
+                AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters
+                    .get(elementClass);
+                collection.addObject(atomicTypeConverter.getObject(values[i]));
             }
-            
+
             return collection;
-            
         }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to get the collection field : " + collectionDescriptor.getFieldName()
-                    + "for " + collectionDescriptor.getElementClassName(), e);
+        catch(ValueFormatException vfe) {
+          throw new PersistenceException("Cannot get the collection field : "
+                  + collectionDescriptor.getFieldName()
+                  + "for class " + collectionDescriptor.getClassDescriptor().getClassName(),
+                  vfe);
         }
     }
-
-}
+}
\ No newline at end of file

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java?rev=376131&r1=376130&r2=376131&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java Wed Feb  8 16:44:25 2006
@@ -1,12 +1,12 @@
 /*
  * Copyright 2000-2005 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.
@@ -16,290 +16,254 @@
 
 package org.apache.portals.graffito.jcr.persistence.collectionconverter.impl;
 
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.ValueFormatException;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.version.VersionException;
 
-import org.apache.commons.beanutils.PropertyUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-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.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/convertion based on node type.
- * 
+ *
  * This collection mapping strategy maps a collection into several nodes based on specific node type.
- * 
- * 
+ *
+ *
  * 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 element node name is a simple indexed constant.
- * 
- * Example - without an id attribute:                 
+ *
+ * Example - without an id attribute:
  *   /test (Main object containing the collection field )
  *          /collection-element1 (node used to store the first collection element)
- *                /item-prop       
+ *                /item-prop
  *                ....
- *          /collection-element2 (node used to store the second collection element) 
+ *          /collection-element2 (node used to store the second collection element)
  *          ...
- *          
- * Example - with an id attribute:                 
- *   /test (Main object containing the collection field ) 
+ *
+ * Example - with an id attribute:
+ *   /test (Main object containing the collection field )
  *          /aValue (id value assigned to the first element)
- *                /item-prop       
+ *                /item-prop
  *                ....
- *          /anotherValue (id value assigned to the first element) 
+ *          /anotherValue (id value assigned to the first element)
  *          ...
  *
  * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
- * 
+ *
  */
-public class NTCollectionConverterImpl extends AbstractCollectionConverterImpl
-{
+public class NTCollectionConverterImpl extends AbstractCollectionConverterImpl {
+
+    private final static Log log = LogFactory.getLog(NTCollectionConverterImpl.class);
 
-	private final static Log log = LogFactory.getLog(NTCollectionConverterImpl.class);
-	
     private static final String COLLECTION_ELEMENT_NAME = "collection-element";
 
     /**
      * Constructor
-     * 
+     *
      * @param atomicTypeConverters
      * @param objectConverter
      * @param mapper
      */
-    public NTCollectionConverterImpl(Map atomicTypeConverters, ObjectConverter objectConverter, Mapper mapper)
-    {
+    public NTCollectionConverterImpl(Map atomicTypeConverters,
+                                     ObjectConverter objectConverter,
+                                     Mapper mapper) {
         super(atomicTypeConverters, objectConverter, mapper);
     }
 
     /**
-     * 
-     * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#insertCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection)
+     * @see AbstractCollectionConverterImpl#doInsertCollection(Session, Node, CollectionDescriptor, ManageableCollection)
      */
-    public void insertCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor, ManageableCollection collection)
-    {
-
-        try
-        {
+    protected void doInsertCollection(Session session,
+                                      Node parentNode,
+                                      CollectionDescriptor collectionDescriptor,
+                                      ManageableCollection collection) {
+        if (collection == null) {
+            return;
+        }
 
-            if (collection == null)
-            {                
-                return;
+        Mapper mapper = collectionDescriptor.getClassDescriptor().getMappingDescriptor().getMapper();
+        ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(
+                ReflectionUtils.forName(collectionDescriptor.getElementClassName()));
+
+        Iterator collectionIterator = collection.getIterator();
+        int elementCollectionCount = 0;
+        while (collectionIterator.hasNext()) {
+            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();
+                elementJcrName = ReflectionUtils.getNestedProperty(item, idFieldName).toString();
             }
-
-
-            Iterator collectionIterator = collection.getIterator();
-            ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor
-                    .getElementClassName()));
-
-            int elementCollectionCount = 0;
-            while (collectionIterator.hasNext())
-            {
-                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();
-                    elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
-                }
-                else
-                {
-
-                    elementCollectionCount++;
-                    elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
-                }
-
-                objectConverter.insert(session, parentNode, elementJcrName, item);
+            else {
+                elementCollectionCount++;
+                elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
             }
-        }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to insert the collection field : " + collectionDescriptor.getFieldName()
-                    + "for " + collectionDescriptor.getElementClassName(), e);
-        }
 
+            objectConverter.insert(session, parentNode, elementJcrName, item);
+        }
     }
 
     /**
-     * 
+     *
      * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#updateCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection)
      */
-    public void updateCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor, ManageableCollection collection)
-    {
-        try
-        {
-
-            ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor
-                    .getElementClassName()));
-            
-            if (collection == null)
-            {
-                this.deleteCollectionItems(session, parentNode, elementClassDescriptor.getJcrNodeType());
-                return;
-            }
-
-            if (!elementClassDescriptor.hasIdField())
-            {
-               this.deleteCollectionItems(session, parentNode, elementClassDescriptor.getJcrNodeType());
-            }
-            
-            Iterator collectionIterator = collection.getIterator();
-            int elementCollectionCount = 0;
-
-            HashMap updatedItems = new HashMap();
-            while (collectionIterator.hasNext())
-            {
-                Object item = collectionIterator.next();
-                
-                elementCollectionCount++;
-                String elementJcrName = null;
-
-                if (elementClassDescriptor.hasIdField())
-                {
+    protected void doUpdateCollection(Session session,
+                                      Node parentNode,
+                                      CollectionDescriptor collectionDescriptor,
+                                      ManageableCollection collection) throws RepositoryException {
+        Mapper mapper = collectionDescriptor.getClassDescriptor().getMappingDescriptor().getMapper();
+        ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(
+                ReflectionUtils.forName(collectionDescriptor.getElementClassName()));
+
+        if (collection == null || !elementClassDescriptor.hasIdField()) {
+            this.deleteCollectionItems(session,
+                                       parentNode,
+                                       elementClassDescriptor.getJcrNodeType());
+        }
 
-                    String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
-                    elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
+        if (collection == null) {
+            return;
+        }
 
-                    // Update existing JCR Nodes
-                    if (parentNode.hasNode(elementJcrName))
-                    {
-                        objectConverter.update(session, parentNode, elementJcrName, item);
-                    }
-                    else
-                    {
-                        // Add new collection elements
-                        objectConverter.insert(session, parentNode, elementJcrName, item);
-                    }
-                    
-                    updatedItems.put(elementJcrName, item);
+        Iterator collectionIterator = collection.getIterator();
+        int elementCollectionCount = 0;
 
+        Map updatedItems = new HashMap();
+        while (collectionIterator.hasNext()) {
+            Object item = collectionIterator.next();
+
+            elementCollectionCount++;
+            String elementJcrName = null;
+
+            if (elementClassDescriptor.hasIdField()) {
+                String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
+                elementJcrName = ReflectionUtils.getNestedProperty(item, idFieldName).toString();
+
+                // Update existing JCR Nodes
+                if (parentNode.hasNode(elementJcrName)) {
+                    objectConverter.update(session, parentNode, elementJcrName, item);
                 }
-                else
-                {
-                    
-                    elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
+                else {
+                    // Add new collection elements
                     objectConverter.insert(session, parentNode, elementJcrName, item);
                 }
 
+                updatedItems.put(elementJcrName, item);
+            }
+            else {
+                elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
+                objectConverter.insert(session, parentNode, elementJcrName, item);
             }
+        }
 
-            // Delete JCR nodes that are not present in the collection
-            if (elementClassDescriptor.hasIdField())
-            {
-                Iterator nodeIterator = this.getCollectionNodes(session, parentNode, elementClassDescriptor.getJcrNodeType()).iterator();
-                while (nodeIterator.hasNext())
-                {
-                    Node child = (Node) nodeIterator.next();
-                    if (! updatedItems.containsKey(child.getName()))
-                    {
-                        child.remove();
-                    }
+        // Delete JCR nodes that are not present in the collection
+        if (elementClassDescriptor.hasIdField()) {
+            Iterator nodeIterator = this.getCollectionNodes(session, parentNode,
+                    elementClassDescriptor.getJcrNodeType()).iterator();
+
+            while (nodeIterator.hasNext()) {
+                Node child = (Node) nodeIterator.next();
+                
+                if (!updatedItems.containsKey(child.getName())) {
+                    child.remove();
                 }
             }
         }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to update the collection field : " + collectionDescriptor.getFieldName()
-                    + "for " + collectionDescriptor.getElementClassName(), e);
-        }
-
     }
 
     /**
      * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#getCollection(javax.jcr.Session, javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, java.lang.Class)
      */
-    public ManageableCollection getCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor, Class collectionFieldClass)
-    {
-        try
-        {
-            ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor
-                    .getElementClassName()));                        
-            Iterator children = this.getCollectionNodes(session, parentNode, elementClassDescriptor.getJcrNodeType()).iterator();
-
-            ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass);
-
-            while (children.hasNext())
-            {
-                Node itemNode = (Node) children.next();
-                log.debug("Collection node found : " + itemNode.getPath());
-                Object item = objectConverter.getObject(session, Class.forName(collectionDescriptor.getElementClassName()),
-                        itemNode.getPath());
-                collection.addObject(item);
-            }
-
-            return collection;
-        }
-        catch (Exception e)
-        {
-            throw new PersistenceException("Impossible to get the collection field : " + collectionDescriptor.getFieldName()
-                    + "for " + collectionDescriptor.getElementClassName(), e);
+    protected ManageableCollection doGetCollection(Session session,
+                                                   Node parentNode,
+                                                   CollectionDescriptor collectionDescriptor,
+                                                   Class collectionFieldClass) throws RepositoryException {
+        Mapper mapper = collectionDescriptor.getClassDescriptor().getMappingDescriptor().getMapper();
+        ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(
+                ReflectionUtils.forName(collectionDescriptor.getElementClassName()));
+        ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass);
+        Class elementClass = ReflectionUtils.forName(collectionDescriptor.getElementClassName());
+        Iterator children = this.getCollectionNodes(session, parentNode,
+                elementClassDescriptor.getJcrNodeType()).iterator();
+
+        while (children.hasNext()) {
+            Node itemNode = (Node) children.next();
+            log.debug("Collection node found : " + itemNode.getPath());
+            Object item = objectConverter.getObject(session, elementClass, itemNode.getPath());
+            collection.addObject(item);
         }
+
+        return collection;
     }
 
-    private Collection getCollectionNodes (Session session, Node parentNode, String itemNodeType) throws Exception
-    {
-    	  
-           ArrayList collectionNodes = new ArrayList();
+    private Collection getCollectionNodes(Session session, Node parentNode, String itemNodeType)
+    throws PathNotFoundException, ValueFormatException, RepositoryException {
+
+        List collectionNodes = new ArrayList();
+
+        // TODO : review this workaround used to support version nodes
+        // Searching on the version storage has some bugs => loop on all child noded and check the property jcr:frozenPrimaryType
+        // I have to investigate in more detail what's happen exactly
+        if (!parentNode.getPath().startsWith("/jcr:system/jcr:versionStorage")) {
+            NodeIterator nodeIterator = parentNode.getNodes();
+            while (nodeIterator.hasNext()) {
+                Node child = nodeIterator.nextNode();
+
+                if (child.isNodeType(itemNodeType)) {
+                    collectionNodes.add(child);
+                }
+            }
+        }
+        else {
+            NodeIterator nodeIterator = parentNode.getNodes();
+            while (nodeIterator.hasNext()) {
+                Node child = nodeIterator.nextNode();
 
-           // TODO : review this workaround used to support version nodes  
-           // Searching on the version storage has some bugs => loop on all child noded and check the property jcr:frozenPrimaryType
-           // I have to investigate in more detail what's happen exactly  
-    	   if (! parentNode.getPath().startsWith("/jcr:system/jcr:versionStorage"))
-    	   {               
-               NodeIterator nodeIterator = parentNode.getNodes();
-               while (nodeIterator.hasNext())
-               {        	   
-            	   Node child = nodeIterator.nextNode();
-
-            	   if (child.isNodeType(itemNodeType))
-            	   {
-                       collectionNodes.add(child);
-            	   }
-               }    		   
-    	   }
-    	   else
-    	   {
-    		                  
-               NodeIterator nodeIterator = parentNode.getNodes();
-               while (nodeIterator.hasNext())
-               {        	   
-            	   Node child = nodeIterator.nextNode();
-
-            	   if (child.getProperty("jcr:frozenPrimaryType").getString().equals(itemNodeType))
-            	   {
-                       collectionNodes.add(child);
-            	   }
-               } 
-    		   
-    	   }
-    	   
-           return collectionNodes;
-           
+                if (child.getProperty("jcr:frozenPrimaryType").getString().equals(itemNodeType)) {
+                    collectionNodes.add(child);
+                }
+            }
+
+        }
+
+        return collectionNodes;
     }
-    
-    private void deleteCollectionItems(Session session, Node parentNode, String itemNodeType) throws Exception
+
+    private void deleteCollectionItems(Session session, Node parentNode, String itemNodeType) 
+    throws VersionException, 
+           LockException, 
+           ConstraintViolationException, 
+           PathNotFoundException, 
+           ValueFormatException, 
+           RepositoryException
     {
-           Iterator nodeIterator = this.getCollectionNodes(session, parentNode, itemNodeType).iterator(); 
-           while (nodeIterator.hasNext())
-           {
-               Node node = (Node) nodeIterator.next();
-               node.remove();
-           }
+        Iterator nodeIterator = this.getCollectionNodes(session, parentNode, itemNodeType).iterator();
+        while (nodeIterator.hasNext()) {
+            Node node = (Node) nodeIterator.next();
+            node.remove();
+        }
     }
-    
-    
 }



Mime
View raw message