Subversion Repositories SmartDukaan

Rev

Rev 27695 | Rev 27712 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21561 ashik.ali 1
package com.spice.profitmandi.web.config;
21555 kshitij.so 2
 
3
import java.io.IOException;
25300 tejbeer 4
import java.time.LocalDateTime;
23869 amit.gupta 5
import java.time.format.DateTimeFormatter;
6
import java.util.HashMap;
7
import java.util.Map;
21555 kshitij.so 8
import java.util.Properties;
9
 
23869 amit.gupta 10
import org.apache.logging.log4j.LogManager;
23568 govind 11
import org.apache.logging.log4j.Logger;
21555 kshitij.so 12
import org.springframework.context.annotation.Bean;
13
import org.springframework.context.annotation.ComponentScan;
14
import org.springframework.context.annotation.Configuration;
15
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
16
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
17
import org.springframework.core.io.ClassPathResource;
18
import org.springframework.core.io.Resource;
23985 tejbeer 19
import org.springframework.mail.javamail.JavaMailSender;
20
import org.springframework.mail.javamail.JavaMailSenderImpl;
21555 kshitij.so 21
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
21625 kshitij.so 22
import org.springframework.web.servlet.ViewResolver;
24507 amit.gupta 23
import org.springframework.web.servlet.view.BeanNameViewResolver;
21625 kshitij.so 24
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
25
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
21555 kshitij.so 26
 
25300 tejbeer 27
import com.google.gson.Gson;
28
import com.google.gson.GsonBuilder;
26063 amit.gupta 29
import com.spice.profitmandi.common.util.Utils;
25300 tejbeer 30
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
31
 
21625 kshitij.so 32
@SuppressWarnings("deprecation")
21555 kshitij.so 33
@Configuration
34
@ComponentScan("com.spice.profitmandi.*")
35
public class AppConfig {
36
 
24639 tejbeer 37
	private static final String VELOCITY_PATH_PREFIX = "/WEB-INF/views/ftl/";
38
	private static final String VELOCITY_PATH_SUFFIX = ".vm";
39
	private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:message";
40
	private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
21555 kshitij.so 41
	private static Resource resource;
24639 tejbeer 42
 
21555 kshitij.so 43
	public static Resource getResource() {
44
		return resource;
45
	}
46
 
47
	public void setResource(Resource resource) {
48
		AppConfig.resource = resource;
49
	}
21625 kshitij.so 50
 
51
	@Bean
24639 tejbeer 52
	public ViewResolver viewResolver() {
21625 kshitij.so 53
		VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
54
		bean.setPrefix("");
55
		bean.setSuffix(VELOCITY_PATH_SUFFIX);
22108 amit.gupta 56
		bean.setRequestContextAttribute("rc");
23869 amit.gupta 57
		Map<String, Object> attributesMap = new HashMap<>();
58
		DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
27607 tejbeer 59
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
23869 amit.gupta 60
		attributesMap.put("dateFormatter", dateFormatter);
61
		attributesMap.put("dateTimeFormatter", dateTimeFormatter);
27697 tejbeer 62
		attributesMap.put("version", "23");
25290 amit.gupta 63
		attributesMap.put("cssVersion", "8");
25811 amit.gupta 64
		attributesMap.put("isDev", getActiveProfile().equals("dev"));
26063 amit.gupta 65
		attributesMap.put("vmUtils", new Utils());
23869 amit.gupta 66
		bean.setAttributesMap(attributesMap);
21625 kshitij.so 67
		return bean;
21555 kshitij.so 68
	}
21625 kshitij.so 69
 
24639 tejbeer 70
	@Bean
71
	public ViewResolver beanNameViewResolver() {
72
		BeanNameViewResolver resolver = new BeanNameViewResolver();
73
		return resolver;
74
	}
24507 amit.gupta 75
 
21555 kshitij.so 76
	@Bean
21625 kshitij.so 77
	public VelocityConfigurer velocityConfig() {
78
		VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
79
		velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
80
		return velocityConfigurer;
21555 kshitij.so 81
	}
82
 
24639 tejbeer 83
	@Bean(name = "messageSource")
21555 kshitij.so 84
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
24639 tejbeer 85
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
86
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21555 kshitij.so 87
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
88
		return messageSource;
89
	}
90
 
24639 tejbeer 91
	@Bean(name = "multipartResolver")
21555 kshitij.so 92
	public CommonsMultipartResolver getCommonsMultipartResolver() {
93
		LOGGER.info("creating common multipart resolver bean");
94
		return new CommonsMultipartResolver();
95
	}
96
 
97
	@Bean
98
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
25811 amit.gupta 99
		String activeProfile = getActiveProfile();
21555 kshitij.so 100
 
24639 tejbeer 101
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 102
 
21555 kshitij.so 103
		if ("prod".equals(activeProfile)) {
104
			resource = new ClassPathResource("/META-INF/prod.properties");
105
		} else if ("staging".equals(activeProfile)) {
106
			resource = new ClassPathResource("/META-INF/staging.properties");
107
		} else {
108
			resource = new ClassPathResource("/META-INF/dev.properties");
109
		}
110
 
111
		propertySourcesPlaceholderConfigurer.setLocation(resource);
112
 
113
		return propertySourcesPlaceholderConfigurer;
114
	}
21625 kshitij.so 115
 
25811 amit.gupta 116
	private String getActiveProfile() {
117
		Properties properties = new Properties();
118
		try {
119
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
120
		} catch (IOException e) {
121
			LOGGER.error("Error in reading env property file.Adding default property" + e);
122
			properties.put("profile", "dev");
123
		}
124
		LOGGER.info("Profile is [{}]", properties.get("profile"));
125
		return (String) properties.get("profile");
126
	}
127
 
24639 tejbeer 128
	@Bean(name = "mailSender")
25091 amit.gupta 129
	public JavaMailSender getSendgridMailSender() {
24639 tejbeer 130
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
131
 
132
		// Using gmail
133
		mailSender.setHost("smtp.sendgrid.net");
134
		mailSender.setPort(587);
27629 amit.gupta 135
		mailSender.setUsername("apikey");
136
		mailSender.setPassword("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");
24639 tejbeer 137
 
138
		Properties javaMailProperties = new Properties();
139
		javaMailProperties.put("mail.smtp.starttls.enable", "false");
140
		javaMailProperties.put("mail.smtp.auth", "true");
141
		javaMailProperties.put("mail.transport.protocol", "smtp");
142
		javaMailProperties.put("mail.debug", "true");// Prints out everything on
143
														// screen
144
 
145
		mailSender.setJavaMailProperties(javaMailProperties);
146
		return mailSender;
147
	}
148
 
25300 tejbeer 149
	@Bean(name = "gson")
150
	public Gson gson() {
151
 
26063 amit.gupta 152
		Gson gson = new GsonBuilder().serializeNulls()
25300 tejbeer 153
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
154
 
155
		return gson;
156
 
157
	}
158
 
21555 kshitij.so 159
}