Subversion Repositories SmartDukaan

Rev

Rev 30367 | Rev 30439 | 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);
30417 tejbeer 88
		attributesMap.put("version", "117");
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();
30043 amit.gupta 105
		Properties velocityProperties = new Properties();
106
		velocityProperties.put("directive.set.null.allowed", "true");
107
		velocityConfigurer.setVelocityProperties(velocityProperties);
21625 kshitij.so 108
		velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
109
		return velocityConfigurer;
21555 kshitij.so 110
	}
111
 
24639 tejbeer 112
	@Bean(name = "messageSource")
21555 kshitij.so 113
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
24639 tejbeer 114
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
115
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21555 kshitij.so 116
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
117
		return messageSource;
118
	}
119
 
24639 tejbeer 120
	@Bean(name = "multipartResolver")
21555 kshitij.so 121
	public CommonsMultipartResolver getCommonsMultipartResolver() {
122
		LOGGER.info("creating common multipart resolver bean");
123
		return new CommonsMultipartResolver();
124
	}
125
 
126
	@Bean
127
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
25811 amit.gupta 128
		String activeProfile = getActiveProfile();
21555 kshitij.so 129
 
24639 tejbeer 130
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 131
 
21555 kshitij.so 132
		if ("prod".equals(activeProfile)) {
133
			resource = new ClassPathResource("/META-INF/prod.properties");
134
		} else if ("staging".equals(activeProfile)) {
135
			resource = new ClassPathResource("/META-INF/staging.properties");
136
		} else {
137
			resource = new ClassPathResource("/META-INF/dev.properties");
138
		}
139
 
140
		propertySourcesPlaceholderConfigurer.setLocation(resource);
141
 
142
		return propertySourcesPlaceholderConfigurer;
143
	}
21625 kshitij.so 144
 
25811 amit.gupta 145
	private String getActiveProfile() {
146
		Properties properties = new Properties();
147
		try {
148
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
149
		} catch (IOException e) {
150
			LOGGER.error("Error in reading env property file.Adding default property" + e);
151
			properties.put("profile", "dev");
152
		}
153
		LOGGER.info("Profile is [{}]", properties.get("profile"));
154
		return (String) properties.get("profile");
155
	}
156
 
24639 tejbeer 157
	@Bean(name = "mailSender")
25091 amit.gupta 158
	public JavaMailSender getSendgridMailSender() {
24639 tejbeer 159
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
160
 
161
		// Using gmail
162
		mailSender.setHost("smtp.sendgrid.net");
163
		mailSender.setPort(587);
27629 amit.gupta 164
		mailSender.setUsername("apikey");
165
		mailSender.setPassword("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");
24639 tejbeer 166
 
167
		Properties javaMailProperties = new Properties();
168
		javaMailProperties.put("mail.smtp.starttls.enable", "false");
169
		javaMailProperties.put("mail.smtp.auth", "true");
170
		javaMailProperties.put("mail.transport.protocol", "smtp");
171
		javaMailProperties.put("mail.debug", "true");// Prints out everything on
172
														// screen
173
 
174
		mailSender.setJavaMailProperties(javaMailProperties);
175
		return mailSender;
176
	}
177
 
25300 tejbeer 178
	@Bean(name = "gson")
179
	public Gson gson() {
180
 
26063 amit.gupta 181
		Gson gson = new GsonBuilder().serializeNulls()
25300 tejbeer 182
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
183
 
184
		return gson;
185
 
186
	}
187
 
28339 tejbeer 188
	@Bean(name = "veloctyEngine")
189
	public VelocityEngine velocityEngine() throws VelocityException, IOException {
190
		VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
191
		// Properties props = new Properties();
192
		// props.put("resource.loader", "file");
193
 
194
		// props.put("file.resource.loader.description", "Velocity File Resource
195
		// Loader");
196
		// props.put("file.resource.loader.class",
197
		// "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
198
		// props.put("file.resource.loader.cache", true);
199
		// props.put("file.resource.loader.path", ".");
200
 
201
		Properties velocityProperties = new Properties();
202
		velocityProperties.put("resource.loader", "class");
203
		velocityProperties.put("class.resource.loader.class",
204
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
205
		velocityProperties.put("file.resource.loader.cache", true);
206
		velocityProperties.put("file.resource.loader.unicode", true);
207
		velocityProperties.put("input.encoding", "UTF-8");
208
		velocityProperties.put("output.encoding", "UTF-8");
209
		velocityProperties.put("overrideLogging", true);
210
 
211
		// velocityProperties.put("file.resource.loader.path", ".");
212
 
213
		factory.setVelocityProperties(velocityProperties);
214
 
215
		return factory.createVelocityEngine();
216
 
217
	}
218
 
21555 kshitij.so 219
}