use of alfio.model.extension.AdditionalInfoItem in project alf.io by alfio-event.
the class PublicUserManager method buildAdditionalInfoWithLabels.
private Map<String, AdditionalInfoWithLabel> buildAdditionalInfoWithLabels(PublicUserProfile existingProfile, PurchaseContext purchaseContext, UpdateTicketOwnerForm form) {
var event = purchaseContext.event().orElseThrow();
var fields = ticketFieldRepository.findAdditionalFieldsOfTypeForEvent(event.getId(), "input:text");
var fieldsById = fields.stream().collect(toMap(TicketFieldConfiguration::getId, Function.identity()));
var userLanguage = form.getUserLanguage();
var filteredItems = extensionManager.filterAdditionalInfoToSave(purchaseContext, form.getAdditional(), existingProfile);
final Map<String, AdditionalInfoItem> filteredItemsByKey;
filteredItemsByKey = filteredItems.map(additionalInfoItems -> additionalInfoItems.stream().collect(toMap(AdditionalInfoItem::getKey, Function.identity()))).orElse(null);
var labels = ticketFieldRepository.findDescriptions(event.getId(), userLanguage).stream().filter(f -> fieldsById.containsKey(f.getTicketFieldConfigurationId())).map(f -> Map.entry(fieldsById.get(f.getTicketFieldConfigurationId()).getName(), f)).filter(f -> filteredItemsByKey == null || filteredItemsByKey.containsKey(f.getKey())).map(e -> {
if (filteredItemsByKey != null) {
return Map.entry(e.getKey(), filteredItemsByKey.get(e.getKey()).getLabel());
}
return Map.entry(e.getKey(), e.getValue().getLabelDescription());
}).collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
Map<String, AdditionalInfoWithLabel> result = new HashMap<>(Optional.ofNullable(existingProfile).map(PublicUserProfile::getAdditionalData).orElse(Map.of()));
// override existing values (if any) with new values.
result.putAll(form.getAdditional().entrySet().stream().filter(e -> labels.containsKey(e.getKey())).map(e -> {
var label = labels.get(e.getKey());
return Map.entry(e.getKey(), new AdditionalInfoWithLabel(Map.of(userLanguage, label), e.getValue()));
}).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
return result;
}
Aggregations