incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r472204 - in /incubator/graffito/trunk/jcr/jcr-mapping/src: dtd/ java/org/apache/portals/graffito/jcr/mapper/impl/ java/org/apache/portals/graffito/jcr/mapper/model/ java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/ java...
Date Tue, 07 Nov 2006 19:06:16 GMT
Author: clombart
Date: Tue Nov  7 11:06:15 2006
New Revision: 472204

URL: http://svn.apache.org/viewvc?view=rev&rev=472204
Log:
Give the possibility to use custom atomic type converter (see issue GRFT-117) 

Added:
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/atomic/Int2BooleanTypeConverterImpl.java
Modified:
    incubator/graffito/trunk/jcr/jcr-mapping/src/dtd/graffito-jcr-mapping.dtd
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterDescriptorReader.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/FieldDescriptor.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/AtomicTypeConverter.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/ObjectConverterImpl.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-atomic.xml
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/atomic/AtomicTest.java
    incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/Atomic.java

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/dtd/graffito-jcr-mapping.dtd
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/dtd/graffito-jcr-mapping.dtd?view=diff&rev=472204&r1=472203&r2=472204
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/dtd/graffito-jcr-mapping.dtd (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/dtd/graffito-jcr-mapping.dtd Tue Nov  7 11:06:15
2006
@@ -61,6 +61,7 @@
     * jcrName : the jcr property name (optional). If it is not defined, fieldname is used
to specify the jcr property name
     * id : specifies if the field is an id. Id is optional. If the class contains an id field,
some process can run faster
     * path : specified if the fiels is the object path
+    * converter : AtomicTypeConverter class name to used for this field descriptor. If not
defined, a default one is provided by default in function of the field type
     * jcrType: specifies the JCR property type (supported jcr property types: STRING, DATE,
LONG, DOUBLE, BOOLEAN), optional
     * jcrAutoCreated: specifies if this JCR property will be autocreated (true/false / default
is false), optional
     * jcrMandatory: specifies if this JCR property is mandatory (true/false / default is
false), optional
@@ -76,6 +77,7 @@
 	jcrName CDATA #IMPLIED 
 	id (true | false) "false"
 	path (true | false) "false"
+	converter CDATA #IMPLIED
     jcrType (String | Date | Long | Double | Boolean | Binary) #IMPLIED
     jcrAutoCreated (true | false) "false"
     jcrMandatory (true | false) "false"

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterDescriptorReader.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterDescriptorReader.java?view=diff&rev=472204&r1=472203&r2=472204
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterDescriptorReader.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/impl/DigesterDescriptorReader.java
Tue Nov  7 11:06:15 2006
@@ -29,8 +29,6 @@
 import org.apache.portals.graffito.jcr.mapper.model.FieldDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.ImplementDescriptor;
 import org.apache.portals.graffito.jcr.mapper.model.MappingDescriptor;
-import org.apache.portals.graffito.jcr.reflection.ReflectionUtils;
-import org.xml.sax.SAXParseException;
 
 /**
  * Helper class that reads the xml mapping file and load all class descriptors into memory
(object graph)
@@ -88,7 +86,7 @@
 			digester.addSetNext("graffito-jcr/class-descriptor", "addClassDescriptor");		
 
 			// --------------------------------------------------------------------------------
-			// Rules used for the field-descriptor element
+			// Rules used for the implement-descriptor element
 			// --------------------------------------------------------------------------------
 			digester.addObjectCreate("graffito-jcr/class-descriptor/implement-descriptor", ImplementDescriptor.class);
 			digester.addSetProperties("graffito-jcr/class-descriptor/implement-descriptor");

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/FieldDescriptor.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/FieldDescriptor.java?view=diff&rev=472204&r1=472203&r2=472204
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/FieldDescriptor.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/mapper/model/FieldDescriptor.java
Tue Nov  7 11:06:15 2006
@@ -42,6 +42,7 @@
     private ClassDescriptor classDescriptor;
     private boolean id;
     private boolean path;
+    private String converter;
    
 
     /**
@@ -106,7 +107,24 @@
         this.jcrName = jcrName;
     }
 
+    
     /**
+     * 
+     * @return The atomic type converter to use, can be null
+     */
+    public String getConverter() {
+		return converter;
+	}
+
+    /**
+     * Set the atomic converter 
+     * @param converter the atomic converter to use
+     */
+	public void setConverter(String converter) {
+		this.converter = converter;
+	}
+
+	/**
      *
      * @return the associated class descriptor
      */

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/AtomicTypeConverter.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/AtomicTypeConverter.java?view=diff&rev=472204&r1=472203&r2=472204
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/AtomicTypeConverter.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/atomictypeconverter/AtomicTypeConverter.java
Tue Nov  7 11:06:15 2006
@@ -51,7 +51,8 @@
 	public Object getObject(Value value);
 	
 	/**
-	 * Get the string converted value
+	 * Get the string converted value. This is mainly used to build xpath expressions 
+	 * 
 	 * @param object The object value
 	 * @return The string converted value	 
 	 */

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/ObjectConverterImpl.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/ObjectConverterImpl.java?view=diff&rev=472204&r1=472203&r2=472204
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/ObjectConverterImpl.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/java/org/apache/portals/graffito/jcr/persistence/objectconverter/impl/ObjectConverterImpl.java
Tue Nov  7 11:06:15 2006
@@ -893,16 +893,27 @@
 
 	private AtomicTypeConverter getAtomicTypeConverter(FieldDescriptor fd, Object object, String
fieldName) {
 		Class fieldTypeClass = null;
-		if (null != fd.getFieldTypeClass()) {
-			fieldTypeClass = fd.getFieldTypeClass();
-		} else if (null != object) {
-			fieldTypeClass = ReflectionUtils.getPropertyType(object, fieldName);
+		// Check if an atomic converter is assigned to the field converter
+		String atomicTypeConverterClass = fd.getConverter();
+		if (null != atomicTypeConverterClass)
+		{
+			return (AtomicTypeConverter) ReflectionUtils.newInstance(atomicTypeConverterClass);
 		}
+		else
+		{
+			// Get the default atomic converter in function of the classname
+			if (null != fd.getFieldTypeClass()) {
+				fieldTypeClass = fd.getFieldTypeClass();
+			} else if (null != object) {
+				fieldTypeClass = ReflectionUtils.getPropertyType(object, fieldName);
+			}
 
-		if (null != fieldTypeClass) {
-			return this.atomicTypeConverterProvider.getAtomicTypeConverter(fieldTypeClass);
-		} else {
-			return NULL_CONVERTER;
+			if (null != fieldTypeClass) {
+				return this.atomicTypeConverterProvider.getAtomicTypeConverter(fieldTypeClass);
+			} else {
+				return NULL_CONVERTER;
+			}
+			
 		}
 	}
 

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-atomic.xml
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-atomic.xml?view=diff&rev=472204&r1=472203&r2=472204
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-atomic.xml (original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test-config/jcrmapping-atomic.xml Tue Nov
 7 11:06:15 2006
@@ -17,6 +17,7 @@
 		<field-descriptor fieldName="doublePrimitive" jcrName="doublePrimitive" />
 		<field-descriptor fieldName="inputStream" jcrName="inputStream" />
 		<field-descriptor fieldName="timestamp" jcrName="timestamp" />
+		<field-descriptor fieldName="int2boolean" jcrName="int2boolean" converter="org.apache.portals.graffito.jcr.persistence.atomic.Int2BooleanTypeConverterImpl"
/>
 	</class-descriptor>
 
     <class-descriptor

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/atomic/AtomicTest.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/atomic/AtomicTest.java?view=diff&rev=472204&r1=472203&r2=472204
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/atomic/AtomicTest.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/atomic/AtomicTest.java
Tue Nov  7 11:06:15 2006
@@ -88,6 +88,7 @@
             a.setIntPrimitive(200);
             a.setString("Test String");
             a.setDate(date);
+            a.setInt2boolean(true);
             
             byte[] content = "Test Byte".getBytes();
             a.setByteArray(content);
@@ -135,6 +136,7 @@
             assertNotNull("Incorrect input stream primitive", a.getInputStream());
             assertNotNull("Incorrect timestamp", a.getTimestamp());
             assertTrue("Invalid timestamp value ", a.getTimestamp().getTime() == now);  
         
+            assertTrue("Invalid int2boolean value ", a.isInt2boolean());
             
         }
         catch (Exception e)

Added: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/atomic/Int2BooleanTypeConverterImpl.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/atomic/Int2BooleanTypeConverterImpl.java?view=auto&rev=472204
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/atomic/Int2BooleanTypeConverterImpl.java
(added)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/persistence/atomic/Int2BooleanTypeConverterImpl.java
Tue Nov  7 11:06:15 2006
@@ -0,0 +1,89 @@
+/*
+ * 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.atomic;
+
+import javax.jcr.Value;
+import javax.jcr.ValueFactory;
+
+import org.apache.portals.graffito.jcr.exception.IncorrectAtomicTypeException;
+import org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter;
+
+/**
+ * This is a simple converter which convert a boolean field value into a jcr long property
(int type is not defined in the jcr spec).
+ *
+ * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
+ * 
+ */
+public class Int2BooleanTypeConverterImpl implements AtomicTypeConverter
+{
+	/**
+	 * 
+	 * @see org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter#getValue(java.lang.Object)
+	 */
+	public Value getValue(ValueFactory valueFactory, Object propValue)
+	{
+		if (propValue == null)
+		{
+			return null;
+		}
+		boolean value = ((Boolean) propValue).booleanValue();
+		int jcrValue = 0;
+		
+		if (value)
+		{
+			jcrValue = 1;			
+		}
+		return valueFactory.createValue(jcrValue);
+	}
+	
+
+    /**
+     * 
+     * @see org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter#getObject(javax.jcr.Value)
+     */
+	public Object getObject(Value value)
+    {
+    	try
+    	{
+    		long jcrValue = value.getLong();
+    		if (jcrValue == 1)
+    		{
+		       return new Boolean(true);
+    		}
+    		else
+    		{
+    		   return new Boolean(false);
+    		}
+		}
+		catch (Exception e)
+		{
+			throw new IncorrectAtomicTypeException("Impossible to convert the value : " + value.toString()
 , e);
+		}
+    }
+	
+	/**
+	 * 
+	 * @see org.apache.portals.graffito.jcr.persistence.atomictypeconverter.AtomicTypeConverter#getStringValue(java.lang.Object)
+	 */
+	public String getStringValue(Object object)
+	{
+		
+		return ((Boolean) object).booleanValue() ? "1" : "0"; 
+	}
+
+}

Modified: incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/Atomic.java
URL: http://svn.apache.org/viewvc/incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/Atomic.java?view=diff&rev=472204&r1=472203&r2=472204
==============================================================================
--- incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/Atomic.java
(original)
+++ incubator/graffito/trunk/jcr/jcr-mapping/src/test/org/apache/portals/graffito/jcr/testmodel/Atomic.java
Tue Nov  7 11:06:15 2006
@@ -44,6 +44,7 @@
     private double doublePrimitive;
     private InputStream inputStream;
     private Timestamp timestamp;
+    private boolean int2boolean;
     private Collection multiValue;
     
     
@@ -150,7 +151,18 @@
     {
         this.timestamp = timestamp;
     }
+        
     
+	public boolean isInt2boolean() 
+	{
+		return int2boolean;
+	}
+	
+	public void setInt2boolean(boolean int2boolean) 
+	{
+		this.int2boolean = int2boolean;
+	}
+	
 	public Collection getMultiValue()
 	{
 		return multiValue;



Mime
View raw message