Search in sources :

Example 1 with AdditionalInfoItem

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;
}
Also used : java.util(java.util) PublicUserProfile(alfio.model.user.PublicUserProfile) PurchaseContext(alfio.model.PurchaseContext) AdditionalInfoWithLabel(alfio.model.user.AdditionalInfoWithLabel) BindingResult(org.springframework.validation.BindingResult) OpenIdAlfioAuthentication(alfio.config.authentication.support.OpenIdAlfioAuthentication) TicketReservationAdditionalInfo(alfio.model.TicketReservationAdditionalInfo) Function(java.util.function.Function) TicketFieldRepository(alfio.repository.TicketFieldRepository) Pair(org.apache.commons.lang3.tuple.Pair) Collectors.toMap(java.util.stream.Collectors.toMap) TicketReservationInvoicingAdditionalInfo(alfio.model.TicketReservationInvoicingAdditionalInfo) UpdateTicketOwnerForm(alfio.controller.form.UpdateTicketOwnerForm) User(alfio.model.user.User) AdditionalInfoItem(alfio.model.extension.AdditionalInfoItem) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) Validate(org.apache.commons.lang3.Validate) Principal(java.security.Principal) ContactAndTicketsForm(alfio.controller.form.ContactAndTicketsForm) UserRepository(alfio.repository.user.UserRepository) UpdateProfileForm(alfio.controller.form.UpdateProfileForm) TicketFieldConfiguration(alfio.model.TicketFieldConfiguration) ExtensionManager(alfio.manager.ExtensionManager) AllArgsConstructor(lombok.AllArgsConstructor) Authentication(org.springframework.security.core.Authentication) Transactional(org.springframework.transaction.annotation.Transactional) AdditionalInfoItem(alfio.model.extension.AdditionalInfoItem) Collectors.toMap(java.util.stream.Collectors.toMap) AdditionalInfoWithLabel(alfio.model.user.AdditionalInfoWithLabel) PublicUserProfile(alfio.model.user.PublicUserProfile)

Aggregations

OpenIdAlfioAuthentication (alfio.config.authentication.support.OpenIdAlfioAuthentication)1 ContactAndTicketsForm (alfio.controller.form.ContactAndTicketsForm)1 UpdateProfileForm (alfio.controller.form.UpdateProfileForm)1 UpdateTicketOwnerForm (alfio.controller.form.UpdateTicketOwnerForm)1 ExtensionManager (alfio.manager.ExtensionManager)1 PurchaseContext (alfio.model.PurchaseContext)1 TicketFieldConfiguration (alfio.model.TicketFieldConfiguration)1 TicketReservationAdditionalInfo (alfio.model.TicketReservationAdditionalInfo)1 TicketReservationInvoicingAdditionalInfo (alfio.model.TicketReservationInvoicingAdditionalInfo)1 AdditionalInfoItem (alfio.model.extension.AdditionalInfoItem)1 AdditionalInfoWithLabel (alfio.model.user.AdditionalInfoWithLabel)1 PublicUserProfile (alfio.model.user.PublicUserProfile)1 User (alfio.model.user.User)1 TicketFieldRepository (alfio.repository.TicketFieldRepository)1 UserRepository (alfio.repository.user.UserRepository)1 Principal (java.security.Principal)1 java.util (java.util)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Collectors.toMap (java.util.stream.Collectors.toMap)1