Property changes on: . ___________________________________________________________________ Modified: svn:ignore - derby.log target *.iws *.ipr *.iml .* jcoverage* junit*.properties + derby.log target *.iws *.ipr *.iml .* jcoverage* junit*.properties target-eclipse Index: src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/EnumCollectionConverterImpl.java =================================================================== --- src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/EnumCollectionConverterImpl.java (revision 0) +++ src/main/java/org/apache/jackrabbit/ocm/manager/collectionconverter/impl/EnumCollectionConverterImpl.java (revision 0) @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.jackrabbit.ocm.manager.collectionconverter.impl; + + +import java.util.Iterator; +import java.util.Map; + +import javax.jcr.Node; +import javax.jcr.Property; +import javax.jcr.RepositoryException; +import javax.jcr.Session; +import javax.jcr.Value; +import javax.jcr.ValueFactory; +import javax.jcr.ValueFormatException; + +import org.apache.jackrabbit.ocm.exception.JcrMappingException; +import org.apache.jackrabbit.ocm.exception.ObjectContentManagerException; +import org.apache.jackrabbit.ocm.manager.atomictypeconverter.AtomicTypeConverter; +import org.apache.jackrabbit.ocm.manager.atomictypeconverter.impl.UndefinedTypeConverterImpl; +import org.apache.jackrabbit.ocm.manager.collectionconverter.ManageableCollection; +import org.apache.jackrabbit.ocm.manager.collectionconverter.ManageableObjectsUtil; +import org.apache.jackrabbit.ocm.manager.collectionconverter.ManageableObjects; +import org.apache.jackrabbit.ocm.manager.enumconverter.EnumTypeConverter; +import org.apache.jackrabbit.ocm.manager.objectconverter.ObjectConverter; +import org.apache.jackrabbit.ocm.mapper.Mapper; +import org.apache.jackrabbit.ocm.mapper.model.CollectionDescriptor; +import org.apache.jackrabbit.ocm.reflection.ReflectionUtils; + +/** + * Collection Mapping/convertion implementation used for enum collections + * + * This collection mapping strategy maps a collection into a JCR multi value property + * + * @author Christophe Lombart + * @author Alexandru Popescu + * @author Boni Gopalan + */ +public class EnumCollectionConverterImpl extends AbstractCollectionConverterImpl { + + private EnumTypeConverter enumConverter = new EnumTypeConverter(); + /** + * Constructor + * + * @param atomicTypeConverters + * @param objectConverter + * @param mapper + */ + public EnumCollectionConverterImpl( 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, + ManageableObjects objects) throws RepositoryException { + try { + if (objects == null) { + return; + } + + String jcrName = getCollectionJcrName(collectionDescriptor); + Value[] values = new Value[objects.getSize()]; + ValueFactory valueFactory = session.getValueFactory(); + Iterator collectionIterator = objects.getIterator(); + for (int i = 0; i < objects.getSize(); i++) { + Object fieldValue = collectionIterator.next(); + values[i] = enumConverter.getValue(valueFactory, fieldValue); + } + + parentNode.setProperty(jcrName, values); + } + catch(ValueFormatException vfe) { + throw new ObjectContentManagerException("Cannot insert collection field : " + + collectionDescriptor.getFieldName() + + " of class " + + collectionDescriptor.getClassDescriptor().getClassName(), vfe); + } + } + + /** + * + * @see AbstractCollectionConverterImpl#doUpdateCollection(Session, Node, CollectionDescriptor, ManageableCollection) + */ + protected void doUpdateCollection(Session session, + Node parentNode, + CollectionDescriptor collectionDescriptor, + ManageableObjects objects) throws RepositoryException { + String jcrName = getCollectionJcrName(collectionDescriptor); + + // Delete existing values + if (parentNode.hasProperty(jcrName)) { + parentNode.setProperty(jcrName, (Value[]) null); + } + + if (objects == null) { + return; + } + + + // Add all collection element into an Value array + Value[] values = new Value[objects.getSize()]; + ValueFactory valueFactory = session.getValueFactory(); + int i = 0; + for (Iterator collectionIterator = objects.getIterator(); collectionIterator.hasNext(); i++) { + Object fieldValue = collectionIterator.next(); + values[i] = enumConverter.getValue(valueFactory, fieldValue); + } + + parentNode.setProperty(jcrName, values); + } + + /** + * @see AbstractCollectionConverterImpl#doGetCollection(Session, Node, CollectionDescriptor, Class) + */ + protected ManageableObjects doGetCollection(Session session, + Node parentNode, + CollectionDescriptor collectionDescriptor, + Class collectionFieldClass) throws RepositoryException { + try { + String jcrName = getCollectionJcrName(collectionDescriptor); + if (!parentNode.hasProperty(jcrName)) { + return null; + } + Property property = parentNode.getProperty(jcrName); + Value[] values = property.getValues(); + if (values == null || values.length <= 0){ + return null; + } + + ManageableObjects objects = ManageableObjectsUtil.getManageableObjects(collectionFieldClass); + for (int i = 0; i < values.length; i++) { + ((ManageableCollection) objects).addObject(enumConverter.getObject(values[i])); + } + + return objects; + } + catch(ValueFormatException vfe) { + throw new ObjectContentManagerException("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); + + if (!parentNode.hasProperty(jcrName)) { + return true; + } + return false; + } +} \ No newline at end of file Index: src/main/java/org/apache/jackrabbit/ocm/manager/enumconverter/EnumTypeConverter.java =================================================================== --- src/main/java/org/apache/jackrabbit/ocm/manager/enumconverter/EnumTypeConverter.java (revision 0) +++ src/main/java/org/apache/jackrabbit/ocm/manager/enumconverter/EnumTypeConverter.java (revision 0) @@ -0,0 +1,82 @@ +/* +* Copyright © BioImagene Inc. 2005-2008 All right reserved. + +* The copyright to the computer program(s) herein is the property of Bioimagene Inc. +* The program(s) may be used and/or copied only with the written permission +* of Bioimagene or in accordance with the terms and conditions stipulated in the +* agreement/contract under which the program(s) have been supplied. +*/ +package org.apache.jackrabbit.ocm.manager.enumconverter; + +import org.apache.commons.lang.StringUtils; + +import org.apache.jackrabbit.ocm.manager.atomictypeconverter.AtomicTypeConverter; + +import java.io.InvalidClassException; + +import javax.jcr.RepositoryException; +import javax.jcr.Value; +import javax.jcr.ValueFactory; +import javax.jcr.ValueFormatException; + + +/** +* +* SimpleEnumerationTypeConverter class. +* +* This converter can map type Enum (java.lang.Enum) to JCR properties and back. +* +* @author Boni Gopalan +*/ +public class EnumTypeConverter implements AtomicTypeConverter { + + public Object getObject(Value value) { + try { + String propertyValue = value.getString(); + String[] enumerationDef = StringUtils.split(propertyValue, ':'); + String enumerationClass = enumerationDef[0]; + String enumerationValue = enumerationDef[1]; + Enum[] enumerations = (Enum[]) Class.forName(enumerationClass) + .getEnumConstants(); + int size = enumerations.length; + + for (int i = 0; i < size; i++) { + if (enumerations[i].name().equals(enumerationValue)) { + return enumerations[i]; + } + } + + throw new RuntimeException(new InvalidClassException(enumerationClass + + " Does not contain an enumeration " + enumerationValue)); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + public Value getValue(ValueFactory valueFactory, Object object) { + if (object == null) { + return null; + } + + if (!(object instanceof Enum)) { + throw new RuntimeException(new InvalidClassException(EnumTypeConverter.class.getSimpleName() + + " Can only convert simple Enumerations")); + } + + String value; + Enum anEnum = (Enum) (object); + value = anEnum.getDeclaringClass().getName() + ":" + anEnum.name(); + + return valueFactory.createValue(value); + } + + public String getXPathQueryValue(ValueFactory valueFactory, Object object) { + Value value = getValue(valueFactory, object); + + try { + return "'" + value.getString() + "'"; + } catch (Exception e) { + throw new RuntimeException(e.fillInStackTrace()); + } + } +} Index: src/test/java/org/apache/jackrabbit/ocm/AnnotationTestBase.java =================================================================== --- src/test/java/org/apache/jackrabbit/ocm/AnnotationTestBase.java (revision 704127) +++ src/test/java/org/apache/jackrabbit/ocm/AnnotationTestBase.java (working copy) @@ -53,6 +53,7 @@ import org.apache.jackrabbit.ocm.testmodel.collection.Element; import org.apache.jackrabbit.ocm.testmodel.collection.Main; import org.apache.jackrabbit.ocm.testmodel.collection.Person; +import org.apache.jackrabbit.ocm.testmodel.enumeration.Odyssey; import org.apache.jackrabbit.ocm.testmodel.inheritance.Ancestor; import org.apache.jackrabbit.ocm.testmodel.inheritance.AnotherDescendant; import org.apache.jackrabbit.ocm.testmodel.inheritance.Descendant; @@ -172,6 +173,7 @@ classes.add(SimpleAnnotedClass.class); classes.add(SimpleInterface.class); classes.add(Person.class); + classes.add(Odyssey.class); Mapper mapper = new AnnotationMapperImpl(classes); ocm = new ObjectContentManagerImpl(session, mapper); Index: src/test/java/org/apache/jackrabbit/ocm/DigesterTestBase.java =================================================================== --- src/test/java/org/apache/jackrabbit/ocm/DigesterTestBase.java (revision 704127) +++ src/test/java/org/apache/jackrabbit/ocm/DigesterTestBase.java (working copy) @@ -58,7 +58,9 @@ "./src/test/test-config/jcrmapping-inheritance.xml", "./src/test/test-config/jcrmapping-jcrnodetypes.xml", "./src/test/test-config/jcrmapping-uuid.xml", - "./src/test/test-config/jcrmapping-complex-collections.xml"}; + "./src/test/test-config/jcrmapping-complex-collections.xml", + "./src/test/test-config/jcrmapping-Enum.xml" + }; session = RepositoryUtil.login(repository, "superuser", "superuser"); ocm = new ObjectContentManagerImpl(session, files); Index: src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/AnnotationSimpleEnumerationTest.java =================================================================== --- src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/AnnotationSimpleEnumerationTest.java (revision 0) +++ src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/AnnotationSimpleEnumerationTest.java (revision 0) @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.jackrabbit.ocm.manager.enumeration; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.jackrabbit.ocm.AnnotationTestBase; +import org.apache.jackrabbit.ocm.RepositoryLifecycleTestSetup; +import org.apache.jackrabbit.ocm.DigesterTestBase; +import org.apache.jackrabbit.ocm.manager.ObjectContentManager; +import org.apache.jackrabbit.ocm.testmodel.A; +import org.apache.jackrabbit.ocm.testmodel.B; +import org.apache.jackrabbit.ocm.testmodel.C; +import org.apache.jackrabbit.ocm.testmodel.Discriminator; +import org.apache.jackrabbit.ocm.testmodel.enumeration.Odyssey; +import org.apache.jackrabbit.ocm.testmodel.enumeration.Planet; + +/** + * Test Simple Enumeration mappings + * + * @author Boni Gopalan + */ +public class AnnotationSimpleEnumerationTest extends AnnotationTestBase +{ + private final static Log logger = LogFactory.getLog(AnnotationSimpleEnumerationTest.class); + + /** + *
Defines the test case name for junit.
+ * @param testName The test case name. + */ + public AnnotationSimpleEnumerationTest(String testName) throws Exception + { + super(testName); + } + + public static Test suite() + { + // All methods starting with "test" will be executed in the test suite. + return new RepositoryLifecycleTestSetup( + new TestSuite(AnnotationSimpleEnumerationTest.class)); + } + + public void testMapSimpleEnumeration() + { + try { + new SimpleEnumerationTestBase(getObjectContentManager()).testMapSimpleEnumeration(); + } catch (Throwable e) { + e.printStackTrace(); + fail("Tests resulted in exception"); + } + } + +} Index: src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/DigesterSimpleEnumerationTest.java =================================================================== --- src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/DigesterSimpleEnumerationTest.java (revision 0) +++ src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/DigesterSimpleEnumerationTest.java (revision 0) @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.jackrabbit.ocm.manager.enumeration; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; +import junit.runner.TestCaseClassLoader; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.jackrabbit.ocm.DigesterTestBase; +import org.apache.jackrabbit.ocm.RepositoryLifecycleTestSetup; + +/** + * Test Persisting and retrieving Enum values. + * + * @author Boni Gopalan + */ +public class DigesterSimpleEnumerationTest extends DigesterTestBase +{ + private final static Log log = LogFactory.getLog(DigesterSimpleEnumerationTest.class); + + /** + *Defines the test case name for junit.
+ * @param testName The test case name. + */ + public DigesterSimpleEnumerationTest(String testName) throws Exception + { + super(testName); + } + + public static Test suite() + { + // All methods starting with "test" will be executed in the test suite. + return new RepositoryLifecycleTestSetup( + new TestSuite(DigesterSimpleEnumerationTest.class)); + } + + public void testMapSimpleEnumeration() + { + try { + new SimpleEnumerationTestBase(getObjectContentManager()).testMapSimpleEnumeration(); + } catch (Throwable e) { + e.printStackTrace(); + fail("Tests resulted in exception"); + } + } + +} Index: src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/SimpleEnumerationTestBase.java =================================================================== --- src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/SimpleEnumerationTestBase.java (revision 0) +++ src/test/java/org/apache/jackrabbit/ocm/manager/enumeration/SimpleEnumerationTestBase.java (revision 0) @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.jackrabbit.ocm.manager.enumeration; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.jackrabbit.ocm.manager.ObjectContentManager; +import org.apache.jackrabbit.ocm.testmodel.enumeration.Odyssey; +import org.apache.jackrabbit.ocm.testmodel.enumeration.Planet; + +/** + * Test Simple Enumeration mappings + * + * @author Boni Gopalan + */ +public class SimpleEnumerationTestBase extends TestCase +{ + private final static Log logger = LogFactory.getLog(SimpleEnumerationTestBase.class); + ObjectContentManager ocm; + public SimpleEnumerationTestBase(ObjectContentManager ocm){ + this.ocm = ocm; + } + + public void testMapSimpleEnumeration() + { + try + { + // -------------------------------------------------------------------------------- + // Create and store an object graph in the repository + // -------------------------------------------------------------------------------- + Odyssey odyssey = new Odyssey(); + odyssey.setPath("/odesseyToMars"); + odyssey.setGoingTo(Planet.MARS); + odyssey.setStartingFrom(Planet.EARTH); + odyssey.setStops(getStops()); + ocm.insert(odyssey); + Odyssey fbOdessey = (Odyssey)ocm.getObject("/odesseyToMars"); + assertTrue("Fetched back Enum did not match the saved data", fbOdessey.getGoingTo() == Planet.MARS); + assertTrue("Fetched back Enum did not match the saved data", fbOdessey.getStartingFrom() == Planet.EARTH); + assertTrue("Fetched back Enum did not match the saved Enum Collection Size", fbOdessey.getStops().size() == odyssey.getStops().size()); + List