incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r239448 - in /incubator/graffito/trunk/jcr-mapping/src: java/org/apache/portals/graffito/jcr/converter/impl/ java/org/apache/portals/graffito/jcr/mapper/ java/org/apache/portals/graffito/jcr/mapper/model/ java/org/apache/portals/graffito/jc...
Date Tue, 23 Aug 2005 19:30:38 GMT
Author: clombart
Date: Tue Aug 23 12:30:22 2005
New Revision: 239448

URL: http://svn.apache.org/viewcvs?rev=239448&view=rev
Log:
Add in the jcr-mapping prototype the collection management

Added:
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/CollectionDescriptor.java
    incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/C.java
Modified:
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/converter/impl/GenericConverter.java
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/DescriptorReader.java
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/ClassDescriptor.java
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/session/impl/JcrSessionImpl.java
    incubator/graffito/trunk/jcr-mapping/src/test/jcrmapping.xml
    incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/mapper/MapperTest.java
    incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/session/impl/JcrSessionTest.java
    incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/A.java

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/converter/impl/GenericConverter.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/converter/impl/GenericConverter.java?rev=239448&r1=239447&r2=239448&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/converter/impl/GenericConverter.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/converter/impl/GenericConverter.java
Tue Aug 23 12:30:22 2005
@@ -15,9 +15,12 @@
  */
 package org.apache.portals.graffito.jcr.converter.impl;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
 
 import javax.jcr.Node;
+import javax.jcr.NodeIterator;
 import javax.jcr.Property;
 import javax.jcr.Session;
 import javax.jcr.Value;
@@ -26,12 +29,14 @@
 import org.apache.portals.graffito.jcr.converter.Converter;
 import org.apache.portals.graffito.jcr.exception.JcrMappingException;
 import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.BeanDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.FieldDescriptor;
 import org.apache.portals.graffito.jcr.repository.RepositoryUtil;
 
 /**
- * Convert any kind of pojo by using the xml mapping file
+ * Convert any kind of bean by using the xml mapping file
  * 
  * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe
</a>
  * @version $Id: Exp $
@@ -39,115 +44,256 @@
 public class GenericConverter implements Converter
 {
     private Mapper mapper;
-    
+
     public GenericConverter(Mapper mapper)
     {
         this.mapper = mapper;
     }
 
-
     /**
      * 
      * @see org.apache.portals.graffito.jcr.converter.Converter#insert(javax.jcr.Session,
java.lang.String, java.lang.Object)
      */
-    public void insert(Session session, String path, Object object)  throws JcrMappingException
+    public void insert(Session session, String path, Object object) throws JcrMappingException
     {
-        try 
+        try
         {
-           ClassDescriptor classDescriptor = mapper.getClassDescriptor(object.getClass());
-              
-           String parentPath = RepositoryUtil.getParentPath(path);
-           String nodeName = RepositoryUtil.getNodeName(path);
-           Node parentNode = (Node) session.getItem(parentPath);
-           
-           String jcrNodeType = classDescriptor.getJcrNodeType();
-           if (jcrNodeType == null || jcrNodeType.equals(""))
-           {
-               throw new JcrMappingException("Undefined node type for  " + path);
-           }
-           
-           Node objectNode = parentNode.addNode(nodeName, jcrNodeType);
-           
-           Iterator fieldDescriptorIterator = classDescriptor.getFieldDescriptors().iterator();
-           while (fieldDescriptorIterator.hasNext())
-           {
-               FieldDescriptor fieldDescriptor = (FieldDescriptor) fieldDescriptorIterator.next();
-               Object fieldValue = PropertyUtils.getNestedProperty(object, fieldDescriptor.getFieldName());
-               Value value = RepositoryUtil.getValue(fieldValue);               
-               objectNode.setProperty(fieldDescriptor.getFieldName(), value);    
-           }
-                      
-           parentNode.save();
-            
+            String parentPath = RepositoryUtil.getParentPath(path);
+            String nodeName = RepositoryUtil.getNodeName(path);
+            Node parentNode = (Node) session.getItem(parentPath);
+            this.addNode(session, parentNode, nodeName, object, false);
+            parentNode.save();
+
         }
-        catch(Exception e)
+        catch (Exception e)
         {
             throw new JcrMappingException("Impossible to store the object at " + path, e);
         }
 
     }
