Subversion Repositories SmartDukaan

Rev

Rev 21749 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.config;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;

@Configuration
@ComponentScan("com.spice.profitmandi.*")
public class AppConfig {

        // private static final String FTL_PATH_PREFIX="/WEB-INF/views/ftl/";
        // private static final String FTL_PATH_SUFFIX=".ftl";
        private static final String HTML_PATH_PREFIX = "/WEB-INF/views/";
        private static final String HTML_PATH_SUFFIX = ".jsp";
        private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:message";
        private static final Logger LOGGER = LoggerFactory.getLogger(AppConfig.class);
        private static Resource resource;

        public static Resource getResource() {
                return resource;
        }

        public void setResource(Resource resource) {
                AppConfig.resource = resource;
        }

        /*
         * @Bean public FreeMarkerConfigurer freemarkerConfig() { FreeMarkerConfigurer
         * freeMarkerConfigurer = new FreeMarkerConfigurer();
         * freeMarkerConfigurer.setTemplateLoaderPath(PATH_PREFIX); return
         * freeMarkerConfigurer; }
         * 
         * @Bean public FreeMarkerViewResolver freemarkerViewResolver() {
         * FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
         * resolver.setCache(false); resolver.setPrefix("");
         * resolver.setSuffix(PATH_SUFFIX); return resolver; }
         */

        @Bean(name = "viewResolver")
        public InternalResourceViewResolver getViewResolver() {
                LOGGER.debug(
                                "creating view resolver bean with prefix : " + HTML_PATH_PREFIX + " and suffix : " + HTML_PATH_SUFFIX);
                InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
                viewResolver.setPrefix(HTML_PATH_PREFIX);
                viewResolver.setSuffix(HTML_PATH_SUFFIX);
                return viewResolver;
        }

        @Bean(name = "messageSource")
        public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
                LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
                ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
                messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
                return messageSource;
        }

        @Bean(name = "multipartResolver")
        public CommonsMultipartResolver getCommonsMultipartResolver() {
                LOGGER.info("creating common multipart resolver bean");
                return new CommonsMultipartResolver();
        }

        @Bean
        public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
                String activeProfile;

                PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();

                Properties properties = new Properties();
                try {
                        properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
                } catch (IOException e) {
                        LOGGER.error("Error in reading env property file.Adding default property" + e);
                        properties.put("profile", "dev");
                }
                activeProfile = (String) properties.get("profile");

                if ("prod".equals(activeProfile)) {
                        resource = new ClassPathResource("/META-INF/prod.properties");
                } else if ("staging".equals(activeProfile)) {
                        resource = new ClassPathResource("/META-INF/staging.properties");
                } else {
                        resource = new ClassPathResource("/META-INF/dev.properties");
                }

                propertySourcesPlaceholderConfigurer.setLocation(resource);

                return propertySourcesPlaceholderConfigurer;
        }

        @Bean(name = "gson")
        public Gson gson() {

                Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls()
                                .registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();

                return gson;

        }

}