Subversion Repositories SmartDukaan

Rev

Rev 32773 | Rev 32788 | 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;
11
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
12
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
13
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
29993 amit.gupta 14
import com.google.gson.Gson;
15
import com.google.gson.GsonBuilder;
16
import com.spice.profitmandi.common.util.Utils;
17
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
23869 amit.gupta 18
import org.apache.logging.log4j.LogManager;
23568 govind 19
import org.apache.logging.log4j.Logger;
28339 tejbeer 20
import org.apache.velocity.app.VelocityEngine;
21
import org.apache.velocity.exception.VelocityException;
21555 kshitij.so 22
import org.springframework.context.annotation.Bean;
23
import org.springframework.context.annotation.ComponentScan;
24
import org.springframework.context.annotation.Configuration;
25
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
26
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
27
import org.springframework.core.io.ClassPathResource;
28
import org.springframework.core.io.Resource;
23985 tejbeer 29
import org.springframework.mail.javamail.JavaMailSender;
30
import org.springframework.mail.javamail.JavaMailSenderImpl;
28339 tejbeer 31
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
21555 kshitij.so 32
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
21625 kshitij.so 33
import org.springframework.web.servlet.ViewResolver;
24507 amit.gupta 34
import org.springframework.web.servlet.view.BeanNameViewResolver;
21625 kshitij.so 35
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
31513 amit.gupta 36
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
21555 kshitij.so 37
 
29993 amit.gupta 38
import java.io.IOException;
39
import java.math.RoundingMode;
40
import java.text.DecimalFormat;
41
import java.text.NumberFormat;
42
import java.time.LocalDateTime;
43
import java.time.format.DateTimeFormatter;
31282 amit.gupta 44
import java.time.format.DateTimeFormatterBuilder;
29993 amit.gupta 45
import java.util.HashMap;
46
import java.util.Locale;
47
import java.util.Map;
48
import java.util.Properties;
25300 tejbeer 49
 
