jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Jackrabbit: Mapping Atomic Fields (page edited)
Date Wed, 20 Feb 2008 20:41:00 GMT
Mapping Atomic Fields (JCR) edited by Jukka Zitting
      Page: http://cwiki.apache.org/confluence/display/JCR/Mapping+Atomic+Fields
   Changes: http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=75359&originalVersion=1&revisedVersion=2

Comment:
---------------------------------------------------------------------



Change summary:
---------------------------------------------------------------------



Change summary:
---------------------------------------------------------------------



Change summary:
---------------------------------------------------------------------



Change summary:
---------------------------------------------------------------------



Content:
---------------------------------------------------------------------

The field-descriptor maps a bean attribute based on a Java primitive           type into a
JCR property. By default, the persistence manager uses           the correct mapping in function
of the attribute type (see below           the section "Supported Types").

Based on our model defined here,           the following field-descriptor maps the bean field
"title"           (String type) into the JCR property "my:title".

h2. Supported Types

 It is not necessary to specify the type in the field-descriptor.           The Persistence
Manager uses the java introspection to get           information on each atomic field.
|| Java Type || Jcr Type ||
| String | STRING |
| Boolean, boolean | BOOLEAN |
| Double, double | DOUBLE |
| Integer, int | DOUBLE |
| Long, long | LONG |
| byte\[\] | BINARY |
| java.io.InputStream | BINARY |
| java.util.Calendar | LONG (corresponding to Calendar.getTimeInMillis() |
| java.sql.Timestamp | LONG (corresponding to Timestamp.getTime() |
| java.util.Date | LONG (corresponding to java.util.Date.getTime() |

 Due to some issues with Jackrabbit (mainly with xpath queries),           Calendar, Timestamp
and date are converted into JCR LONG.           We plan to add other converters for those
types in the next release.

h2. Using Another Atomic Type Converter

The OCM framework gives you the freedom to choose another kind of          mapping for atomic
fields. For example, you can convert          java.util.Date bean field into a JCR Date type
instead of a          JCR Long type. This can be done by writing your own atomic type    
     converter class.

Let's start with a simple example. If you want to use a mapping           strategy which convert
a boolean bean field into a JCR Long type,           you have to make the following steps:

h3. Specify the converter class in the field descriptor

{code}
<class-descriptor
    className="org.apache.jackrabbit.ocm.testmodel.Atomic"
    jcrType="nt:unstructured">
  <field-descriptor
      fieldName="int2boolean" jcrName="int2boolean"
      converter="org.apache.jackrabbit.ocm.persistence.atomic.Int2BooleanTypeConverterImpl"
/>
</class-descriptor>
{code}

h3. Implement the converter class

Use the interface             org.apache.jackrabbit.ocm.persistence.atomic.AtomicTypeConverter

{code:title=Int2BooleanTypeConverterImpl.java}
package org.apache.jackrabbit.ocm.persistence.atomic;

import javax.jcr.Value;
import javax.jcr.ValueFactory;

import org.apache.jackrabbit.ocm.exception.IncorrectAtomicTypeException;
import org.apache.jackrabbit.ocm.persistence.atomictypeconverter.AtomicTypeConverter;

/**
 * This is a simple converter which convert a boolean field value into a jcr long property.
 *
 * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
 */
public class Int2BooleanTypeConverterImpl implements AtomicTypeConverter
{
  /**
   *
   * @see org.apache.jackrabbit.ocm.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.jackrabbit.ocm.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.jackrabbit.ocm.persistence.atomictypeconverter.AtomicTypeConverter#getStringValue(java.lang.Object)
   */
  public String getStringValue(Object object)
  {

    return ((Boolean) object).booleanValue() ? "1" : "0";
  }

}
{code}


---------------------------------------------------------------------
CONFLUENCE INFORMATION
This message is automatically generated by Confluence

Unsubscribe or edit your notifications preferences
   http://cwiki.apache.org/confluence/users/viewnotifications.action

If you think it was sent incorrectly contact one of the administrators
   http://cwiki.apache.org/confluence/administrators.action

If you want more information on Confluence, or have a bug to report see
   http://www.atlassian.com/software/confluence



Mime
View raw message