Subversion Repositories SmartDukaan

Rev

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