Hi,
Thank you for your response. I will try to explain a bit better what I was trying to ask from you with some piece of codes. Basically we have our own data structure and we have to fulfill a standard protocol with this data. In several places we have mappings like this one:
using(new CountryOfOriginsToSPSConsignmentItemTypeListConverter()).map(source.getCoiExtractTranslation()
.getCountryOfOrigins(),
destination.getTotalSPSTradeLineItemType()
.getOriginSPSCountry());
Then countryOfOrigins is a list of CountryTranslation objects
public class CountryTranslation implements....{
private String countryName;
private String countryCode;
...........
//getters + setters
}
On the other hand the originSPSCountry (a standard protocol we got where a list is wrongly called in singular...) is based on a list of SPSCountryType:
public class SPSCountryType {
protected IDType id;
protected List<TextType> name;
....
//getters + setters
}
Now:The converter takes 2 lists and iterates them this way:
public class CountryOfOriginsToSPSConsignmentItemTypeListConverter implements SourceTranslatableConverter<List<CountryTranslation>, List<SPSCountryType>> {
@Override
public List<SPSCountryType> convert(MappingContext<List<CountryTranslation>, List<SPSCountryType>> context) {
List<CountryTranslation> sourceCountries = context.getSource();
LanguageCode languageCode = extractLanguageCode(context);
return sourceCountries.stream()
.map(c -> UncefactFactory.createSPSCountryType(c.getCode(), c.getTranslation(), languageCode.getCode()))
.collect(Collectors.toList());
}
}
Probably a more clean solution:We were thinking that it would be more clear for us if we would use Java 8 streaming inside the config definition, (somehow) having a converter that just maps the iterated CountryTranslation into SPSCountryType objects and not a full ListCountryTranslation> into a full List<SPSCountryType>.
A difficult point would be whether the destination list is empty or not, whether to update or create a list item. In our case creation would be enough as we are just orchestrating 2 protocols, but generally speaking their content, equals, comparators could be used to identify whether to update or create...
So, basically these are our thoughts, mainly starting from our use cases. Then we can use the advantages of the mapping declaration having our code free of ugly if statements.
I am wondering about your opinion.
Thank you in advance!
Ernesto
(by the way we have places where we have to map a list that is inside of an item, which is inside another list, but I will post it after your opinion)