-    
+
     /**
      * 
      * @see org.apache.portals.graffito.jcr.converter.Converter#update(javax.jcr.Session,
java.lang.String, java.lang.Object)
      */
     public void update(Session session, String path, Object object) throws JcrMappingException
     {
-//        try 
-//        {
-//           Folder folder = (Folder) object; 
-//           Node folderNode = (Node) session.getItem(path);               
-//           folderNode.setProperty("graffito:description", new StringValue(folder.getDescription()));
-//           folderNode.setProperty("graffito:title", new StringValue(folder.getTitle()));
-//           
-//           Calendar calendar = Calendar.getInstance();
-//           calendar.setTime(folder.getLastModified());
-//           folderNode.setProperty("graffito:lastmodified", new DateValue(calendar));
-//           
-//           folderNode.save();
-//            
-//        }
-//        catch(Exception e)
-//        {
-//            e.printStackTrace();
-//            throw new JcrMappingException("Impossible to store the object at " + path,
e);
-//        }
+        try
+        {
+            String parentPath = RepositoryUtil.getParentPath(path);
+            String nodeName = RepositoryUtil.getNodeName(path);
+            Node parentNode = (Node) session.getItem(parentPath);
+            this.addNode(session, parentNode, nodeName, object, true);
+            parentNode.save();
+
+        }
+        catch (Exception e)
+        {
+            throw new JcrMappingException("Impossible to store the object at " + path, e);
+        }
     }
-    
 
     /**
      * 
      * @see org.apache.portals.graffito.jcr.converter.Converter#getObject(javax.jcr.Session,
java.lang.Class, java.lang.String)
      */
-    public Object getObject(Session session, Class clazz,  String path)  throws JcrMappingException
+    public Object getObject(Session session, Class clazz, String path) throws JcrMappingException
     {
-        
-        try 
+
+        try
         {
-           ClassDescriptor classDescriptor = mapper.getClassDescriptor(clazz); 
-               
-           Node node = (Node) session.getItem(path);
-           Object object = clazz.newInstance();
-           
-           Iterator fieldDescriptorIterator = classDescriptor.getFieldDescriptors().iterator();
-           while (fieldDescriptorIterator.hasNext())
-           {
-               FieldDescriptor fieldDescriptor = (FieldDescriptor) fieldDescriptorIterator.next();
-               String propertyName = fieldDescriptor.getJcrName();
-               Property property = node.getProperty(propertyName);
-               Value value = property.getValue();
-               Object fieldValue = RepositoryUtil.getObject(value);
-               String fieldName = fieldDescriptor.getFieldName();               
-               PropertyUtils.setNestedProperty(object, fieldName, fieldValue);
-           }           
-           
-           return object;
-           
+            ClassDescriptor classDescriptor = mapper.getClassDescriptor(clazz);
+
+            Node node = (Node) session.getItem(path);
+            Object object = clazz.newInstance();
+
+            Iterator fieldDescriptorIterator = classDescriptor.getFieldDescriptors().iterator();
+            while (fieldDescriptorIterator.hasNext())
+            {
+                FieldDescriptor fieldDescriptor = (FieldDescriptor) fieldDescriptorIterator.next();
+                String propertyName = fieldDescriptor.getJcrName();
+                Property property = node.getProperty(propertyName);
+                Value value = property.getValue();
+                Object fieldValue = RepositoryUtil.getObject(value);
+                String fieldName = fieldDescriptor.getFieldName();
+                PropertyUtils.setNestedProperty(object, fieldName, fieldValue);
+            }
+
+            Iterator beanDescriptorIterator = classDescriptor.getBeanDescriptors().iterator();
+            while (beanDescriptorIterator.hasNext())
+            {
+                BeanDescriptor beanDescriptor = (BeanDescriptor) beanDescriptorIterator.next();
+                String beanName = beanDescriptor.getFieldName();
+                Class beanClass = PropertyUtils.getPropertyDescriptor(object, beanName).getPropertyType();
+                Object bean = this.getObject(session, beanClass, path + "/" + beanDescriptor.getJcrName());
+                PropertyUtils.setNestedProperty(object, beanName, bean);
+            }
+
+            Iterator collectionDescriptorIterator = classDescriptor.getCollectionDescriptors().iterator();
+            while(collectionDescriptorIterator.hasNext())
+            {
+                CollectionDescriptor collectionDescriptor = (CollectionDescriptor) collectionDescriptorIterator.next();
+                String jcrName = collectionDescriptor.getJcrName();
+                String fieldName = collectionDescriptor.getFieldName();
+                
+                Node collectionNode = node.getNode(jcrName);
+                NodeIterator children = collectionNode.getNodes();
+                
+                //TODO : support other collection type and Map ? 
+                Collection collection = new ArrayList();
+                while (children.hasNext())
+                {
+                    Node itemNode = children.nextNode();
+                    Object item = this.getObject(session, Class.forName(collectionDescriptor.getClassName()),
itemNode.getPath());
+                    collection.add(item);                    
+                }
+                
+                PropertyUtils.setNestedProperty(object, fieldName, collection);
+            }
+            
+            return object;
+
         }
-        catch(Exception e)
+        catch (Exception e)
         {
             throw new JcrMappingException("Impossible to get the object at " + path, e);
         }
     }
 
