cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert a Customer into a B2BCustomer?

Former Member
0 Kudos

How can I convert a Customer into a B2BCustomer? Assume that I have extra attributes for the B2BCustomer with default values. Also assume that can assign a certain defaultB2BUnit. I would probably create an action button at the backoffice. I'm looking for suggestions on how to implement this.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

How about using recommended approach with converter/populator where you can get all attributes of one object and set them in the other?

Former Member
0 Kudos

Hi Gustavo,
There is no straight forward method to do so in java, as downcasting is not a possibility (http://stackoverflow.com/questions/4033118/how-to-downcast-a-java-object). If this were pure java, the recommendation would be to create a constructor of B2BCustomer which would accept Customer as its parameter, but as this is not possible in hybris (as I assume you're working with model classes), you would need a factory class as specified in the stackoverflow answer.
In the factory, ideally you should be copying one by one all the required attributes to a new B2BCustomer object in order to get a minimal required copy:

 to.setTitle(from.getTitle);
 to.setToken(from.getToken);
 //...

If you want a more "automatic" way you could use Java reflections in order to get all attributes of one object and setting them in the other, but this is not recommended as it is a way of "hacking" Java.
Also, either way you will need to take care of not duplicating all the unique attributes, otherwise you ll get an error when saving in to the db, or make sure to delete the original before saving the new one. Finally you will need to specify manually the attributes of B2BCustomer that are not present in Customer.
Regards, Juan