| 21561 |
ashik.ali |
1 |
package com.spice.profitmandi.web.config;
|
| 21555 |
kshitij.so |
2 |
|
| 32569 |
amit.gupta |
3 |
import com.fasterxml.jackson.core.JsonParser;
|
|
|
4 |
import com.fasterxml.jackson.databind.DeserializationContext;
|
| 32186 |
amit.gupta |
5 |
import com.fasterxml.jackson.databind.DeserializationFeature;
|
| 31282 |
amit.gupta |
6 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
| 32569 |
amit.gupta |
7 |
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;
|
|
|
8 |
import com.fasterxml.jackson.databind.module.SimpleModule;
|
| 31282 |
amit.gupta |
9 |
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
|
|
10 |
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
|
11 |
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
|
|
12 |
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
|
|
13 |
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
|
| 29993 |
amit.gupta |
14 |
import com.google.gson.Gson;
|
|
|
15 |
import com.google.gson.GsonBuilder;
|
|
|
16 |
import com.spice.profitmandi.common.util.Utils;
|
|
|
17 |
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
|
| 23869 |
amit.gupta |
18 |
import org.apache.logging.log4j.LogManager;
|
| 23568 |
govind |
19 |
import org.apache.logging.log4j.Logger;
|
| 28339 |
tejbeer |
20 |
import org.apache.velocity.app.VelocityEngine;
|
|
|
21 |
import org.apache.velocity.exception.VelocityException;
|
| 21555 |
kshitij.so |
22 |
import org.springframework.context.annotation.Bean;
|
|
|
23 |
import org.springframework.context.annotation.ComponentScan;
|
|
|
24 |
import org.springframework.context.annotation.Configuration;
|
|
|
25 |
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
|
|
26 |
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
|
|
|
27 |
import org.springframework.core.io.ClassPathResource;
|
|
|
28 |
import org.springframework.core.io.Resource;
|
| 23985 |
tejbeer |
29 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
30 |
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
| 28339 |
tejbeer |
31 |
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
|
| 21555 |
kshitij.so |
32 |
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
| 21625 |
kshitij.so |
33 |
import org.springframework.web.servlet.ViewResolver;
|
| 24507 |
amit.gupta |
34 |
import org.springframework.web.servlet.view.BeanNameViewResolver;
|
| 21625 |
kshitij.so |
35 |
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
| 31513 |
amit.gupta |
36 |
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
|
| 21555 |
kshitij.so |
37 |
|
| 29993 |
amit.gupta |
38 |
import java.io.IOException;
|
|
|
39 |
import java.math.RoundingMode;
|
|
|
40 |
import java.text.DecimalFormat;
|
|
|
41 |
import java.text.NumberFormat;
|
|
|
42 |
import java.time.LocalDateTime;
|
|
|
43 |
import java.time.format.DateTimeFormatter;
|
| 31282 |
amit.gupta |
44 |
import java.time.format.DateTimeFormatterBuilder;
|
| 29993 |
amit.gupta |
45 |
import java.util.HashMap;
|
|
|
46 |
import java.util.Locale;
|
|
|
47 |
import java.util.Map;
|
|
|
48 |
import java.util.Properties;
|
| 25300 |
tejbeer |
49 |
|
| 21625 |
kshitij.so |
50 |
@SuppressWarnings("deprecation")
|
| 21555 |
kshitij.so |
51 |
@Configuration
|
|
|
52 |
@ComponentScan("com.spice.profitmandi.*")
|
| 31511 |
amit.gupta |
53 |
//@Import(RepositoryRestMvcConfiguration.class)
|
| 21555 |
kshitij.so |
54 |
public class AppConfig {
|
|
|
55 |
|
| 32054 |
tejbeer |
56 |
private static final String VELOCITY_PATH_PREFIX = "/WEB-INF/views/ftl/";
|
|
|
57 |
private static final String VELOCITY_PATH_SUFFIX = ".vm";
|
| 32773 |
raveendra. |
58 |
private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:messages";
|
| 32054 |
tejbeer |
59 |
private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
|
|
|
60 |
private static Resource resource;
|
| 24639 |
tejbeer |
61 |
|
| 32054 |
tejbeer |
62 |
public static Resource getResource() {
|
|
|
63 |
return resource;
|
|
|
64 |
}
|
| 21555 |
kshitij.so |
65 |
|
| 32054 |
tejbeer |
66 |
public void setResource(Resource resource) {
|
|
|
67 |
AppConfig.resource = resource;
|
|
|
68 |
}
|
| 21625 |
kshitij.so |
69 |
|
| 32054 |
tejbeer |
70 |
@Bean(value = "velocityAttributesMap")
|
|
|
71 |
public Map<String, Object> velocityAttributesMap() {
|
|
|
72 |
Map<String, Object> attributesMap = new HashMap<>();
|
|
|
73 |
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
|
|
|
74 |
DateTimeFormatter dateMonthFormatter = DateTimeFormatter.ofPattern("dd''MMM");
|
|
|
75 |
DateTimeFormatter datehiphenFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
76 |
DateTimeFormatter dateMonthYearFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
|
|
|
77 |
DateTimeFormatter dateYearMonthFormatter = DateTimeFormatter.ofPattern("MMM''uu");
|
| 31218 |
tejbeer |
78 |
|
| 32054 |
tejbeer |
79 |
DateTimeFormatter dateMonthChYear = DateTimeFormatter.ofPattern("d''MMM''uu");
|
|
|
80 |
DecimalFormat decimalFormatter = new DecimalFormat("0.#");
|
| 30859 |
tejbeer |
81 |
|
| 32054 |
tejbeer |
82 |
NumberFormat numberformat = NumberFormat.getNumberInstance(new Locale("en", "IN"));
|
| 30859 |
tejbeer |
83 |
|
| 32054 |
tejbeer |
84 |
numberformat.setRoundingMode(RoundingMode.HALF_DOWN);
|
|
|
85 |
numberformat.setMaximumFractionDigits(2);
|
|
|
86 |
numberformat.setMinimumFractionDigits(0);
|
|
|
87 |
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
|
|
|
88 |
attributesMap.put("dateFormatter", dateFormatter);
|
|
|
89 |
attributesMap.put("nf", numberformat);
|
|
|
90 |
attributesMap.put("dateMonthFormatter", dateMonthFormatter);
|
|
|
91 |
attributesMap.put("datehiphenFormatter", datehiphenFormatter);
|
|
|
92 |
attributesMap.put("dateYearMonthFormatter", dateYearMonthFormatter);
|
| 31218 |
tejbeer |
93 |
|
| 32054 |
tejbeer |
94 |
attributesMap.put("dateMonthYearFormatter", dateMonthYearFormatter);
|
|
|
95 |
attributesMap.put("dateMonthChYear", dateMonthChYear);
|
|
|
96 |
attributesMap.put("decimalFormatter", decimalFormatter);
|
|
|
97 |
attributesMap.put("noData", "<tr><td colspan=\"20\" style=\"text-align:center;\">No results found for the given criteria</td></tr>");
|
|
|
98 |
attributesMap.put("dateTimeFormatter", dateTimeFormatter);
|
| 33013 |
shampa |
99 |
attributesMap.put("version", "201");
|
| 32359 |
amit.gupta |
100 |
attributesMap.put("cssVersion", "12");
|
| 32054 |
tejbeer |
101 |
attributesMap.put("isDev", getActiveProfile().equals("dev"));
|
|
|
102 |
attributesMap.put("vmUtils", new Utils());
|
| 32200 |
amit.gupta |
103 |
//attributesMap.put("esc", new EscapeTool());
|
| 32054 |
tejbeer |
104 |
attributesMap.put("ru", RoundingMode.HALF_UP);
|
|
|
105 |
return attributesMap;
|
|
|
106 |
}
|
| 30470 |
amit.gupta |
107 |
|
| 32054 |
tejbeer |
108 |
@Bean
|
|
|
109 |
public ViewResolver viewResolver() {
|
|
|
110 |
VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
|
|
|
111 |
bean.setPrefix("");
|
|
|
112 |
bean.setSuffix(VELOCITY_PATH_SUFFIX);
|
|
|
113 |
bean.setRequestContextAttribute("rc");
|
| 32773 |
raveendra. |
114 |
bean.setExposeSpringMacroHelpers(true);
|
| 32054 |
tejbeer |
115 |
bean.setAttributesMap(this.velocityAttributesMap());
|
|
|
116 |
return bean;
|
|
|
117 |
}
|
| 21625 |
kshitij.so |
118 |
|
| 31512 |
amit.gupta |
119 |
// @Bean
|
|
|
120 |
// public ViewResolver freemarkerViewResolver() {
|
|
|
121 |
// FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
|
|
|
122 |
// resolver.setCache(true);
|
|
|
123 |
// resolver.setPrefix("");
|
|
|
124 |
// resolver.setSuffix(".ftl");
|
|
|
125 |
// return resolver;
|
|
|
126 |
// }
|
|
|
127 |
|
| 32054 |
tejbeer |
128 |
@Bean
|
|
|
129 |
public ViewResolver beanNameViewResolver() {
|
|
|
130 |
BeanNameViewResolver resolver = new BeanNameViewResolver();
|
|
|
131 |
return resolver;
|
|
|
132 |
}
|
| 24507 |
amit.gupta |
133 |
|
| 32054 |
tejbeer |
134 |
@Bean
|
|
|
135 |
public VelocityConfigurer velocityConfig() {
|
|
|
136 |
VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
|
|
|
137 |
Properties velocityProperties = new Properties();
|
|
|
138 |
velocityProperties.put("directive.set.null.allowed", "true");
|
|
|
139 |
velocityConfigurer.setVelocityProperties(velocityProperties);
|
|
|
140 |
velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
|
|
|
141 |
return velocityConfigurer;
|
|
|
142 |
}
|
| 21555 |
kshitij.so |
143 |
|
| 32788 |
amit.gupta |
144 |
@Bean(name = "messageSource")
|
|
|
145 |
public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
|
| 32054 |
tejbeer |
146 |
LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
|
|
|
147 |
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
|
|
|
148 |
messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
|
| 32773 |
raveendra. |
149 |
messageSource.setDefaultEncoding("UTF-8");
|
|
|
150 |
//messageSource.setDefaultEncoding("ISO 8859-1");
|
|
|
151 |
// messageSource.setDefaultEncoding("U+0900 ");
|
| 33013 |
shampa |
152 |
//messageSource.setDefaultEncoding("UTF-32");
|
| 32773 |
raveendra. |
153 |
|
| 32054 |
tejbeer |
154 |
return messageSource;
|
|
|
155 |
}
|
| 21555 |
kshitij.so |
156 |
|
| 32054 |
tejbeer |
157 |
@Bean(name = "multipartResolver")
|
|
|
158 |
public CommonsMultipartResolver getCommonsMultipartResolver() {
|
|
|
159 |
LOGGER.info("creating common multipart resolver bean");
|
|
|
160 |
return new CommonsMultipartResolver();
|
|
|
161 |
}
|
| 21555 |
kshitij.so |
162 |
|
| 32054 |
tejbeer |
163 |
@Bean
|
|
|
164 |
public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
|
|
|
165 |
String activeProfile = getActiveProfile();
|
| 21555 |
kshitij.so |
166 |
|
| 32054 |
tejbeer |
167 |
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
|
| 21625 |
kshitij.so |
168 |
|
| 32054 |
tejbeer |
169 |
if ("prod".equals(activeProfile)) {
|
|
|
170 |
resource = new ClassPathResource("/META-INF/prod.properties");
|
|
|
171 |
} else if ("staging".equals(activeProfile)) {
|
|
|
172 |
resource = new ClassPathResource("/META-INF/staging.properties");
|
|
|
173 |
} else {
|
|
|
174 |
resource = new ClassPathResource("/META-INF/dev.properties");
|
|
|
175 |
}
|
| 21555 |
kshitij.so |
176 |
|
| 32054 |
tejbeer |
177 |
propertySourcesPlaceholderConfigurer.setLocation(resource);
|
| 21555 |
kshitij.so |
178 |
|
| 32054 |
tejbeer |
179 |
return propertySourcesPlaceholderConfigurer;
|
|
|
180 |
}
|
| 21625 |
kshitij.so |
181 |
|
| 32054 |
tejbeer |
182 |
private String getActiveProfile() {
|
|
|
183 |
Properties properties = new Properties();
|
|
|
184 |
try {
|
|
|
185 |
properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
|
|
|
186 |
} catch (IOException e) {
|
|
|
187 |
LOGGER.error("Error in reading env property file.Adding default property" + e);
|
|
|
188 |
properties.put("profile", "dev");
|
|
|
189 |
}
|
|
|
190 |
LOGGER.info("Profile is [{}]", properties.get("profile"));
|
|
|
191 |
return (String) properties.get("profile");
|
|
|
192 |
}
|
| 25811 |
amit.gupta |
193 |
|
| 32054 |
tejbeer |
194 |
@Bean(name = "mailSender")
|
|
|
195 |
public JavaMailSender getSendgridMailSender() {
|
|
|
196 |
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
| 24639 |
tejbeer |
197 |
|
| 32054 |
tejbeer |
198 |
// Using gmail
|
|
|
199 |
mailSender.setHost("smtp.sendgrid.net");
|
|
|
200 |
mailSender.setPort(587);
|
|
|
201 |
mailSender.setUsername("apikey");
|
|
|
202 |
mailSender.setPassword("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");
|
| 24639 |
tejbeer |
203 |
|
| 32054 |
tejbeer |
204 |
Properties javaMailProperties = new Properties();
|
|
|
205 |
javaMailProperties.put("mail.smtp.starttls.enable", "false");
|
|
|
206 |
javaMailProperties.put("mail.smtp.auth", "true");
|
|
|
207 |
javaMailProperties.put("mail.transport.protocol", "smtp");
|
|
|
208 |
javaMailProperties.put("mail.debug", "true");// Prints out everything on
|
|
|
209 |
// screen
|
| 24639 |
tejbeer |
210 |
|
| 32054 |
tejbeer |
211 |
mailSender.setJavaMailProperties(javaMailProperties);
|
|
|
212 |
return mailSender;
|
|
|
213 |
}
|
| 24639 |
tejbeer |
214 |
|
| 32399 |
amit.gupta |
215 |
@Bean
|
| 32054 |
tejbeer |
216 |
public JavaMailSender googleMailSender() {
|
|
|
217 |
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
|
|
218 |
// Using gmail
|
|
|
219 |
mailSender.setHost("smtp.gmail.com");
|
|
|
220 |
mailSender.setPort(587);
|
|
|
221 |
mailSender.setUsername("build@shop2020.in");
|
|
|
222 |
mailSender.setPassword("cafe@nes");
|
| 31511 |
amit.gupta |
223 |
|
| 32054 |
tejbeer |
224 |
Properties javaMailProperties = new Properties();
|
|
|
225 |
javaMailProperties.put("mail.smtp.starttls.enable", "true");
|
|
|
226 |
javaMailProperties.put("mail.smtp.auth", "true");
|
|
|
227 |
javaMailProperties.put("mail.transport.protocol", "smtp");
|
|
|
228 |
javaMailProperties.put("mail.debug", "true");// Prints out everything on screen
|
|
|
229 |
mailSender.setJavaMailProperties(javaMailProperties);
|
|
|
230 |
return mailSender;
|
|
|
231 |
}
|
| 31511 |
amit.gupta |
232 |
|
| 32054 |
tejbeer |
233 |
@Bean(name = "gson")
|
|
|
234 |
public Gson gson() {
|
| 25300 |
tejbeer |
235 |
|
| 32054 |
tejbeer |
236 |
Gson gson = new GsonBuilder().serializeNulls().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
|
| 25300 |
tejbeer |
237 |
|
| 32054 |
tejbeer |
238 |
return gson;
|
| 25300 |
tejbeer |
239 |
|
| 32054 |
tejbeer |
240 |
}
|
| 25300 |
tejbeer |
241 |
|
| 32054 |
tejbeer |
242 |
@Bean(name = "veloctyEngine")
|
|
|
243 |
public VelocityEngine velocityEngine() throws VelocityException, IOException {
|
|
|
244 |
VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
|
|
|
245 |
// Properties props = new Properties();
|
|
|
246 |
// props.put("resource.loader", "file");
|
| 28339 |
tejbeer |
247 |
|
| 32054 |
tejbeer |
248 |
// props.put("file.resource.loader.description", "Velocity File Resource
|
|
|
249 |
// Loader");
|
|
|
250 |
// props.put("file.resource.loader.class",
|
|
|
251 |
// "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
|
|
|
252 |
// props.put("file.resource.loader.cache", true);
|
|
|
253 |
// props.put("file.resource.loader.path", ".");
|
| 32773 |
raveendra. |
254 |
factory.setPreferFileSystemAccess(false);
|
|
|
255 |
// factory.setResourceLoaderPath("classpath:/templates/");
|
| 28339 |
tejbeer |
256 |
|
| 32773 |
raveendra. |
257 |
|
| 32054 |
tejbeer |
258 |
Properties velocityProperties = new Properties();
|
|
|
259 |
velocityProperties.put("resource.loader", "class");
|
|
|
260 |
velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
|
|
|
261 |
velocityProperties.put("file.resource.loader.cache", true);
|
|
|
262 |
velocityProperties.put("file.resource.loader.unicode", true);
|
|
|
263 |
velocityProperties.put("input.encoding", "UTF-8");
|
|
|
264 |
velocityProperties.put("output.encoding", "UTF-8");
|
|
|
265 |
velocityProperties.put("overrideLogging", true);
|
| 32773 |
raveendra. |
266 |
velocityProperties.setProperty("input.encoding", "UTF-8");
|
|
|
267 |
velocityProperties.setProperty("output.encoding", "UTF-8");
|
|
|
268 |
factory.setVelocityProperties(velocityProperties);
|
| 28339 |
tejbeer |
269 |
|
| 32054 |
tejbeer |
270 |
// velocityProperties.put("file.resource.loader.path", ".");
|
| 28339 |
tejbeer |
271 |
|
| 32054 |
tejbeer |
272 |
factory.setVelocityProperties(velocityProperties);
|
|
|
273 |
return factory.createVelocityEngine();
|
| 28339 |
tejbeer |
274 |
|
| 32054 |
tejbeer |
275 |
}
|
| 28339 |
tejbeer |
276 |
|
| 32054 |
tejbeer |
277 |
@Bean
|
|
|
278 |
public ObjectMapper objectMapper() {
|
|
|
279 |
DateTimeFormatter df = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).optionalStart().appendLiteral('T').optionalEnd().appendLiteral(' ').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
|
|
|
280 |
DateTimeFormatter sf = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral('T').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
|
|
|
281 |
LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(sf);
|
|
|
282 |
LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(df);
|
|
|
283 |
JavaTimeModule jtm = new JavaTimeModule();
|
|
|
284 |
jtm.addSerializer(LocalDateTime.class, serializer);
|
|
|
285 |
jtm.addDeserializer(LocalDateTime.class, deserializer);
|
| 32569 |
amit.gupta |
286 |
SimpleModule stringModule = new SimpleModule("String trimmer deserialize module");
|
|
|
287 |
stringModule.addDeserializer(String.class, new CustomStringDeserializer());
|
| 32186 |
amit.gupta |
288 |
ObjectMapper mapper = new ObjectMapper()
|
| 32187 |
amit.gupta |
289 |
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
|
| 32569 |
amit.gupta |
290 |
.registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module()).registerModule(jtm)
|
|
|
291 |
.registerModule(stringModule); // new module, NOT JSR310Module
|
| 32054 |
tejbeer |
292 |
return mapper;
|
|
|
293 |
}
|
| 31282 |
amit.gupta |
294 |
|
| 32569 |
amit.gupta |
295 |
class CustomStringDeserializer extends StringDeserializer {
|
|
|
296 |
@Override
|
|
|
297 |
public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
|
|
298 |
String text = super.deserialize(p, ctxt);
|
|
|
299 |
//clean up value
|
|
|
300 |
return text.trim();
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
|
| 21555 |
kshitij.so |
305 |
}
|