Subversion Repositories SmartDukaan

Rev

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