Subversion Repositories SmartDukaan

Rev

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