Subversion Repositories SmartDukaan

Rev

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