Subversion Repositories SmartDukaan

Rev

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