Subversion Repositories SmartDukaan

Rev

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