incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r279633 - in /incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter: CollectionConverter.java impl/AbstractCollectionConverterImpl.java impl/DefaultCollectionConverterImpl.java
Date Thu, 08 Sep 2005 21:27:51 GMT
Author: clombart
Date: Thu Sep  8 14:27:46 2005
New Revision: 279633

URL: http://svn.apache.org/viewcvs?rev=279633&view=rev
Log:
Work on the CollectionDescriptor. 

Modified:
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/CollectionConverter.java
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java
    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/CollectionConverter.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/CollectionConverter.java?rev=279633&r1=279632&r2=279633&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/CollectionConverter.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/CollectionConverter.java
Thu Sep  8 14:27:46 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2000-2004 The Apache Software Foundation.
+ * 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.
@@ -15,8 +15,6 @@
  */
 package org.apache.portals.graffito.jcr.persistence.collectionconverter;
 
-import java.util.Collection;
-
 import javax.jcr.Node;
 import javax.jcr.Session;
 
@@ -24,21 +22,18 @@
 import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
 
 /**
- * Convert any kind of Collection into severals JCR nodes
+ * Convert any kind of {@link ManageableCollection} into severals JCR nodes.
  * 
  * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe
</a>
- * @version $Id: Exp $
+ * 
  */
 public interface CollectionConverter
 {
 
-    public void insertCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
-            Object object) throws JcrMappingException;
+    public void insertCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
ManageableCollection collection) throws JcrMappingException;
 
-    public void updateCollection(Session session, Node objectNode, CollectionDescriptor collectionDescriptor,
-            Object object) throws JcrMappingException;
+    public void updateCollection(Session session, Node objectNode, CollectionDescriptor collectionDescriptor,
ManageableCollection collection) throws JcrMappingException;
 
-    public Collection getCollection(Session session, Node parentNode, CollectionDescriptor
collectionDescriptor,
-            Class collectionFieldClass) throws JcrMappingException;
+    public ManageableCollection getCollection(Session session, Node parentNode, CollectionDescriptor
collectionDescriptor, Class collectionFieldClass) throws JcrMappingException;
 
 }

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java?rev=279633&r1=279632&r2=279633&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java
Thu Sep  8 14:27:46 2005
@@ -16,6 +16,7 @@
 
 package org.apache.portals.graffito.jcr.persistence.collectionconverter.impl;
 
+import org.apache.portals.graffito.jcr.mapper.Mapper;
 import org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter;
 
 /** 
@@ -26,10 +27,13 @@
 public abstract class AbstractCollectionConverterImpl
 {
     protected ObjectConverter objectConverter;
+    protected Mapper mapper;
     
-    public AbstractCollectionConverterImpl(ObjectConverter objectConverter)
+    
+    public AbstractCollectionConverterImpl(ObjectConverter objectConverter, Mapper mapper)
     {
         this.objectConverter = objectConverter;
+        this.mapper = mapper;
     }
 
 

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=279633&r1=279632&r2=279633&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
Thu Sep  8 14:27:46 2005
@@ -16,11 +16,7 @@
 
 package org.apache.portals.graffito.jcr.persistence.collectionconverter.impl;
 
-import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Iterator;
-import java.util.List;
-import java.util.Vector;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
@@ -28,12 +24,42 @@
 
 import org.apache.commons.beanutils.PropertyUtils;
 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.CollectionConverter;
+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;
 
 /** 
- *
+ * Default Collection Mapping/convertion implementation.
+ * 
+ * This collection mapping strategy maps a collection under an extra JCR node (specify by
the jcrName).
+ * It can be usefull when the node type "nt:unstructured" is used for 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 name is a simple indexed constant.
+ * 
+ * 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       
+ *                ....
+ *          /collection-element2 (node used to store the second collection element) 
+ *          ...
+ *          
+ * 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       
+ *                ....
+ *          /anotherValue (id value assigned to the first element) 
+ *          ...
+
  * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
  * 
  */
