Subversion Repositories SmartDukaan

Rev

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