jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r689932 - in /jackrabbit/trunk/jackrabbit-ocm/src: main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/ test/java/org/apache/jackrabbit/ocm/manager/collectionconverter/
Date Thu, 28 Aug 2008 19:06:36 GMT
Author: clombart
Date: Thu Aug 28 12:06:36 2008
New Revision: 689932

URL: http://svn.apache.org/viewvc?rev=689932&view=rev
Log:
Add better suppport of Map. From now, it is not necessary to use an id for object that will
be used inside a map. 

See the AnnotationHashMapTest to get an example. 


Modified:
    jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/DefaultCollectionConverterImpl.java
    jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/NTCollectionConverterImpl.java
    jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/collectionconverter/AnnotationHashMapTest.java

Modified: jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/DefaultCollectionConverterImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/DefaultCollectionConverterImpl.java?rev=689932&r1=689931&r2=689932&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/DefaultCollectionConverterImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/DefaultCollectionConverterImpl.java
Thu Aug 28 12:06:36 2008
@@ -24,10 +24,15 @@
 import java.util.List;
 import java.util.Map;
 
+import javax.jcr.ItemExistsException;
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
+import javax.jcr.PathNotFoundException;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.lock.LockException;
+import javax.jcr.nodetype.ConstraintViolationException;
+import javax.jcr.version.VersionException;
 
 import org.apache.jackrabbit.ocm.exception.JcrMappingException;
 import org.apache.jackrabbit.ocm.manager.collectionconverter.ManageableCollection;
@@ -110,7 +115,17 @@
 
         ClassDescriptor elementClassDescriptor = mapper.getClassDescriptorByClass( ReflectionUtils.forName(collectionDescriptor.getElementClassName()));
 
