cancel
Showing results for 
Search instead for 
Did you mean: 

How to write populator for Map

former_member595526
Active Participant
0 Kudos

Hi Experts,

I have Map to be converted from Model object to Data Object. Map contains userModel as key , and list of users Wishlist(Wishlist2Model)

Now I want to write populator to convert the map of these model object to Data.

As of now, I know how to write populator of list of model object. but now it is for map of model

Any suggestions

Thanks in advance

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hello

Just curious as to why you have a Map of <User, Wishlist>. There is a One-To-Many relation between User and Wishlist that can be used to find the wishlist for a user.

The right way depends on your use case exactly but in general, assuming you have this map type defined, and let's say you are referring to it in an entity called Container, you would basically create a populator called ContainerUserWishlistMapPopulator where your source and target would be something like ContainerModel and ContainerData.

Your populate method should look something like this -

 public void populate(SOURCE source, TARGET target)throws ConversionException {

     ServicesUtil.validateParameterNotNullStandardMessage("source", source);
     ServicesUtil.validateParameterNotNullStandardMessage("target", target);
     if (MapUtils.isEmpty(source.getUserWishlistMap())) {
         target.setUserWishlistMap(Collections.emptyMap());
     }
     target.setUserWishlistMap(source.getUserWishlistMap().entrySet()
             .stream()
             .collect(Collectors.toMap(
                     entry -> getUserConverter().convert(entry.getKey()),
                     entry -> getWishlistConverter().convert(entry.getValue())
             )));
 }


Former Member
0 Kudos

Hi Ansari,

I feel you should iterate through the map.

First get the key, convert it to data . Now, get the value for that key ,call the coverters,covertALL(listvalue) .

Create new map and place your data in that.

Regards, Sid

former_member595526
Active Participant
0 Kudos

I don't think it's a typical way of writing populator

Former Member
0 Kudos

It is. First register your converter as bean and define the populator list for it.