incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r279147 - /incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/ObjectConverter.java
Date Tue, 06 Sep 2005 22:04:31 GMT
Author: clombart
Date: Tue Sep  6 15:04:28 2005
New Revision: 279147

URL: http://svn.apache.org/viewcvs?rev=279147&view=rev
Log:
* Apply patches provided by Martin Koci 
- Review catch blocks
- Manage mandatory & autocreated JCR properties (need to makes more unit tests)

* Use the CollectionConverter component

Modified:
    incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/ObjectConverter.java

Modified: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/ObjectConverter.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/ObjectConverter.java?rev=279147&r1=279146&r2=279147&view=diff
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/ObjectConverter.java
(original)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/ObjectConverter.java
Tue Sep  6 15:04:28 2005
@@ -16,18 +16,18 @@
 package org.apache.portals.graffito.jcr.persistence.objectconverter;
 
 import java.lang.reflect.InvocationTargetException;
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
 
 import javax.jcr.Node;
-import javax.jcr.NodeIterator;
 import javax.jcr.PathNotFoundException;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Value;
 import javax.jcr.ValueFormatException;
+import javax.jcr.nodetype.PropertyDefinition;
 
+import org.apache.commons.beanutils.ConstructorUtils;
 import org.apache.commons.beanutils.PropertyUtils;
 import org.apache.portals.graffito.jcr.exception.JcrMappingException;
 import org.apache.portals.graffito.jcr.mapper.Mapper;
