Subversion Repositories SmartDukaan

Rev

Rev 30683 | Rev 30703 | 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 dateYearMonthFormatter = DateTimeFormatter.ofPattern("MMM''uu");
65
		DateTimeFormatter dateMonthYearFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
29266 manish 66
		DateTimeFormatter dateMonthChYear = DateTimeFormatter.ofPattern("d''MMM''uu");
29923 amit.gupta 67
		DecimalFormat decimalFormatter = new DecimalFormat("0.#");
29977 manish 68
		NumberFormat numberformat = NumberFormat.getNumberInstance(new Locale("en", "IN"));
69
		numberformat.setRoundingMode(RoundingMode.HALF_DOWN);
70
		numberformat.setMaximumFractionDigits(2);
71
		numberformat.setMinimumFractionDigits(0);
27607 tejbeer 72
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
23869 amit.gupta 73
		attributesMap.put("dateFormatter", dateFormatter);
29977 manish 74
		attributesMap.put("nf", numberformat);
27867 tejbeer 75
		attributesMap.put("dateMonthFormatter", dateMonthFormatter);
27763 tejbeer 76
		attributesMap.put("datehiphenFormatter", datehiphenFormatter);
28455 tejbeer 77
		attributesMap.put("dateYearMonthFormatter", dateYearMonthFormatter);
78
		attributesMap.put("dateMonthYearFormatter", dateMonthYearFormatter);
29266 manish 79
		attributesMap.put("dateMonthChYear", dateMonthChYear);
29923 amit.gupta 80
		attributesMap.put("decimalFormatter", decimalFormatter);
27876 amit.gupta 81
		attributesMap.put("noData",
82
				"<tr><td colspan=\"20\" style=\"text-align:center;\">No results found for the given criteria</td></tr>");
23869 amit.gupta 83
		attributesMap.put("dateTimeFormatter", dateTimeFormatter);
30697 amit.gupta 84
		attributesMap.put("version", "132");
29010 amit.gupta 85
		attributesMap.put("cssVersion", "10");
25811 amit.gupta 86
		attributesMap.put("isDev", getActiveProfile().equals("dev"));
26063 amit.gupta 87
		attributesMap.put("vmUtils", new Utils());
30470 amit.gupta 88
		attributesMap.put("esc", new EscapeTool());
89
		return attributesMap;
90
	}
91
 
92
	@Bean
93
	public ViewResolver viewResolver() {
94
		VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
95
		bean.setPrefix("");
96
		bean.setSuffix(VELOCITY_PATH_SUFFIX);
97
		bean.setRequestContextAttribute("rc");
98
		bean.setAttributesMap(this.velocityAttributesMap());
21625 kshitij.so 99
		return bean;
21555 kshitij.so 100
	}
21625 kshitij.so 101
 
24639 tejbeer 102
	@Bean
103
	public ViewResolver beanNameViewResolver() {
104
		BeanNameViewResolver resolver = new BeanNameViewResolver();
105
		return resolver;
106
	}
24507 amit.gupta 107
 
21555 kshitij.so 108
	@Bean
21625 kshitij.so 109
	public VelocityConfigurer velocityConfig() {
110
		VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
30043 amit.gupta 111
		Properties velocityProperties = new Properties();
112
		velocityProperties.put("directive.set.null.allowed", "true");
113
		velocityConfigurer.setVelocityProperties(velocityProperties);
21625 kshitij.so 114
		velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
115
		return velocityConfigurer;
21555 kshitij.so 116
	}
117
 
24639 tejbeer 118
	@Bean(name = "messageSource")
21555 kshitij.so 119
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
24639 tejbeer 120
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
121
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21555 kshitij.so 122
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
123
		return messageSource;
124
	}
125
 
24639 tejbeer 126
	@Bean(name = "multipartResolver")
