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);
}
}
}
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;
}
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));
}
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);
}
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));
}
Aggregations