Subversion Repositories SmartDukaan

Rev

Rev 35769 | Rev 35775 | 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;
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;
24639 tejbeer 64
 
32054 tejbeer 65
    public static Resource getResource() {
66
        return resource;
67
    }
21555 kshitij.so 68
 
32054 tejbeer 69
    public void setResource(Resource resource) {
70
        AppConfig.resource = resource;
71
    }
21625 kshitij.so 72
 
32054 tejbeer 73
    @Bean(value = "velocityAttributesMap")
74
    public Map<String, Object> velocityAttributesMap() {
75
        Map<String, Object> attributesMap = new HashMap<>();
76
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
77
        DateTimeFormatter dateMonthFormatter = DateTimeFormatter.ofPattern("dd''MMM");
78
        DateTimeFormatter datehiphenFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
79
        DateTimeFormatter dateMonthYearFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
80
        DateTimeFormatter dateYearMonthFormatter = DateTimeFormatter.ofPattern("MMM''uu");
31218 tejbeer 81
 
32054 tejbeer 82
        DateTimeFormatter dateMonthChYear = DateTimeFormatter.ofPattern("d''MMM''uu");
83
        DecimalFormat decimalFormatter = new DecimalFormat("0.#");
30859 tejbeer 84
 
32054 tejbeer 85
        NumberFormat numberformat = NumberFormat.getNumberInstance(new Locale("en", "IN"));
30859 tejbeer 86
 
32054 tejbeer 87
        numberformat.setRoundingMode(RoundingMode.HALF_DOWN);
88
        numberformat.setMaximumFractionDigits(2);
89
        numberformat.setMinimumFractionDigits(0);
90
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
91
        attributesMap.put("dateFormatter", dateFormatter);
92
        attributesMap.put("nf", numberformat);
93
        attributesMap.put("dateMonthFormatter", dateMonthFormatter);
94
        attributesMap.put("datehiphenFormatter", datehiphenFormatter);
95
        attributesMap.put("dateYearMonthFormatter", dateYearMonthFormatter);
31218 tejbeer 96
 
32054 tejbeer 97
        attributesMap.put("dateMonthYearFormatter", dateMonthYearFormatter);
98
        attributesMap.put("dateMonthChYear", dateMonthChYear);
99
        attributesMap.put("decimalFormatter", decimalFormatter);
100
        attributesMap.put("noData", "<tr><td colspan=\"20\" style=\"text-align:center;\">No results found for the given criteria</td></tr>");
101
        attributesMap.put("dateTimeFormatter", dateTimeFormatter);
35772 ranu 102
        attributesMap.put("version", "319");
35676 amit 103
        attributesMap.put("cssVersion", "37");
32054 tejbeer 104
        attributesMap.put("isDev", getActiveProfile().equals("dev"));
105
        attributesMap.put("vmUtils", new Utils());
32200 amit.gupta 106
        //attributesMap.put("esc", new EscapeTool());
32054 tejbeer 107
        attributesMap.put("ru", RoundingMode.HALF_UP);
108
        return attributesMap;
109
    }
30470 amit.gupta 110
 
32054 tejbeer 111
    @Bean
112
    public ViewResolver viewResolver() {
113
        VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
114
        bean.setPrefix("");
115
        bean.setSuffix(VELOCITY_PATH_SUFFIX);
35587 ranu 116
        bean.setContentType("text/html;charset=UTF-8");
32054 tejbeer 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
 
32054 tejbeer 123
    @Bean
124
    public ViewResolver beanNameViewResolver() {
125
        BeanNameViewResolver resolver = new BeanNameViewResolver();
126
        return resolver;
127
    }
24507 amit.gupta 128
 
32054 tejbeer 129
    @Bean
130
    public VelocityConfigurer velocityConfig() {
131
        VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
132
        Properties velocityProperties = new Properties();
35294 ranu 133
       /* velocityProperties.put("velocimacro.library", "macros.vm");
35289 amit 134
        velocityProperties.put("velocimacro.permissions.allow.inline", "true");
35294 ranu 135
        velocityProperties.put("velocimacro.permissions.allow.inline.to.replace.global", "true");*/
32054 tejbeer 136
        velocityProperties.put("directive.set.null.allowed", "true");
137
        velocityConfigurer.setVelocityProperties(velocityProperties);
138
        velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
139
        return velocityConfigurer;
140
    }
21555 kshitij.so 141
 
32788 amit.gupta 142
    @Bean(name = "messageSource")
143
    public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
32054 tejbeer 144
        LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
145
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
146
        messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
32773 raveendra. 147
        messageSource.setDefaultEncoding("UTF-8");
148
 
32054 tejbeer 149
        return messageSource;
150
    }
