incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r465362 - in /incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl: ManagedHashMap.java ResidualNodesCollectionConverterImpl.java ResidualPropertiesCollectionConverterImpl.java
Date Wed, 18 Oct 2006 20:32:07 GMT
Author: clombart
Date: Wed Oct 18 13:32:06 2006
New Revision: 465362

URL: http://svn.apache.org/viewvc?view=rev&rev=465362
Log:
Apply new collection converters provided by Felix Meschberger (see GRFT-110)

Added:
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ManagedHashMap.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ResidualNodesCollectionConverterImpl.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ResidualPropertiesCollectionConverterImpl.java

Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ManagedHashMap.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ManagedHashMap.java?view=auto&rev=465362
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ManagedHashMap.java
(added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ManagedHashMap.java
Wed Oct 18 13:32:06 2006
@@ -0,0 +1,79 @@
+/*
+ * 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 java.util.Map;
+
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection;
+
+/**
+ * The <code>ManagedHashMap</code> class provides Map support to JCR Mapping
+ *
+ * @author <a href="mailto:fmeschbe[at]apache[dot]com">Felix Meschberger</a>
+ */
+public class ManagedHashMap extends HashMap implements ManageableCollection {
+
+    /**
+     * 
+     */
+    public ManagedHashMap() {
+    }
+
+    /**
+     * @param initialCapacity
+     */
+    public ManagedHashMap(int initialCapacity) {
+        super(initialCapacity);
+    }
+
+    /**
+     * @param m
+     */
+    public ManagedHashMap(Map m) {
+        super(m);
+    }
+
+    /**
+     * @param initialCapacity
+     * @param loadFactor
+     */
+    public ManagedHashMap(int initialCapacity, float loadFactor) {
+        super(initialCapacity, loadFactor);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection#addObject(java.lang.Object)
+     */
+    public void addObject(Object object) {
+        put(object, object);
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection#getIterator()
+     */
+    public Iterator getIterator() {
+        return values().iterator();
+    }
+
+    /* (non-Javadoc)
+     * @see org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollection#getSize()
+     */
+    public int getSize() {
+        return size();
+    }
+}

Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ResidualNodesCollectionConverterImpl.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ResidualNodesCollectionConverterImpl.java?view=auto&rev=465362
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ResidualNodesCollectionConverterImpl.java
(added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ResidualNodesCollectionConverterImpl.java
Wed Oct 18 13:32:06 2006
@@ -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.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import javax.jcr.Node;
+import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.ValueFormatException;
+
+import org.apache.portals.graffito.jcr.exception.PersistenceException;
+import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
+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.collectionconverter.impl.AbstractCollectionConverterImpl;
+import org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter;
+
+/**
+ * The <code>ResidualNodesCollectionConverterImpl</code> is a collection
+ * converter for multiple child nodes accessed through Node.getNodes(String
+ * pattern).
+ * 
+ * @author <a href="mailto:fmeschbe[at]apache[dot]com">Felix Meschberger</a>
+ */
+public class ResidualNodesCollectionConverterImpl extends
+        AbstractCollectionConverterImpl {
+
+    
+    /**
+     * Constructor
+     *
+     * @param atomicTypeConverters
+     * @param objectConverter
+     * @param mapper
+     */
+    public ResidualNodesCollectionConverterImpl(Map atomicTypeConverters,
+        ObjectConverter objectConverter, Mapper mapper) {
+        super(atomicTypeConverters, objectConverter, mapper);
+    }
+
+    /**
+     *
+     * @see AbstractCollectionConverterImpl#doInsertCollection(Session, Node, CollectionDescriptor,
ManageableCollection)
+     */
+    protected void doInsertCollection(Session session, Node parentNode,
+        CollectionDescriptor collectionDescriptor,
+        ManageableCollection collection) /* throws PersistenceException */ {
+
+        if (!(collection instanceof Map)) { 
+            return;
+        }
+        
+        Map map = (Map) collection;
+        for (Iterator ei=map.entrySet().iterator(); ei.hasNext(); ) {
+            Map.Entry entry = (Map.Entry) ei.next();
+            String name = String.valueOf(entry.getKey());
+            objectConverter.insert(session, parentNode, name, entry.getValue());
+        }
+    }
+
+    /**
+     *
+     * @see AbstractCollectionConverterImpl#doUpdateCollection(Session, Node, CollectionDescriptor,
ManageableCollection)
+     */
+    protected void doUpdateCollection(Session session, Node parentNode,
+            CollectionDescriptor collectionDescriptor,
+            ManageableCollection collection) throws RepositoryException {
+
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+        if (!(collection instanceof Map)) {
+            for (NodeIterator ni=parentNode.getNodes(jcrName); ni.hasNext(); ) {
+                ni.nextNode().remove();
+            }
+            return;
+        }
+
+        Map map = (Map) collection;
+        Set updatedItems = new HashSet();
+        for (Iterator ei=map.entrySet().iterator(); ei.hasNext(); ) {
+            Map.Entry entry = (Map.Entry) ei.next();
+            String elementJcrName = String.valueOf(entry.getKey());
+            Object item = entry.getValue();
+
+            // Update existing JCR Nodes
+            if (parentNode.hasNode(elementJcrName)) {
+                objectConverter.update(session, parentNode, elementJcrName, item);
+            }
+            else {
+                // Add new collection elements
+                objectConverter.insert(session, parentNode, elementJcrName, item);
+            }
+
+            updatedItems.add(elementJcrName);
+        }
+
+        // Delete JCR nodes that are not present in the collection
+        NodeIterator nodeIterator = parentNode.getNodes(jcrName);
+        List removeNodes = new ArrayList();
+        while (nodeIterator.hasNext()) {
+            Node child = nodeIterator.nextNode();
+            if (!updatedItems.contains(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)
+     */
+    protected ManageableCollection doGetCollection(Session session,
+        Node parentNode, CollectionDescriptor collectionDescriptor,
+        Class collectionFieldClass) throws RepositoryException {
+        
+        try {
+            String jcrName = getCollectionJcrName(collectionDescriptor);
+            NodeIterator ni = parentNode.getNodes(jcrName);
+            if (!ni.hasNext()) {
+                return null;
+            }
+
+            ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass);
+            while (ni.hasNext()) {
+                Node node = ni.nextNode();
+
+                // ignore protected nodes here
+                if (node.getDefinition().isProtected()) {
+                    continue;
+                }
+
+                Object item = objectConverter.getObject(session, node.getPath());
+                if (collection instanceof Map) {
+                    String name = node.getName();
+                    ((Map) collection).put(name, item);
+                } else {
+                    collection.addObject(item);
+                }
+            }
+
+            return collection;
+        } catch (ValueFormatException vfe) {
+            throw new PersistenceException("Cannot get the collection field : "
+                + collectionDescriptor.getFieldName() + "for class "
+                + collectionDescriptor.getClassDescriptor().getClassName(), vfe);
+        }
+    }
+
+    /**
+     * @see AbstractCollectionConverterImpl#doIsNull(Session, Node, CollectionDescriptor,
Class)
+     */
+    protected boolean doIsNull(Session session, Node parentNode,
+            CollectionDescriptor collectionDescriptor, Class collectionFieldClass)
+            throws RepositoryException {
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+        return (parentNode == null || !parentNode.getNodes(jcrName).hasNext());
+    }
+}
\ No newline at end of file

Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ResidualPropertiesCollectionConverterImpl.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ResidualPropertiesCollectionConverterImpl.java?view=auto&rev=465362
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ResidualPropertiesCollectionConverterImpl.java
(added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/collectionconverter/impl/ResidualPropertiesCollectionConverterImpl.java
Wed Oct 18 13:32:06 2006
@@ -0,0 +1,204 @@
+/*
+ * 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.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.PropertyIterator;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+import javax.jcr.ValueFormatException;
+
+import org.apache.portals.graffito.jcr.exception.PersistenceException;
+import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.CollectionDescriptor;
+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.collectionconverter.ManageableCollection;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.ManageableCollectionUtil;
+import org.apache.portals.graffito.jcr.persistence.collectionconverter.impl.AbstractCollectionConverterImpl;
+import org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter;
+import org.apache.portals.graffito.jcr.reflection.ReflectionUtils;
+
+/**
+ * The <code>ResidualPropertiesCollectionConverterImpl</code> is a collection
+ * converter for multiple properties accessed through
+ * Node.getProperties(String pattern).
+ *
+ * @author <a href="mailto:fmeschbe[at]apache[dot]com">Felix Meschberger</a>
+ */
+public class ResidualPropertiesCollectionConverterImpl extends
+        AbstractCollectionConverterImpl {
+    
+    /**
+     * Constructor
+     *
+     * @param atomicTypeConverters
+     * @param objectConverter
+     * @param mapper
+     */
+    public ResidualPropertiesCollectionConverterImpl(Map atomicTypeConverters,
+        ObjectConverter objectConverter, Mapper mapper) {
+        super(atomicTypeConverters, objectConverter, mapper);
+    }
+
+    /**
+     *
+     * @see AbstractCollectionConverterImpl#doInsertCollection(Session, Node, CollectionDescriptor,
ManageableCollection)
+     */
+    protected void doInsertCollection(Session session, Node parentNode,
+        CollectionDescriptor collectionDescriptor,
+        ManageableCollection collection) throws RepositoryException {
+        internalSetProperties(session, parentNode, collectionDescriptor,
+            collection, false);
+    }
+
+    /**
+     *
+     * @see AbstractCollectionConverterImpl#doUpdateCollection(Session, Node, CollectionDescriptor,
ManageableCollection)
+     */
+    protected void doUpdateCollection(Session session, Node parentNode,
+        CollectionDescriptor collectionDescriptor,
+        ManageableCollection collection) throws RepositoryException {
+        internalSetProperties(session, parentNode, collectionDescriptor,
+            collection, true);
+    }
+
+    /**
+     * @see AbstractCollectionConverterImpl#doGetCollection(Session, Node, CollectionDescriptor,
Class)
+     */
+    protected ManageableCollection doGetCollection(Session session,
+        Node parentNode, CollectionDescriptor collectionDescriptor,
+        Class collectionFieldClass) throws RepositoryException {
+        try {
+            String jcrName = getCollectionJcrName(collectionDescriptor);
+            PropertyIterator pi = parentNode.getProperties(jcrName);
+            if (!pi.hasNext()) {
+                return null;
+            }
+
+            ManageableCollection collection = ManageableCollectionUtil.getManageableCollection(collectionFieldClass);
+            String elementClassName = collectionDescriptor.getElementClassName();
+            Class elementClass = ReflectionUtils.forName(elementClassName);
+            AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters.get(elementClass);
+
+            while (pi.hasNext()) {
+                Property prop = pi.nextProperty();
+
+                // ignore protected properties here
+                if (prop.getDefinition().isProtected()) {
+                    continue;
+                }
+
+                // handle multvalues as a list
+                Object value;
+                if (prop.getDefinition().isMultiple()) {
+                    List valueList = new ArrayList();
+                    Value[] values = prop.getValues();
+                    for (int i = 0; i < values.length; i++) {
+                        valueList.add(atomicTypeConverter.getObject(values[i]));
+                    }
+                    value = valueList;
+                } else {
+                    value = atomicTypeConverter.getObject(prop.getValue());
+                }
+
+                if (collection instanceof Map) {
+                    String name = prop.getName();
+                    ((Map) collection).put(name, value);
+                } else {
+                    collection.addObject(value);
+                }
+            }
+
+            return collection;
+        } catch (ValueFormatException vfe) {
+            throw new PersistenceException("Cannot get the collection field : "
+                + collectionDescriptor.getFieldName() + "for class "
+                + collectionDescriptor.getClassDescriptor().getClassName(), vfe);
+        }
+    }
+
+    /**
+     * @see AbstractCollectionConverterImpl#doIsNull(Session, Node, CollectionDescriptor,
Class)
+     */
+    protected boolean doIsNull(Session session, Node parentNode,
+            CollectionDescriptor collectionDescriptor, Class collectionFieldClass)
+            throws RepositoryException {
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+        return (parentNode == null || !parentNode.getProperties(jcrName).hasNext());
+    }
+
+    private void internalSetProperties(Session session, Node parentNode,
+        CollectionDescriptor collectionDescriptor,
+        ManageableCollection collection, boolean removeExisting)
+        throws RepositoryException {
+
+        String jcrName = getCollectionJcrName(collectionDescriptor);
+
+        // Delete existing values - before checking for collection !
+        if (removeExisting) {
+            for (PropertyIterator pi = parentNode.getProperties(jcrName); pi.hasNext();)
{
+                pi.nextProperty().remove();
+            }
+        }
+
+        // can only persist maps, not general collections
+        if (!(collection instanceof Map)) {
+            return;
+        }
+
+        try {
+            Map map = (Map) collection;
+            ValueFactory valueFactory = session.getValueFactory();
+            for (Iterator ei = map.entrySet().iterator(); ei.hasNext();) {
+                Map.Entry entry = (Map.Entry) ei.next();
+                String name = String.valueOf(entry.getKey());
+                Object value = entry.getValue();
+                if (value instanceof List) {
+                    // multi value
+                    List valueList = (List) value;
+                    Value[] jcrValues = new Value[valueList.size()];
+                    int i = 0;
+                    for (Iterator vi = valueList.iterator(); vi.hasNext();) {
+                        value = vi.next();
+                        AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters.get(value);
+                        jcrValues[i++] = atomicTypeConverter.getValue(
+                            valueFactory, value);
+                    }
+                    parentNode.setProperty(name, jcrValues);
+                } else {
+                    // single value
+                    AtomicTypeConverter atomicTypeConverter = (AtomicTypeConverter) atomicTypeConverters.get(value);
+                    Value jcrValue = atomicTypeConverter.getValue(valueFactory,
+                        value);
+                    parentNode.setProperty(name, jcrValue);
+                }
+            }
+        } catch (ValueFormatException vfe) {
+            throw new PersistenceException("Cannot insert collection field : "
+                + collectionDescriptor.getFieldName() + " of class "
+                + collectionDescriptor.getClassDescriptor().getClassName(), vfe);
+        }
+    }
+}
\ No newline at end of file



Mime
View raw message