Afaque
Afaque

Reputation: 129

Dozer: Map a field to function output

I am using Dozer to do object Mapping. Everything works reaaly well just that I'm not able to map this particular thing.

<mapping>
<class-a>User</class-a>
<class-b>UAUserBean</class-b>
<field>
<a>RightLst.Right</a>
<b>Rights</b>
<a-hint>Right</a-hint>
<b-hint>UARightBean</b-hint>
</field>
<field>
<a>RightLst.NumInLst</a> 
<b>Rights.length</b> 
</field>
</mapping>

//here RightLst is an object of a class and numInLst (int prop)
//rights is an array of objects

what i want to do is

lUser.getRightLst().setNumInLst(uaUserBean.getRights().length);

Any suggestions??

Thanks in advance.


User{ 
protected RightLst rightLst;
}

RightLst{
protected Integer numInLst;
protected Collection right = new ArrayList();
}

public class UAUserBean{
private UARightBean[] rights;
}

Upvotes: 0

Views: 1254

Answers (1)

davidmontoyago
davidmontoyago

Reputation: 1833

When you do this:

...
   <b>rights.length</b> 
</field>

Dozer will try to access the first position of the rights array and call the getter for the length property on an instance of UARightBean (which is the type of the array), obviously the length property doesn't exist in UARightBean and Dozer will throw an Exception.

I suggest to create a getter method in UAUserBean to return the length of the rights property, it would look like this:

 class UAUserBean {
        ... 
        public int getRightsLength() {
            return rights != null ? rights.length : 0;
        }       
    }

Mapping file:

<class-a>User</class-a>
<class-b>UAUserBean</class-b>
<field>
    <a>rightLst.numInLst</a> 
    <b>rightsLength</b> 
</field>

If you can't modify UAUserBean, your last option would be a custom converter from UARightBean[] to Integer, but it would look ugly.

Upvotes: 1

Related Questions