Subversion Repositories SmartDukaan

Rev

Rev 34582 | Rev 34636 | 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();
33495 amit.gupta 228
        LOGGER.info("Google mail sender - here");
32054 tejbeer 229
        // Using gmail
33494 amit.gupta 230
        mailSender.setHost("smtp.sendgrid.net");
231
        mailSender.setPort(587);
232
        mailSender.setUsername("apikey");
33496 amit.gupta 233
        mailSender.setPassword("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");
33494 amit.gupta 234
 
235
        Properties javaMailProperties = new Properties();
236
        javaMailProperties.put("mail.smtp.starttls.enable", "false");
237
        javaMailProperties.put("mail.smtp.auth", "true");
238
        javaMailProperties.put("mail.transport.protocol", "smtp");
239
        javaMailProperties.put("mail.debug", "true");// Prints out everything on
240
        // screen
241
 
242
        mailSender.setJavaMailProperties(javaMailProperties);
243
        return mailSender;
244
        /*JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
245
        // Using gmail
32054 tejbeer 246
        mailSender.setHost("smtp.gmail.com");
247
        mailSender.setPort(587);
248
        mailSender.setUsername("build@shop2020.in");
249
        mailSender.setPassword("cafe@nes");
31511 amit.gupta 250
 
32054 tejbeer 251
        Properties javaMailProperties = new Properties();
252
        javaMailProperties.put("mail.smtp.starttls.enable", "true");
253
        javaMailProperties.put("mail.smtp.auth", "true");
254
        javaMailProperties.put("mail.transport.protocol", "smtp");
255
        javaMailProperties.put("mail.debug", "true");// Prints out everything on screen
256
        mailSender.setJavaMailProperties(javaMailProperties);
33494 amit.gupta 257
        return mailSender;*/
32054 tejbeer 258
    }
31511 amit.gupta 259
 
32054 tejbeer 260
    @Bean(name = "gson")
261
    public Gson gson() {
25300 tejbeer 262
 
32054 tejbeer 263
        Gson gson = new GsonBuilder().serializeNulls().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
25300 tejbeer 264
 
32054 tejbeer 265
        return gson;
25300 tejbeer 266
 
32054 tejbeer 267
    }
25300 tejbeer 268
 
32054 tejbeer 269
    @Bean(name = "veloctyEngine")
270
    public VelocityEngine velocityEngine() throws VelocityException, IOException {
271
        VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
272
        // Properties props = new Properties();
273
        // props.put("resource.loader", "file");
28339 tejbeer 274
 
32054 tejbeer 275
        // props.put("file.resource.loader.description", "Velocity File Resource
276
        // Loader");
277
        // props.put("file.resource.loader.class",
278
        // "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
279
        // props.put("file.resource.loader.cache", true);
280
        // props.put("file.resource.loader.path", ".");
32773 raveendra. 281
        factory.setPreferFileSystemAccess(false);
282
        // factory.setResourceLoaderPath("classpath:/templates/");
28339 tejbeer 283
 
32773 raveendra. 284
 
32054 tejbeer 285
        Properties velocityProperties = new Properties();
286
        velocityProperties.put("resource.loader", "class");
287
        velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
288
        velocityProperties.put("file.resource.loader.cache", true);
289
        velocityProperties.put("file.resource.loader.unicode", true);
290
        velocityProperties.put("input.encoding", "UTF-8");
291
        velocityProperties.put("output.encoding", "UTF-8");
292
        velocityProperties.put("overrideLogging", true);
32773 raveendra. 293
        velocityProperties.setProperty("input.encoding", "UTF-8");
294
        velocityProperties.setProperty("output.encoding", "UTF-8");
295
        factory.setVelocityProperties(velocityProperties);
28339 tejbeer 296
 
32054 tejbeer 297
        // velocityProperties.put("file.resource.loader.path", ".");
28339 tejbeer 298
 
32054 tejbeer 299
        factory.setVelocityProperties(velocityProperties);
300
        return factory.createVelocityEngine();
28339 tejbeer 301
 
32054 tejbeer 302
    }
28339 tejbeer 303
 
32054 tejbeer 304
    @Bean
305
    public ObjectMapper objectMapper() {
306
        DateTimeFormatter df = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).optionalStart().appendLiteral('T').optionalEnd().appendLiteral(' ').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
307
        DateTimeFormatter sf = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral('T').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
33618 tejus.loha 308
 
309
        DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).toFormatter();
32054 tejbeer 310
        JavaTimeModule jtm = new JavaTimeModule();
33618 tejus.loha 311
        jtm.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(sf));
312
        jtm.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(df));
313
        jtm.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));
314
        jtm.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));
32569 amit.gupta 315
        SimpleModule stringModule = new SimpleModule("String trimmer deserialize module");
316
        stringModule.addDeserializer(String.class, new CustomStringDeserializer());
32186 amit.gupta 317
        ObjectMapper mapper = new ObjectMapper()
32187 amit.gupta 318
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
32569 amit.gupta 319
                .registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module()).registerModule(jtm)
320
                .registerModule(stringModule); // new module, NOT JSR310Module
32054 tejbeer 321
        return mapper;
322
    }
31282 amit.gupta 323
 
32569 amit.gupta 324
    class CustomStringDeserializer extends StringDeserializer {
325
        @Override
326
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
327
            String text = super.deserialize(p, ctxt);
328
            //clean up value
329
            return text.trim();
330
        }
331
    }
332
 
333
 
21555 kshitij.so 334
}