Copy XmlGregorianCalendar to Date

153 views
Skip to first unread message

tgrip

unread,
Oct 19, 2010, 2:27:56 PM10/19/10
to beanlib
Hi,

I have a bean which has properties of type XmlGregorianCalendar and
want to convert it to a bean with property of same name of type
java.util.Date. However, the date is null after the conversion.
I added a DateTransformer, and checked in the isTransformable if
toClass.isAssignableFrom(Date.class) and in the transform method I
transform from XmlGregorianCalendar to the date.
What else do I have to do?

Thanks and best regards
Theo

Hanson Char

unread,
Oct 19, 2010, 2:48:40 PM10/19/10
to bea...@googlegroups.com
The custom DateTransformer you mentioned should work.   Maybe you can post the code or a unit test to demo why it doesn't ? 

tgrip

unread,
Oct 19, 2010, 2:58:23 PM10/19/10
to beanlib
The code is as follows:

// Transformer.java

BeanTransformerSpi transformer = new BeanTransformer(new
DateTransformerFactory());
BeanReplicator beanReplicator = new BeanReplicator(transformer);
beanReplicator.replicateBean(inputBean, OutputBean.class);

//DateTransformerFactory.java

@Override
public CustomBeanTransformerSpi
newCustomBeanTransformer(BeanTransformerSpi arg0) {
return new DateTransformer();
}

//DateTransformer.java
@Override
public boolean isTransformable(Object from, Class<?> toClass,
PropertyInfo propInfo) {
if (from == null) {
return false;
}
boolean toClassCorrecto = toClass.isAssignableFrom(Date.class);
boolean fromCorrecto =
XMLGregorianCalendar.class.isAssignableFrom(from.getClass());
return toClassCorrecto;
}

@SuppressWarnings("unchecked")
@Override
public <T> T transform(Object from, Class<T> toClass, PropertyInfo
propInfo) {
XMLGregorianCalendar cal = (XMLGregorianCalendar) from;
return (T) cal.toGregorianCalendar().getTime();
}

Thanks
Theo

Hanson Char

unread,
Oct 19, 2010, 3:52:55 PM10/19/10
to bea...@googlegroups.com
It seems  "fromCorrecto" is computed but not used.  Can you attach actual java source files that can be compiled and executed ?

tgrip

unread,
Oct 19, 2010, 5:55:34 PM10/19/10
to beanlib
Sure, I am adding the test case and all source file used.
The files can be downloaded at https://www.yousendit.com/download/ZGJjK3BDSWVRR2RjR0E9PQ
I think you can not attach files here. To make it smaller, I haven't
sent Junit 4, Log4J or beanlib jars. I am using the latest version of
bean lib, 5.0.3.beta.
I debugged the code, and I see why it does not copy it, because it
enters in JavaBeanDetailedPropertyFilter, where it checks if date is
assignable from XmlGregorianCalender. Of course it shouldn't check
this,

Thanks
Theo

Hanson Char

unread,
Oct 20, 2010, 3:07:14 AM10/20/10
to bea...@googlegroups.com
The default behavior of Beanlib is to only allow properties of "related" types to be propagated.  Yet XmlGregorianCalender is not a Date.  But you can override this.  For example, the changes below would work via configuring a "MyDetailedPropertyFilter":

public class Transformer {

public OutputBean transformToOutputBean(InputBean inputBean) {

BeanTransformerSpi transformer = new BeanTransformer(new DateTransformerFactory())

    .initDetailedPropertyFilter(new MyDetailedPropertyFilter())

;

BeanReplicator beanReplicator = new BeanReplicator(transformer);

return beanReplicator.replicateBean(inputBean, OutputBean.class);

}

}


// ==================================

// File MyDetailedPropertyFilter.java

// ==================================


import java.lang.reflect.Method;

import java.util.Date;


import javax.xml.datatype.XMLGregorianCalendar;


import net.sf.beanlib.provider.JavaBeanDetailedPropertyFilter;


public class MyDetailedPropertyFilter extends JavaBeanDetailedPropertyFilter 

{

@Override public boolean propagate(

            String propertyName, 

            Object fromBean, 

            Method readerMethod, 

            Object toBean, 

            Method setterMethod) 

{

Class<?> returnType = readerMethod.getReturnType();

Class<?> paramType = setterMethod.getParameterTypes()[0];

// allow cross types propagation from GregorianCalendar to Date via custom transformer

return XMLGregorianCalendar.class.isAssignableFrom(returnType) && Date.class.isAssignableFrom(paramType)

    || paramType.isAssignableFrom(returnType);

Hanson Char

unread,
Oct 20, 2010, 3:09:26 AM10/20/10
to bea...@googlegroups.com
>public class MyDetailedPropertyFilter extends JavaBeanDetailedPropertyFilter 

Or simply:

  public class MyDetailedPropertyFilter implements DetailedPropertyFilter 


But you get the idea.


h

Reply all
Reply to author
Forward
0 new messages