Search in sources :

Example 1 with ContentLanguage

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;
        }
    };
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) ZonedDateTime(java.time.ZonedDateTime) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) org.springframework.web.servlet.config.annotation(org.springframework.web.servlet.config.annotation) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ModelMap(org.springframework.ui.ModelMap) HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter) RequestContextUtils(org.springframework.web.servlet.support.RequestContextUtils) LocalizationMessageInterceptor(org.springframework.web.servlet.view.mustache.jmustache.LocalizationMessageInterceptor) HandlerMethod(org.springframework.web.method.HandlerMethod) Matcher(java.util.regex.Matcher) DefaultMessageSourceResolvable(org.springframework.context.support.DefaultMessageSourceResolvable) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) EventDescriptor(alfio.controller.decorator.EventDescriptor) JMustacheTemplateFactory(org.springframework.web.servlet.view.mustache.jmustache.JMustacheTemplateFactory) ConfigurationPathKey(alfio.model.system.Configuration.ConfigurationPathKey) MediaType(org.springframework.http.MediaType) ContentLanguage(alfio.model.ContentLanguage) Collectors(java.util.stream.Collectors) JMustacheTemplateLoader(org.springframework.web.servlet.view.mustache.jmustache.JMustacheTemplateLoader) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Configuration(org.springframework.context.annotation.Configuration) LocaleChangeInterceptor(org.springframework.web.servlet.i18n.LocaleChangeInterceptor) Environment(org.springframework.core.env.Environment) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Pattern(java.util.regex.Pattern) java.util(java.util) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ConfigurationManager(alfio.manager.system.ConfigurationManager) CommonsMultipartResolver(org.springframework.web.multipart.commons.CommonsMultipartResolver) MustacheViewResolver(org.springframework.web.servlet.view.mustache.MustacheViewResolver) HttpServletRequest(javax.servlet.http.HttpServletRequest) org.springframework.web.servlet(org.springframework.web.servlet) MessageSource(org.springframework.context.MessageSource) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) SessionLocaleResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) Mustache(com.samskivert.mustache.Mustache) ComponentScan(org.springframework.context.annotation.ComponentScan) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) DateTimeFormatter(java.time.format.DateTimeFormatter) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) I18nManager(alfio.manager.i18n.I18nManager) Event(alfio.model.Event) Bean(org.springframework.context.annotation.Bean) CsrfToken(org.springframework.security.web.csrf.CsrfToken) ConfigurationKeys(alfio.model.system.ConfigurationKeys) MustacheCustomTagInterceptor(alfio.util.MustacheCustomTagInterceptor) SessionLocaleResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) HttpServletResponse(javax.servlet.http.HttpServletResponse) ContentLanguage(alfio.model.ContentLanguage) HandlerMethod(org.springframework.web.method.HandlerMethod) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) HttpServletRequest(javax.servlet.http.HttpServletRequest) HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter) Bean(org.springframework.context.annotation.Bean)

Example 2 with ContentLanguage

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;
}
Also used : Organization(alfio.model.user.Organization) Event(alfio.model.Event) ContentLanguage(alfio.model.ContentLanguage) SendCodeModification(alfio.model.modification.SendCodeModification)

Aggregations

ContentLanguage (alfio.model.ContentLanguage)2 Event (alfio.model.Event)2 EventDescriptor (alfio.controller.decorator.EventDescriptor)1 I18nManager (alfio.manager.i18n.I18nManager)1 ConfigurationManager (alfio.manager.system.ConfigurationManager)1 SendCodeModification (alfio.model.modification.SendCodeModification)1 ConfigurationPathKey (alfio.model.system.Configuration.ConfigurationPathKey)1 ConfigurationKeys (alfio.model.system.ConfigurationKeys)1 Organization (alfio.model.user.Organization)1 MustacheCustomTagInterceptor (alfio.util.MustacheCustomTagInterceptor)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 SerializationFeature (com.fasterxml.jackson.databind.SerializationFeature)1 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)1 Mustache (com.samskivert.mustache.Mustache)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 java.util (java.util)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1