@@ -41,86 +67,106 @@
 {
         
 
-    public DefaultCollectionConverterImpl(ObjectConverter objectConverter)
+    private static final String COLLECTION_ELEMENT_NAME = "collection-element";
+
+    public DefaultCollectionConverterImpl(ObjectConverter objectConverter, Mapper mapper)
     {
-        super(objectConverter);
+        super(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, java.lang.Object)
+     * 
+     * @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,
Object object) throws JcrMappingException
+    public void insertCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
ManageableCollection collection) throws JcrMappingException
     {
-        
+
         try
         {
             String jcrName = collectionDescriptor.getJcrName();
-            String fieldIdName = collectionDescriptor.getFieldId();
-            Node collectionNode = null;
-            
-            // If a jcrName is defined for this collection : add or get the node used for
the collection                
-            if (jcrName != null)
-            {
-                collectionNode = parentNode.addNode(jcrName);
-            }
-            else
+
+            if (jcrName == null)
             {
-                collectionNode = parentNode; 
+                throw new JcrMappingException("The JcrName attribute is not defined for the
CollectionDescriptor : "
+                        + collectionDescriptor.getCollectionConverterClassName());
             }
-            
-           
-            Collection collection = (Collection) PropertyUtils.getNestedProperty(object,
collectionDescriptor.getFieldName());
-            
-            Iterator collectionIterator = collection.iterator();
-            while(collectionIterator.hasNext())
+
+            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 jcrNameCollection = (String) PropertyUtils.getNestedProperty(item,
fieldIdName);
-                objectConverter.insertNode(session, collectionNode, jcrNameCollection, item);
   
+                String elementJcrName = null;
+
+                if (elementClassDescriptor.hasIdField())
+                {
+                      String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
+                      elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
+                }
+                else
+                {
+
+                    elementCollectionCount++;
+                    elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
+                }
+
+                objectConverter.insertNode(session, collectionNode, elementJcrName, item);
             }
         }
         catch (Exception e)
         {
-            throw new JcrMappingException("Impossible to insert the collection field : "
+ collectionDescriptor.getFieldName() + "for " + collectionDescriptor.getClassName(), e);
           
+            throw new JcrMappingException("Impossible to insert the collection field : "
+ collectionDescriptor.getFieldName()
+                    + "for " + collectionDescriptor.getElementClassName(), e);
         }
-        
+
     }
 
+
     /**
-     * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter#updateCollection(javax.jcr.Session,
javax.jcr.Node, org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor, java.lang.Object)
+     * 
+     * @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 objectNode, CollectionDescriptor collectionDescriptor,
Object object) throws JcrMappingException
+    public void updateCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
ManageableCollection collection) throws JcrMappingException
     {
         try
         {
             String jcrName = collectionDescriptor.getJcrName();
-            String fieldIdName = collectionDescriptor.getFieldId();
-            Node collectionNode = null;
-            
-            // If a jcrName is defined for this collection : add or get the node used for
the collection                
-            if (jcrName != null)
-            {
-                collectionNode = objectNode.getNode(jcrName);
-            }
-            else
+                                       
+            if (jcrName == null)
             {
-                collectionNode = objectNode; 
+                throw new JcrMappingException("The JcrName attribute is not defined for the
CollectionDescriptor : " + collectionDescriptor.getCollectionConverterClassName());
             }
             
-            //TODO : manage null value for the collection
-            Collection collection = (Collection) PropertyUtils.getNestedProperty(object,
collectionDescriptor.getFieldName());
-            
-            Iterator collectionIterator = collection.iterator();
+            Node collectionNode = parentNode.getNode(jcrName);
+            ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor.getElementClassName()));
+            Iterator collectionIterator = collection.getIterator();
+            int elementCollectionCount = 0;
             while(collectionIterator.hasNext())
             {
                 Object item = collectionIterator.next();
-                String jcrNameCollection = (String) PropertyUtils.getNestedProperty(item,
fieldIdName);
-                objectConverter.updateNode(session, collectionNode, jcrNameCollection, item);
   
+                elementCollectionCount++;  
+                String elementJcrName = null;
+                if (elementClassDescriptor.hasIdField())
+                {
+                      String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
+                      elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
+                }
+                else
+                {
+
+                    elementCollectionCount++;
+                    elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
+                }                
+                objectConverter.updateNode(session, collectionNode, elementJcrName, item);
   
             }
         }
         catch (Exception e)
         {
-            throw new JcrMappingException("Impossible to update the collection field : "
+ collectionDescriptor.getFieldName() + "for " + collectionDescriptor.getClassName(), e);
           
+            throw new JcrMappingException("Impossible to update the collection field : "
+ collectionDescriptor.getFieldName() + "for " + collectionDescriptor.getElementClassName(),
e);            
         }
         
         
@@ -130,9 +176,8 @@
     /**
      * @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 Collection 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();            
@@ -140,41 +185,22 @@
             Node collectionNode = parentNode.getNode(jcrName);
             NodeIterator children = collectionNode.getNodes();
                         
-            Collection collection = this.createCollectionObject(collectionFieldClass);  
              
+            ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass);
                
             
             while (children.hasNext())
             {
                 Node itemNode = children.nextNode();
-                Object item = objectConverter.getObject(session, Class.forName(collectionDescriptor.getClassName()),
itemNode.getPath());                                
-                collection.add(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.getClassName(), e);
+            throw new JcrMappingException("Impossible to get the collection field : " + collectionDescriptor.getFieldName()
+ "for " + collectionDescriptor.getElementClassName(), e);
         }
     }
     
-    private Collection createCollectionObject(Class collectionFieldClass) throws JcrMappingException
-    {
-        if (collectionFieldClass.equals(ArrayList.class))
-        {
-            return new ArrayList();
-        }
-        
-        if (collectionFieldClass.equals(Vector.class))
-        {
-            return new Vector();
-        }        
-        
-        if (collectionFieldClass.equals(Collection.class) || collectionFieldClass.equals(List.class))
-        {
-            return new ArrayList();
-        }
-        
-        throw new JcrMappingException("Unsupported Collection Type : " + collectionFieldClass.getName());

-    }
 
 }



Mime
View raw message