Subversion Repositories SmartDukaan

Rev

Rev 28614 | Rev 28716 | 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;
28339 tejbeer 12
import org.apache.velocity.app.VelocityEngine;
13
import org.apache.velocity.exception.VelocityException;
21555 kshitij.so 14
import org.springframework.context.annotation.Bean;
15
import org.springframework.context.annotation.ComponentScan;
16
import org.springframework.context.annotation.Configuration;
17
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
18
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
19
import org.springframework.core.io.ClassPathResource;
20
import org.springframework.core.io.Resource;
23985 tejbeer 21
import org.springframework.mail.javamail.JavaMailSender;
22
import org.springframework.mail.javamail.JavaMailSenderImpl;
28339 tejbeer 23
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
21555 kshitij.so 24
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
21625 kshitij.so 25
import org.springframework.web.servlet.ViewResolver;
24507 amit.gupta 26
import org.springframework.web.servlet.view.BeanNameViewResolver;
21625 kshitij.so 27
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
28
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
21555 kshitij.so 29
 
25300 tejbeer 30
import com.google.gson.Gson;
31
import com.google.gson.GsonBuilder;
26063 amit.gupta 32
import com.spice.profitmandi.common.util.Utils;
25300 tejbeer 33
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
34
 
21625 kshitij.so 35
@SuppressWarnings("deprecation")
21555 kshitij.so 36
@Configuration
37
@ComponentScan("com.spice.profitmandi.*")
38
public class AppConfig {
39
 
24639 tejbeer 40
	private static final String VELOCITY_PATH_PREFIX = "/WEB-INF/views/ftl/";
41
	private static final String VELOCITY_PATH_SUFFIX = ".vm";
42
	private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:message";
43
	private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
21555 kshitij.so 44
	private static Resource resource;
24639 tejbeer 45
 
21555 kshitij.so 46
	public static Resource getResource() {
47
		return resource;
48
	}
49
 
50
	public void setResource(Resource resource) {
51
		AppConfig.resource = resource;
52
	}
21625 kshitij.so 53
 
54
	@Bean
24639 tejbeer 55
	public ViewResolver viewResolver() {
21625 kshitij.so 56
		VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
57
		bean.setPrefix("");
58
		bean.setSuffix(VELOCITY_PATH_SUFFIX);
22108 amit.gupta 59
		bean.setRequestContextAttribute("rc");
23869 amit.gupta 60
		Map<String, Object> attributesMap = new HashMap<>();
61
		DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
27867 tejbeer 62
		DateTimeFormatter dateMonthFormatter = DateTimeFormatter.ofPattern("dd''MMM");
27763 tejbeer 63
		DateTimeFormatter datehiphenFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
28455 tejbeer 64
		DateTimeFormatter dateYearMonthFormatter = DateTimeFormatter.ofPattern("MMM''uu");
65
		DateTimeFormatter dateMonthYearFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
27763 tejbeer 66
 
27607 tejbeer 67
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
23869 amit.gupta 68
		attributesMap.put("dateFormatter", dateFormatter);
27867 tejbeer 69
		attributesMap.put("dateMonthFormatter", dateMonthFormatter);
27763 tejbeer 70
		attributesMap.put("datehiphenFormatter", datehiphenFormatter);
28455 tejbeer 71
		attributesMap.put("dateYearMonthFormatter", dateYearMonthFormatter);
72
		attributesMap.put("dateMonthYearFormatter", dateMonthYearFormatter);
73
 
27876 amit.gupta 74
		attributesMap.put("noData",
75
				"<tr><td colspan=\"20\" style=\"text-align:center;\">No results found for the given criteria</td></tr>");
23869 amit.gupta 76
		attributesMap.put("dateTimeFormatter", dateTimeFormatter);
28652 amit.gupta 77
		attributesMap.put("version", "65");
27712 amit.gupta 78
		attributesMap.put("cssVersion", "9");
25811 amit.gupta 79
		attributesMap.put("isDev", getActiveProfile().equals("dev"));
26063 amit.gupta 80
		attributesMap.put("vmUtils", new Utils());
23869 amit.gupta 81
		bean.setAttributesMap(attributesMap);
21625 kshitij.so 82
		return bean;
21555 kshitij.so 83
	}
21625 kshitij.so 84
 
24639 tejbeer 85
	@Bean
86
	public ViewResolver beanNameViewResolver() {
87
		BeanNameViewResolver resolver = new BeanNameViewResolver();
88
		return resolver;
89
	}
24507 amit.gupta 90
 
21555 kshitij.so 91
	@Bean
21625 kshitij.so 92
	public VelocityConfigurer velocityConfig() {
93
		VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
94
		velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
95
		return velocityConfigurer;
21555 kshitij.so 96
	}
97
 
24639 tejbeer 98
	@Bean(name = "messageSource")
21555 kshitij.so 99
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
24639 tejbeer 100
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
101
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21555 kshitij.so 102
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
103
		return messageSource;
104
	}
