incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r515772 - /incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/beanconverter/impl/ReferenceBeanConverterImpl.java
Date Wed, 07 Mar 2007 21:19:15 GMT
Author: clombart
Date: Wed Mar  7 13:19:14 2007
New Revision: 515772

URL: http://svn.apache.org/viewvc?view=rev&rev=515772
Log:
continue to work on  GRFT-40 : 
* Add the class ReferenceBeanConverter : map a Reference jcr property into a bean field (readonly).
* Add more unit tests on uuid & reference support. 

Added:
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/beanconverter/impl/ReferenceBeanConverterImpl.java

Added: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/beanconverter/impl/ReferenceBeanConverterImpl.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/beanconverter/impl/ReferenceBeanConverterImpl.java?view=auto&rev=515772
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/beanconverter/impl/ReferenceBeanConverterImpl.java
(added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/beanconverter/impl/ReferenceBeanConverterImpl.java
Wed Mar  7 13:19:14 2007
@@ -0,0 +1,112 @@
+/*
+ * 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.portals.graffito.jcr.persistence.beanconverter.impl;
+
+import javax.jcr.ItemNotFoundException;
+import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
+import javax.jcr.PropertyType;
+import javax.jcr.Session;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.portals.graffito.jcr.exception.JcrMappingException;
+import org.apache.portals.graffito.jcr.exception.PersistenceException;
+import org.apache.portals.graffito.jcr.exception.RepositoryException;
+import org.apache.portals.graffito.jcr.mapper.Mapper;
+import org.apache.portals.graffito.jcr.mapper.model.BeanDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.ClassDescriptor;
+import org.apache.portals.graffito.jcr.mapper.model.FieldDescriptor;
+import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverterProvider;
+import org.apache.portals.graffito.jcr.persistence.beanconverter.BeanConverter;
+import org.apache.portals.graffito.jcr.persistence.objectconverter.ObjectConverter;
+import org.apache.portals.graffito.jcr.reflection.ReflectionUtils;
+/**
+ * 
+ * Map a bean attribute into a reference jcr property. It is not possible to update direclty
the referenced bean. 
+ * Only the corresponding uuid can be updated in the main object. The modifications on the
referenced bean attributes are ignored
+ * 
+ * 
+ * @author <a href="mailto:christophe.lombart@gmail.com">Lombart Christophe </a>
+ *
+ */
+public class ReferenceBeanConverterImpl extends AbstractBeanConverterImpl  implements BeanConverter
{
+
+	private final static Log log = LogFactory.getLog(ReferenceBeanConverterImpl.class);
+	
+	public ReferenceBeanConverterImpl(Mapper mapper, ObjectConverter objectConverter, AtomicTypeConverterProvider
atomicTypeConverterProvider) 
+	{
+		super(mapper, objectConverter, atomicTypeConverterProvider);	
+	}
+
+	public void insert(Session session, Node parentNode, BeanDescriptor beanDescriptor, ClassDescriptor
beanClassDescriptor, Object object, ClassDescriptor parentClassDescriptor, Object parent)
+			throws PersistenceException, RepositoryException, 	JcrMappingException 
+	{
+		updateReferenceProperty(parentNode, beanDescriptor, beanClassDescriptor, object); 
+		
+	}
+
+	public void update(Session session, Node parentNode, BeanDescriptor beanDescriptor, ClassDescriptor
beanClassDescriptor, Object object, ClassDescriptor parentClassDescriptor, Object parent)
+			throws PersistenceException, RepositoryException,	JcrMappingException 
+	{
+		updateReferenceProperty(parentNode, beanDescriptor, beanClassDescriptor, object);
+	}
+
+	public Object getObject(Session session, Node parentNode, BeanDescriptor beanDescriptor,
ClassDescriptor beanClassDescriptor, Class beanClass, Object parent)
+			throws PersistenceException, RepositoryException,JcrMappingException 
+	{
+        try {
+			String uuid = parentNode.getProperty(beanDescriptor.getJcrName()).getString();
+			String path = session.getNodeByUUID(uuid).getPath();
+			
+			return objectConverter.getObject(session, path);
+		} catch (Exception e) {
+			return null;		
+			
+		}
+		
+	}
+
+	public void remove(Session session, Node parentNode, BeanDescriptor beanDescriptor, ClassDescriptor
beanClassDescriptor, Object object, ClassDescriptor parentClassDescriptor, Object parent)
+	          throws PersistenceException,	RepositoryException, JcrMappingException 
+	{
+		updateReferenceProperty(parentNode, beanDescriptor, beanClassDescriptor, null);	
+	}
+	
+	private void updateReferenceProperty(Node parentNode, BeanDescriptor beanDescriptor,ClassDescriptor
beanClassDescriptor, Object object) {
+		try {
+			if (object == null)
+			{
+				parentNode.setProperty(beanDescriptor.getJcrName(), (Value) null);
+			}
+			
+			FieldDescriptor fieldDescriptor = beanClassDescriptor.getUuidFieldDescriptor();
+			if (fieldDescriptor == null)
+			{
+				throw new JcrMappingException("The bean doesn't have an uuid - classdescriptor : " +
beanClassDescriptor.getClassName());
+			}
+			
+			String uuid = (String) ReflectionUtils.getNestedProperty(object, fieldDescriptor.getFieldName());
+			parentNode.setProperty(beanDescriptor.getJcrName(), uuid, PropertyType.REFERENCE);
+		} catch (Exception e) {
+			throw new PersistenceException("Impossible to insert the bean attribute into the repository",
e);
+		}
+	}
+
+}



Mime
View raw message