+    private void addNode(Session session, Node parentNode, String nodeName, Object object,
boolean toUpdate) throws JcrMappingException
+    {
+        try
+        {
+            ClassDescriptor classDescriptor = mapper.getClassDescriptor(object.getClass());
+            String jcrNodeType = classDescriptor.getJcrNodeType();
+            if (jcrNodeType == null || jcrNodeType.equals(""))
+            {
+                throw new JcrMappingException("Undefined node type for  " + parentNode);
+            }
+
+            Node objectNode = null;
+            if (toUpdate)
+            {
+                objectNode = parentNode.getNode(nodeName);
+            }
+            else
+            {
+                objectNode = parentNode.addNode(nodeName, jcrNodeType);
+            }
+            
+
+            // ---------------------------------------------------------------------------------------
+            // FieldDescriptors
+            // ---------------------------------------------------------------------------------------
+            storeSimpleFields(object, classDescriptor, objectNode);
+
+            // ---------------------------------------------------------------------------------------
+            // BeanDescriptors
+            // ---------------------------------------------------------------------------------------
+            storeBeanFields(session, object, classDescriptor, objectNode, toUpdate);
+            
+            // ---------------------------------------------------------------------------------------
+            // CollectionDescriptors
+            // ---------------------------------------------------------------------------------------
+            storeCollectionFields(session, object, classDescriptor, objectNode, toUpdate);
+            
+        }
+        catch (Exception e)
+        {
+            throw new JcrMappingException("Impossible to add the node : " + parentNode);
+        }
+
+    }
+
+    private void storeBeanFields(Session session, Object object, ClassDescriptor classDescriptor,
Node objectNode, boolean toUpdate) throws JcrMappingException
+    {
+        try
+        {
+            Iterator beanDescriptorIterator = classDescriptor.getBeanDescriptors().iterator();
+            while (beanDescriptorIterator.hasNext())
+            {
+                BeanDescriptor beanDescriptor = (BeanDescriptor) beanDescriptorIterator.next();
+                String jcrName = beanDescriptor.getJcrName();
+                Object bean = PropertyUtils.getNestedProperty(object, beanDescriptor.getFieldName());
+                this.addNode(session, objectNode, jcrName, bean, toUpdate);
+
+            }
+        }
+        catch (Exception e)
+        {
+            throw new JcrMappingException("Impossible to store the bean fields", e);
+        }
+        
+    }
+    
+    private void storeCollectionFields(Session session, Object object, ClassDescriptor classDescriptor,
Node objectNode, boolean toUpdate) throws JcrMappingException
+    {
+        try
+        {
+            Iterator collectionDescriptorIterator = classDescriptor.getCollectionDescriptors().iterator();
+            while (collectionDescriptorIterator.hasNext())
+            {
+                CollectionDescriptor collectionDescriptor = (CollectionDescriptor) collectionDescriptorIterator.next();
+                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)
+                {
+                    if (toUpdate)
+                    {
+                        collectionNode = objectNode.getNode(jcrName);
+                    }
+                    else
+                    {
+                        collectionNode = objectNode.addNode(jcrName);
+                    }
+                }
+                else
+                {
+                    collectionNode = objectNode; 
+                }
+                
+                //TODO : manage null value for the collection
+                Collection collection = (Collection) PropertyUtils.getNestedProperty(object,
collectionDescriptor.getFieldName());
+                
+                
+                Iterator collectionIterator = collection.iterator();
+                while(collectionIterator.hasNext())
+                {
+                    Object item = collectionIterator.next();
+                    String jcrNameCollection = (String) PropertyUtils.getNestedProperty(item,
fieldIdName);
+                    this.addNode(session, collectionNode, jcrNameCollection, item, toUpdate);
   
+                }
+                
+                
+
+            }
+        }
+        catch (Exception e)
+        {
+            throw new JcrMappingException("Impossible to store the bean fields", e);
+        }
+        
+    }    
+
+    private void storeSimpleFields(Object object, ClassDescriptor classDescriptor, Node objectNode)
throws JcrMappingException
+    {
+        try
+        {
+            Iterator fieldDescriptorIterator = classDescriptor.getFieldDescriptors().iterator();
+            while (fieldDescriptorIterator.hasNext())
+            {
+                FieldDescriptor fieldDescriptor = (FieldDescriptor) fieldDescriptorIterator.next();
+                Object fieldValue = PropertyUtils.getNestedProperty(object, fieldDescriptor.getFieldName());
+                Value value = RepositoryUtil.getValue(fieldValue);
+                objectNode.setProperty(fieldDescriptor.getJcrName(), value);
+            }
+        }
+        catch (Exception e)
+        {
+            throw new JcrMappingException("Impossible to store the bean fields", e);
+        }
+    }
 }

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/DescriptorReader.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/DescriptorReader.java?rev=239448&r1=239447&r2=239448&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/DescriptorReader.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/DescriptorReader.java
Tue Aug 23 12:30:22 2005
@@ -21,6 +21,7 @@
 import org.apache.portals.graffito.jcr.exception.JcrMappingException;
 import org.apache.portals.graffito.jcr.mapper.model.BeanDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.FieldDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.MappingDescriptor;
 