21625 kshitij.so 50
@SuppressWarnings("deprecation")
21555 kshitij.so 51
@Configuration
52
@ComponentScan("com.spice.profitmandi.*")
31511 amit.gupta 53
//@Import(RepositoryRestMvcConfiguration.class)
21555 kshitij.so 54
public class AppConfig {
55
 
32054 tejbeer 56
    private static final String VELOCITY_PATH_PREFIX = "/WEB-INF/views/ftl/";
57
    private static final String VELOCITY_PATH_SUFFIX = ".vm";
32773 raveendra. 58
    private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:messages";
32054 tejbeer 59
    private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
60
    private static Resource resource;
24639 tejbeer 61
 
32054 tejbeer 62
    public static Resource getResource() {
63
        return resource;
64
    }
21555 kshitij.so 65
 
32054 tejbeer 66
    public void setResource(Resource resource) {
67
        AppConfig.resource = resource;
68
    }
21625 kshitij.so 69
 
32054 tejbeer 70
    @Bean(value = "velocityAttributesMap")
71
    public Map<String, Object> velocityAttributesMap() {
72
        Map<String, Object> attributesMap = new HashMap<>();
73
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
74
        DateTimeFormatter dateMonthFormatter = DateTimeFormatter.ofPattern("dd''MMM");
75
        DateTimeFormatter datehiphenFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
76
        DateTimeFormatter dateMonthYearFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
77
        DateTimeFormatter dateYearMonthFormatter = DateTimeFormatter.ofPattern("MMM''uu");
31218 tejbeer 78
 
32054 tejbeer 79
        DateTimeFormatter dateMonthChYear = DateTimeFormatter.ofPattern("d''MMM''uu");
80
        DecimalFormat decimalFormatter = new DecimalFormat("0.#");
30859 tejbeer 81
 
32054 tejbeer 82
        NumberFormat numberformat = NumberFormat.getNumberInstance(new Locale("en", "IN"));
30859 tejbeer 83
 
32054 tejbeer 84
        numberformat.setRoundingMode(RoundingMode.HALF_DOWN);
85
        numberformat.setMaximumFractionDigits(2);
86
        numberformat.setMinimumFractionDigits(0);
87
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
88
        attributesMap.put("dateFormatter", dateFormatter);
89
        attributesMap.put("nf", numberformat);
90
        attributesMap.put("dateMonthFormatter", dateMonthFormatter);
91
        attributesMap.put("datehiphenFormatter", datehiphenFormatter);
92
        attributesMap.put("dateYearMonthFormatter", dateYearMonthFormatter);
31218 tejbeer 93
 
32054 tejbeer 94
        attributesMap.put("dateMonthYearFormatter", dateMonthYearFormatter);
95
        attributesMap.put("dateMonthChYear", dateMonthChYear);
96
        attributesMap.put("decimalFormatter", decimalFormatter);
97
        attributesMap.put("noData", "<tr><td colspan=\"20\" style=\"text-align:center;\">No results found for the given criteria</td></tr>");
98
        attributesMap.put("dateTimeFormatter", dateTimeFormatter);
32787 amit.gupta 99
        attributesMap.put("version", "198");
32359 amit.gupta 100
        attributesMap.put("cssVersion", "12");
32054 tejbeer 101
        attributesMap.put("isDev", getActiveProfile().equals("dev"));
102
        attributesMap.put("vmUtils", new Utils());
32200 amit.gupta 103
        //attributesMap.put("esc", new EscapeTool());
32054 tejbeer 104
        attributesMap.put("ru", RoundingMode.HALF_UP);
105
        return attributesMap;
106
    }
30470 amit.gupta 107
 
32054 tejbeer 108
    @Bean
109
    public ViewResolver viewResolver() {
110
        VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
111
        bean.setPrefix("");
112
        bean.setSuffix(VELOCITY_PATH_SUFFIX);
113
        bean.setRequestContextAttribute("rc");
32773 raveendra. 114
        bean.setExposeSpringMacroHelpers(true);
32054 tejbeer 115
        bean.setAttributesMap(this.velocityAttributesMap());
116
        return bean;
117
    }
21625 kshitij.so 118
 
31512 amit.gupta 119
//	@Bean
120
//	public ViewResolver freemarkerViewResolver() {
121
//		FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
122
//		resolver.setCache(true);
123
//		resolver.setPrefix("");
124
//		resolver.setSuffix(".ftl");
125
//		return resolver;
126
//	}
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();
138
        velocityProperties.put("directive.set.null.allowed", "true");
32787 amit.gupta 139
        velocityProperties.put("file.resource.loader.cache", true);
140
        velocityProperties.put("file.resource.loader.unicode", true);
141
        velocityProperties.put("input.encoding", "UTF-8");
142
        velocityProperties.put("output.encoding", "UTF-8");
143
        velocityProperties.put("contentType", "text/html");
144
        velocityProperties.put("charset", "UTF-8");
145
        velocityProperties.put("response.encoding", "UTF-8");
32054 tejbeer 146
        velocityConfigurer.setVelocityProperties(velocityProperties);
147
        velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
148
        return velocityConfigurer;
149
    }
21555 kshitij.so 150
 
32787 amit.gupta 151
    @Bean
152
    public ReloadableResourceBundleMessageSource messageSource() {
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
        //messageSource.setDefaultEncoding("ISO 8859-1");
158
        // messageSource.setDefaultEncoding("U+0900 ");
159
        // messageSource.setDefaultEncoding("UTF-32");
160
 
32054 tejbeer 161
        return messageSource;
162
    }
21555 kshitij.so 163
 
32054 tejbeer 164
    @Bean(name = "multipartResolver")
165
    public CommonsMultipartResolver getCommonsMultipartResolver() {
166
        LOGGER.info("creating common multipart resolver bean");
167
        return new CommonsMultipartResolver();
168
    }
21555 kshitij.so 169
 
32054 tejbeer 170
    @Bean
171
    public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
172
        String activeProfile = getActiveProfile();
21555 kshitij.so 173
 
32054 tejbeer 174
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 175
 
