Subversion Repositories SmartDukaan

Rev

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