Search in sources :

Example 6 with ValidationResult

use of alfio.model.result.ValidationResult in project alf.io by alfio-event.

the class EventController method handleCode.

@RequestMapping(value = "/event/{eventName}/code/{code}", method = { RequestMethod.GET, RequestMethod.HEAD })
public String handleCode(@PathVariable("eventName") String eventName, @PathVariable("code") String code, Model model, ServletWebRequest request, RedirectAttributes redirectAttributes, Locale locale) {
    String trimmedCode = StringUtils.trimToNull(code);
    return eventRepository.findOptionalByShortName(eventName).map(event -> {
        String redirectToEvent = "redirect:/event/" + eventName + "/";
        ValidationResult res = savePromoCode(eventName, trimmedCode, model, request.getRequest());
        CodeType codeType = getCodeType(event.getId(), trimmedCode);
        if (res.isSuccess() && codeType == CodeType.PROMO_CODE_DISCOUNT) {
            return redirectToEvent;
        } else if (codeType == CodeType.TICKET_CATEGORY_CODE) {
            TicketCategory category = ticketCategoryRepository.findCodeInEvent(event.getId(), trimmedCode).get();
            if (!category.isAccessRestricted()) {
                return makeSimpleReservation(eventName, request, redirectAttributes, locale, null, event, category.getId());
            } else {
                Optional<SpecialPrice> specialPrice = specialPriceRepository.findActiveNotAssignedByCategoryId(category.getId()).stream().findFirst();
                if (!specialPrice.isPresent()) {
                    return redirectToEvent;
                }
                savePromoCode(eventName, specialPrice.get().getCode(), model, request.getRequest());
                return makeSimpleReservation(eventName, request, redirectAttributes, locale, specialPrice.get().getCode(), event, category.getId());
            }
        } else if (res.isSuccess() && codeType == CodeType.SPECIAL_PRICE) {
            int ticketCategoryId = specialPriceRepository.getByCode(trimmedCode).get().getTicketCategoryId();
            return makeSimpleReservation(eventName, request, redirectAttributes, locale, trimmedCode, event, ticketCategoryId);
        } else {
            return redirectToEvent;
        }
    }).orElse("redirect:/");
}
Also used : ValidationResult(alfio.model.result.ValidationResult) alfio.repository(alfio.repository) ReservationForm(alfio.controller.form.ReservationForm) java.util(java.util) TicketReservationModification(alfio.model.modification.TicketReservationModification) SaleableAdditionalService(alfio.controller.decorator.SaleableAdditionalService) PaymentProxy(alfio.model.transaction.PaymentProxy) ZonedDateTime(java.time.ZonedDateTime) BindingResult(org.springframework.validation.BindingResult) SaleableTicketCategory(alfio.controller.decorator.SaleableTicketCategory) ConfigurationManager(alfio.manager.system.ConfigurationManager) Controller(org.springframework.stereotype.Controller) StringUtils(org.apache.commons.lang3.StringUtils) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) Model(org.springframework.ui.Model) HttpServletRequest(javax.servlet.http.HttpServletRequest) LocationDescriptor(alfio.model.modification.support.LocationDescriptor) SessionUtil.addToFlash(alfio.controller.support.SessionUtil.addToFlash) EventDescriptor(alfio.controller.decorator.EventDescriptor) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) OrganizationRepository(alfio.repository.user.OrganizationRepository) Predicate(java.util.function.Predicate) HttpMethod(org.springframework.http.HttpMethod) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) SessionUtil(alfio.controller.support.SessionUtil) ErrorsCode(alfio.util.ErrorsCode) Collectors(java.util.stream.Collectors) DateUtils(org.apache.commons.lang3.time.DateUtils) TicketReservationManager(alfio.manager.TicketReservationManager) EventManager(alfio.manager.EventManager) alfio.model(alfio.model) EventStatisticsManager(alfio.manager.EventStatisticsManager) Configuration(alfio.model.system.Configuration) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) EventUtil(alfio.util.EventUtil) ResponseEntity(org.springframework.http.ResponseEntity) I18nManager(alfio.manager.i18n.I18nManager) AllArgsConstructor(lombok.AllArgsConstructor) ConfigurationKeys(alfio.model.system.ConfigurationKeys) OptionalWrapper.optionally(alfio.util.OptionalWrapper.optionally) ValidationResult(alfio.model.result.ValidationResult) SaleableTicketCategory(alfio.controller.decorator.SaleableTicketCategory)

