Subversion Repositories SmartDukaan

Rev

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