jackrabbit-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yonder <zy...@yahoo.com.cn>
Subject Arbitrary property support
Date Fri, 13 Jun 2008 02:32:38 GMT
nt:unstructured allows any number of properties with any names. but with OCM, we can only set
 predefined properties on a jcr node. It's not flexible sometime. for example, a CmsObject
may have some optional properties then user can place owner information or something else.
I have added a callback feature in ObjectConverterImpl that allows some annotated method in
an ocm object will be callback during saving or retrieving the object, like this:

    //ObjectConverterImpl.java
    public void insert(Session session, Node parentNode, String nodeName, Object object) {
        ...
        simpleFieldsHelp.storeSimpleFields(session, object, classDescriptor, objectNode);
        insertBeanFields(session, object, classDescriptor, objectNode);
        insertCollectionFields(session, object, classDescriptor, objectNode);
        simpleFieldsHelp.refreshUuidPath(session, classDescriptor, objectNode, object);
        
        CallbackHelper.save(object,objectNode);
    }

theh I create two new annotations:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD })
public @interface Retrieve
{}


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD })
public @interface Save
{}

So in the ocm object, we can place following codes :
   
   private Map<String, String> optionalProperties = new HashMap<String, String>();
   @Save
    public void saveOptionalProperties(javax.jcr.Node node)
    {
        //...
    }
    @Retrieve
    public void readOptionalProperties(javax.jcr.Node node)
    {
       //...
    }

CallbackHelper is very simple:

public class CallbackHelper
{
    public static void save(Object obj,Node node)
    {
        Method m = getMethod(obj,Save.class);
        if(m!=null)
        {
           //invode the method
        }
    }
    
    public static void retrieve(Object obj,Node node)
    {
        Method m = getMethod(obj,Retrieve.class);
        if(m!=null)
        {
            //invoke the method
        }
    }
    
    private static Method getMethod(Object object , Class annClass)
    {
        Method[] methods = object.getClass().getMethods();
        for(Method m : methods)
        {
            if(m.getAnnotation(annClass)!=null) return m;
        }
        return null;
    }
}

This solution helped me. 

WDYT? 


      ___________________________________________________________ 
 雅虎邮箱,您的终生邮箱! 
http://cn.mail.yahoo.com/

Mime
View raw message