Subversion Repositories SmartDukaan

Rev

Rev 25811 | Rev 26063 | 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;
25300 tejbeer 4
import java.time.LocalDateTime;
23869 amit.gupta 5
import java.time.format.DateTimeFormatter;
6
import java.util.HashMap;
7
import java.util.Map;
21555 kshitij.so 8
import java.util.Properties;
9
 
23869 amit.gupta 10
import org.apache.logging.log4j.LogManager;
23568 govind 11
import org.apache.logging.log4j.Logger;
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;
21555 kshitij.so 21
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
21625 kshitij.so 22
import org.springframework.web.servlet.ViewResolver;
24507 amit.gupta 23
import org.springframework.web.servlet.view.BeanNameViewResolver;
21625 kshitij.so 24
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
25
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
21555 kshitij.so 26
 
25300 tejbeer 27
import com.google.gson.Gson;
28
import com.google.gson.GsonBuilder;
29
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
30
 
21625 kshitij.so 31
@SuppressWarnings("deprecation")
21555 kshitij.so 32
@Configuration
33
@ComponentScan("com.spice.profitmandi.*")
34
public class AppConfig {
35
 
24639 tejbeer 36
	private static final String VELOCITY_PATH_PREFIX = "/WEB-INF/views/ftl/";
37
	private static final String VELOCITY_PATH_SUFFIX = ".vm";
38
	private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:message";
39
	private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
21555 kshitij.so 40
	private static Resource resource;
24639 tejbeer 41
 
21555 kshitij.so 42
	public static Resource getResource() {
43
		return resource;
44
	}
45
 
46
	public void setResource(Resource resource) {
47
		AppConfig.resource = resource;
48
	}
21625 kshitij.so 49
 
50
	@Bean
24639 tejbeer 51
	public ViewResolver viewResolver() {
21625 kshitij.so 52
		VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
53
		bean.setPrefix("");
54
		bean.setSuffix(VELOCITY_PATH_SUFFIX);
22108 amit.gupta 55
		bean.setRequestContextAttribute("rc");
23869 amit.gupta 56
		Map<String, Object> attributesMap = new HashMap<>();
57
		DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
25213 amit.gupta 58
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy H:m");
23869 amit.gupta 59
		attributesMap.put("dateFormatter", dateFormatter);
60
		attributesMap.put("dateTimeFormatter", dateTimeFormatter);
26016 amit.gupta 61
		attributesMap.put("version", "68");
25290 amit.gupta 62
		attributesMap.put("cssVersion", "8");
25811 amit.gupta 63
		attributesMap.put("isDev", getActiveProfile().equals("dev"));
23869 amit.gupta 64
		bean.setAttributesMap(attributesMap);
21625 kshitij.so 65
		return bean;
21555 kshitij.so 66
	}
21625 kshitij.so 67
 
24639 tejbeer 68
	@Bean
69
	public ViewResolver beanNameViewResolver() {
70
		BeanNameViewResolver resolver = new BeanNameViewResolver();
71
		return resolver;
72
	}
24507 amit.gupta 73
 
21555 kshitij.so 74
	@Bean
21625 kshitij.so 75
	public VelocityConfigurer velocityConfig() {
76
		VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
77
		velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
78
		return velocityConfigurer;
21555 kshitij.so 79
	}
80
 
24639 tejbeer 81
	@Bean(name = "messageSource")
21555 kshitij.so 82
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
24639 tejbeer 83
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
84
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21555 kshitij.so 85
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
86
		return messageSource;
87
	}
88
 
24639 tejbeer 89
	@Bean(name = "multipartResolver")
21555 kshitij.so 90
	public CommonsMultipartResolver getCommonsMultipartResolver() {
91
		LOGGER.info("creating common multipart resolver bean");
92
		return new CommonsMultipartResolver();
93
	}
94
 
95
	@Bean
96
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
25811 amit.gupta 97
		String activeProfile = getActiveProfile();
21555 kshitij.so 98
 
24639 tejbeer 99
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 100
 
21555 kshitij.so 101
		if ("prod".equals(activeProfile)) {
102
			resource = new ClassPathResource("/META-INF/prod.properties");
103
		} else if ("staging".equals(activeProfile)) {
104
			resource = new ClassPathResource("/META-INF/staging.properties");
105
		} else {
106
			resource = new ClassPathResource("/META-INF/dev.properties");
107
		}
108
 
109
		propertySourcesPlaceholderConfigurer.setLocation(resource);
110
 
111
		return propertySourcesPlaceholderConfigurer;
112
	}
21625 kshitij.so 113
 
25811 amit.gupta 114
	private String getActiveProfile() {
115
		Properties properties = new Properties();
116
		try {
117
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
118
		} catch (IOException e) {
119
			LOGGER.error("Error in reading env property file.Adding default property" + e);
120
			properties.put("profile", "dev");
121
		}
122
		LOGGER.info("Profile is [{}]", properties.get("profile"));
123
		return (String) properties.get("profile");
124
	}
125
 
24639 tejbeer 126
	@Bean(name = "mailSender")
25091 amit.gupta 127
	public JavaMailSender getSendgridMailSender() {
24639 tejbeer 128
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
129
 
130
		// Using gmail
131
		mailSender.setHost("smtp.sendgrid.net");
132
		mailSender.setPort(587);
133
		mailSender.setUsername("shop2020");
134
		mailSender.setPassword("U2/=fP,t");
135
 
136
		Properties javaMailProperties = new Properties();
137
		javaMailProperties.put("mail.smtp.starttls.enable", "false");
138
		javaMailProperties.put("mail.smtp.auth", "true");
139
		javaMailProperties.put("mail.transport.protocol", "smtp");
140
		javaMailProperties.put("mail.debug", "true");// Prints out everything on
141
														// screen
142
 
143
		mailSender.setJavaMailProperties(javaMailProperties);
144
		return mailSender;
145
	}
146
 
25300 tejbeer 147
	@Bean(name = "gson")
148
	public Gson gson() {
149
 
150
		Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls()
151
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
152
 
153
		return gson;
154
 
155
	}
156
 
21555 kshitij.so 157
}