Subversion Repositories SmartDukaan

Rev

Rev 28082 | Rev 28143 | Go to most recent revision | 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.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;

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

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

        private static final String VELOCITY_PATH_PREFIX = "/WEB-INF/views/ftl/";
        private static final String VELOCITY_PATH_SUFFIX = ".vm";
        private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:message";
        private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
        private static Resource resource;

        public static Resource getResource() {
                return resource;
        }

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

        @Bean
        public ViewResolver viewResolver() {
                VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
                bean.setPrefix("");
                bean.setSuffix(VELOCITY_PATH_SUFFIX);
                bean.setRequestContextAttribute("rc");
                Map<String, Object> attributesMap = new HashMap<>();
                DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
                DateTimeFormatter dateMonthFormatter = DateTimeFormatter.ofPattern("dd''MMM");
                DateTimeFormatter datehiphenFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
                attributesMap.put("dateFormatter", dateFormatter);
                attributesMap.put("dateMonthFormatter", dateMonthFormatter);
                attributesMap.put("datehiphenFormatter", datehiphenFormatter);
                attributesMap.put("noData",
                                "<tr><td colspan=\"20\" style=\"text-align:center;\">No results found for the given criteria</td></tr>");
                attributesMap.put("dateTimeFormatter", dateTimeFormatter);
                attributesMap.put("version", "42");
                attributesMap.put("cssVersion", "9");
                attributesMap.put("isDev", getActiveProfile().equals("dev"));
                attributesMap.put("vmUtils", new Utils());
                bean.setAttributesMap(attributesMap);
                return bean;
        }

        @Bean
        public ViewResolver beanNameViewResolver() {
                BeanNameViewResolver resolver = new BeanNameViewResolver();
                return resolver;
        }

        @Bean
        public VelocityConfigurer velocityConfig() {
                VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
                velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
                return velocityConfigurer;
        }

        @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 = getActiveProfile();

                PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();

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

        private String getActiveProfile() {
                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");
                }
                LOGGER.info("Profile is [{}]", properties.get("profile"));
                return (String) properties.get("profile");
        }

        @Bean(name = "mailSender")
        public JavaMailSender getSendgridMailSender() {
                JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

                // Using gmail
                mailSender.setHost("smtp.sendgrid.net");
                mailSender.setPort(587);
                mailSender.setUsername("apikey");
                mailSender.setPassword("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");

                Properties javaMailProperties = new Properties();
                javaMailProperties.put("mail.smtp.starttls.enable", "false");
                javaMailProperties.put("mail.smtp.auth", "true");
                javaMailProperties.put("mail.transport.protocol", "smtp");
                javaMailProperties.put("mail.debug", "true");// Prints out everything on
                                                                                                                // screen

                mailSender.setJavaMailProperties(javaMailProperties);
                return mailSender;
        }

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

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

                return gson;

        }

}