Search in sources :

Example 1 with ValidationResult

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

the class PaymentForm method validate.

public void validate(BindingResult bindingResult, TotalPrice reservationCost, Event event, List<TicketFieldConfiguration> fieldConf) {
    List<PaymentProxy> allowedPaymentMethods = event.getAllowedPaymentProxies();
    Optional<PaymentProxy> paymentProxyOptional = Optional.ofNullable(paymentMethod);
    PaymentProxy paymentProxy = paymentProxyOptional.filter(allowedPaymentMethods::contains).orElse(PaymentProxy.STRIPE);
    boolean priceGreaterThanZero = reservationCost.getPriceWithVAT() > 0;
    boolean multiplePaymentMethods = allowedPaymentMethods.size() > 1;
    if (multiplePaymentMethods && priceGreaterThanZero && !paymentProxyOptional.isPresent()) {
        bindingResult.reject(ErrorsCode.STEP_2_MISSING_PAYMENT_METHOD);
    } else if (priceGreaterThanZero && (paymentProxy == PaymentProxy.STRIPE && StringUtils.isBlank(stripeToken))) {
        bindingResult.reject(ErrorsCode.STEP_2_MISSING_STRIPE_TOKEN);
    }
    if (Objects.isNull(termAndConditionsAccepted) || !termAndConditionsAccepted) {
        bindingResult.reject(ErrorsCode.STEP_2_TERMS_NOT_ACCEPTED);
    }
    email = StringUtils.trim(email);
    fullName = StringUtils.trim(fullName);
    firstName = StringUtils.trim(firstName);
    lastName = StringUtils.trim(lastName);
    billingAddress = StringUtils.trim(billingAddress);
    ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult, "email", ErrorsCode.STEP_2_EMPTY_EMAIL);
    rejectIfOverLength(bindingResult, "email", ErrorsCode.STEP_2_MAX_LENGTH_EMAIL, email, 255);
    if (event.mustUseFirstAndLastName()) {
        ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult, "firstName", ErrorsCode.STEP_2_EMPTY_FIRSTNAME);
        rejectIfOverLength(bindingResult, "firstName", ErrorsCode.STEP_2_MAX_LENGTH_FIRSTNAME, fullName, 255);
        ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult, "lastName", ErrorsCode.STEP_2_EMPTY_LASTNAME);
        rejectIfOverLength(bindingResult, "lastName", ErrorsCode.STEP_2_MAX_LENGTH_LASTNAME, fullName, 255);
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult, "fullName", ErrorsCode.STEP_2_EMPTY_FULLNAME);
        rejectIfOverLength(bindingResult, "fullName", ErrorsCode.STEP_2_MAX_LENGTH_FULLNAME, fullName, 255);
    }
    rejectIfOverLength(bindingResult, "billingAddress", ErrorsCode.STEP_2_MAX_LENGTH_BILLING_ADDRESS, billingAddress, 450);
    if (email != null && !email.contains("@") && !bindingResult.hasFieldErrors("email")) {
        bindingResult.rejectValue("email", ErrorsCode.STEP_2_INVALID_EMAIL);
    }
    if (hasPaypalTokens() && !PaypalManager.isValidHMAC(new CustomerName(fullName, firstName, lastName, event), email, billingAddress, hmac, event)) {
        bindingResult.reject(ErrorsCode.STEP_2_INVALID_HMAC);
    }
    if (!postponeAssignment) {
        boolean success = Optional.ofNullable(tickets).filter(m -> !m.isEmpty()).map(m -> m.entrySet().stream().map(e -> Validator.validateTicketAssignment(e.getValue(), fieldConf, Optional.empty(), event))).filter(s -> s.allMatch(ValidationResult::isSuccess)).isPresent();
        if (!success) {
            bindingResult.reject(ErrorsCode.STEP_2_MISSING_ATTENDEE_DATA);
        }
    }
}
Also used : PaymentProxy(alfio.model.transaction.PaymentProxy) ValidationResult(alfio.model.result.ValidationResult) java.util(java.util) PaypalManager(alfio.manager.PaypalManager) PaymentProxy(alfio.model.transaction.PaymentProxy) ValidationUtils(org.springframework.validation.ValidationUtils) alfio.model(alfio.model) Data(lombok.Data) BindingResult(org.springframework.validation.BindingResult) ErrorsCode(alfio.util.ErrorsCode) Validator(alfio.util.Validator) StringUtils(org.apache.commons.lang3.StringUtils) Serializable(java.io.Serializable) ValidationResult(alfio.model.result.ValidationResult)