@@ -78,6 +79,19 @@
             digester.addSetProperties("graffito-jcr/class-descriptor/bean-descriptor", "proxy",
"proxy");
             digester.addSetNext("graffito-jcr/class-descriptor/bean-descriptor", "addBeanDescriptor");
 
+            // --------------------------------------------------------------------------------
+            // Rules used for the collection-descriptor element
+            // --------------------------------------------------------------------------------
+
+            digester.addObjectCreate("graffito-jcr/class-descriptor/collection-descriptor",
CollectionDescriptor.class);
+            digester.addSetProperties("graffito-jcr/class-descriptor/collection-descriptor",
"fieldName", "fieldName");
+            digester.addSetProperties("graffito-jcr/class-descriptor/collection-descriptor",
"jcrName", "jcrName");
+            digester.addSetProperties("graffito-jcr/class-descriptor/collection-descriptor",
"proxy", "proxy");
+            digester.addSetProperties("graffito-jcr/class-descriptor/collection-descriptor",
"fieldId", "fieldId");
+            digester.addSetProperties("graffito-jcr/class-descriptor/collection-descriptor",
"className", "className");
+            digester.addSetNext("graffito-jcr/class-descriptor/collection-descriptor", "addCollectionDescriptor");
+            
+            
             File input = new File(xmlFile);
             MappingDescriptor mappingDescriptor= (MappingDescriptor)digester.parse( input
);
             return mappingDescriptor;

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/ClassDescriptor.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/ClassDescriptor.java?rev=239448&r1=239447&r2=239448&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/ClassDescriptor.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/ClassDescriptor.java
Tue Aug 23 12:30:22 2005
@@ -33,6 +33,7 @@
      
      HashMap fieldDescriptors = new HashMap();
      HashMap beanDescriptors = new HashMap();
