Details
-
Type:
Improvement
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Won't Fix
-
Affects Version/s: None
-
Fix Version/s: 1.9
-
Component/s: Web - Struts
-
Labels:None
Description
This code is able to perform a global convert on pojo or struts form.
Also, it keeps the original state of the object pass through the method.
(convertLists change the original object and i think it's boring)
public static Object convertAll(Object o) throws Exception {
if (o == null) {
return null;
}
Object targetO = getOpposingObject(o);
BeanUtils.copyProperties(targetO, o);
Object target = null;
PropertyDescriptor[] origDescriptors =
PropertyUtils.getPropertyDescriptors(o);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if (origDescriptors[i].getPropertyType().equals(List.class)) {
List list = (List) PropertyUtils.getProperty(o, name);
List b =null;
if(list!=null)
b = new ArrayList(list.size());
for (int j=0; j < list.size(); j++) {
Object origin = list.get(j);
// je convertit l'objet dans la liste
target = convert(origin);
b.add(target);
}
PropertyUtils.setProperty(targetO, name, b);
}
}
return targetO;
}
and add to the BaseAction class the method convertAll
Also, it keeps the original state of the object pass through the method.
(convertLists change the original object and i think it's boring)
public static Object convertAll(Object o) throws Exception {
if (o == null) {
return null;
}
Object targetO = getOpposingObject(o);
BeanUtils.copyProperties(targetO, o);
Object target = null;
PropertyDescriptor[] origDescriptors =
PropertyUtils.getPropertyDescriptors(o);
for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName();
if (origDescriptors[i].getPropertyType().equals(List.class)) {
List list = (List) PropertyUtils.getProperty(o, name);
List b =null;
if(list!=null)
b = new ArrayList(list.size());
for (int j=0; j < list.size(); j++) {
Object origin = list.get(j);
// je convertit l'objet dans la liste
target = convert(origin);
b.add(target);
}
PropertyUtils.setProperty(targetO, name, b);
}
}
return targetO;
}
and add to the BaseAction class the method convertAll
Looks like a good idea overall.