Search in sources :

Example 1 with ConfigurationPathKey

use of alfio.model.system.Configuration.ConfigurationPathKey in project alf.io by alfio-event.

the class DynamicResourcesController method getGoogleAnalyticsScript.

@RequestMapping("/resources/js/google-analytics")
public void getGoogleAnalyticsScript(HttpSession session, HttpServletResponse response, @RequestParam("e") Integer eventId) throws IOException {
    response.setContentType("application/javascript");
    Optional<Event> ev = Optional.ofNullable(eventId).flatMap(id -> Optional.ofNullable(eventRepository.findById(id)));
    ConfigurationPathKey pathKey = ev.map(e -> Configuration.from(e.getOrganizationId(), e.getId(), GOOGLE_ANALYTICS_KEY)).orElseGet(() -> Configuration.getSystemConfiguration(GOOGLE_ANALYTICS_KEY));
    final Optional<String> id = configurationManager.getStringConfigValue(pathKey);
    final String script;
    ConfigurationPathKey anonymousPathKey = ev.map(e -> Configuration.from(e.getOrganizationId(), e.getId(), GOOGLE_ANALYTICS_ANONYMOUS_MODE)).orElseGet(() -> Configuration.getSystemConfiguration(GOOGLE_ANALYTICS_ANONYMOUS_MODE));
    if (id.isPresent() && configurationManager.getBooleanConfigValue(anonymousPathKey, true)) {
        String trackingId = Optional.ofNullable(StringUtils.trimToNull((String) session.getAttribute("GA_TRACKING_ID"))).orElseGet(() -> UUID.randomUUID().toString());
        Map<String, Object> model = new HashMap<>();
        model.put("clientId", trackingId);
        model.put("account", id.get());
        script = templateManager.renderTemplate(TemplateResource.GOOGLE_ANALYTICS, model, Locale.ENGLISH);
    } else {
        script = id.map(x -> String.format(GOOGLE_ANALYTICS_SCRIPT, x)).orElse(EMPTY);
    }
    response.getWriter().write(script);
}
Also used : ConfigurationPathKey(alfio.model.system.Configuration.ConfigurationPathKey) HttpSession(javax.servlet.http.HttpSession) RequestParam(org.springframework.web.bind.annotation.RequestParam) java.util(java.util) ConfigurationPathKey(alfio.model.system.Configuration.ConfigurationPathKey) GOOGLE_ANALYTICS_KEY(alfio.model.system.ConfigurationKeys.GOOGLE_ANALYTICS_KEY) TemplateManager(alfio.util.TemplateManager) HttpServletResponse(javax.servlet.http.HttpServletResponse) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) IOException(java.io.IOException) ConfigurationManager(alfio.manager.system.ConfigurationManager) Controller(org.springframework.stereotype.Controller) EventRepository(alfio.repository.EventRepository) StringUtils(org.apache.commons.lang3.StringUtils) GOOGLE_ANALYTICS_ANONYMOUS_MODE(alfio.model.system.ConfigurationKeys.GOOGLE_ANALYTICS_ANONYMOUS_MODE) Configuration(alfio.model.system.Configuration) Event(alfio.model.Event) TemplateResource(alfio.util.TemplateResource) Event(alfio.model.Event) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ConfigurationPathKey

use of alfio.model.system.Configuration.ConfigurationPathKey in project alf.io by alfio-event.

the class MvcConfiguration method getDefaultTemplateObjectsFiller.

@Bean
public HandlerInterceptorAdapter getDefaultTemplateObjectsFiller() {
    return new HandlerInterceptorAdapter() {

        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
            Optional.ofNullable(modelAndView).filter(mv -> !StringUtils.startsWith(mv.getViewName(), "redirect:")).ifPresent(mv -> {
                mv.addObject("request", request);
                final ModelMap modelMap = mv.getModelMap();
                boolean demoModeEnabled = environment.acceptsProfiles(Initializer.PROFILE_DEMO);
                modelMap.put("demoModeEnabled", demoModeEnabled);
                Optional.ofNullable(request.getAttribute("ALFIO_EVENT_NAME")).map(Object::toString).ifPresent(eventName -> {
                    List<?> availableLanguages = i18nManager.getEventLanguages(eventName);
                    modelMap.put("showAvailableLanguagesInPageTop", availableLanguages.size() > 1);
                    modelMap.put("availableLanguages", availableLanguages);
                });
                modelMap.putIfAbsent("event", null);
                modelMap.putIfAbsent("pageTitle", "empty");
                Event event = modelMap.get("event") == null ? null : modelMap.get("event") instanceof Event ? (Event) modelMap.get("event") : ((EventDescriptor) modelMap.get("event")).getEvent();
                ConfigurationPathKey googleAnalyticsKey = Optional.ofNullable(event).map(e -> alfio.model.system.Configuration.from(e.getOrganizationId(), e.getId(), GOOGLE_ANALYTICS_KEY)).orElseGet(() -> alfio.model.system.Configuration.getSystemConfiguration(GOOGLE_ANALYTICS_KEY));
                modelMap.putIfAbsent("analyticsEnabled", StringUtils.isNotBlank(configurationManager.getStringConfigValue(googleAnalyticsKey, "")));
                if (demoModeEnabled) {
                    modelMap.putIfAbsent("paypalTestUsername", configurationManager.getStringConfigValue(alfio.model.system.Configuration.getSystemConfiguration(PAYPAL_DEMO_MODE_USERNAME), "<missing>"));
                    modelMap.putIfAbsent("paypalTestPassword", configurationManager.getStringConfigValue(alfio.model.system.Configuration.getSystemConfiguration(PAYPAL_DEMO_MODE_PASSWORD), "<missing>"));
                }
            });
        }
    };
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) 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) ConfigurationPathKey(alfio.model.system.Configuration.ConfigurationPathKey) EventDescriptor(alfio.controller.decorator.EventDescriptor) ModelMap(org.springframework.ui.ModelMap) HandlerInterceptorAdapter(org.springframework.web.servlet.handler.HandlerInterceptorAdapter) HttpServletResponse(javax.servlet.http.HttpServletResponse) Event(alfio.model.Event) Bean(org.springframework.context.annotation.Bean)

Aggregations

ConfigurationManager (alfio.manager.system.ConfigurationManager)2 Event (alfio.model.Event)2 ConfigurationPathKey (alfio.model.system.Configuration.ConfigurationPathKey)2 java.util (java.util)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 StringUtils (org.apache.commons.lang3.StringUtils)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 EventDescriptor (alfio.controller.decorator.EventDescriptor)1 I18nManager (alfio.manager.i18n.I18nManager)1 ContentLanguage (alfio.model.ContentLanguage)1 Configuration (alfio.model.system.Configuration)1 ConfigurationKeys (alfio.model.system.ConfigurationKeys)1 GOOGLE_ANALYTICS_ANONYMOUS_MODE (alfio.model.system.ConfigurationKeys.GOOGLE_ANALYTICS_ANONYMOUS_MODE)1 GOOGLE_ANALYTICS_KEY (alfio.model.system.ConfigurationKeys.GOOGLE_ANALYTICS_KEY)1 EventRepository (alfio.repository.EventRepository)1 MustacheCustomTagInterceptor (alfio.util.MustacheCustomTagInterceptor)1 TemplateManager (alfio.util.TemplateManager)1 TemplateResource (alfio.util.TemplateResource)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1