Subversion Repositories SmartDukaan

Rev

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