Example 2 with ValidationResult

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

the class WaitingQueueApiController method subscribe.

@RequestMapping(value = "/event/{eventName}/waiting-queue/subscribe", method = RequestMethod.POST, headers = "X-Requested-With=XMLHttpRequest")
public Map<String, Object> subscribe(WaitingQueueSubscriptionForm subscription, BindingResult bindingResult, Model model, @PathVariable("eventName") String eventName, HttpServletRequest request) {
    Optional<Event> optional = eventRepository.findOptionalByShortName(eventName);
    Map<String, Object> result = new HashMap<>();
    if (!optional.isPresent()) {
        bindingResult.reject("");
        result.put("validationResult", ValidationResult.failed(new ValidationResult.ErrorDescriptor("shortName", "error.shortName")));
        return result;
    }
    Event event = optional.get();
    ValidationResult validationResult = Validator.validateWaitingQueueSubscription(subscription, bindingResult, event);
    if (validationResult.isSuccess()) {
        model.addAttribute("error", !waitingQueueManager.subscribe(event, subscription.toCustomerName(event), subscription.getEmail(), subscription.getSelectedCategory(), subscription.getUserLanguage()));
        result.put("partial", templateManager.renderServletContextResource("/WEB-INF/templates/event/waiting-queue-subscription-result.ms", model.asMap(), request, HTML));
    }
    result.put("validationResult", validationResult);
    return result;
}
Also used : HashMap(java.util.HashMap) Event(alfio.model.Event) ValidationResult(alfio.model.result.ValidationResult) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with ValidationResult

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

the class EventApiController method validateEvent.

