Subversion Repositories SmartDukaan

Rev

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