+     HashMap collectionDescriptors = new HashMap();
     
     
     /**
@@ -124,6 +125,37 @@
     public Collection getBeanDescriptors()
     {
         return beanDescriptors.values();   
+    }
+
+    
+    /**
+     * Add a new CollectionDescriptor
+     * @param collectionDescriptor the new collection descriptor to add
+     */
+                  
+    public void addCollectionDescriptor(CollectionDescriptor collectionDescriptor )
+    {
+        collectionDescriptors.put(collectionDescriptor.getFieldName(), collectionDescriptor);
+    }
+
+    /**
+     * Get the CollectionDescriptor to used for a specific java bean attribute
+     * @param fieldName The java bean attribute name
+     * 
+     * @return the {@link CollectionDescriptor} found or null
+     */
+    public CollectionDescriptor getCollectionDescriptor(String fieldName)
+    {
+        return (CollectionDescriptor) collectionDescriptors.get(fieldName);
+    }
+        
+    /**
+     * 
+     * @return all {@link BeanDescriptor} defined in this ClassDescriptor
+     */
+    public Collection getCollectionDescriptors()
+    {
+        return collectionDescriptors.values();   
     }
     
 }

Added: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/CollectionDescriptor.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/CollectionDescriptor.java?rev=239448&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/CollectionDescriptor.java
(added)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/CollectionDescriptor.java
Tue Aug 23 12:30:22 2005
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2000-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.graffito.jcr.mapper.model;
+
+/**
+ * 
+ * This class information to map one java bean field (attribute) into subnode
+ * 
+ * 
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe
</a>
+ * @version $Id: Exp $
+ */
+public class CollectionDescriptor
+{
+
+     private String fieldName;
+     private String jcrName;
+     private String fieldId;
+     private String className; 
+     private boolean proxy; 
+    
+    /**
+     * @return Returns the fieldName.
+     */
+    public String getFieldName()
+    {
+        return fieldName;
+    }
+    /**
+     * @param fieldName The fieldName to set.
+     */
+    public void setFieldName(String fieldName)
+    {
+        this.fieldName = fieldName;
+    }
+    /**
+     * @return Returns the jcrName.
+     */
+    public String getJcrName()
+    {
+        return jcrName;
+    }
+    /**
+     * @param jcrName The jcrName to set.
+     */
+    public void setJcrName(String jcrName)
+    {
+        this.jcrName = jcrName;
+    }
+    
+    
+    /**
+     * @return Returns the className.
+     */
+    public String getClassName()
+    {
+        return className;
+    }
+    /**
+     * @param className The className to set.
+     */
+    public void setClassName(String className)
+    {
+        this.className = className;
+    }
+    /**
+     * @return Returns the fieldId.
+     */
+    public String getFieldId()
+    {
+        return fieldId;
+    }
+    /**
+     * @param fieldId The fieldId to set.
+     */
+    public void setFieldId(String fieldId)
+    {
+        this.fieldId = fieldId;
+    }
+    /**
+     * @return Returns the proxy.
+     */
+    public boolean isProxy()
+    {
+        return proxy;
+    }
+    /**
+     * @param proxy The proxy to set.
+     */
+    public void setProxy(boolean proxy)
+    {
+        this.proxy = proxy;
+    }
+}

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/session/impl/JcrSessionImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/session/impl/JcrSessionImpl.java?rev=239448&r1=239447&r2=239448&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/session/impl/JcrSessionImpl.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/session/impl/JcrSessionImpl.java
Tue Aug 23 12:30:22 2005
@@ -209,7 +209,7 @@
         {
             if (! session.itemExists(path))
             {
-                throw new JcrMappingException("Path already exists : " + path);
+                throw new JcrMappingException("Path is not existing : " + path);
             }
         }
         catch (RepositoryException re)

