Subversion Repositories SmartDukaan

Rev

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