Subversion Repositories SmartDukaan

Rev

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