Subversion Repositories SmartDukaan

Rev

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