`

po转vo

 
阅读更多

package com.eloancn.nback.common.utils;

 

import java.lang.reflect.Field;

import java.lang.reflect.Type;

import java.math.BigDecimal;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;

 

import org.apache.commons.lang.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

 

/**

 * VO PO 转换帮助类

 * 

 * @author nyy

 * @param <S> PO类

 * @param <T> VO类

 */

public class VoPoConvertUtil<S, T> {

protected Logger logger = LoggerFactory.getLogger(this.getClass());

private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public SimpleDateFormat getFormat() {

return format;

}

public void setFormat(SimpleDateFormat format) {

this.format = format;

}

private List<String> excludeFields = null;

/**

* 构造VO PO 转换帮助类

*/

public VoPoConvertUtil() {

}

 

/**

* 构造VO PO 转换帮助类

* @param excludeFields 排除字段集合

*/

public VoPoConvertUtil(List<String> excludeFields) {

this.excludeFields = excludeFields;

}

 

/**

* PO转VO

* @param source PO对象

* @param target VO对象

*/

public void convert(S source, T target) {

if (source == null || target == null) {

return;

}

convert(source, target, source.getClass(), target.getClass());

}

/**

* PO转VO 自定义时间类型属性样式

* @param source PO对象

* @param target VO对象

*/

public void convert(S source, T target,String dateFormat) {

if (source == null || target == null) {

return;

}

convert(source, target, source.getClass(), target.getClass(),dateFormat);

}

/**

* 自定义时间类型属性样式

* @param source

* @param target

* @param sc

* @param tc

* @param dateFormat 时间类型属性样式

* @author YYJ

* @date 2016年6月1日

*/

private void convert(S source, T target, Class<?> sc, Class<?> tc,String dateFormat) {

SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);

Class<?> scs = sc.getSuperclass();

if (scs != null) {

convert(source, target, scs, tc.getSuperclass(), dateFormat);

}

for (Field field : tc.getDeclaredFields()) {

field.setAccessible(true);

if (notFlterField(field.getName())) {

try {

Field sourceField = null;

try {

sourceField = sc.getDeclaredField(field.getName());

} catch (Exception e) {

//vo对象中有的变量在po中不存在,不需要转换,不需要打印错误日志

}

if(sourceField != null){

sourceField.setAccessible(true);

Type type = sourceField.getGenericType();

Object value = sourceField.get(source);

if (value == null) {

continue;

} else if (type.equals(String.class)) {

field.set(target, value);

} else if (type.equals(BigDecimal.class)) {

field.set(target, String.valueOf(value));

} else if (type.equals(Date.class)) {

String ddd = fmt.format((Date) value);

field.set(target, ddd);

} else if (type.equals(Long.class) || type.equals(long.class)) {

field.set(target, String.valueOf(value));

} else if (type.equals(Double.class) || type.equals(double.class)) {

field.set(target, String.valueOf(value));

} else if (type.equals(Integer.class) || type.equals(int.class)) {

field.set(target, String.valueOf(value));

} else if (type.equals(Float.class) || type.equals(float.class)) {

field.set(target, String.valueOf(value));

} else if (type.equals(Byte.class) || type.equals(byte.class)) {

field.set(target, String.valueOf(value));

}

}

} catch (Exception e) {

logger.info("Po转VO发生了异常", e);

}

}

}

}

private void convert(S source, T target, Class<?> sc, Class<?> tc) {

Class<?> scs = sc.getSuperclass();

if (scs != null) {

convert(source, target, scs, tc.getSuperclass());

}

for (Field field : tc.getDeclaredFields()) {

field.setAccessible(true);

if (notFlterField(field.getName())) {

try {

Field sourceField = null;

try {

sourceField = sc.getDeclaredField(field.getName());

} catch (Exception e) {

//vo对象中有的变量在po中不存在,不需要转换,不需要打印错误日志

}

if(sourceField != null){

sourceField.setAccessible(true);

Type type = sourceField.getGenericType();

Object value = sourceField.get(source);

if (value == null) {

continue;

} else if (type.equals(String.class)) {

field.set(target, value);

} else if (type.equals(BigDecimal.class)) {

field.set(target, String.valueOf(value));

} else if (type.equals(Date.class)) {

field.set(target, format.format((Date) value));

} else if (type.equals(Long.class) || type.equals(long.class)) {

field.set(target, String.valueOf(value));

} else if (type.equals(Double.class) || type.equals(double.class)) {

field.set(target, String.valueOf(value));

} else if (type.equals(Integer.class) || type.equals(int.class)) {

field.set(target, String.valueOf(value));

} else if (type.equals(Float.class) || type.equals(float.class)) {

field.set(target, String.valueOf(value));

} else if (type.equals(Byte.class) || type.equals(byte.class)) {

field.set(target, String.valueOf(value));

}

}

} catch (Exception e) {

logger.info("Po转VO发生了异常", e);

}

}

}

}

 

/**

* VO 还原 PO

* @param source

*            VO 对象

* @param target

*            PO 对象

*/

public void revert(T source, S target) {

if (source == null || target == null) {

return;

}

revert(source, target, source.getClass(), target.getClass());

}

 

private void revert(T source, S target, Class<?> sc, Class<?> tc) {

Class<?> scs = sc.getSuperclass();

if (scs != null) {

revert(source, target, scs, tc.getSuperclass());

}

for (Field field : sc.getDeclaredFields()) {

field.setAccessible(true);

if (notFlterField(field.getName())) {

try {

Field targetField = tc.getDeclaredField(field.getName());

targetField.setAccessible(true);

Type type = targetField.getGenericType();

String value = null;

try {

value = (String) field.get(source);

} catch (Exception e) {

value = String.valueOf(field.get(source));

}

if (value == null || (type.equals(String.class) == false && StringUtils.isEmpty(value.trim()))) {

continue;

} else if (type.equals(String.class)) {

targetField.set(target, value);

} else {

if (type.equals(Long.class)) {

targetField.set(target, Long.valueOf(value));

} else if (type.equals(Double.class) || type.equals(double.class)) {

targetField.set(target, Double.valueOf(value));

} else if (type.equals(Integer.class) || type.equals(int.class)) {

targetField.set(target, Integer.valueOf(value));

} else if (type.equals(Float.class) || type.equals(float.class)) {

targetField.set(target, Float.valueOf(value));

} else if (type.equals(Byte.class) || type.equals(byte.class)) {

targetField.set(target, Byte.valueOf(value));

} else if (type.equals(Date.class)) {

if (", ".equals(value) == false) {

if (value.split(" ").length < 2) {

value = value + " 00:00:00";

}

targetField.set(target, format.parse(value));

}

} else if (type.equals(BigDecimal.class)) {

BigDecimal bigDecimal = new BigDecimal(value);

bigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP);

targetField.set(target, bigDecimal);

}

}

} catch (Exception e) {

logger.info("Vo转Po发生了异常", e);

}

}

}

}

 

/**

* 不排除的字段

* @param fieldName

* @return boolean

* @exception

* @since 1.0.0

*/

private boolean notFlterField(String fieldName) {

return "serialVersionUID".equals(fieldName) == false

&& (excludeFields == null || !excludeFields.contains(fieldName));

}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics