Subversion Repositories SmartDukaan

Rev

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