Subversion Repositories SmartDukaan

Rev

Rev 31414 | Rev 31477 | 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;
30470 amit.gupta 17
import org.apache.velocity.tools.generic.EscapeTool;
21555 kshitij.so 18
import org.springframework.context.annotation.Bean;
19
import org.springframework.context.annotation.ComponentScan;
20
import org.springframework.context.annotation.Configuration;
21
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
22
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
23
import org.springframework.core.io.ClassPathResource;
24
import org.springframework.core.io.Resource;
23985 tejbeer 25
import org.springframework.mail.javamail.JavaMailSender;
26
import org.springframework.mail.javamail.JavaMailSenderImpl;
28339 tejbeer 27
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
21555 kshitij.so 28
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
21625 kshitij.so 29
import org.springframework.web.servlet.ViewResolver;
24507 amit.gupta 30
import org.springframework.web.servlet.view.BeanNameViewResolver;
21625 kshitij.so 31
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
32
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
21555 kshitij.so 33
 
29993 amit.gupta 34
import java.io.IOException;
35
import java.math.RoundingMode;
36
import java.text.DecimalFormat;
37
import java.text.NumberFormat;
38
import java.time.LocalDateTime;
39
import java.time.format.DateTimeFormatter;
31282 amit.gupta 40
import java.time.format.DateTimeFormatterBuilder;
29993 amit.gupta 41
import java.util.HashMap;
42
import java.util.Locale;
43
import java.util.Map;
44
import java.util.Properties;
25300 tejbeer 45
 
21625 kshitij.so 46
@SuppressWarnings("deprecation")
21555 kshitij.so 47
@Configuration
48
@ComponentScan("com.spice.profitmandi.*")
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);
31455 tejbeer 95
		attributesMap.put("version", "156");
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());
30470 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() {
106
		VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
107
		bean.setPrefix("");
108
		bean.setSuffix(VELOCITY_PATH_SUFFIX);
109
		bean.setRequestContextAttribute("rc");
110
		bean.setAttributesMap(this.velocityAttributesMap());
21625 kshitij.so 111
		return bean;
21555 kshitij.so 112
	}
21625 kshitij.so 113
 
24639 tejbeer 114
	@Bean
115
	public ViewResolver beanNameViewResolver() {
116
		BeanNameViewResolver resolver = new BeanNameViewResolver();
117
		return resolver;
118
	}
24507 amit.gupta 119
 
21555 kshitij.so 120
	@Bean
21625 kshitij.so 121
	public VelocityConfigurer velocityConfig() {
122
		VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
30043 amit.gupta 123
		Properties velocityProperties = new Properties();
124
		velocityProperties.put("directive.set.null.allowed", "true");
125
		velocityConfigurer.setVelocityProperties(velocityProperties);
21625 kshitij.so 126
		velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
127
		return velocityConfigurer;
21555 kshitij.so 128
	}
129
 
24639 tejbeer 130
	@Bean(name = "messageSource")
21555 kshitij.so 131
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
24639 tejbeer 132
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
133
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21555 kshitij.so 134
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
135
		return messageSource;
136
	}
137
 
24639 tejbeer 138
	@Bean(name = "multipartResolver")
21555 kshitij.so 139
	public CommonsMultipartResolver getCommonsMultipartResolver() {
140
		LOGGER.info("creating common multipart resolver bean");
141
		return new CommonsMultipartResolver();
142
	}
143
 
144
	@Bean
145
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
25811 amit.gupta 146
		String activeProfile = getActiveProfile();
21555 kshitij.so 147
 
24639 tejbeer 148
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 149
 
21555 kshitij.so 150
		if ("prod".equals(activeProfile)) {
151
			resource = new ClassPathResource("/META-INF/prod.properties");
152
		} else if ("staging".equals(activeProfile)) {
153
			resource = new ClassPathResource("/META-INF/staging.properties");
154
		} else {
155
			resource = new ClassPathResource("/META-INF/dev.properties");
156
		}
157
 
158
		propertySourcesPlaceholderConfigurer.setLocation(resource);
159
 
160
		return propertySourcesPlaceholderConfigurer;
161
	}