Modified: incubator/graffito/trunk/jcr-mapping/src/test/jcrmapping.xml
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/test/jcrmapping.xml?rev=239448&r1=239447&r2=239448&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/test/jcrmapping.xml (original)
+++ incubator/graffito/trunk/jcr-mapping/src/test/jcrmapping.xml Tue Aug 23 12:30:22 2005
@@ -13,21 +13,44 @@
 
         <!--
 		       The following classes are used to test object associations
-				A has an assocition (1..1) with B => B is mapped as subnode in A
-				A has a collection of C => there are n subnode used to map the collection of C
+				A has an assocition (1..1) with B (field b) => B is mapped as subnode in A
+				A has a collection of C (field "collection" ) => there are n subnodes used to map
the "C" collection 
 		
-		-->
-		
-		<class-descriptor className="org.apache.portals.graffito.jcr.testmodel.A"  jcrNodeType=
"nt:unstructured" >
+		-->		
+		<class-descriptor className="org.apache.portals.graffito.jcr.testmodel.A" jcrNodeType="nt:unstructured"
>
 		    <!-- Field-descriptor is used to map simple attributes to jcr property -->
 			<field-descriptor fieldName="a1" jcrName="a1" />   
 			<field-descriptor fieldName="a2" jcrName="a2" />
             
-            <!-- bean-descriptor is used to map bean into subnode : the definition of
the associated class has to be defined in this mapping file 
-			     proxy="true" => use lazy laoding for this attribute b when retrieving A
+            <!-- bean-descriptor is used to map a bean into subnode
+				 * The definition of the associated class has to be defined in this mapping file 
+			     * proxy="true" => use lazy laoding for this attribute b when retrieving A
 			-->
-            <bean-descriptor fieldName = "b" proxy="false"  jcrName="b" />
-             			
+            <bean-descriptor fieldName="b" jcrName="b" proxy="false"  />
+			
+			<!-- collection-descriptor is used to map a collection into subnodes : 
+				  * fieldId is the attribute name used to defined the jcr node name. It is a unique value
field.			     
+          		  *  proxy="true" => use lazy laoding for this attribute b when retrieving
A
+			      There are 2 ways to map a collection into subnodes.	  
+				  
+				  1. 
+						/mypath/mynode
+								/mypath/mynode/mycollection
+									/mypath/mynode/mycollection/item1
+									/mypath/mynode/mycollection/item2
+									...
+				  2.
+						/mypath/mynode
+									/mypath/mynode/item1
+									/mypath/mynode/item2
+									...
+                 The first one gives the possibilty to use unstructured node type.
+				 The second strategy required mapping based on the node type because "/mypath/mynode"
can contain other subnodes (which are not items for this collection)
+		    -->
+			
+			<!-- First way to map a collection : use an extra node containing all collection items
-->
+			<collection-descriptor fieldName="collection" jcrName="collection" proxy="false" fieldId="id"
className="org.apache.portals.graffito.jcr.testmodel.C" />
+			
 		</class-descriptor>
 		
 		<class-descriptor className="org.apache.portals.graffito.jcr.testmodel.B"  jcrNodeType=
"nt:unstructured" >
@@ -37,5 +60,11 @@
             
 		</class-descriptor>
 
+		<class-descriptor className="org.apache.portals.graffito.jcr.testmodel.C"  jcrNodeType=
"nt:unstructured" >
+		    <!-- Field-descriptor is used to map simple attributes to jcr property -->
+			<field-descriptor fieldName="id" jcrName="id" />   
+			<field-descriptor fieldName="name" jcrName="name" />
+            
+		</class-descriptor>		
 		
 </graffito-jcr>

Modified: incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/mapper/MapperTest.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/mapper/MapperTest.java?rev=239448&r1=239447&r2=239448&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/mapper/MapperTest.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/mapper/MapperTest.java
Tue Aug 23 12:30:22 2005
@@ -23,9 +23,9 @@
 import org.apache.portals.graffito.jcr.exception.JcrMappingException;
 import org.apache.portals.graffito.jcr.mapper.model.BeanDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.FieldDescriptor;
 import org.apache.portals.graffito.jcr.testmodel.A;
