package com.server.game.utils;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Johny
*/
public class ObjectUtil {
private static Logger logger = LoggerFactory.getLogger(ObjectUtil.class);
public static Map removeCustom(Map m, String[] s) {
for (int i = 0; s != null && i < s.length; i++) {
m.remove(s[i]);
}
return m;
}
public static Map removeCommonAndCustom(Map m, String s[]) {
String commons[] = new String[] { "insertTime", "dataFlag", "insertPerson", "modifyPerson", "modifyTime", "deleteTime", "deletePerson" };
for (int i = 0; commons != null && i < commons.length; i++) {
m.remove(commons[i]);
}
for (int i = 0; s != null && i < s.length; i++) {
m.remove(s[i]);
}
return m;
}
public static Map removeCommon(Map m) {
String commons[] = new String[] { "insertTime", "dataFlag", "insertPerson", "modifyPerson", "modifyTime", "deleteTime", "deletePerson" };
for (int i = 0; commons != null && i < commons.length; i++) {
m.remove(commons[i]);
}
return m;
}
/**
* 删除 Map 结构中 value 为null的节点.
*
* @param m
* @return
*/
public static Map removeNull(Map m) {
List list = new ArrayList();
Iterator> it = m.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = it.next();
if (entry.getValue().equals("null")) {
list.add(entry.getKey());
}
}
for (int n = 0; n < list.size(); n++) {
m.remove(list.get(n));
}
return m;
}
/**
* 拷贝PO值至HashMap
*
* @param obj 数据源,PO retrun m 目标HashMap
*/
public static Map poToMap(Object obj) {
String tmp = obj.getClass().getName();
Map m = new HashMap();
try {
Class c;
if (tmp.indexOf("$") != -1) {
c = Class.forName(tmp.substring(0, tmp.indexOf("$")));
} else {
c = Class.forName(tmp);
}
PropertyDescriptor filed[] = PropertyUtils.getPropertyDescriptors(obj);
for (int i = 0; i < filed.length; i++) {
try {
Object v = PropertyUtils.getProperty(obj, filed[i].getName());
if ("java.lang.String".equals(filed[i].getPropertyType().getName())) {
m.put(filed[i].getName(), WebUtil.objToString(v, ""));
} else if ("java.lang.Double".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), 0.0);
} else {
m.put(filed[i].getName(), v);
}
} else if ("java.lang.Byte".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), "");
} else {
m.put(filed[i].getName(), v);
}
} else if ("java.lang.Integer".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), 0);
} else {
m.put(filed[i].getName(), v);
}
} else if ("java.util.Date".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), null);
} else {
m.put(filed[i].getName(), v);
}
}
} catch (java.lang.NoSuchMethodException e1) {}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return m;
}
/**
* 拷贝PO值至HashMap
*
* @param obj 数据源,PO retrun m 目标HashMap
*/
public static Map poToMap2(Object obj) {
String tmp = obj.getClass().getName();
Map m = new HashMap();
try {
Class c;
if (tmp.indexOf("$") != -1) {
c = Class.forName(tmp.substring(0, tmp.indexOf("$")));
} else {
c = Class.forName(tmp);
}
PropertyDescriptor filed[] = PropertyUtils.getPropertyDescriptors(obj);
for (int i = 0; i < filed.length; i++) {
try {
Object v = PropertyUtils.getProperty(obj, filed[i].getName());
if ("java.lang.String".equals(filed[i].getPropertyType().getName())) {
m.put(filed[i].getName(), WebUtil.objToString(v, ""));
} else if ("java.lang.Double".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), "0.0");
} else {
m.put(filed[i].getName(), String.valueOf(v));
}
} else if ("java.lang.Byte".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), "");
} else {
m.put(filed[i].getName(), String.valueOf(v));
}
} else if ("java.lang.Integer".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), "0");
} else {
m.put(filed[i].getName(), String.valueOf(v));
}
}
} catch (java.lang.NoSuchMethodException e1) {}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return m;
}
/**
* 拷贝PO值至HashMap
* @param m 目标HashMap
* @param obj 数据源,PO
*
*/
public static void poToMap(Map m, Object obj) {
String tmp = obj.getClass().getName();
try {
Class c;
if (tmp.indexOf("$") != -1) {
c = Class.forName(tmp.substring(0, tmp.indexOf("$")));
} else {
c = Class.forName(tmp);
}
PropertyDescriptor filed[] = PropertyUtils.getPropertyDescriptors(obj);
for (int i = 0; i < filed.length; i++) {
try {
//logger.debug(filed[i].getName());
Object v = PropertyUtils.getProperty(obj, filed[i].getName());
//logger.info(filed[i].getName()+" : "+v);
if ("java.lang.String".equals(filed[i].getPropertyType().getName())) {
m.put(filed[i].getName(), WebUtil.objToString(v, ""));
} else if ("java.lang.Double".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), "0.0");
} else {
m.put(filed[i].getName(), String.valueOf(v));
}
} else if ("java.lang.Byte".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), "");
} else {
m.put(filed[i].getName(), String.valueOf(v));
}
} else if ("java.lang.Integer".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), "0");
} else {
m.put(filed[i].getName(), String.valueOf(v));
}
}
} catch (NoSuchMethodException e1) {}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 拷贝PO值至HashMap
*
* @param obj 数据源,PO retrun m 目标HashMap
*/
public static Map poToMap3(Object obj) {
String tmp = obj.getClass().getName();
Map m = new HashMap();
try {
Class c;
if (tmp.indexOf("$") != -1) {
c = Class.forName(tmp.substring(0, tmp.indexOf("$")));
} else {
c = Class.forName(tmp);
}
PropertyDescriptor filed[] = PropertyUtils.getPropertyDescriptors(obj);
for (int i = 0; i < filed.length; i++) {
try {
Object v = PropertyUtils.getProperty(obj, filed[i].getName());
if ("java.lang.String".equals(filed[i].getPropertyType().getName())) {
m.put(filed[i].getName(), WebUtil.objToString(v, ""));
} else if ("java.lang.Double".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), "0.0");
} else {
m.put(filed[i].getName(), String.valueOf(v));
}
} else if ("java.lang.Byte".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), "");
} else {
m.put(filed[i].getName(), String.valueOf(v));
}
} else if ("java.lang.Integer".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), "0");
} else {
m.put(filed[i].getName(), String.valueOf(v));
}
} else if ("java.lang.Long".equals(filed[i].getPropertyType().getName())) {
if (v == null) {
m.put(filed[i].getName(), "0");
} else {
m.put(filed[i].getName(), String.valueOf(v));
}
} else if ("java.sql.Clob".equals(filed[i].getPropertyType().getName())) {
m.put(filed[i].getName(), clobToString((java.sql.Clob) v));
}
} catch (java.lang.NoSuchMethodException e1) {}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return m;
}
/**
* clob转String
*
* @param clob
* @return
*/
public static String clobToString(Clob clob) {
if (clob == null) {
return "";
}
try {
Reader inStreamDoc = clob.getCharacterStream();
char[] tempDoc = new char[(int) clob.length()];
inStreamDoc.read(tempDoc);
inStreamDoc.close();
return new String(tempDoc);
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException es) {
es.printStackTrace();
}
return "";
}
}