incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r290785 - /incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java
Date Wed, 21 Sep 2005 18:46:31 GMT
Author: clombart
Date: Wed Sep 21 11:46:27 2005
New Revision: 290785

URL: http://svn.apache.org/viewcvs?rev=290785&view=rev
Log:
review code format

Modified:
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java?rev=290785&r1=290784&r2=290785&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java
Wed Sep 21 11:46:27 2005
@@ -16,6 +16,7 @@
 
 package org.apache.portals.graffito.jcr.persistence.collectionconverter.impl;
 
+import java.util.HashMap;
 import java.util.Iterator;
 
 import javax.jcr.Node;
@@ -65,7 +66,6 @@
  */
 public class DefaultCollectionConverterImpl extends AbstractCollectionConverterImpl implements
CollectionConverter
 {
-        
 
     private static final String COLLECTION_ELEMENT_NAME = "collection-element";
 
@@ -78,17 +78,18 @@
      * 
      * @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) throws JcrMappingException
+    public void insertCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
+            ManageableCollection collection) throws JcrMappingException
     {
 
         try
         {
-            
+
             if (collection == null)
             {
-                return ;
+                return;
             }
-            
+
             String jcrName = collectionDescriptor.getJcrName();
 
             if (jcrName == null)
@@ -100,7 +101,8 @@
             Node collectionNode = parentNode.addNode(jcrName);
 
             Iterator collectionIterator = collection.getIterator();
-            ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor.getElementClassName()));
+            ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor
+                    .getElementClassName()));
 
             int elementCollectionCount = 0;
             while (collectionIterator.hasNext())
@@ -111,8 +113,8 @@
                 // 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();
+                    String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
+                    elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
                 }
                 else
                 {
@@ -132,113 +134,141 @@
 
     }
 
-
     /**
      * 
      * @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) throws JcrMappingException
+    public void updateCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
+            ManageableCollection collection) throws JcrMappingException
     {
         try
         {
-            
+
             String jcrName = collectionDescriptor.getJcrName();
-                                       
+
             if (jcrName == null)
             {
-                throw new JcrMappingException("The JcrName attribute is not defined for the
CollectionDescriptor : " + collectionDescriptor.getCollectionConverterClassName());
+                throw new JcrMappingException("The JcrName attribute is not defined for the
CollectionDescriptor : "
+                        + collectionDescriptor.getCollectionConverterClassName());
             }
-            
+
             if (collection == null)
             {
                 if (parentNode.hasNode(jcrName))
                 {
                     parentNode.getNode(jcrName).remove();
-                    
+
                 }
                 return;
             }
-            
+
             Node collectionNode = parentNode.getNode(jcrName);
-            ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor.getElementClassName()));
+            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())
+            if (!elementClassDescriptor.hasIdField())
             {
-               collectionNode.remove();
-               collectionNode = parentNode.addNode(jcrName);
+                collectionNode.remove();
+                collectionNode = parentNode.addNode(jcrName);
             }
-            
-            while(collectionIterator.hasNext())
+
+            HashMap updatedItems = new HashMap();
+            while (collectionIterator.hasNext())
             {
                 Object item = collectionIterator.next();
-                elementCollectionCount++;  
-                String elementJcrName = null;
                 
+                elementCollectionCount++;
+                String elementJcrName = null;
+
                 if (elementClassDescriptor.hasIdField())
                 {
-                      // Update existing JCR Node
-                      String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
-                      elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
-                      objectConverter.updateNode(session, collectionNode, elementJcrName,
item);
-                      
-                      // Delete JCR nodes that are not present in the collection
-                      
-                      // Add new collection elements
+
+                    String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
+                    elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
+
+                    // Update existing JCR Nodes
+                    if (collectionNode.hasNode(elementJcrName))
+                    {
+                        objectConverter.updateNode(session, collectionNode, elementJcrName,
item);
+                    }
+                    else
+                    {
+                        // Add new collection elements
+                        objectConverter.insertNode(session, collectionNode, elementJcrName,
item);
+                    }
+                    
+                    updatedItems.put(elementJcrName, item);
+
                 }
                 else
                 {
-                    
+
                     elementCollectionCount++;
                     elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
                     objectConverter.insertNode(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();
+                    }
+                }
             }
         }
         catch (Exception e)
         {
-            throw new JcrMappingException("Impossible to update the collection field : "
+ collectionDescriptor.getFieldName() + "for " + collectionDescriptor.getElementClassName(),
e);            
+            throw new JcrMappingException("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) throws JcrMappingException
-    {        
+    public ManageableCollection getCollection(Session session, Node parentNode, CollectionDescriptor
collectionDescriptor,
+            Class collectionFieldClass) throws JcrMappingException
+    {
         try
         {
-            String jcrName = collectionDescriptor.getJcrName();            
-            if (! parentNode.hasNode(jcrName))
+            String jcrName = collectionDescriptor.getJcrName();
+            if (!parentNode.hasNode(jcrName))
             {
                 return null;
             }
-            
+
             Node collectionNode = parentNode.getNode(jcrName);
             NodeIterator children = collectionNode.getNodes();
-                        
-            ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass);
                
-            
+
+            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);                    
+                Object item = objectConverter.getObject(session, Class.forName(collectionDescriptor.getElementClassName()),
+                        itemNode.getPath());
+                collection.addObject(item);
             }
-            
+
             return collection;
         }
         catch (Exception e)
         {
-            throw new JcrMappingException("Impossible to get the collection field : " + collectionDescriptor.getFieldName()
+ "for " + collectionDescriptor.getElementClassName(), e);
+            throw new JcrMappingException("Impossible to get the collection field : " + collectionDescriptor.getFieldName()
+                    + "for " + collectionDescriptor.getElementClassName(), e);
         }
     }
-    
 
 }



Mime
View raw message