Subversion Repositories SmartDukaan

Rev

Rev 33758 | Rev 33776 | 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);
33765 ranu 102
        attributesMap.put("version", "238");
33751 ranu 103
        attributesMap.put("cssVersion", "16");
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);
116
        bean.setRequestContextAttribute("rc");
32773 raveendra. 117
        bean.setExposeSpringMacroHelpers(true);
32054 tejbeer 118
        bean.setAttributesMap(this.velocityAttributesMap());
119
        return bean;
120
    }
21625 kshitij.so 121
 
31512 amit.gupta 122
//	@Bean
123
//	public ViewResolver freemarkerViewResolver() {
124
//		FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
125
//		resolver.setCache(true);
126
//		resolver.setPrefix("");
127
//		resolver.setSuffix(".ftl");
128
//		return resolver;
129
//	}
130
 
32054 tejbeer 131
    @Bean
132
    public ViewResolver beanNameViewResolver() {
133
        BeanNameViewResolver resolver = new BeanNameViewResolver();
134
        return resolver;
135
    }
24507 amit.gupta 136
 
32054 tejbeer 137
    @Bean
138
    public VelocityConfigurer velocityConfig() {
139
        VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
140
        Properties velocityProperties = new Properties();
141
        velocityProperties.put("directive.set.null.allowed", "true");
142
        velocityConfigurer.setVelocityProperties(velocityProperties);
143
        velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
144
        return velocityConfigurer;
145
    }
21555 kshitij.so 146
 
32788 amit.gupta 147
    @Bean(name = "messageSource")
148
    public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
32054 tejbeer 149
        LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
150
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
151
        messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
32773 raveendra. 152
        messageSource.setDefaultEncoding("UTF-8");
153
        //messageSource.setDefaultEncoding("ISO 8859-1");
154
        // messageSource.setDefaultEncoding("U+0900 ");
33013 shampa 155
        //messageSource.setDefaultEncoding("UTF-32");
32773 raveendra. 156
 
32054 tejbeer 157
        return messageSource;
158
    }
21555 kshitij.so 159
 
32054 tejbeer 160
    @Bean(name = "multipartResolver")
161
    public CommonsMultipartResolver getCommonsMultipartResolver() {
162
        LOGGER.info("creating common multipart resolver bean");
163
        return new CommonsMultipartResolver();
164
    }
21555 kshitij.so 165
 
32054 tejbeer 166
    @Bean
167
    public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
168
        String activeProfile = getActiveProfile();
21555 kshitij.so 169
 
32054 tejbeer 170
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 171
 
32054 tejbeer 172
        if ("prod".equals(activeProfile)) {
173
            resource = new ClassPathResource("/META-INF/prod.properties");
174
        } else if ("staging".equals(activeProfile)) {
175
            resource = new ClassPathResource("/META-INF/staging.properties");
176
        } else {
177
            resource = new ClassPathResource("/META-INF/dev.properties");
178
        }
21555 kshitij.so 179
 
32054 tejbeer 180
        propertySourcesPlaceholderConfigurer.setLocation(resource);
21555 kshitij.so 181
 
32054 tejbeer 182
        return propertySourcesPlaceholderConfigurer;
183
    }
21625 kshitij.so 184
 
32054 tejbeer 185
    private String getActiveProfile() {
186
        Properties properties = new Properties();
187
        try {
188
            properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
189
        } catch (IOException e) {
190
            LOGGER.error("Error in reading env property file.Adding default property" + e);
191
            properties.put("profile", "dev");
192
        }
193
        LOGGER.info("Profile is [{}]", properties.get("profile"));
194
        return (String) properties.get("profile");
195
    }
25811 amit.gupta 196
 
32054 tejbeer 197
    @Bean(name = "mailSender")
198
    public JavaMailSender getSendgridMailSender() {
199
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
24639 tejbeer 200
 
32054 tejbeer 201
        // Using gmail
202
        mailSender.setHost("smtp.sendgrid.net");
203
        mailSender.setPort(587);
204
        mailSender.setUsername("apikey");
33496 amit.gupta 205
        mailSender.setPassword("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");
24639 tejbeer 206
 
32054 tejbeer 207
        Properties javaMailProperties = new Properties();
208
        javaMailProperties.put("mail.smtp.starttls.enable", "false");
209
        javaMailProperties.put("mail.smtp.auth", "true");
210
        javaMailProperties.put("mail.transport.protocol", "smtp");
211
        javaMailProperties.put("mail.debug", "true");// Prints out everything on
212
        // screen
24639 tejbeer 213
 
32054 tejbeer 214
        mailSender.setJavaMailProperties(javaMailProperties);
215
        return mailSender;
216
    }
24639 tejbeer 217
 
32399 amit.gupta 218
    @Bean