@RequestMapping(value = "/events/check", method = POST)
public ValidationResult validateEvent(@RequestBody EventModification eventModification, Errors errors) {
    ValidationResult base = validateEventHeader(Optional.empty(), eventModification, errors).or(validateTicketCategories(eventModification, errors)).or(validateEventPrices(Optional.empty(), eventModification, errors)).or(eventModification.getAdditionalServices().stream().map(as -> validateAdditionalService(as, eventModification, errors)).reduce(ValidationResult::or).orElse(ValidationResult.success()));
    AtomicInteger counter = new AtomicInteger();
    return base.or(eventModification.getTicketCategories().stream().map(c -> validateCategory(c, errors, "ticketCategories[" + counter.getAndIncrement() + "].", eventModification)).reduce(ValidationResult::or).orElse(ValidationResult.success())).or(validateAdditionalTicketFields(eventModification.getTicketFields(), errors));
}
Also used : alfio.manager(alfio.manager) TemplateProcessor(alfio.controller.support.TemplateProcessor) StringUtils(org.apache.commons.lang3.StringUtils) BigDecimal(java.math.BigDecimal) Json(alfio.util.Json) Model(org.springframework.ui.Model) Pair(org.apache.commons.lang3.tuple.Pair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SponsorScanRepository(alfio.repository.SponsorScanRepository) ParseException(java.text.ParseException) Triple(org.apache.commons.lang3.tuple.Triple) ZipEntry(java.util.zip.ZipEntry) DateFormat(java.text.DateFormat) PageAndContent(alfio.controller.api.support.PageAndContent) TicketCategoryDescriptionRepository(alfio.repository.TicketCategoryDescriptionRepository) Organization(alfio.model.user.Organization) Predicate(java.util.function.Predicate) TemplateManager(alfio.util.TemplateManager) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) DynamicFieldTemplateRepository(alfio.repository.DynamicFieldTemplateRepository) Role(alfio.model.user.Role) Principal(java.security.Principal) alfio.model(alfio.model) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) alfio.model.modification(alfio.model.modification) OptionalWrapper.optionally(alfio.util.OptionalWrapper.optionally) ValidationResult(alfio.model.result.ValidationResult) ZipOutputStream(java.util.zip.ZipOutputStream) DataAccessException(org.springframework.dao.DataAccessException) Errors(org.springframework.validation.Errors) java.util(java.util) TicketHelper(alfio.controller.api.support.TicketHelper) Getter(lombok.Getter) EventListItem(alfio.controller.api.support.EventListItem) CSVReader(com.opencsv.CSVReader) SimpleDateFormat(java.text.SimpleDateFormat) Function(java.util.function.Function) TicketFieldRepository(alfio.repository.TicketFieldRepository) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) StreamUtils(org.springframework.util.StreamUtils) OutputStream(java.io.OutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) CSVWriter(com.opencsv.CSVWriter) IOException(java.io.IOException) Validator(alfio.util.Validator) InputStreamReader(java.io.InputStreamReader) DateUtils(org.apache.commons.lang3.time.DateUtils) HttpStatus(org.springframework.http.HttpStatus) Collectors.toList(java.util.stream.Collectors.toList) Validate(org.apache.commons.lang3.Validate) MonetaryUtil(alfio.util.MonetaryUtil) UserManager(alfio.manager.user.UserManager) Log4j2(lombok.extern.log4j.Log4j2) ResponseEntity(org.springframework.http.ResponseEntity) I18nManager(alfio.manager.i18n.I18nManager) DescriptionsLoader(alfio.controller.api.support.DescriptionsLoader) AllArgsConstructor(lombok.AllArgsConstructor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ValidationResult(alfio.model.result.ValidationResult)

Example 4 with ValidationResult

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

the class AdditionalServiceApiController method insert.

@PostMapping(value = "/event/{eventId}/additional-services")
@Transactional
public ResponseEntity<EventModification.AdditionalService> insert(@PathVariable("eventId") int eventId, @RequestBody EventModification.AdditionalService additionalService, BindingResult bindingResult) {
    ValidationResult validationResult = Validator.validateAdditionalService(additionalService, bindingResult);
    Validate.isTrue(validationResult.isSuccess(), "validation failed");
    return eventRepository.findOptionalById(eventId).map(event -> ResponseEntity.ok(eventManager.insertAdditionalService(event, additionalService))).orElseThrow(IllegalArgumentException::new);
}
Also used : PriceContainer(alfio.model.PriceContainer) ValidationResult(alfio.model.result.ValidationResult) ExportUtils(alfio.util.ExportUtils) RequiredArgsConstructor(lombok.RequiredArgsConstructor) BindingResult(org.springframework.validation.BindingResult) EventModification(alfio.model.modification.EventModification) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) BigDecimal(java.math.BigDecimal) Map(java.util.Map) AdditionalServiceManager(alfio.manager.AdditionalServiceManager) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) EventRepository(alfio.repository.EventRepository) Validator(alfio.util.Validator) Collectors(java.util.stream.Collectors) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) EventManager(alfio.manager.EventManager) AdditionalService(alfio.model.AdditionalService) Validate(org.apache.commons.lang3.Validate) Principal(java.security.Principal) Stream(java.util.stream.Stream) MonetaryUtil(alfio.util.MonetaryUtil) DateTimeFormatter(java.time.format.DateTimeFormatter) 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) Transactional(org.springframework.transaction.annotation.Transactional) ValidationResult(alfio.model.result.ValidationResult) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ValidationResult

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

the class EventApiController method validateEvent.

