You are viewing a plain text version of this content. The canonical link for it is here.
Posted to graffito-commits@incubator.apache.org by cl...@apache.org on 2005/09/06 22:01:41 UTC

svn commit: r279143 - in /incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter: CollectionConverter.java impl/ impl/AbstractCollectionConverterImpl.java impl/DefaultCollectionConverterImpl.java

Author: clombart
Date: Tue Sep  6 15:01:38 2005
New Revision: 279143

URL: http://svn.apache.org/viewcvs?rev=279143&view=rev
Log:
Starting refactoring of the CollectionConverter (not yet finalised)

Added:
    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/
    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

Added: 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=279143&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/CollectionConverter.java (added)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/CollectionConverter.java Tue Sep  6 15:01:38 2005
@@ -0,0 +1,44 @@
+/*
+ * 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.persistence.collectionconverter;
+
+import java.util.Collection;
+
+import javax.jcr.Node;
+import javax.jcr.Session;
+
+import org.apache.portals.graffito.jcr.exception.JcrMappingException;
+import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
+
+/**
+ * Convert any kind of Collection 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 updateCollection(Session session, Node objectNode, CollectionDescriptor collectionDescriptor,
+            Object object) throws JcrMappingException;
+
+    public Collection getCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
+            Class collectionFieldClass) throws JcrMappingException;
+
+}
\ No newline at end of file

Added: 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=279143&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java (added)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/AbstractCollectionConverterImpl.java Tue Sep  6 15:01:38 2005
@@ -0,0 +1,36 @@
+/*
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.portals.graffito.jcr.persistence.collectionconverter.impl;
+
+import org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter;
+
+/** 
+ *
+ * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
+ * 
+ */
+public abstract class AbstractCollectionConverterImpl
+{
+    protected ObjectConverter objectConverter;
+    
+    public AbstractCollectionConverterImpl(ObjectConverter objectConverter)
+    {
+        this.objectConverter = objectConverter;
+    }
+
+
+}

Added: 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=279143&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java (added)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/DefaultCollectionConverterImpl.java Tue Sep  6 15:01:38 2005
@@ -0,0 +1,180 @@
+/*
+ * 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+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;
+import javax.jcr.Session;
+
+import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.portals.graffito.jcr.exception.JcrMappingException;
+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.objectconverter.ObjectConverter;
+
+/** 
+ *
+ * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
+ * 
+ */
+public class DefaultCollectionConverterImpl extends AbstractCollectionConverterImpl implements CollectionConverter
+{
+        
+
+    public DefaultCollectionConverterImpl(ObjectConverter objectConverter)
+    {
+        super(objectConverter);
+    }
+
+    /**
+     * @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)
+     */
+    public void insertCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor, Object object) 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
+            {
+                collectionNode = parentNode; 
+            }
+            
+           
+            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);
+                objectConverter.insertNode(session, collectionNode, jcrNameCollection, item);    
+            }
+        }
+        catch (Exception e)
+        {
+            throw new JcrMappingException("Impossible to insert the collection field : " + collectionDescriptor.getFieldName() + "for " + collectionDescriptor.getClassName(), 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)
+     */
+    public void updateCollection(Session session, Node objectNode, CollectionDescriptor collectionDescriptor, Object object) 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
+            {
+                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);
+                objectConverter.updateNode(session, collectionNode, jcrNameCollection, item);    
+            }
+        }
+        catch (Exception e)
+        {
+            throw new JcrMappingException("Impossible to update the collection field : " + collectionDescriptor.getFieldName() + "for " + collectionDescriptor.getClassName(), 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 Collection getCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor, Class collectionFieldClass) throws JcrMappingException
+    {
+        
+        try
+        {
+            String jcrName = collectionDescriptor.getJcrName();            
+            
+            Node collectionNode = parentNode.getNode(jcrName);
+            NodeIterator children = collectionNode.getNodes();
+                        
+            Collection collection = this.createCollectionObject(collectionFieldClass);                 
+            
+            while (children.hasNext())
+            {
+                Node itemNode = children.nextNode();
+                Object item = objectConverter.getObject(session, Class.forName(collectionDescriptor.getClassName()), itemNode.getPath());                                
+                collection.add(item);                    
+            }
+            
+            return collection;
+        }
+        catch (Exception e)
+        {
+            throw new JcrMappingException("Impossible to get the collection field : " + collectionDescriptor.getFieldName() + "for " + collectionDescriptor.getClassName(), 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()); 
+    }
+
+}