-import org.apache.portals.graffito.jcr.testmodel.Folder;
 
 /**
  * Test Mapper
@@ -84,12 +84,15 @@
             
             FieldDescriptor fieldDescriptor = classDescriptor.getFieldDescriptor("a1");
             assertNotNull("FieldDescriptor is null", fieldDescriptor);
-            assertTrue("Invalid jcrName for field description", fieldDescriptor.getJcrName().equals("a1"));
+            assertTrue("Invalid jcrName for field a1", fieldDescriptor.getJcrName().equals("a1"));
             
             BeanDescriptor beanDescriptor = classDescriptor.getBeanDescriptor("b");
             assertNotNull("BeanDescriptor is null", beanDescriptor);
-            assertTrue("Invalid jcrName for field description", beanDescriptor.getJcrName().equals("b"));
+            assertTrue("Invalid jcrName for field b", beanDescriptor.getJcrName().equals("b"));
             
+            CollectionDescriptor collectionDescriptor = classDescriptor.getCollectionDescriptor("collection");
+            assertNotNull("CollectionDescriptor is null", collectionDescriptor);
+            assertTrue("Invalid jcrName for field collection", collectionDescriptor.getJcrName().equals("collection"));
             
         }
         catch (JcrMappingException e)

Modified: incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/session/impl/JcrSessionTest.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/session/impl/JcrSessionTest.java?rev=239448&r1=239447&r2=239448&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/session/impl/JcrSessionTest.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/session/impl/JcrSessionTest.java
Tue Aug 23 12:30:22 2005
@@ -16,6 +16,8 @@
  */
 package org.apache.portals.graffito.jcr.session.impl;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Date;
 
 import javax.jcr.Node;
@@ -27,12 +29,11 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-
-import org.apache.portals.graffito.jcr.testmodel.Content;
-import org.apache.portals.graffito.jcr.testmodel.Document;
-import org.apache.portals.graffito.jcr.testmodel.Folder;
-
 import org.apache.portals.graffito.jcr.TestBase;