-        Iterator collectionIterator = objects.getIterator();
+        if (objects instanceof ManageableCollection)
+           insertManageableCollection(session, objects, collectionNode, elementClassDescriptor);
+        else 
+           insertManageableMap(session, objects, collectionNode);
+        	
+    }
+
+	private void insertManageableCollection(Session session,
+			ManageableObjects objects, Node collectionNode,
+			ClassDescriptor elementClassDescriptor) {
+		Iterator collectionIterator = objects.getIterator();
         while (collectionIterator.hasNext()) {
             Object item = collectionIterator.next();
             String elementJcrName = null;
@@ -127,7 +142,19 @@
 
             objectConverter.insert(session, collectionNode, elementJcrName, item);
         }
-    }
+	}
+	
+	private void insertManageableMap(Session session, ManageableObjects objects, Node collectionNode)
{
+
+		
+		Map map = (Map) objects.getObjects(); 
+		for (Object key : map.keySet())
+		{
+			Object item = map.get(key);
+			objectConverter.insert(session, collectionNode, key.toString(), item);
+			
+		}
+	}
 
     /**
      *
@@ -158,10 +185,23 @@
     	}
 
     	// update process
+    	if (objects instanceof ManageableCollection)
+           updateManagableCollection(session, parentNode, collectionDescriptor, objects,
jcrName);
+    	else
+    	   updateManagableMap(session, parentNode, collectionDescriptor, objects, jcrName);
+    		
+    }
 
-        ClassDescriptor elementClassDescriptor = mapper.getClassDescriptorByClass( ReflectionUtils.forName(collectionDescriptor.getElementClassName()));
+	private void updateManagableCollection(Session session, Node parentNode,
+			CollectionDescriptor collectionDescriptor,
+			ManageableObjects objects, String jcrName)
+			throws PathNotFoundException, RepositoryException,
+			VersionException, LockException, ConstraintViolationException,
+			ItemExistsException {
+		ClassDescriptor elementClassDescriptor = mapper.getClassDescriptorByClass( ReflectionUtils.forName(collectionDescriptor.getElementClassName()));
         Node collectionNode = parentNode.getNode(jcrName);
-        //  If the collection elements have not an id, it is not possible to find the matching
JCR nodes => delete the complete collection
+        //  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()) {
             collectionNode.remove();
             collectionNode = parentNode.addNode(jcrName);
@@ -211,7 +251,50 @@
                 ((Node) removeNodes.get(i)).remove();
             }
         }
-    }
+	}
+	
+	
+	private void updateManagableMap(Session session, Node parentNode,
+									CollectionDescriptor collectionDescriptor,
+									ManageableObjects objects, String jcrName)
+									throws PathNotFoundException, RepositoryException,
+									VersionException, LockException, ConstraintViolationException,
+									ItemExistsException {
+		
+		
+		ClassDescriptor elementClassDescriptor = mapper.getClassDescriptorByClass( ReflectionUtils.forName(collectionDescriptor.getElementClassName()));
+        Node collectionNode = parentNode.getNode(jcrName);
+
+        Map map = (Map) objects.getObjects(); 
+        Map updatedItems = new HashMap();
+		for (Object key : map.keySet())
+		{
+			Object item = map.get(key);
+			// Update existing JCR Nodes
+            if (collectionNode.hasNode(key.toString())) {
+                objectConverter.update(session, collectionNode, key.toString(), item);
+            }
+            else {
+                // Add new collection elements
+                objectConverter.insert(session, collectionNode, key.toString(), item);
+            } 
+            updatedItems.put(key.toString(), item);
+		}
+
+		// Delete the nodes that are not present in the Map 
+        NodeIterator nodeIterator = collectionNode.getNodes();
+        List removeNodes = new ArrayList();
+        while (nodeIterator.hasNext()) {
+            Node child = nodeIterator.nextNode();
+            if (!updatedItems.containsKey(child.getName())) {
+                    removeNodes.add(child);
+            }
+        }
+        for(int i = 0; i < removeNodes.size(); i++) {
+            ((Node) removeNodes.get(i)).remove();
+        }
+
+	}
 
     /**
      * @see AbstractCollectionConverterImpl#doGetCollection(Session, Node, CollectionDescriptor,
Class)
@@ -236,19 +319,9 @@
             Object item = objectConverter.getObject(session, elementClass, itemNode.getPath());
             if ( objects instanceof ManageableCollection)
             	((ManageableCollection)objects).addObject(item);
-            else {
-            	ClassDescriptor elementClassDescriptor = mapper.getClassDescriptorByClass(elementClass);
-            	if (!elementClassDescriptor.hasIdField())
-            	{
-            		throw new JcrMappingException("Impossible to use a map for the field : "
-            				                      + collectionDescriptor.getFieldName()
-            				                      + " in the class : " + collectionDescriptor.getCollectionClassName()
-            				                      + ". The element objects have no id field (check their
OCM mapping).");
-            	}
-            	Object elementId = ReflectionUtils.getNestedProperty(item,
-            			                           elementClassDescriptor.getIdFieldDescriptor().getFieldName());
-                ((ManageableMap) objects).addObject(elementId, item);
-            }
+            else 
+            	((ManageableMap) objects).addObject(itemNode.getName(), item);
+
         }
 
         return objects;

Modified: jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/NTCollectionConverterImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/NTCollectionConverterImpl.java?rev=689932&r1=689931&r2=689932&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/NTCollectionConverterImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-ocm/src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/NTCollectionConverterImpl.java
Thu Aug 28 12:06:36 2008
@@ -54,9 +54,10 @@
  *
  * This collection mapping strategy maps the collection elements into subnodes based on the
same node types.
  *
- * There are 2 constraints in this collection converter :
+ * There are 3 constraints in this collection converter :
  * 1/ this is not possible to have 2 different collections in the main object which are used
the same jcr node type for their elements.
  * 2/ this is not possible to make a distinction between an empty collection and an null
collection.
+ * 3/ Map are not supported. 
  *
  *
  * If the collection element class contains an id (see the FieldDescriptor definition), this
id value is used to build the collection element node name.

Modified: jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/collectionconverter/AnnotationHashMapTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/collectionconverter/AnnotationHashMapTest.java?rev=689932&r1=689931&r2=689932&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/collectionconverter/AnnotationHashMapTest.java
(original)
+++ jackrabbit/trunk/jackrabbit-ocm/src/test/java/org/apache/jackrabbit/ocm/manager/collectionconverter/AnnotationHashMapTest.java
Thu Aug 28 12:06:36 2008
@@ -96,13 +96,13 @@
             e1.setId("e1");
             e1.setText("Element 1");
             hashMapElement.addObject(e1);
-            map.put("e1", e1);
+            map.put("keyE1", e1);
 
             Element e2 = new Element();
             e2.setId("e2");
             e2.setText("Element 2");
             hashMapElement.addObject(e2);
-            map.put("e2", e2);
+            map.put("keyE2", e2);
 
             main.setHashMapElement(hashMapElement);
             main.setMap(map);
@@ -118,8 +118,12 @@
             assertNotNull("main.getHashMap() is null", main.getMap());
             assertTrue("Incorrect text", main.getText().equals("Main text"));
             map = (Map) main.getHashMapElement().getObjects();
-            assertTrue("Incorrect para element", ((Element) map.get("e1")).getText().equals("Element
1"));
-            assertTrue("Incorrect para element", ((Element) map.get("e1")).getText().equals("Element
1"));
+            assertTrue("Incorrect para element", map.get("e1").getText().equals("Element
1"));
+            assertTrue("Incorrect para element", map.get("e2").getText().equals("Element
2"));
+            
+            map = main.getMap();
+            assertTrue("Incorrect para element", map.get("keyE1").getText().equals("Element
1"));
+            assertTrue("Incorrect para element", map.get("keyE2").getText().equals("Element
2"));
 
             // --------------------------------------------------------------------------------
             // Update the object
@@ -131,19 +135,19 @@
             e1.setId("e1");
             e1.setText("Element 1");
             hashMapElement.addObject(e1);
-            map.put("e1", e1);
+            map.put("keyE1", e1);
 
             e2 = new Element();
             e2.setId("e3");
             e2.setText("Element 3");
             hashMapElement.addObject(e2);
-            map.put("e3", e2);
+            map.put("keyE3", e2);
 
             Element e3 = new Element();
             e3.setId("e4");
             e3.setText("Element 4");
             hashMapElement.addObject(e3);
-            map.put("e4", e3);
+            map.put("keyE4", e3);
 
             main.setHashMapElement(hashMapElement);
             main.setMap(map);
@@ -157,8 +161,12 @@
             assertNotNull("main.getElements() is null", main.getHashMapElement());
             assertTrue("Incorrect text", main.getText().equals("Main text"));
             map = (Map) main.getHashMapElement().getObjects();
-            assertTrue("Incorrect para element", ((Element) map.get("e4")).getText().equals("Element
4"));
-            assertTrue("Incorrect para element", main.getMap().get("e4").getText().equals("Element
4"));
+            assertTrue("Incorrect para element", map.get("e4").getText().equals("Element
4"));
+            assertTrue("Incorrect para element", map.get("e4").getText().equals("Element
4"));
+            
+            map = main.getMap();
+            assertTrue("Incorrect para element", map.get("keyE4").getText().equals("Element
4"));
+            assertTrue("Incorrect para element", map.get("keyE4").getText().equals("Element
4"));
         }
         catch (Exception e)
         {



Mime
View raw message