105
 
24639 tejbeer 106
	@Bean(name = "multipartResolver")
21555 kshitij.so 107
	public CommonsMultipartResolver getCommonsMultipartResolver() {
108
		LOGGER.info("creating common multipart resolver bean");
109
		return new CommonsMultipartResolver();
110
	}
111
 
112
	@Bean
113
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
25811 amit.gupta 114
		String activeProfile = getActiveProfile();
21555 kshitij.so 115
 
24639 tejbeer 116
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 117
 
21555 kshitij.so 118
		if ("prod".equals(activeProfile)) {
119
			resource = new ClassPathResource("/META-INF/prod.properties");
120
		} else if ("staging".equals(activeProfile)) {
121
			resource = new ClassPathResource("/META-INF/staging.properties");
122
		} else {
123
			resource = new ClassPathResource("/META-INF/dev.properties");
124
		}
125
 
126
		propertySourcesPlaceholderConfigurer.setLocation(resource);
127
 
128
		return propertySourcesPlaceholderConfigurer;
129
	}
21625 kshitij.so 130
 
25811 amit.gupta 131
	private String getActiveProfile() {
132
		Properties properties = new Properties();
133
		try {
134
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
135
		} catch (IOException e) {
136
			LOGGER.error("Error in reading env property file.Adding default property" + e);
137
			properties.put("profile", "dev");
138
		}
139
		LOGGER.info("Profile is [{}]", properties.get("profile"));
140
		return (String) properties.get("profile");
141
	}
142
 
24639 tejbeer 143
	@Bean(name = "mailSender")
25091 amit.gupta 144
	public JavaMailSender getSendgridMailSender() {
24639 tejbeer 145
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
146
 
147
		// Using gmail
148
		mailSender.setHost("smtp.sendgrid.net");
149
		mailSender.setPort(587);
27629 amit.gupta 150
		mailSender.setUsername("apikey");
151
		mailSender.setPassword("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");
24639 tejbeer 152
 
153
		Properties javaMailProperties = new Properties();
154
		javaMailProperties.put("mail.smtp.starttls.enable", "false");
155
		javaMailProperties.put("mail.smtp.auth", "true");
156
		javaMailProperties.put("mail.transport.protocol", "smtp");
157
		javaMailProperties.put("mail.debug", "true");// Prints out everything on
158
														// screen
159
 
160
		mailSender.setJavaMailProperties(javaMailProperties);
161
		return mailSender;
162
	}
163
 
25300 tejbeer 164
	@Bean(name = "gson")
165
	public Gson gson() {
166
 
26063 amit.gupta 167
		Gson gson = new GsonBuilder().serializeNulls()
25300 tejbeer 168
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
169
 
170
		return gson;
171
 
172
	}
173
 
28339 tejbeer 174
	@Bean(name = "veloctyEngine")
175
	public VelocityEngine velocityEngine() throws VelocityException, IOException {
176
		VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
177
		// Properties props = new Properties();
178
		// props.put("resource.loader", "file");
179
 
180
		// props.put("file.resource.loader.description", "Velocity File Resource
181
		// Loader");
182
		// props.put("file.resource.loader.class",
183
		// "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
184
		// props.put("file.resource.loader.cache", true);
185
		// props.put("file.resource.loader.path", ".");
186
 
187
		Properties velocityProperties = new Properties();
188
		velocityProperties.put("resource.loader", "class");
189
		velocityProperties.put("class.resource.loader.class",
190
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
191
		velocityProperties.put("file.resource.loader.cache", true);
192
		velocityProperties.put("file.resource.loader.unicode", true);
193
		velocityProperties.put("input.encoding", "UTF-8");
194
		velocityProperties.put("output.encoding", "UTF-8");
195
		velocityProperties.put("overrideLogging", true);
196
 
197
		// velocityProperties.put("file.resource.loader.path", ".");
198
 
199
		factory.setVelocityProperties(velocityProperties);
200
 
201
		return factory.createVelocityEngine();
202
 
203
	}
204
 
21555 kshitij.so 205
}