Subversion Repositories SmartDukaan

Rev

Rev 28102 | Rev 28143 | 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");
27867 tejbeer 59
		DateTimeFormatter dateMonthFormatter = DateTimeFormatter.ofPattern("dd''MMM");
27763 tejbeer 60
		DateTimeFormatter datehiphenFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
61
 
27607 tejbeer 62
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
23869 amit.gupta 63
		attributesMap.put("dateFormatter", dateFormatter);
27867 tejbeer 64
		attributesMap.put("dateMonthFormatter", dateMonthFormatter);
27763 tejbeer 65
		attributesMap.put("datehiphenFormatter", datehiphenFormatter);
27876 amit.gupta 66
		attributesMap.put("noData",
67
				"<tr><td colspan=\"20\" style=\"text-align:center;\">No results found for the given criteria</td></tr>");
23869 amit.gupta 68
		attributesMap.put("dateTimeFormatter", dateTimeFormatter);
28136 tejbeer 69
		attributesMap.put("version", "43");
27712 amit.gupta 70
		attributesMap.put("cssVersion", "9");
25811 amit.gupta 71
		attributesMap.put("isDev", getActiveProfile().equals("dev"));
26063 amit.gupta 72
		attributesMap.put("vmUtils", new Utils());
23869 amit.gupta 73
		bean.setAttributesMap(attributesMap);
21625 kshitij.so 74
		return bean;
21555 kshitij.so 75
	}
21625 kshitij.so 76
 
24639 tejbeer 77
	@Bean
78
	public ViewResolver beanNameViewResolver() {
79
		BeanNameViewResolver resolver = new BeanNameViewResolver();
80
		return resolver;
81
	}
24507 amit.gupta 82
 
21555 kshitij.so 83
	@Bean
21625 kshitij.so 84
	public VelocityConfigurer velocityConfig() {
85
		VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
86
		velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
87
		return velocityConfigurer;
21555 kshitij.so 88
	}
89
 
24639 tejbeer 90
	@Bean(name = "messageSource")
21555 kshitij.so 91
	public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
24639 tejbeer 92
		LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
93
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
21555 kshitij.so 94
		messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
95
		return messageSource;
96
	}
97
 
24639 tejbeer 98
	@Bean(name = "multipartResolver")
21555 kshitij.so 99
	public CommonsMultipartResolver getCommonsMultipartResolver() {
100
		LOGGER.info("creating common multipart resolver bean");
101
		return new CommonsMultipartResolver();
102
	}
103
 
104
	@Bean
105
	public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
25811 amit.gupta 106
		String activeProfile = getActiveProfile();
21555 kshitij.so 107
 
24639 tejbeer 108
		PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 109
 
21555 kshitij.so 110
		if ("prod".equals(activeProfile)) {
111
			resource = new ClassPathResource("/META-INF/prod.properties");
112
		} else if ("staging".equals(activeProfile)) {
113
			resource = new ClassPathResource("/META-INF/staging.properties");
114
		} else {
115
			resource = new ClassPathResource("/META-INF/dev.properties");
116
		}
117
 
118
		propertySourcesPlaceholderConfigurer.setLocation(resource);
119
 
120
		return propertySourcesPlaceholderConfigurer;
121
	}
21625 kshitij.so 122
 
25811 amit.gupta 123
	private String getActiveProfile() {
124
		Properties properties = new Properties();
125
		try {
126
			properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
127
		} catch (IOException e) {
128
			LOGGER.error("Error in reading env property file.Adding default property" + e);
129
			properties.put("profile", "dev");
130
		}
131
		LOGGER.info("Profile is [{}]", properties.get("profile"));
132
		return (String) properties.get("profile");
133
	}
134
 
24639 tejbeer 135
	@Bean(name = "mailSender")
25091 amit.gupta 136
	public JavaMailSender getSendgridMailSender() {
24639 tejbeer 137
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
138
 
139
		// Using gmail
140
		mailSender.setHost("smtp.sendgrid.net");
141
		mailSender.setPort(587);
27629 amit.gupta 142
		mailSender.setUsername("apikey");
143
		mailSender.setPassword("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");
24639 tejbeer 144
 
145
		Properties javaMailProperties = new Properties();
146
		javaMailProperties.put("mail.smtp.starttls.enable", "false");
147
		javaMailProperties.put("mail.smtp.auth", "true");
148
		javaMailProperties.put("mail.transport.protocol", "smtp");
149
		javaMailProperties.put("mail.debug", "true");// Prints out everything on
150
														// screen
151
 
152
		mailSender.setJavaMailProperties(javaMailProperties);
153
		return mailSender;
154
	}
155
 
25300 tejbeer 156
	@Bean(name = "gson")
157
	public Gson gson() {
158
 
26063 amit.gupta 159
		Gson gson = new GsonBuilder().serializeNulls()
25300 tejbeer 160
				.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
161
 
162
		return gson;
163
 
164
	}
165
 
21555 kshitij.so 166
}