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