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/21 18:47:56 UTC
svn commit: r290786 -
/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java
Author: clombart
Date: Wed Sep 21 11:47:52 2005
New Revision: 290786
URL: http://svn.apache.org/viewcvs?rev=290786&view=rev
Log:
Add a new collection converter : this one is based on the JCR Node Type.
By this way, it is not necessary to use an extra jcr node to store the collection.
Added:
incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java (with props)
Added: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java?rev=290786&view=auto
==============================================================================
--- incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java (added)
+++ incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java Wed Sep 21 11:47:52 2005
@@ -0,0 +1,261 @@
+/*
+ * 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.HashMap;
+import java.util.Iterator;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.Session;
+import javax.jcr.query.Query;
+import javax.jcr.query.QueryResult;
+
+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;
+
+/**
+ * Collection Mapping/convertion based on node type.
+ *
+ * This collection mapping strategy maps a collection into several nodes based on specific node type.
+ *
+ *
+ * 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 element node name is a simple indexed constant.
+ *
+ * Example - without an id attribute:
+ * /test (Main object containing the collection field )
+ * /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 )
+ * /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>
+ *
+ */
+public class NTCollectionConverterImpl extends AbstractCollectionConverterImpl implements CollectionConverter
+{
+
+ private static final String COLLECTION_ELEMENT_NAME = "collection-element";
+
+ public NTCollectionConverterImpl(ObjectConverter objectConverter, Mapper mapper)
+ {
+ 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, org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection)
+ */
+ public void insertCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
+ ManageableCollection collection) throws JcrMappingException
+ {
+
+ try
+ {
+
+ if (collection == null)
+ {
+ return;
+ }
+
+
+ Iterator collectionIterator = collection.getIterator();
+ ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor
+ .getElementClassName()));
+
+ int elementCollectionCount = 0;
+ while (collectionIterator.hasNext())
+ {
+ Object item = collectionIterator.next();
+ String elementJcrName = null;
+
+ // If the element object has a unique id => the element jcr node name = the id value
+ if (elementClassDescriptor.hasIdField())
+ {
+ String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
+ elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
+ }
+ else
+ {
+
+ elementCollectionCount++;
+ elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
+ }
+
+ objectConverter.insertNode(session, parentNode, elementJcrName, item);
+ }
+ }
+ catch (Exception 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, org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection)
+ */
+ public void updateCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
+ ManageableCollection collection) throws JcrMappingException
+ {
+ try
+ {
+
+ ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor
+ .getElementClassName()));
+
+ if (collection == null)
+ {
+ this.deleteCollectionItems(session, parentNode.getPath(), elementClassDescriptor.getJcrNodeType());
+ return;
+ }
+
+ if (!elementClassDescriptor.hasIdField())
+ {
+ this.deleteCollectionItems(session, parentNode.getPath(), elementClassDescriptor.getJcrNodeType());
+ }
+
+ Iterator collectionIterator = collection.getIterator();
+ int elementCollectionCount = 0;
+
+ HashMap updatedItems = new HashMap();
+ while (collectionIterator.hasNext())
+ {
+ Object item = collectionIterator.next();
+
+ elementCollectionCount++;
+ String elementJcrName = null;
+
+ if (elementClassDescriptor.hasIdField())
+ {
+
+ String idFieldName = elementClassDescriptor.getIdFieldDescriptor().getFieldName();
+ elementJcrName = PropertyUtils.getNestedProperty(item, idFieldName).toString();
+
+ // Update existing JCR Nodes
+ if (parentNode.hasNode(elementJcrName))
+ {
+ objectConverter.updateNode(session, parentNode, elementJcrName, item);
+ }
+ else
+ {
+ // Add new collection elements
+ objectConverter.insertNode(session, parentNode, elementJcrName, item);
+ }
+
+ updatedItems.put(elementJcrName, item);
+
+ }
+ else
+ {
+
+ elementCollectionCount++;
+ elementJcrName = COLLECTION_ELEMENT_NAME + elementCollectionCount;
+ objectConverter.insertNode(session, parentNode, elementJcrName, item);
+ }
+
+ }
+
+ // Delete JCR nodes that are not present in the collection
+ if (elementClassDescriptor.hasIdField())
+ {
+ NodeIterator nodeIterator = this.getCollectionNodes(session, parentNode.getPath(), elementClassDescriptor.getJcrNodeType());
+ while (nodeIterator.hasNext())
+ {
+ Node child = nodeIterator.nextNode();
+ if (! updatedItems.containsKey(child.getName()))
+ {
+ child.remove();
+ }
+ }
+ }
+ }
+ catch (Exception e)
+ {
+ throw new JcrMappingException("Impossible to update the collection field : " + collectionDescriptor.getFieldName()
+ + "for " + collectionDescriptor.getElementClassName(), 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 ManageableCollection getCollection(Session session, Node parentNode, CollectionDescriptor collectionDescriptor,
+ Class collectionFieldClass) throws JcrMappingException
+ {
+ try
+ {
+ ClassDescriptor elementClassDescriptor = mapper.getClassDescriptor(Class.forName(collectionDescriptor
+ .getElementClassName()));
+ NodeIterator children = this.getCollectionNodes(session, parentNode.getPath(), elementClassDescriptor.getJcrNodeType());
+
+ ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass);
+
+ while (children.hasNext())
+ {
+ Node itemNode = children.nextNode();
+ 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.getElementClassName(), e);
+ }
+ }
+
+ private NodeIterator getCollectionNodes (Session session, String parentNodePath, String itemNodeType) throws Exception
+ {
+ Query query = session.getWorkspace().getQueryManager().createQuery("/jcr:root" + parentNodePath + "//element(*," + itemNodeType + ")", Query.XPATH);
+ QueryResult queryResult = query.execute();
+ return queryResult.getNodes();
+ }
+
+ private void deleteCollectionItems(Session session, String parentNodePath, String itemNodeType) throws Exception
+ {
+ NodeIterator nodeIterator = this.getCollectionNodes(session, parentNodePath, itemNodeType);
+ while (nodeIterator.hasNext())
+ {
+ Node node = nodeIterator.nextNode();
+ node.remove();
+ }
+ }
+}
Propchange: incubator/graffito/trunk/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/NTCollectionConverterImpl.java
------------------------------------------------------------------------------
svn:executable = *