21555 kshitij.so 127
	public CommonsMultipartResolver getCommonsMultipartResolver() {
128
		LOGGER.info("creating common multipart resolver bean");
129
		return new CommonsMultipartResolver();
130
	}
131
 
132
	@Bean
133
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
25811 amit.gupta 134
		String activeProfile = getActiveProfile();
21555 kshitij.so 135
 
24639 tejbeer 136
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 137
 
21555 kshitij.so 138
		if ("prod".equals(activeProfile)) {
139
			resource = new ClassPathResource("/META-INF/prod.properties");
140
		} else if ("staging".equals(activeProfile)) {
141
			resource = new ClassPathResource("/META-INF/staging.properties");
142
		} else {
143
			resource = new ClassPathResource("/META-INF/dev.properties");
144
		}
145
 
146
		propertySourcesPlaceholderConfigurer.setLocation(resource);
147
 
148
		return propertySourcesPlaceholderConfigurer;
149
	}
21625 kshitij.so 150
 
25811 amit.gupta 151
	private String getActiveProfile() {
152
		Properties properties = new Properties();
153
		try {
154
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
155
		} catch (IOException e) {
156
			LOGGER.error("Error in reading env property file.Adding default property" + e);
157
			properties.put("profile", "dev");
158
		}
159
		LOGGER.info("Profile is [{}]", properties.get("profile"));
160
		return (String) properties.get("profile");
161
	}
162
 
24639 tejbeer 163
	@Bean(name = "mailSender")
25091 amit.gupta 164
	public JavaMailSender getSendgridMailSender() {
24639 tejbeer 165
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
166
 
167
		// Using gmail
168
		mailSender.setHost("smtp.sendgrid.net");
169
		mailSender.setPort(587);
27629 amit.gupta 170
		mailSender.setUsername("apikey");
171
		mailSender.setPassword("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");
24639 tejbeer 172
 
173
		Properties javaMailProperties = new Properties();
174
		javaMailProperties.put("mail.smtp.starttls.enable", "false");
175
		javaMailProperties.put("mail.smtp.auth", "true");
176
		javaMailProperties.put("mail.transport.protocol", "smtp");
177
		javaMailProperties.put("mail.debug", "true");// Prints out everything on
30470 amit.gupta 178
		// screen
24639 tejbeer 179
 
180
		mailSender.setJavaMailProperties(javaMailProperties);
181
		return mailSender;
182
	}
183
 
25300 tejbeer 184
	@Bean(name = "gson")
185
	public Gson gson() {
186
 
26063 amit.gupta 187
		Gson gson = new GsonBuilder().serializeNulls()
25300 tejbeer 188
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
189
 
190
		return gson;
191
 
192
	}
193
 
28339 tejbeer 194
	@Bean(name = "veloctyEngine")
195
	public VelocityEngine velocityEngine() throws VelocityException, IOException {
196
		VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
197
		// Properties props = new Properties();
198
		// props.put("resource.loader", "file");
199
 
200
		// props.put("file.resource.loader.description", "Velocity File Resource
201
		// Loader");
202
		// props.put("file.resource.loader.class",
203
		// "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
204
		// props.put("file.resource.loader.cache", true);
205
		// props.put("file.resource.loader.path", ".");
206
 
207
		Properties velocityProperties = new Properties();
208
		velocityProperties.put("resource.loader", "class");
209
		velocityProperties.put("class.resource.loader.class",
210
				"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
211
		velocityProperties.put("file.resource.loader.cache", true);
212
		velocityProperties.put("file.resource.loader.unicode", true);
213
		velocityProperties.put("input.encoding", "UTF-8");
214
		velocityProperties.put("output.encoding", "UTF-8");
215
		velocityProperties.put("overrideLogging", true);
216
 
217
		// velocityProperties.put("file.resource.loader.path", ".");
218
 
219
		factory.setVelocityProperties(velocityProperties);
220
		return factory.createVelocityEngine();
221
 
222
	}
223
 
21555 kshitij.so 224
}