use of alfio.model.ContentLanguage in project alf.io by alfio-event.
the class MvcConfiguration method getEventLocaleSetterInterceptor.
@Bean
public HandlerInterceptor getEventLocaleSetterInterceptor() {
return new HandlerInterceptorAdapter() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = ((HandlerMethod) handler);
RequestMapping reqMapping = handlerMethod.getMethodAnnotation(RequestMapping.class);
// check if the request mapping value has the form "/event/{something}"
Pattern eventPattern = Pattern.compile("^/event/\\{(\\w+)}/{0,1}.*");
if (reqMapping != null && reqMapping.value().length == 1 && eventPattern.matcher(reqMapping.value()[0]).matches()) {
Matcher m = eventPattern.matcher(reqMapping.value()[0]);
m.matches();
String pathVariableName = m.group(1);
// extract the parameter name
Arrays.stream(handlerMethod.getMethodParameters()).map(methodParameter -> methodParameter.getParameterAnnotation(PathVariable.class)).filter(Objects::nonNull).map(PathVariable::value).filter(pathVariableName::equals).findFirst().ifPresent((val) -> {
// fetch the parameter value
@SuppressWarnings("unchecked") String eventName = Optional.ofNullable(((Map<String, Object>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).get(val)).orElse("").toString();
LocaleResolver resolver = RequestContextUtils.getLocaleResolver(request);
Locale locale = resolver.resolveLocale(request);
List<ContentLanguage> cl = i18nManager.getEventLanguages(eventName);
request.setAttribute("ALFIO_EVENT_NAME", eventName);
if (cl.stream().noneMatch(contentLanguage -> contentLanguage.getLanguage().equals(Optional.ofNullable(locale).orElse(Locale.ENGLISH).getLanguage()))) {
// override the user locale if it's not in the one permitted by the event
resolver.setLocale(request, response, cl.stream().findFirst().map(ContentLanguage::getLocale).orElse(Locale.ENGLISH));
} else {
resolver.setLocale(request, response, locale);
}
});
}
}
return true;
}
};
}
use of alfio.model.ContentLanguage in project alf.io by alfio-event.
the class SpecialPriceManager method sendCodeToAssignee.
public boolean sendCodeToAssignee(List<SendCodeModification> input, String eventName, int categoryId, String username) {
final Event event = eventManager.getSingleEvent(eventName, username);
final Organization organization = eventManager.loadOrganizer(event, username);
Set<SendCodeModification> set = new LinkedHashSet<>(input);
checkCodeAssignment(set, categoryId, event, username);
Validate.isTrue(set.stream().allMatch(IS_CODE_PRESENT), "There are missing codes. Please check input file.");
List<ContentLanguage> eventLanguages = i18nManager.getEventLanguages(event.getLocales());
Validate.isTrue(!eventLanguages.isEmpty(), "No locales have been defined for the event. Please check the configuration");
ContentLanguage defaultLocale = eventLanguages.contains(ContentLanguage.ENGLISH) ? ContentLanguage.ENGLISH : eventLanguages.get(0);
set.forEach(m -> {
Locale locale = Locale.forLanguageTag(StringUtils.defaultString(StringUtils.trimToNull(m.getLanguage()), defaultLocale.getLanguage()));
Map<String, Object> model = TemplateResource.prepareModelForSendReservedCode(organization, event, m, eventManager.getEventUrl(event));
notificationManager.sendSimpleEmail(event, m.getEmail(), messageSource.getMessage("email-code.subject", new Object[] { event.getDisplayName() }, locale), () -> templateManager.renderTemplate(event, TemplateResource.SEND_RESERVED_CODE, model, locale));
int marked = specialPriceRepository.markAsSent(ZonedDateTime.now(event.getZoneId()), m.getAssignee().trim(), m.getEmail().trim(), m.getCode().trim());
Validate.isTrue(marked == 1, "Expected exactly one row updated, got " + marked);
});
return true;
}
Aggregations