@@ -37,6 +37,8 @@
 import org.apache.portals.graffito.jcr.mapper.model.FieldDescriptor;
 import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter;
 import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverterFactory;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.CollectionConverter;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.impl.DefaultCollectionConverterImpl;
 import org.apache.portals.graffito.jcr.repository.RepositoryUtil;
 
 /**
@@ -107,6 +109,11 @@
         try
         {
             ClassDescriptor classDescriptor = mapper.getClassDescriptor(clazz);
+            if (classDescriptor == null) 
+            {
+                throw new JcrMappingException("Class of type: " + clazz.getName() + " is
not JCR persistable. Maybe element 'class-descriptor' for this type in mapping file is missing"
);
+            }
+            
 
             Node node = (Node) session.getItem(path);
             Object object = clazz.newInstance();
@@ -197,30 +204,23 @@
         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);
+            CollectionConverter collectionConverter = this.getCollectionConverter(collectionDescriptor);
+            Class collectionFieldClass = PropertyUtils.getPropertyDescriptor(object, collectionDescriptor.getFieldName()).getPropertyType();
+            Collection collection = collectionConverter.getCollection(session, node, collectionDescriptor,
collectionFieldClass );
+            PropertyUtils.setNestedProperty(object, collectionDescriptor.getFieldName(),
collection);
         }
     }
 
-    private void insertNode(Session session, Node parentNode, String nodeName, Object object)
throws JcrMappingException
+    public void insertNode(Session session, Node parentNode, String nodeName, Object object)
throws JcrMappingException
     {
         try
         {
             ClassDescriptor classDescriptor = mapper.getClassDescriptor(object.getClass());
+            if (classDescriptor == null) 
+            {
+                throw new JcrMappingException("Class of type: " + object.getClass().getName()
+ " is not JCR persistable. Maybe element 'class-descriptor' for this type in mapping file
is missing" );
+            }
+            
             String jcrNodeType = classDescriptor.getJcrNodeType();
             if (jcrNodeType == null || jcrNodeType.equals(""))
             {
@@ -237,16 +237,21 @@
         }
         catch (Exception e)
         {
-            throw new JcrMappingException("Impossible to add the node : " + parentNode);
+            throw new JcrMappingException("Impossible to add the node : " + parentNode, e);
         }
 
     }
 
-    private void updateNode(Session session, Node parentNode, String nodeName, Object object)
throws JcrMappingException
+    public void updateNode(Session session, Node parentNode, String nodeName, Object object)
throws JcrMappingException
     {
         try
         {
             ClassDescriptor classDescriptor = mapper.getClassDescriptor(object.getClass());
+            if (classDescriptor == null) 
+            {
+                throw new JcrMappingException("Class of type: " + object.getClass().getName()
+ " is not JCR persistable. Maybe element 'class-descriptor' for this type in mapping file
is missing" );
+            }
+            
             String jcrNodeType = classDescriptor.getJcrNodeType();
             if (jcrNodeType == null || jcrNodeType.equals(""))
             {
@@ -263,10 +268,11 @@
         }
         catch (Exception e)
         {
-            throw new JcrMappingException("Impossible to add the node : " + parentNode);
+            throw new JcrMappingException("Impossible to add the node : " + parentNode, e);
         }
 
     }    
+    
     private void insertBeanFields(Session session, Object object, ClassDescriptor classDescriptor,
Node objectNode) throws JcrMappingException
     {
         try
@@ -317,30 +323,8 @@
             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)
-                {
-                    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.insertNode(session, collectionNode, jcrNameCollection, item);  
 
-                }
+                CollectionConverter collectionConverter = this.getCollectionConverter(collectionDescriptor);
+                collectionConverter.insertCollection(session, objectNode, collectionDescriptor,object);
             }
         }
         catch (Exception e)
@@ -358,30 +342,8 @@
             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)
-                {
-                    collectionNode = objectNode.getNode(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.updateNode(session, collectionNode, jcrNameCollection, item);  
 
-                }
+                CollectionConverter collectionConverter = this.getCollectionConverter(collectionDescriptor);
+                collectionConverter.updateCollection(session, objectNode, collectionDescriptor,
object);
             }
         }
         catch (Exception e)
@@ -400,18 +362,81 @@
             {
                 FieldDescriptor fieldDescriptor = (FieldDescriptor) fieldDescriptorIterator.next();
                 String fieldName = fieldDescriptor.getFieldName();
-                String jcrName = fieldDescriptor.getJcrName();
+                String jcrName = fieldDescriptor.getJcrName();                
+                
+                // Check if the node property is "autocreated"
+                boolean autoCreated = false;
+                
+                if (objectNode.hasProperty(jcrName))
+                {
+                      autoCreated = objectNode.getProperty(jcrName).getDefinition().isAutoCreated();
+                }
+                
+               // All auto created JCR properties are ignored
+                if (! autoCreated)
+                {
+                   Object fieldValue = PropertyUtils.getNestedProperty(object, fieldName);
               
                 
-                Object fieldValue = PropertyUtils.getNestedProperty(object, fieldName); 
              
+                   AtomicTypeConverter converter = atomicTypeConverterFactory.getJCRTypeConverter(PropertyUtils.getPropertyType(object,
fieldName));
+                   Value value = converter.getJcrValueFromJavaObject(fieldValue);
+                   // Check if mandatory property are not null
+                   this.checkMandatoryProperty(objectNode, fieldDescriptor, value);
+                   
+                   objectNode.setProperty(jcrName, value);
+                }
                 
-                AtomicTypeConverter converter = atomicTypeConverterFactory.getJCRTypeConverter(PropertyUtils.getPropertyType(object,
fieldName));
-                Value value = converter.getJcrValueFromJavaObject(fieldValue);
-                objectNode.setProperty(jcrName, value);
+                                               
             }
         }
         catch (Exception e)
         {
             throw new JcrMappingException("Impossible to store the bean fields", e);
+        }
+    }
+    
+    private CollectionConverter getCollectionConverter(CollectionDescriptor collectionDescriptor)
throws JcrMappingException
+    {
+        
+         try
+        {
+            String className = collectionDescriptor.getCollectionConverterClassName();
+             if (className == null)
+             {
+                 return new DefaultCollectionConverterImpl(this);
+             }
+             else
+             {
+                 Class converterClass = Class.forName(className);
+                 return (CollectionConverter) ConstructorUtils.invokeConstructor(converterClass,
this);
+             }
+        }
+        catch (Exception e)
+        {
+            throw new JcrMappingException("Impossible to get the collection descriptor for
" + collectionDescriptor.getCollectionConverterClassName(), e); 
+        }
+       
+    }
+    
+    private void checkMandatoryProperty(Node objectNode, FieldDescriptor fieldDescriptor,
Value value ) throws JcrMappingException
+    {
+        try
+        {
+            PropertyDefinition[] propertyDefinitions = objectNode.getPrimaryNodeType().getDeclaredPropertyDefinitions();
+            for (int i = 0; i < propertyDefinitions.length; i++)
+            {
+                PropertyDefinition definition = propertyDefinitions[i];
+                if (definition.getName().equals(fieldDescriptor.getJcrName()) &&
definition.isMandatory()
+                        && definition.isAutoCreated() == false && value ==
null)
+                {
+                    throw new JcrMappingException("Class of type:" + fieldDescriptor.getClassDescriptor().getClassName()
+                            + " has property: " + fieldDescriptor.getFieldName() + " declared
as JCR property: "
+                            + fieldDescriptor.getJcrName() + " This property is mandatory
but property in bean has value null");
+                }
+            }
+        }
+        catch (Exception e)
+        {
+            throw new JcrMappingException("Impossible to check mandatory property" , e);
         }
     }
 }



Mime
View raw message