21555 kshitij.so 151
 
32054 tejbeer 152
    @Bean(name = "multipartResolver")
153
    public CommonsMultipartResolver getCommonsMultipartResolver() {
154
        LOGGER.info("creating common multipart resolver bean");
155
        return new CommonsMultipartResolver();
156
    }
21555 kshitij.so 157
 
32054 tejbeer 158
    @Bean
159
    public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
160
        String activeProfile = getActiveProfile();
21555 kshitij.so 161
 
32054 tejbeer 162
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 163
 
35381 amit 164
        Resource appResource;
165
        Resource sharedResource;
166
 
32054 tejbeer 167
        if ("prod".equals(activeProfile)) {
35381 amit 168
            appResource = new ClassPathResource("/META-INF/prod.properties");
169
            sharedResource = new ClassPathResource("/shared-prod.properties");
32054 tejbeer 170
        } else if ("staging".equals(activeProfile)) {
35381 amit 171
            appResource = new ClassPathResource("/META-INF/staging.properties");
172
            sharedResource = new ClassPathResource("/shared-staging.properties");
32054 tejbeer 173
        } else {
35381 amit 174
            appResource = new ClassPathResource("/META-INF/dev.properties");
175
            sharedResource = new ClassPathResource("/shared-dev.properties");
32054 tejbeer 176
        }
21555 kshitij.so 177
 
35381 amit 178
        resource = appResource;
21555 kshitij.so 179
 
35381 amit 180
        // Load shared properties from dao first, then app-specific (app-specific can override)
181
        propertySourcesPlaceholderConfigurer.setLocations(sharedResource, appResource);
182
        propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(true);
183
 
32054 tejbeer 184
        return propertySourcesPlaceholderConfigurer;
185
    }
21625 kshitij.so 186
 
32054 tejbeer 187
    private String getActiveProfile() {
188
        Properties properties = new Properties();
189
        try {
190
            properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
191
        } catch (IOException e) {
192
            LOGGER.error("Error in reading env property file.Adding default property" + e);
193
            properties.put("profile", "dev");
194
        }
195
        LOGGER.info("Profile is [{}]", properties.get("profile"));
196
        return (String) properties.get("profile");
197
    }
25811 amit.gupta 198
 
34667 amit.gupta 199
    @Bean(name = "mailSender")
32054 tejbeer 200
    public JavaMailSender getSendgridMailSender() {
201
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
24639 tejbeer 202
 
32054 tejbeer 203
        // Using gmail
204
        mailSender.setHost("smtp.sendgrid.net");
205
        mailSender.setPort(587);
206
        mailSender.setUsername("apikey");
34667 amit.gupta 207
        mailSender.setPassword("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");
24639 tejbeer 208
 
32054 tejbeer 209
        Properties javaMailProperties = new Properties();
210
        javaMailProperties.put("mail.smtp.starttls.enable", "false");
211
        javaMailProperties.put("mail.smtp.auth", "true");
212
        javaMailProperties.put("mail.transport.protocol", "smtp");
213
        javaMailProperties.put("mail.debug", "true");// Prints out everything on
214
        // screen
24639 tejbeer 215
 
32054 tejbeer 216
        mailSender.setJavaMailProperties(javaMailProperties);
217
        return mailSender;
218
    }
24639 tejbeer 219
 
34667 amit.gupta 220
 
32399 amit.gupta 221
    @Bean