Example 7 with ValidationResult

use of alfio.model.result.ValidationResult in project alf.io by alfio-event.

the class AdditionalServiceApiController method update.

@RequestMapping(value = "/event/{eventId}/additional-services/{additionalServiceId}", method = RequestMethod.PUT)
@Transactional
public ResponseEntity<EventModification.AdditionalService> update(@PathVariable("eventId") int eventId, @PathVariable("additionalServiceId") int additionalServiceId, @RequestBody EventModification.AdditionalService additionalService, BindingResult bindingResult) {
    ValidationResult validationResult = Validator.validateAdditionalService(additionalService, bindingResult);
    Validate.isTrue(validationResult.isSuccess(), "validation failed");
    Validate.isTrue(additionalServiceId == additionalService.getId(), "wrong input");
    return eventRepository.findOptionalById(eventId).map(event -> {
        int result = additionalServiceRepository.update(additionalServiceId, additionalService.isFixPrice(), additionalService.getOrdinal(), additionalService.getAvailableQuantity(), additionalService.getMaxQtyPerOrder(), additionalService.getInception().toZonedDateTime(event.getZoneId()), additionalService.getExpiration().toZonedDateTime(event.getZoneId()), additionalService.getVat(), additionalService.getVatType(), Optional.ofNullable(additionalService.getPrice()).map(MonetaryUtil::unitToCents).orElse(0));
        Validate.isTrue(result <= 1, "too many records updated");
        Stream.concat(additionalService.getTitle().stream(), additionalService.getDescription().stream()).forEach(t -> {
            if (t.getId() != null) {
                additionalServiceTextRepository.update(t.getId(), t.getLocale(), t.getType(), t.getValue());
            } else {
                additionalServiceTextRepository.insert(additionalService.getId(), t.getLocale(), t.getType(), t.getValue());
            }
        });
        return ResponseEntity.ok(additionalService);
    }).orElseThrow(IllegalArgumentException::new);
}
Also used : PriceContainer(alfio.model.PriceContainer) ValidationResult(alfio.model.result.ValidationResult) AffectedRowCountAndKey(ch.digitalfondue.npjt.AffectedRowCountAndKey) Autowired(org.springframework.beans.factory.annotation.Autowired) BindingResult(org.springframework.validation.BindingResult) EventModification(alfio.model.modification.EventModification) BigDecimal(java.math.BigDecimal) AdditionalServiceRepository(alfio.repository.AdditionalServiceRepository) AdditionalServiceTextRepository(alfio.repository.AdditionalServiceTextRepository) EventRepository(alfio.repository.EventRepository) Validator(alfio.util.Validator) Collectors(java.util.stream.Collectors) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) AdditionalService(alfio.model.AdditionalService) Validate(org.apache.commons.lang3.Validate) Principal(java.security.Principal) Stream(java.util.stream.Stream) MonetaryUtil(alfio.util.MonetaryUtil) Log4j2(lombok.extern.log4j.Log4j2) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) Event(alfio.model.Event) Collections(java.util.Collections) OptionalWrapper.optionally(alfio.util.OptionalWrapper.optionally) Transactional(org.springframework.transaction.annotation.Transactional) ValidationResult(alfio.model.result.ValidationResult) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with ValidationResult

use of alfio.model.result.ValidationResult in project alf.io by alfio-event.

the class TicketHelper method assignTicket.

private Triple<ValidationResult, Event, Ticket> assignTicket(UpdateTicketOwnerForm updateTicketOwner, Optional<Errors> bindingResult, HttpServletRequest request, Optional<UserDetails> userDetails, Triple<Event, TicketReservation, Ticket> result) {
    Ticket t = result.getRight();
    final Event event = result.getLeft();
    if (t.getLockedAssignment()) {
        // in case of locked assignment, fullName and Email will be overwritten
        updateTicketOwner.setFirstName(t.getFirstName());
        updateTicketOwner.setLastName(t.getLastName());
        updateTicketOwner.setFullName(t.getFullName());
        updateTicketOwner.setEmail(t.getEmail());
    }
    final TicketReservation ticketReservation = result.getMiddle();
    List<TicketFieldConfiguration> fieldConf = ticketFieldRepository.findAdditionalFieldsForEvent(event.getId());
    ValidationResult validationResult = Validator.validateTicketAssignment(updateTicketOwner, fieldConf, bindingResult, event).ifSuccess(() -> updateTicketOwner(updateTicketOwner, request, t, event, ticketReservation, userDetails));
    return Triple.of(validationResult, event, ticketRepository.findByUUID(t.getUuid()));
}
Also used : ValidationResult(alfio.model.result.ValidationResult)

