Subversion Repositories SmartDukaan

Rev

Rev 29887 | Rev 29913 | 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");
29266 manish 66
		DateTimeFormatter dateMonthChYear = DateTimeFormatter.ofPattern("d''MMM''uu");
27763 tejbeer 67
 
27607 tejbeer 68
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
23869 amit.gupta 69
		attributesMap.put("dateFormatter", dateFormatter);
27867 tejbeer 70
		attributesMap.put("dateMonthFormatter", dateMonthFormatter);
27763 tejbeer 71
		attributesMap.put("datehiphenFormatter", datehiphenFormatter);
28455 tejbeer 72
		attributesMap.put("dateYearMonthFormatter", dateYearMonthFormatter);
73
		attributesMap.put("dateMonthYearFormatter", dateMonthYearFormatter);
29266 manish 74
		attributesMap.put("dateMonthChYear", dateMonthChYear);
28455 tejbeer 75
 
27876 amit.gupta 76
		attributesMap.put("noData",
77
				"<tr><td colspan=\"20\" style=\"text-align:center;\">No results found for the given criteria</td></tr>");
23869 amit.gupta 78
		attributesMap.put("dateTimeFormatter", dateTimeFormatter);
29909 amit.gupta 79
		attributesMap.put("version", "98");
29010 amit.gupta 80
		attributesMap.put("cssVersion", "10");
25811 amit.gupta 81
		attributesMap.put("isDev", getActiveProfile().equals("dev"));
26063 amit.gupta 82
		attributesMap.put("vmUtils", new Utils());
23869 amit.gupta 83
		bean.setAttributesMap(attributesMap);
21625 kshitij.so 84
		return bean;
21555 kshitij.so 85
	}
21625 kshitij.so 86
 
24639 tejbeer 87
	@Bean
88
	public ViewResolver beanNameViewResolver() {
89
		BeanNameViewResolver resolver = new BeanNameViewResolver();
90
		return resolver;
91
	}
24507 amit.gupta 92
 
21555 kshitij.so 93
	@Bean
21625 kshitij.so 94
	public VelocityConfigurer velocityConfig() {
95
		VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
96
		velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
97
		return velocityConfigurer;
21555 kshitij.so 98
	}
99
 
24639 tejbeer 100
	@Bean(name = "messageSource")
21555 kshitij.so 101
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
24639 tejbeer 102
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
103
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21555 kshitij.so 104
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
105
		return messageSource;
106
	}
107
 
24639 tejbeer 108
	@Bean(name = "multipartResolver")
21555 kshitij.so 109
	public CommonsMultipartResolver getCommonsMultipartResolver() {
110
		LOGGER.info("creating common multipart resolver bean");
111
		return new CommonsMultipartResolver();
112
	}
113
 
114
	@Bean
115
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
25811 amit.gupta 116
		String activeProfile = getActiveProfile();
21555 kshitij.so 117
 
24639 tejbeer 118
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 119
 
21555 kshitij.so 120
		if ("prod".equals(activeProfile)) {
121
			resource = new ClassPathResource("/META-INF/prod.properties");
122
		} else if ("staging".equals(activeProfile)) {
123
			resource = new ClassPathResource("/META-INF/staging.properties");
124
		} else {
125
			resource = new ClassPathResource("/META-INF/dev.properties");
126
		}
127
 
128
		propertySourcesPlaceholderConfigurer.setLocation(resource);
129
 
130
		return propertySourcesPlaceholderConfigurer;
131
	}
21625 kshitij.so 132
 
25811 amit.gupta 133
	private String getActiveProfile() {
134
		Properties properties = new Properties();
135
		try {
136
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
137
		} catch (IOException e) {
138
			LOGGER.error("Error in reading env property file.Adding default property" + e);
139
			properties.put("profile", "dev");
140
		}
141
		LOGGER.info("Profile is [{}]", properties.get("profile"));
142
		return (String) properties.get("profile");
143
	}
144
 
24639 tejbeer 145
	@Bean(name = "mailSender")
25091 amit.gupta 146
	public JavaMailSender getSendgridMailSender() {
24639 tejbeer 147
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
148
 
149
		// Using gmail
150
		mailSender.setHost("smtp.sendgrid.net");
151
		mailSender.setPort(587);
27629 amit.gupta 152
		mailSender.setUsername("apikey");
153
		mailSender.setPassword("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");
24639 tejbeer 154
 
155
		Properties javaMailProperties = new Properties();
156
		javaMailProperties.put("mail.smtp.starttls.enable", "false");
157
		javaMailProperties.put("mail.smtp.auth", "true");
158
		javaMailProperties.put("mail.transport.protocol", "smtp");
159
		javaMailProperties.put("mail.debug", "true");// Prints out everything on
160
														// screen
161
 
162
		mailSender.setJavaMailProperties(javaMailProperties);
163
		return mailSender;
164
	}
165
 
25300 tejbeer 166
	@Bean(name = "gson")
167
	public Gson gson() {
168
 
26063 amit.gupta 169
		Gson gson = new GsonBuilder().serializeNulls()
25300 tejbeer 170
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
171
 
172
		return gson;
173
 
174
	}
175
 
28339 tejbeer 176
	@Bean(name = "veloctyEngine")
177
	public VelocityEngine velocityEngine() throws VelocityException, IOException {
178
		VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
179
		// Properties props = new Properties();
180
		// props.put("resource.loader", "file");
181
 
182
		// props.put("file.resource.loader.description", "Velocity File Resource
183
		// Loader");
184
		// props.put("file.resource.loader.class",
185
		// "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
186
		// props.put("file.resource.loader.cache", true);
187
		// props.put("file.resource.loader.path", ".");
188
 
189
		Properties velocityProperties = new Properties();
190
		velocityProperties.put("resource.loader", "class");
191
		velocityProperties.put("class.resource.loader.class",
192
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
193
		velocityProperties.put("file.resource.loader.cache", true);
194
		velocityProperties.put("file.resource.loader.unicode", true);
195
		velocityProperties.put("input.encoding", "UTF-8");
196
		velocityProperties.put("output.encoding", "UTF-8");
197
		velocityProperties.put("overrideLogging", true);
198
 
199
		// velocityProperties.put("file.resource.loader.path", ".");
200
 
201
		factory.setVelocityProperties(velocityProperties);
202
 
203
		return factory.createVelocityEngine();
204
 
205
	}
206
 
21555 kshitij.so 207
}