32054 tejbeer 222
    public JavaMailSender googleMailSender() {
223
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
34636 ranu 224
        mailSender.setHost("smtp.gmail.com");
225
        mailSender.setPort(465);
226
        mailSender.setUsername("sdtech@smartdukaan.com");
227
        mailSender.setPassword("gpdschroalhhirox"); // App Password
228
 
229
        Properties props = mailSender.getJavaMailProperties();
230
        props.put("mail.smtp.auth", "true");
231
        props.put("mail.smtp.ssl.enable", "true");
232
        props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
233
        props.put("mail.smtp.socketFactory.port", "465");
234
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
235
        props.put("mail.debug", "true");
236
 
237
        return mailSender;
238
    }
239
 
32054 tejbeer 240
    @Bean(name = "gson")
241
    public Gson gson() {
25300 tejbeer 242
 
32054 tejbeer 243
        Gson gson = new GsonBuilder().serializeNulls().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
25300 tejbeer 244
 
32054 tejbeer 245
        return gson;
25300 tejbeer 246
 
32054 tejbeer 247
    }
25300 tejbeer 248
 
32054 tejbeer 249
    @Bean(name = "veloctyEngine")
250
    public VelocityEngine velocityEngine() throws VelocityException, IOException {
251
        VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
252
        // Properties props = new Properties();
253
        // props.put("resource.loader", "file");
28339 tejbeer 254
 
32054 tejbeer 255
        // props.put("file.resource.loader.description", "Velocity File Resource
256
        // Loader");
257
        // props.put("file.resource.loader.class",
258
        // "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
259
        // props.put("file.resource.loader.cache", true);
260
        // props.put("file.resource.loader.path", ".");
32773 raveendra. 261
        factory.setPreferFileSystemAccess(false);
262
        // factory.setResourceLoaderPath("classpath:/templates/");
28339 tejbeer 263
 
32773 raveendra. 264
 
32054 tejbeer 265
        Properties velocityProperties = new Properties();
266
        velocityProperties.put("resource.loader", "class");
267
        velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
268
        velocityProperties.put("file.resource.loader.cache", true);
269
        velocityProperties.put("file.resource.loader.unicode", true);
270
        velocityProperties.put("input.encoding", "UTF-8");
271
        velocityProperties.put("output.encoding", "UTF-8");
272
        velocityProperties.put("overrideLogging", true);
32773 raveendra. 273
        velocityProperties.setProperty("input.encoding", "UTF-8");
274
        velocityProperties.setProperty("output.encoding", "UTF-8");
275
        factory.setVelocityProperties(velocityProperties);
28339 tejbeer 276
 
32054 tejbeer 277
        // velocityProperties.put("file.resource.loader.path", ".");
28339 tejbeer 278
 
32054 tejbeer 279
        factory.setVelocityProperties(velocityProperties);
280
        return factory.createVelocityEngine();
28339 tejbeer 281
 
32054 tejbeer 282
    }
28339 tejbeer 283
 
32054 tejbeer 284
    @Bean
285
    public ObjectMapper objectMapper() {
286
        DateTimeFormatter df = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).optionalStart().appendLiteral('T').optionalEnd().appendLiteral(' ').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
287
        DateTimeFormatter sf = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral('T').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
33618 tejus.loha 288
 
289
        DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).toFormatter();
32054 tejbeer 290
        JavaTimeModule jtm = new JavaTimeModule();
33618 tejus.loha 291
        jtm.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(sf));
292
        jtm.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(df));
293
        jtm.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));
294
        jtm.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));
32569 amit.gupta 295
        SimpleModule stringModule = new SimpleModule("String trimmer deserialize module");
296
        stringModule.addDeserializer(String.class, new CustomStringDeserializer());
32186 amit.gupta 297
        ObjectMapper mapper = new ObjectMapper()
32187 amit.gupta 298
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
32569 amit.gupta 299
                .registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module()).registerModule(jtm)
300
                .registerModule(stringModule); // new module, NOT JSR310Module
32054 tejbeer 301
        return mapper;
302
    }
31282 amit.gupta 303
 
32569 amit.gupta 304
    class CustomStringDeserializer extends StringDeserializer {
305
        @Override
306
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
307
            String text = super.deserialize(p, ctxt);
308
            //clean up value
309
            return text.trim();
310
        }
311
    }
312
 
313
 
21555 kshitij.so 314
}