public static ValidationResult validateEvent(EventModification eventModification, Errors errors, int descriptionMaxLength) {
    ValidationResult base = validateEventHeader(Optional.empty(), eventModification, descriptionMaxLength, errors).or(validateEventDates(eventModification, errors)).or(validateTicketCategories(eventModification, errors)).or(validateEventPrices(eventModification, errors)).or(eventModification.getAdditionalServices().stream().map(as -> validateAdditionalService(as, eventModification, errors)).reduce(ValidationResult::or).orElse(ValidationResult.success()));
    AtomicInteger counter = new AtomicInteger();
    return base.or(eventModification.getTicketCategories().stream().map(c -> validateCategory(c, errors, "ticketCategories[" + counter.getAndIncrement() + "].", eventModification, descriptionMaxLength)).reduce(ValidationResult::or).orElse(ValidationResult.success())).or(validateAdditionalTicketFields(eventModification.getTicketFields(), errors));
}
Also used : EventDescriptionRepository(alfio.repository.EventDescriptionRepository) alfio.manager(alfio.manager) PaymentProxy(alfio.model.transaction.PaymentProxy) TemplateProcessor(alfio.controller.support.TemplateProcessor) SneakyThrows(lombok.SneakyThrows) ZonedDateTime(java.time.ZonedDateTime) AlfioMetadata(alfio.model.metadata.AlfioMetadata) StringUtils(org.apache.commons.lang3.StringUtils) BigDecimal(java.math.BigDecimal) Pair(org.apache.commons.lang3.tuple.Pair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SponsorScanRepository(alfio.repository.SponsorScanRepository) Triple(org.apache.commons.lang3.tuple.Triple) ZipEntry(java.util.zip.ZipEntry) PageAndContent(alfio.controller.api.support.PageAndContent) Organization(alfio.model.user.Organization) Predicate(java.util.function.Predicate) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) DynamicFieldTemplateRepository(alfio.repository.DynamicFieldTemplateRepository) Role(alfio.model.user.Role) Principal(java.security.Principal) Stream(java.util.stream.Stream) alfio.model(alfio.model) StringUtils.defaultString(org.apache.commons.lang3.StringUtils.defaultString) LocalDate(java.time.LocalDate) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) alfio.model.modification(alfio.model.modification) ValidationResult(alfio.model.result.ValidationResult) ZipOutputStream(java.util.zip.ZipOutputStream) Transaction(alfio.model.transaction.Transaction) DataAccessException(org.springframework.dao.DataAccessException) Errors(org.springframework.validation.Errors) java.util(java.util) TicketHelper(alfio.controller.api.support.TicketHelper) ConfigurationLevel(alfio.manager.system.ConfigurationLevel) CsvException(com.opencsv.exceptions.CsvException) Getter(lombok.Getter) EventListItem(alfio.controller.api.support.EventListItem) CSVReader(com.opencsv.CSVReader) ConfigurationManager(alfio.manager.system.ConfigurationManager) AlfioScriptingException(alfio.extension.exception.AlfioScriptingException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) alfio.util(alfio.util) CollectionUtils(org.apache.commons.collections4.CollectionUtils) TicketFieldRepository(alfio.repository.TicketFieldRepository) HttpServletRequest(javax.servlet.http.HttpServletRequest) StreamUtils(org.springframework.util.StreamUtils) OutputStream(java.io.OutputStream) ItalianEInvoicing(alfio.model.TicketReservationInvoicingAdditionalInfo.ItalianEInvoicing) UTF_8(java.nio.charset.StandardCharsets.UTF_8) HttpServletResponse(javax.servlet.http.HttpServletResponse) User(alfio.model.user.User) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Validator(alfio.util.Validator) HttpStatus(org.springframework.http.HttpStatus) Collectors.toList(java.util.stream.Collectors.toList) Validate(org.apache.commons.lang3.Validate) ChronoUnit(java.time.temporal.ChronoUnit) ExtensionCapability(alfio.manager.support.extension.ExtensionCapability) UserManager(alfio.manager.user.UserManager) DateTimeFormatter(java.time.format.DateTimeFormatter) Data(lombok.Data) Log4j2(lombok.extern.log4j.Log4j2) ResponseEntity(org.springframework.http.ResponseEntity) Wrappers.optionally(alfio.util.Wrappers.optionally) I18nManager(alfio.manager.i18n.I18nManager) AllArgsConstructor(lombok.AllArgsConstructor) ConfigurationKeys(alfio.model.system.ConfigurationKeys) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ValidationResult(alfio.model.result.ValidationResult)

Aggregations

ValidationResult (alfio.model.result.ValidationResult)11 Validator (alfio.util.Validator)6 IOException (java.io.IOException)5 Collectors (java.util.stream.Collectors)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 ResponseEntity (org.springframework.http.ResponseEntity)5 org.springframework.web.bind.annotation (org.springframework.web.bind.annotation)5 alfio.model (alfio.model)4 I18nManager (alfio.manager.i18n.I18nManager)3 Event (alfio.model.Event)3 PaymentProxy (alfio.model.transaction.PaymentProxy)3 Organization (alfio.model.user.Organization)3 BigDecimal (java.math.BigDecimal)3 Principal (java.security.Principal)3 java.util (java.util)3 Log4j2 (lombok.extern.log4j.Log4j2)3 StringUtils (org.apache.commons.lang3.StringUtils)3 Validate (org.apache.commons.lang3.Validate)3 HttpStatus (org.springframework.http.HttpStatus)3 EventListItem (alfio.controller.api.support.EventListItem)2