Example 9 with ValidationResult

use of alfio.model.result.ValidationResult in project alf.io by alfio-event.

the class Validator method validateAdditionalService.

public static ValidationResult validateAdditionalService(EventModification.AdditionalService additionalService, EventModification eventModification, Errors errors) {
    if (additionalService.isFixPrice() && !Optional.ofNullable(additionalService.getPrice()).filter(p -> p.compareTo(BigDecimal.ZERO) >= 0).isPresent()) {
        errors.rejectValue("additionalServices", "error.price");
    }
    List<EventModification.AdditionalServiceText> descriptions = additionalService.getDescription();
    List<EventModification.AdditionalServiceText> titles = additionalService.getTitle();
    if (descriptions == null || titles == null || titles.size() != descriptions.size()) {
        errors.rejectValue("additionalServices", "error.title");
        errors.rejectValue("additionalServices", "error.description");
    } else {
        if (!validateDescriptionList(titles) || !containsAllRequiredTranslations(eventModification, titles)) {
            errors.rejectValue("additionalServices", "error.title");
        }
        if (!validateDescriptionList(descriptions) || !containsAllRequiredTranslations(eventModification, descriptions)) {
            errors.rejectValue("additionalServices", "error.description");
        }
    }
    DateTimeModification inception = additionalService.getInception();
    DateTimeModification expiration = additionalService.getExpiration();
    if (inception == null || expiration == null || expiration.isBefore(inception)) {
        errors.rejectValue("additionalServices", "error.inception");
        errors.rejectValue("additionalServices", "error.expiration");
    } else if (eventModification != null && expiration.isAfter(eventModification.getEnd())) {
        errors.rejectValue("additionalServices", "error.expiration");
    }
    return evaluateValidationResult(errors);
}
Also used : ValidationResult(alfio.model.result.ValidationResult) Errors(org.springframework.validation.Errors) TicketCategoryModification(alfio.model.modification.TicketCategoryModification) WaitingQueueSubscriptionForm(alfio.controller.form.WaitingQueueSubscriptionForm) Collection(java.util.Collection) UpdateTicketOwnerForm(alfio.controller.form.UpdateTicketOwnerForm) ContentLanguage(alfio.model.ContentLanguage) EventModification(alfio.model.modification.EventModification) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) DateTimeModification(alfio.model.modification.DateTimeModification) BigDecimal(java.math.BigDecimal) List(java.util.List) ErrorCode(alfio.model.result.ErrorCode) ValidationUtils(org.springframework.validation.ValidationUtils) CollectionUtils(org.apache.commons.collections.CollectionUtils) TicketFieldConfiguration(alfio.model.TicketFieldConfiguration) Optional(java.util.Optional) Event(alfio.model.Event) Pattern(java.util.regex.Pattern) DateTimeModification(alfio.model.modification.DateTimeModification)

Aggregations

ValidationResult (alfio.model.result.ValidationResult)9 Collectors (java.util.stream.Collectors)5 Event (alfio.model.Event)4 OptionalWrapper.optionally (alfio.util.OptionalWrapper.optionally)4 BigDecimal (java.math.BigDecimal)4 StringUtils (org.apache.commons.lang3.StringUtils)4 ResponseEntity (org.springframework.http.ResponseEntity)4 BindingResult (org.springframework.validation.BindingResult)4 org.springframework.web.bind.annotation (org.springframework.web.bind.annotation)4 alfio.model (alfio.model)3 EventModification (alfio.model.modification.EventModification)3 Validator (alfio.util.Validator)3 I18nManager (alfio.manager.i18n.I18nManager)2 AdditionalService (alfio.model.AdditionalService)2 PriceContainer (alfio.model.PriceContainer)2 PaymentProxy (alfio.model.transaction.PaymentProxy)2 MonetaryUtil (alfio.util.MonetaryUtil)2 Principal (java.security.Principal)2 ZonedDateTime (java.time.ZonedDateTime)2 java.util (java.util)2