package com.ygsoft.czp.ticketsys.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.ygsoft.czp.model.AcUser;
import com.ygsoft.czp.model.WebCzpCzrw;
import com.ygsoft.czp.test.common.ClassPropertyBean;
/**
* 进行一些JAVABEAN的相关操作 〖功能模块〗: BeanUtil(功能) 〖目 的〗: 〖作 者〗: * 〖创建日期〗: 〖版 本〗: 1.00<br>
* 〖版权信息〗: * 〖更改记录〗: 更改时间、更改人、更改原因、更改内容<br>
*/
public class BeanUtil {
/**
* 将源对象中的数据拷贝到目标对象中
*
* @param target
* @param source
*/
public static void copyProperty(Object target, Object source) {
Field[] tarFields = target.getClass().getDeclaredFields();
Field[] sourFields = source.getClass().getDeclaredFields();
for (int i = 0; i < tarFields.length; i++) {
Field f = tarFields[i];
int n = f.getModifiers();
// 判断是FINAL字段
if (Modifier.isFinal(n)) {
continue;
}
f.setAccessible(true);
String tarType = tarFields[i].getType().toString();
String name = tarFields[i].getName();
for (int j = 0; j < sourFields.length; j++) {
Field temp = sourFields[j];
int m = temp.getModifiers();
if (Modifier.isFinal(m)) {
continue;
}
temp.setAccessible(true);
String sourType = sourFields[j].getType().toString();
String sourName = sourFields[j].getName();
if (name.equalsIgnoreCase(sourName)) {
try {
if (tarType.equals(sourType)) {
f.set(target, temp.get(source));
} else if ((tarType.indexOf("Date") != -1 || (tarType
.indexOf("Timestamp") != -1) )
&& sourType.indexOf("String") != -1) {
String time = (String) temp.get(source);
if (time != null && !(time.trim().equals(""))) {
String dateStr = "yyyy-MM-dd HH:mm:ss";
if (time.trim().length() < 11) {
dateStr = "yyyy-MM-dd";
}
SimpleDateFormat myFmt2 = new SimpleDateFormat( dateStr);
Date date = new Date();
try {
date = myFmt2.parse(time);
} catch (ParseException e) {
System.out
.println("copyProperty 时不能进行字符串到日期的转换");
e.printStackTrace();
}
f.set(target, date);
}
} else if (tarType.indexOf("String") != -1
&& (sourType.indexOf("Date") != -1 || (sourType
.indexOf("Timestamp") != -1)||(sourType
.indexOf("CnDate") != -1))) {
Date time = (Date) temp.get(source);
if (time != null) {
SimpleDateFormat myFmt2 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
f.set(target, myFmt2.format(time));
}
}else if(tarType.indexOf("String") != -1 && sourType.indexOf("Long") != -1){
if(temp.get(source)!=null){
f.set(target, ""+temp.get(source) );
}else{
f.set(target, "");
}
}else if(tarType.indexOf("Long") != -1 && sourType.indexOf("String") != -1){
if(temp.get(source)!=null && !temp.get(source).equals("")){
f.set(target, new Long(temp.get(source).toString()) );
}
}else if(tarType.indexOf("Double") != -1 && sourType.indexOf("String") != -1){
if(temp.get(source)!=null && !temp.get(source).equals("")){
f.set(target, new Double(temp.get(source).toString()) );
}
}else if(tarType.indexOf("String") != -1 && sourType.indexOf("Double") != -1){
if(temp.get(source)!=null){
f.set(target, ""+temp.get(source) );
}else{
f.set(target, "");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
temp.setAccessible(false);
}
f.setAccessible(false);
}
}
/**
* 将源对象中的数据拷贝到目标对象中
*
* @param target
* @param source
* @param namesList 如果传入某个属性,则某个属性不进行COPY
*
*/
public static void copyProperty(Object target, Object source,List namesList) {
Field[] tarFields = target.getClass().getDeclaredFields();
Field[] sourFields = source.getClass().getDeclaredFields();
for (int i = 0; i < tarFields.length; i++) {
Field f = tarFields[i];
int n = f.getModifiers();
// 判断是FINAL字段
if (Modifier.isFinal(n)) {
continue;
}
f.setAccessible(true);
String tarType = tarFields[i].getType().toString();
String name = tarFields[i].getName();
if(namesList.contains(name)){
continue;
}
for (int j = 0; j < sourFields.length; j++) {
Field temp = sourFields[j];
int m = temp.getModifiers();
if (Modifier.isFinal(m)) {
continue;
}
temp.setAccessible(true);
String sourType = sourFields[j].getType().toString();
String sourName = sourFields[j].getName();
if (name.equalsIgnoreCase(sourName)) {
try {
if (tarType.equals(sourType)) {
f.set(target, temp.get(source));
} else if ((tarType.indexOf("Date") != -1 || (tarType
.indexOf("Timestamp") != -1))
&& sourType.indexOf("String") != -1) {
String time = (String) temp.get(source);
if (time != null && !(time.trim().equals(""))) {
String dateStr = "yyyy-MM-dd HH:mm:ss";
if (time.trim().length() < 11) {
dateStr = "yyyy-MM-dd";
}
SimpleDateFormat myFmt2 = new SimpleDateFormat(
dateStr);
Date date = new Date();
try {
date = myFmt2.parse(time);
} catch (ParseException e) {
System.out
.println("copyProperty 时不能进行字符串到日期的转换");
e.printStackTrace();
}
f.set(target, date);
}
} else if (tarType.indexOf("String") != -1
&& (sourType.indexOf("Date") != -1 || (sourType
.indexOf("Timestamp") != -1))) {
Date time = (Date) temp.get(source);
if (time != null) {
SimpleDateFormat myFmt2 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
f.set(target, myFmt2.format(time));
}
}else if(tarType.indexOf("String") != -1 && sourType.indexOf("Long") != -1){
f.set(target, new Long(temp.get(source).toString()) );
}else if(tarType.indexOf("Long") != -1 && sourType.indexOf("String") != -1){
f.set(target, ""+temp.get(source) );
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
temp.setAccessible(false);
}
f.setAccessible(false);
}
}
//
private static ConcurrentHashMap<Class, Field[]> classPropertyMap = new ConcurrentHashMap<Class, Field[]>();
/**
* 由类得到类的所有属性,并进行缓存,使得再次使用时可以取到相关信息。
* @param cla
* @return
*/
public static Field[] getClassProperty(Class cla) {
Field[] fileds= classPropertyMap.get(cla);
if (fileds != null) {
return fileds;
}else {
fileds = cla.getDeclaredFields();
classPropertyMap.put(cla, fileds);
return fileds;
}
}
/**
* 将对象转换为一个XML字符串,返回
*
* @param target
* @param source
*/
public static String convertBeanToXML(Object source,String beanName) {
StringBuffer sb = new StringBuffer();
Field[] sourFields = getClassProperty(source.getClass());
beanName = beanName.equals("")?source.getClass().getName().substring(source.getClass().getName().lastIndexOf(".")+1):beanName;
sb.append("<"+beanName+">");
for (int j = 0; j < sourFields.length; j++) {
Field temp = sourFields[j];
int m = temp.getModifiers();
if (Modifier.isFinal(m)) {
continue;
}
temp.setAccessible(true);
String sourName = sourFields[j].getName();
sb.append("\r\n\t<"+sourName+">");
try {
if(temp.get(source) != null){
sb.append(temp.get(source) );
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
sb.append("</"+sourName+">");
temp.setAccessible(false);
}
sb.append("\r\n</"+beanName+">");
return sb.toString();
}
/**
* 将对象转换为一个XML字符串,返回
*
* @param target
* @param source
*/
public static String convertBeanToXML(Object source,String beanName,Map map) {
StringBuffer sb = new StringBuffer();
Field[] sourFields = getClassProperty(source.getClass());
beanName = beanName.equals("")?source.getClass().getName():beanName;
sb.append("<"+beanName+">");
for (int j = 0; j < sourFields.length; j++) {
Field temp = sourFields[j];
int m = temp.getModifiers();
if (Modifier.isFinal(m)) {
continue;
}
temp.setAccessible(true);
String sourName = sourFields[j].getName();
if(map.containsKey(sourName)){
sb.append("\r\n\t<"+sourName+">");
try {
if(temp.get(source) != null){
sb.append(temp.get(source) );
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
sb.append("</"+sourName+">");
}
temp.setAccessible(false);
}
sb.append("\r\n</"+beanName+">");
return sb.toString();
}
}