32054 tejbeer 219
    public JavaMailSender googleMailSender() {
220
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
33495 amit.gupta 221
        LOGGER.info("Google mail sender - here");
32054 tejbeer 222
        // Using gmail
33494 amit.gupta 223
        mailSender.setHost("smtp.sendgrid.net");
224
        mailSender.setPort(587);
225
        mailSender.setUsername("apikey");
33496 amit.gupta 226
        mailSender.setPassword("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");
33494 amit.gupta 227
 
228
        Properties javaMailProperties = new Properties();
229
        javaMailProperties.put("mail.smtp.starttls.enable", "false");
230
        javaMailProperties.put("mail.smtp.auth", "true");
231
        javaMailProperties.put("mail.transport.protocol", "smtp");
232
        javaMailProperties.put("mail.debug", "true");// Prints out everything on
233
        // screen
234
 
235
        mailSender.setJavaMailProperties(javaMailProperties);
236
        return mailSender;
237
        /*JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
238
        // Using gmail
32054 tejbeer 239
        mailSender.setHost("smtp.gmail.com");
240
        mailSender.setPort(587);
241
        mailSender.setUsername("build@shop2020.in");
242
        mailSender.setPassword("cafe@nes");
31511 amit.gupta 243
 
32054 tejbeer 244
        Properties javaMailProperties = new Properties();
245
        javaMailProperties.put("mail.smtp.starttls.enable", "true");
246
        javaMailProperties.put("mail.smtp.auth", "true");
247
        javaMailProperties.put("mail.transport.protocol", "smtp");
248
        javaMailProperties.put("mail.debug", "true");// Prints out everything on screen
249
        mailSender.setJavaMailProperties(javaMailProperties);
33494 amit.gupta 250
        return mailSender;*/
32054 tejbeer 251
    }
31511 amit.gupta 252
 
32054 tejbeer 253
    @Bean(name = "gson")
254
    public Gson gson() {
25300 tejbeer 255
 
32054 tejbeer 256
        Gson gson = new GsonBuilder().serializeNulls().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
25300 tejbeer 257
 
32054 tejbeer 258
        return gson;
25300 tejbeer 259
 
32054 tejbeer 260
    }
25300 tejbeer 261
 
32054 tejbeer 262
    @Bean(name = "veloctyEngine")
263
    public VelocityEngine velocityEngine() throws VelocityException, IOException {
264
        VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
265
        // Properties props = new Properties();
266
        // props.put("resource.loader", "file");
28339 tejbeer 267
 
32054 tejbeer 268
        // props.put("file.resource.loader.description", "Velocity File Resource
269
        // Loader");
270
        // props.put("file.resource.loader.class",
271
        // "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
272
        // props.put("file.resource.loader.cache", true);
273
        // props.put("file.resource.loader.path", ".");
32773 raveendra. 274
        factory.setPreferFileSystemAccess(false);
275
        // factory.setResourceLoaderPath("classpath:/templates/");
28339 tejbeer 276
 
32773 raveendra. 277
 
32054 tejbeer 278
        Properties velocityProperties = new Properties();
279
        velocityProperties.put("resource.loader", "class");
280
        velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
281
        velocityProperties.put("file.resource.loader.cache", true);
282
        velocityProperties.put("file.resource.loader.unicode", true);
283
        velocityProperties.put("input.encoding", "UTF-8");
284
        velocityProperties.put("output.encoding", "UTF-8");
285
        velocityProperties.put("overrideLogging", true);
32773 raveendra. 286
        velocityProperties.setProperty("input.encoding", "UTF-8");
287
        velocityProperties.setProperty("output.encoding", "UTF-8");
288
        factory.setVelocityProperties(velocityProperties);
28339 tejbeer 289
 
32054 tejbeer 290
        // velocityProperties.put("file.resource.loader.path", ".");
28339 tejbeer 291
 
32054 tejbeer 292
        factory.setVelocityProperties(velocityProperties);
293
        return factory.createVelocityEngine();
28339 tejbeer 294
 
32054 tejbeer 295
    }
28339 tejbeer 296
 
32054 tejbeer 297
    @Bean
298
    public ObjectMapper objectMapper() {
299
        DateTimeFormatter df = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).optionalStart().appendLiteral('T').optionalEnd().appendLiteral(' ').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
300
        DateTimeFormatter sf = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral('T').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
33618 tejus.loha 301
 
302
        DateTimeFormatter dateFormatter = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).toFormatter();
32054 tejbeer 303
        JavaTimeModule jtm = new JavaTimeModule();
33618 tejus.loha 304
        jtm.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(sf));
305
        jtm.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(df));
306
        jtm.addSerializer(LocalDate.class, new LocalDateSerializer(dateFormatter));
307
        jtm.addDeserializer(LocalDate.class, new LocalDateDeserializer(dateFormatter));
32569 amit.gupta 308
        SimpleModule stringModule = new SimpleModule("String trimmer deserialize module");
309
        stringModule.addDeserializer(String.class, new CustomStringDeserializer());
32186 amit.gupta 310
        ObjectMapper mapper = new ObjectMapper()
32187 amit.gupta 311
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
32569 amit.gupta 312
                .registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module()).registerModule(jtm)
313
                .registerModule(stringModule); // new module, NOT JSR310Module
32054 tejbeer 314
        return mapper;
315
    }
31282 amit.gupta 316
 
32569 amit.gupta 317
    class CustomStringDeserializer extends StringDeserializer {
318
        @Override
319
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
320
            String text = super.deserialize(p, ctxt);
321
            //clean up value
322
            return text.trim();
323
        }
324
    }
325
 
326
 
21555 kshitij.so 327
}