package com.icil.booking.service.util; import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.icil.fw.logging.ICILLogger;
import com.icil.fw.logging.ICILLoggerFactory; import net.sf.cglib.beans.BeanCopier;
import net.sf.cglib.core.Converter; public class CglibBeanCopierUtils { private static final ICILLogger logger = ICILLoggerFactory
.getInstance(CglibBeanCopierUtils.class); public static Map<String, BeanCopier> beanCopierMap = new HashMap<String, BeanCopier>(); /**
* @Title: copyProperties
* @Description: TODO(bean属性转换)
* @param source
* 资源类
* @param target
* 目标类
* @author yushaojian
* @date 2015年11月25日下午4:56:44
*/
public static void copyProperties(Object source, Object target) {
String beanKey = generateKey(source.getClass(), target.getClass());
BeanCopier copier = null;
if (!beanCopierMap.containsKey(beanKey)) {
copier = BeanCopier.create(source.getClass(), target.getClass(),
true);
beanCopierMap.put(beanKey, copier);
} else {
copier = beanCopierMap.get(beanKey);
}
copier.copy(source, target, new Converter() {
private final Map<String, BeanCopier> bcMap = new HashMap<String, BeanCopier>(); @Override
public Object convert(Object sourcePropVal, Class targetPropClazz,
Object targetSetMethodName) {
/*logger.debug("======>>>>> sourcePropVal " + sourcePropVal
+ "======>>>>>targetPropClazz " + targetPropClazz
+ "======>>>>>targetSetMethodName "
+ targetSetMethodName);*/
/*if (BigDecimal.class.equals(targetPropClazz)) {
BigDecimal bd = null;
if (sourcePropVal != null && !"".equals(sourcePropVal)) {
bd = new BigDecimal(sourcePropVal.toString());
bd=bd.setScale(4, BigDecimal.ROUND_HALF_UP);
}
return bd;
} else if (Integer.class.equals(targetPropClazz)) {
Integer value = null;
if (sourcePropVal != null && !"".equals(sourcePropVal)) {
value = Integer.parseInt(sourcePropVal.toString());
}
return value;
} else if (Date.class.equals(targetPropClazz)) {
Date date = null;
if(sourcePropVal!=null && !"".equals(sourcePropVal)){
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
try {
date = sdf.parse((String)sourcePropVal);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return date;
}else if (int.class.equals(targetPropClazz)){
int value =0 ;
if (sourcePropVal != null && !"".equals(sourcePropVal)) {
value = Integer.parseInt(sourcePropVal.toString());
}
return value;
}*/
return sourcePropVal;
}
});
} protected static Object convertListGenericType(Object sourcePropVal) {
// TODO Auto-generated method stub
return null;
} private static String generateKey(Class<?> class1, Class<?> class2) {
return class1.toString() + class2.toString();
}
}