21625 kshitij.so 162
 
25811 amit.gupta 163
	private String getActiveProfile() {
164
		Properties properties = new Properties();
165
		try {
166
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
167
		} catch (IOException e) {
168
			LOGGER.error("Error in reading env property file.Adding default property" + e);
169
			properties.put("profile", "dev");
170
		}
171
		LOGGER.info("Profile is [{}]", properties.get("profile"));
172
		return (String) properties.get("profile");
173
	}
174
 
24639 tejbeer 175
	@Bean(name = "mailSender")
25091 amit.gupta 176
	public JavaMailSender getSendgridMailSender() {
24639 tejbeer 177
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
178
 
179
		// Using gmail
180
		mailSender.setHost("smtp.sendgrid.net");
181
		mailSender.setPort(587);
27629 amit.gupta 182
		mailSender.setUsername("apikey");
183
		mailSender.setPassword("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");
24639 tejbeer 184
 
185
		Properties javaMailProperties = new Properties();
186
		javaMailProperties.put("mail.smtp.starttls.enable", "false");
187
		javaMailProperties.put("mail.smtp.auth", "true");
188
		javaMailProperties.put("mail.transport.protocol", "smtp");
189
		javaMailProperties.put("mail.debug", "true");// Prints out everything on
30470 amit.gupta 190
		// screen
24639 tejbeer 191
 
192
		mailSender.setJavaMailProperties(javaMailProperties);
193
		return mailSender;
194
	}
195
 
25300 tejbeer 196
	@Bean(name = "gson")
197
	public Gson gson() {
198
 
26063 amit.gupta 199
		Gson gson = new GsonBuilder().serializeNulls()
25300 tejbeer 200
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
201
 
202
		return gson;
203
 
204
	}
205
 
28339 tejbeer 206
	@Bean(name = "veloctyEngine")
207
	public VelocityEngine velocityEngine() throws VelocityException, IOException {
208
		VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
209
		// Properties props = new Properties();
210
		// props.put("resource.loader", "file");
211
 
212
		// props.put("file.resource.loader.description", "Velocity File Resource
213
		// Loader");
214
		// props.put("file.resource.loader.class",
215
		// "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
216
		// props.put("file.resource.loader.cache", true);
217
		// props.put("file.resource.loader.path", ".");
218
 
219
		Properties velocityProperties = new Properties();
220
		velocityProperties.put("resource.loader", "class");
221
		velocityProperties.put("class.resource.loader.class",
222
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
223
		velocityProperties.put("file.resource.loader.cache", true);
224
		velocityProperties.put("file.resource.loader.unicode", true);
225
		velocityProperties.put("input.encoding", "UTF-8");
226
		velocityProperties.put("output.encoding", "UTF-8");
227
		velocityProperties.put("overrideLogging", true);
228
 
229
		// velocityProperties.put("file.resource.loader.path", ".");
230
 
231
		factory.setVelocityProperties(velocityProperties);
232
		return factory.createVelocityEngine();
233
 
234
	}
235
 
31282 amit.gupta 236
	@Bean
237
	public ObjectMapper objectMapper() {
238
		DateTimeFormatter df = new DateTimeFormatterBuilder().parseCaseInsensitive()
239
				.append(DateTimeFormatter.ISO_LOCAL_DATE).optionalStart().appendLiteral('T').optionalEnd()
240
				.appendLiteral(' ').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
241
		DateTimeFormatter sf = new DateTimeFormatterBuilder().parseCaseInsensitive()
242
				.append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral('T').append(DateTimeFormatter.ISO_LOCAL_TIME)
243
				.toFormatter();
244
		LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(sf);
245
		LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(df);
246
		JavaTimeModule jtm = new JavaTimeModule();
247
		jtm.addSerializer(LocalDateTime.class, serializer);
248
		jtm.addDeserializer(LocalDateTime.class, deserializer);
249
		ObjectMapper mapper = new ObjectMapper().registerModule(new ParameterNamesModule())
250
				.registerModule(new Jdk8Module()).registerModule(jtm); // new module, NOT JSR310Module
251
		return mapper;
252
	}
253
 
21555 kshitij.so 254
}