I am using Selma and I have the following class:
public class Customer
{
private int id;
private String email;
private String firstName;
private String lastName;
private Date registeredDate;
private List<Address> addresses;
}
I want to map it to:
public class Customer
{
private String id;
private String email;
private String firstName;
private String lastName;
private Date registeredDate;
private String company;
private Address address1;
private Address address2;
}
Is there any way to cast the (int) id
to (String) id
and to set the first address from the List
to address1
and the second address to address2
?
I was thinking of using an interceptor, but this way I will have to manually map the Address
classes. Is there a way to use Selma to automatically map the address classes in the interceptor? For example:
public class CustomerCustomMapper
{
public void interceptMyCustomerToCustomer(com.mycode.domain.Customer source, Customer destination) {
if(source.getAddresses() != null && source.getAddresses().size() > 0)
{
com.mycode.domain.Address myAddress1 = source.getAddresses().get(0);
AddressMapper addressMapper = Selma.builder(AddressMapper.class).build();
Address address1 = addressMapper.mapAddress(myAddress1);
destination.setAddress1(address1);
// do the same for address2
}
}
EDIT
Regarding mapping the Address class I did it the way I showed above. I created an AddressMapper
with the following code inside: Address toAddress(com.mycode.domain.Address address);
Then I created an addressMapper and used it to map the Address autimatically:
AddressMapper addressMapper = Selma.builder(AddressMapper.class).build();
As for the id, currently I have to create an interceptor for every class that I map (almost every class has an Id in it (SQL)), and manually set the id like so: destination.setId(Integer.toString(source.getId()));
It is quite frustrating actually, but sadly I can't find a better option.
for the list to Address the interceptor is the good answer.
For the int to String you can specify a custom mapper mapping from int to String (see http://www.selma-java.org/#custom-mapper).
But I would recommend using an abstract mapper instead of building a new mapper to map the Address. This way you'll be able to integrate your specific code inside the mapper and call the address mapper method directly (see http://www.selma-java.org/#abstract-mapper).
For the toString thing, you can also add a feature request to Github. I'll be glad to add this feature.