+import org.apache.portals.graffito.jcr.testmodel.A;
+import org.apache.portals.graffito.jcr.testmodel.B;
+import org.apache.portals.graffito.jcr.testmodel.C;
+import org.apache.portals.graffito.jcr.testmodel.Folder;
 
 /**
  * Test JcrSession
@@ -58,6 +59,107 @@
         return new TestSuite(JcrSessionTest.class);
     }
 
+    public void testClassA()
+    {
+        try
+        {
+
+            if (getJcrSession().itemExists("/test"))
+            {
+                getJcrSession().remove("/test");
+            }
+
+            // --------------------------------------------------------------------------------
+            // Create and store an object graph in the repository
+            // --------------------------------------------------------------------------------
+            A a = new A();
+            a.setA1("a1");
+            a.setA2("a2");
+            B b = new B();
+            b.setB1("b1");
+            b.setB2("b2");
+            a.setB(b);
+            
+            C c1 = new C();
+            c1.setId("first");
+            c1.setName("First Element");
+            C c2 = new C();
+            c2.setId("second");
+            c2.setName("second Element");
+            
+            
+            Collection collection = new ArrayList();
+            collection.add(c1);
+            collection.add(c2);
+            
+            a.setCollection(collection);
+            
+            getJcrSession().insert("/test", a);
+
+            // --------------------------------------------------------------------------------
+            // Check the jcr node structure
+            // --------------------------------------------------------------------------------
+            Session session = getJcrSession().getSession();
+            Node node = (Node) session.getItem("/test");
+            assertNotNull("A is null", node);
+            
+            assertTrue("Incorrect Name for '/test'", node.getName().equals("test"));
+
+            Property a1 = node.getProperty("a1");
+            assertNotNull("a1  is null", a1);
+            assertTrue("Incorrect a1", a1.getString().equals("a1"));
+            
+            Node nodeB = node.getNode("b");
+            assertNotNull("b is null", nodeB);
+            assertTrue("Incorrect b", nodeB.getName().equals("b"));
+            
+            Node nodeCollection = node.getNode("collection");
+            assertNotNull("collection is null", nodeCollection);
+            assertTrue("Incorrect number of items in the collection", nodeCollection.getNodes().getSize()
== 2);
+            
+             
+            // --------------------------------------------------------------------------------
+            // Get the object
+            // --------------------------------------------------------------------------------
          
+            a = (A) getJcrSession().getObject(A.class, "/test");
+            assertNotNull("a is null", a);
+            assertTrue("Incorrect a1", a.getA1().equals("a1"));
+            assertNotNull("a.b is null", a.getB());
+            assertTrue("Incorrect a.b.b1", a.getB().getB1().equals("b1"));
+            assertNotNull("a.collection is null", a.getCollection());
+            assertTrue("Incorrect a.collection", ((C) a.getCollection().iterator().next()).getId().equals("first"));
+            
+            // --------------------------------------------------------------------------------
+            // Update the object
+            // --------------------------------------------------------------------------------
+            a.setA1("new value");
+            B newB = new B();
+            newB.setB1("new B1");
+            newB.setB2("new B2");
+            a.setB(newB);
+            
+            
+            getJcrSession().update("/test", a);
+
+            // --------------------------------------------------------------------------------
+            // Get the object
+            // --------------------------------------------------------------------------------
          
+            a = (A) getJcrSession().getObject(A.class, "/test");
+            assertNotNull("a is null", a);
+            assertTrue("Incorrect a1", a.getA1().equals("new value"));
+            assertNotNull("a.b is null", a.getB());
+            assertTrue("Incorrect a.b.b1", a.getB().getB1().equals("new B1"));
+
+            
+        }
+        catch (Exception e)
+        {
+            e.printStackTrace();
+            fail("Exception occurs during the unit test : " + e);
+        }
+        
+    }
+    
     /**
      * 
      * Test for Folder object 

Modified: incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/A.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/A.java?rev=239448&r1=239447&r2=239448&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/A.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/A.java
Tue Aug 23 12:30:22 2005
@@ -15,6 +15,9 @@
  */
 package org.apache.portals.graffito.jcr.testmodel;
 
+import java.util.ArrayList;
+import java.util.Collection;
+
 /**
  *
  * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe
</a>
@@ -25,6 +28,7 @@
     private String a1;
     private String a2;
     private B b;
+    private Collection collection;
     
     
     /**
@@ -68,5 +72,32 @@
     public void setB(B b)
     {
         this.b = b;
+    }
+    
+        
+    /**
+     * @return Returns the collection.
+     */
+    public Collection getCollection()
+    {
+        return collection;
+    }
+    
+    /**
+     * @param collection The collection to set.
+     */
+    public void setCollection(Collection collection)
+    {
+        this.collection = collection;
+    }
+    
+    public void addC(C c)
+    {
+       if (collection == null )
+       {
+           collection = new ArrayList();
+       }
+       
+       collection.add(c);   
     }
 }

Added: incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/C.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/C.java?rev=239448&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/C.java
(added)
+++ incubator/graffito/trunk/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/C.java
Tue Aug 23 12:30:22 2005
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2000-2004 The Apache Software Foundation.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.graffito.jcr.testmodel;
+
+/**
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe
</a>
+ * @version $Id: Exp $
+ */
+public class C
+{
+     private String id;
+     private String name;
+     
+    /**
+     * @return Returns the id.
+     */
+    public String getId()
+    {
+        return id;
+    }
+    /**
+     * @param id The id to set.
+     */
+    public void setId(String id)
+    {
+        this.id = id;
+    }
+    /**
+     * @return Returns the name.
+     */
+    public String getName()
+    {
+        return name;
+    }
+    /**
+     * @param name The name to set.
+     */
+    public void setName(String name)
+    {
+        this.name = name;
+    }
+}



Mime
View raw message