package hotbox.smis.service.webcall; import hotbox.smis.query.QueryObject; import hotbox.smis.query.SearchItem; import java.lang.reflect.Field; import java.util.Calendar; import java.util.Date; import java.util.List; import javax.xml.datatype.XMLGregorianCalendar; public class WSObjectConverter { public static void copyFormWSQueryObjectProperty(Object wsQuery, QueryObject query) { copyFromWSObjectProperty(wsQuery, query); try { List<Object> val = (List<Object>) wsQuery.getClass().getMethod( "getSearchItems").invoke(wsQuery); SearchItem[] items = new SearchItem[val.size()]; for (int i = 0; i < val.size(); i++) { copyFromWSObjectProperty(val.get(i), items[i]); } query.setSearchItems(items); } catch (Exception e) { } } /** * 将本地query拷贝到wsquery * * @param query * @param wsQuery * @param wsSearchItem */ public static void copyToWSQueryObjectProperty(QueryObject query, Object wsQuery, Class wsSearchItem) { copyToWSObjectProperty(query, wsQuery); try { List<Object> val = (List<Object>) wsQuery.getClass().getMethod( "getSearchItems").invoke(wsQuery); for (int i = 0; i < query.getSearchItems().length; i++) { Object newobj = wsSearchItem.getConstructor().newInstance(); copyToWSObjectProperty(query.getSearchItems()[i], newobj); val.add(newobj); } } catch (Exception e) { } } /** * 拷贝WSOBJECT里面的属性到目标对象LIST(只做浅层拷贝) * * @param wsObj * @param dest */ public static void copyFormWSObjectProperty(List wsList, List list, Class objClass) { try { Object obj = objClass.getConstructor().newInstance(); for (int i = 0; i < wsList.size(); i++) { copyFromWSObjectProperty(wsList.get(i), obj); list.add(obj); } } catch (Exception e) { } } /** * 拷贝WSOBJECT里面的属性到目标对象(只做浅层拷贝) * * @param wsObj * @param dest */ @SuppressWarnings("deprecation") public static void copyFromWSObjectProperty(Object wsObj, Object dest) { try { Class destCls = dest.getClass(); Class srcCls = wsObj.getClass(); for (Field field : wsObj.getClass().getDeclaredFields()) { try { String fieldName = field.getName(); String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length()); String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length()); Object val = srcCls.getMethod(getMethodName).invoke(wsObj); if (val != null) { /* * if (val instanceof Float) { * destCls.getMethod(setMethodName, float.class) * .invoke(dest, val); } else { * destCls.getMethod(setMethodName, field.getType()) * .invoke(dest, val); } */ if (val instanceof XMLGregorianCalendar) { // System.out.println(val.toString()); XMLGregorianCalendar xmlDate = (XMLGregorianCalendar) val; java.util.Calendar calendar = java.util.Calendar .getInstance(); calendar.set(xmlDate.getYear(), xmlDate.getMonth(), xmlDate.getDay(), xmlDate.getHour(), xmlDate.getMinute(), xmlDate.getSecond()); // Date date = new Date(xmlDate.getYear(), xmlDate // .getMonth(), xmlDate.getDay(), xmlDate // .getHour(), xmlDate.getMinute(), xmlDate // .getSecond()); destCls.getMethod(setMethodName, Date.class) .invoke(dest, calendar.getTime()); } else { destCls.getMethod(setMethodName, field.getType()) .invoke(dest, val); } } } catch (Exception e) { e.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } /** * 将对象属性拷贝到WSObjectLIST(只做浅层拷贝) * * @param src * @param wsObj */ public static void copyToWSObjectProperty(List<Object> srcList, List<Object> wsObjList) { try { for (int i = 0; i < wsObjList.size(); i++) { copyToWSObjectProperty(srcList.get(i), wsObjList.get(i)); } } catch (Exception e) { } } /** * 将对象属性拷贝到WSObject(只做浅层拷贝) * * @param src * @param wsObj */ public static void copyToWSObjectProperty(Object src, Object wsObj) { try { Class destCls = wsObj.getClass(); Class srcCls = src.getClass(); for (Field field : src.getClass().getDeclaredFields()) { try { String fieldName = field.getName(); String setMethodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length()); String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1, fieldName.length()); Object val = srcCls.getMethod(getMethodName).invoke(src); // System.out.println(field.getType().toString()); if (val != null) { destCls.getMethod(setMethodName, field.getType()) .invoke(wsObj, val); } } catch (Exception e) { } } } catch (Exception e) { } } }