AppFuse JIRA

  • Log In Access more options
    • Online Help
    • GreenHopper Help
    • Agile Answers
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What’s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • Agile
  • AppFuse
  • APF-81

Convert, ConvertLists in one ConvertAll

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Improvement Improvement
  • Status: Resolved Resolved
  • Priority: Major 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

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
  • Source
Hide
Permalink
David Carter added a comment - 15/Sep/05 2:02 PM
Is there any reason this method shouldn't be recursive? In other words, shouldn't it call convertAll(origin) rather than convert(origin) in the inner loop? This would handle lists inside of lists.

Looks like a good idea overall.
Show
David Carter added a comment - 15/Sep/05 2:02 PM Is there any reason this method shouldn't be recursive? In other words, shouldn't it call convertAll(origin) rather than convert(origin) in the inner loop? This would handle lists inside of lists. Looks like a good idea overall.
Hide
Permalink
Matt Raible added a comment - 21/Sep/05 5:25 PM
I'd rather just add convertLists() to the convert() method, but there's an ugly side-effect I've found - lazy loaded collections will get instantiated:

    public static Object convert(Object o) throws Exception {
        if (o == null) {
         return null;
        }
        Object target = getOpposingObject(o);
        BeanUtils.copyProperties(target, o);
 + convertLists(target);
        return target;
    }

I believe this convertAll would have the same effect, and that doesn't seem like a wise thing to do IMO.
Show
Matt Raible added a comment - 21/Sep/05 5:25 PM I'd rather just add convertLists() to the convert() method, but there's an ugly side-effect I've found - lazy loaded collections will get instantiated:     public static Object convert(Object o) throws Exception {         if (o == null) {          return null;         }         Object target = getOpposingObject(o);         BeanUtils.copyProperties(target, o);  + convertLists(target);         return target;     } I believe this convertAll would have the same effect, and that doesn't seem like a wise thing to do IMO.

People

  • Assignee:
    Matt Raible
    Reporter:
    beno?t moraillon
Vote (1)
Watch (0)

Dates

  • Created:
    24/Jun/05 1:22 AM
    Updated:
    03/Oct/06 11:55 AM
    Resolved:
    03/Oct/06 11:55 AM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for AppFuse. Try JIRA - bug tracking software for your team.