32054 tejbeer 176
        if ("prod".equals(activeProfile)) {
177
            resource = new ClassPathResource("/META-INF/prod.properties");
178
        } else if ("staging".equals(activeProfile)) {
179
            resource = new ClassPathResource("/META-INF/staging.properties");
180
        } else {
181
            resource = new ClassPathResource("/META-INF/dev.properties");
182
        }
21555 kshitij.so 183
 
32054 tejbeer 184
        propertySourcesPlaceholderConfigurer.setLocation(resource);
21555 kshitij.so 185
 
32054 tejbeer 186
        return propertySourcesPlaceholderConfigurer;
187
    }
21625 kshitij.so 188
 
32054 tejbeer 189
    private String getActiveProfile() {
190
        Properties properties = new Properties();
191
        try {
192
            properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
193
        } catch (IOException e) {
194
            LOGGER.error("Error in reading env property file.Adding default property" + e);
195
            properties.put("profile", "dev");
196
        }
197
        LOGGER.info("Profile is [{}]", properties.get("profile"));
198
        return (String) properties.get("profile");
199
    }
25811 amit.gupta 200
 
32054 tejbeer 201
    @Bean(name = "mailSender")
202
    public JavaMailSender getSendgridMailSender() {
203
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
24639 tejbeer 204
 
32054 tejbeer 205
        // Using gmail
206
        mailSender.setHost("smtp.sendgrid.net");
207
        mailSender.setPort(587);
208
        mailSender.setUsername("apikey");
209
        mailSender.setPassword("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");
24639 tejbeer 210
 
32054 tejbeer 211
        Properties javaMailProperties = new Properties();
212
        javaMailProperties.put("mail.smtp.starttls.enable", "false");
213
        javaMailProperties.put("mail.smtp.auth", "true");
214
        javaMailProperties.put("mail.transport.protocol", "smtp");
215
        javaMailProperties.put("mail.debug", "true");// Prints out everything on
216
        // screen
24639 tejbeer 217
 
32054 tejbeer 218
        mailSender.setJavaMailProperties(javaMailProperties);
219
        return mailSender;
220
    }
24639 tejbeer 221
 
32399 amit.gupta 222
    @Bean
32054 tejbeer 223
    public JavaMailSender googleMailSender() {
224
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
225
        // Using gmail
226
        mailSender.setHost("smtp.gmail.com");
227
        mailSender.setPort(587);
228
        mailSender.setUsername("build@shop2020.in");
229
        mailSender.setPassword("cafe@nes");
31511 amit.gupta 230
 
32054 tejbeer 231
        Properties javaMailProperties = new Properties();
232
        javaMailProperties.put("mail.smtp.starttls.enable", "true");
233
        javaMailProperties.put("mail.smtp.auth", "true");
234
        javaMailProperties.put("mail.transport.protocol", "smtp");
235
        javaMailProperties.put("mail.debug", "true");// Prints out everything on screen
236
        mailSender.setJavaMailProperties(javaMailProperties);
237
        return mailSender;
238
    }
31511 amit.gupta 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();
288
        LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(sf);
289
        LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(df);
290
        JavaTimeModule jtm = new JavaTimeModule();
291
        jtm.addSerializer(LocalDateTime.class, serializer);
292
        jtm.addDeserializer(LocalDateTime.class, deserializer);
32569 amit.gupta 293
        SimpleModule stringModule = new SimpleModule("String trimmer deserialize module");
294
        stringModule.addDeserializer(String.class, new CustomStringDeserializer());
32186 amit.gupta 295
        ObjectMapper mapper = new ObjectMapper()
32187 amit.gupta 296
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
32569 amit.gupta 297
                .registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module()).registerModule(jtm)
298
                .registerModule(stringModule); // new module, NOT JSR310Module
32054 tejbeer 299
        return mapper;
300
    }
31282 amit.gupta 301
 
32569 amit.gupta 302
    class CustomStringDeserializer extends StringDeserializer {
303
        @Override
304
        public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
305
            String text = super.deserialize(p, ctxt);
306
            //clean up value
307
            return text.trim();
308
        }
309
    }
310
 
311
 
21555 kshitij.so 312
}