Subversion Repositories SmartDukaan

Rev

Rev 32303 | Rev 32329 | 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
 
32186 amit.gupta 3
import com.fasterxml.jackson.databind.DeserializationFeature;
31282 amit.gupta 4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
6
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
7
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
8
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
9
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
29993 amit.gupta 10
import com.google.gson.Gson;
11
import com.google.gson.GsonBuilder;
12
import com.spice.profitmandi.common.util.Utils;
13
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
23869 amit.gupta 14
import org.apache.logging.log4j.LogManager;
23568 govind 15
import org.apache.logging.log4j.Logger;
28339 tejbeer 16
import org.apache.velocity.app.VelocityEngine;
17
import org.apache.velocity.exception.VelocityException;
21555 kshitij.so 18
import org.springframework.context.annotation.Bean;
19
import org.springframework.context.annotation.ComponentScan;
20
import org.springframework.context.annotation.Configuration;
21
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
22
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
23
import org.springframework.core.io.ClassPathResource;
24
import org.springframework.core.io.Resource;
23985 tejbeer 25
import org.springframework.mail.javamail.JavaMailSender;
26
import org.springframework.mail.javamail.JavaMailSenderImpl;
28339 tejbeer 27
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
21555 kshitij.so 28
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
21625 kshitij.so 29
import org.springframework.web.servlet.ViewResolver;
24507 amit.gupta 30
import org.springframework.web.servlet.view.BeanNameViewResolver;
21625 kshitij.so 31
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
31513 amit.gupta 32
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
21555 kshitij.so 33
 
29993 amit.gupta 34
import java.io.IOException;
35
import java.math.RoundingMode;
36
import java.text.DecimalFormat;
37
import java.text.NumberFormat;
38
import java.time.LocalDateTime;
39
import java.time.format.DateTimeFormatter;
31282 amit.gupta 40
import java.time.format.DateTimeFormatterBuilder;
29993 amit.gupta 41
import java.util.HashMap;
42
import java.util.Locale;
43
import java.util.Map;
44
import java.util.Properties;
25300 tejbeer 45
 
21625 kshitij.so 46
@SuppressWarnings("deprecation")
21555 kshitij.so 47
@Configuration
48
@ComponentScan("com.spice.profitmandi.*")
31511 amit.gupta 49
//@Import(RepositoryRestMvcConfiguration.class)
21555 kshitij.so 50
public class AppConfig {
51
 
32054 tejbeer 52
    private static final String VELOCITY_PATH_PREFIX = "/WEB-INF/views/ftl/";
53
    private static final String VELOCITY_PATH_SUFFIX = ".vm";
54
    private static final String MESSAGE_PATH_SOURCE_NAME = "classpath:message";
55
    private static final Logger LOGGER = LogManager.getLogger(AppConfig.class);
56
    private static Resource resource;
24639 tejbeer 57
 
32054 tejbeer 58
    public static Resource getResource() {
59
        return resource;
60
    }
21555 kshitij.so 61
 
32054 tejbeer 62
    public void setResource(Resource resource) {
63
        AppConfig.resource = resource;
64
    }
21625 kshitij.so 65
 
32054 tejbeer 66
    @Bean(value = "velocityAttributesMap")
67
    public Map<String, Object> velocityAttributesMap() {
68
        Map<String, Object> attributesMap = new HashMap<>();
69
        DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
70
        DateTimeFormatter dateMonthFormatter = DateTimeFormatter.ofPattern("dd''MMM");
71
        DateTimeFormatter datehiphenFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
72
        DateTimeFormatter dateMonthYearFormatter = DateTimeFormatter.ofPattern("MM-yyyy");
73
        DateTimeFormatter dateYearMonthFormatter = DateTimeFormatter.ofPattern("MMM''uu");
31218 tejbeer 74
 
32054 tejbeer 75
        DateTimeFormatter dateMonthChYear = DateTimeFormatter.ofPattern("d''MMM''uu");
76
        DecimalFormat decimalFormatter = new DecimalFormat("0.#");
30859 tejbeer 77
 
32054 tejbeer 78
        NumberFormat numberformat = NumberFormat.getNumberInstance(new Locale("en", "IN"));
30859 tejbeer 79
 
32054 tejbeer 80
        numberformat.setRoundingMode(RoundingMode.HALF_DOWN);
81
        numberformat.setMaximumFractionDigits(2);
82
        numberformat.setMinimumFractionDigits(0);
83
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
84
        attributesMap.put("dateFormatter", dateFormatter);
85
        attributesMap.put("nf", numberformat);
86
        attributesMap.put("dateMonthFormatter", dateMonthFormatter);
87
        attributesMap.put("datehiphenFormatter", datehiphenFormatter);
88
        attributesMap.put("dateYearMonthFormatter", dateYearMonthFormatter);
31218 tejbeer 89
 
32054 tejbeer 90
        attributesMap.put("dateMonthYearFormatter", dateMonthYearFormatter);
91
        attributesMap.put("dateMonthChYear", dateMonthChYear);
92
        attributesMap.put("decimalFormatter", decimalFormatter);
93
        attributesMap.put("noData", "<tr><td colspan=\"20\" style=\"text-align:center;\">No results found for the given criteria</td></tr>");
94
        attributesMap.put("dateTimeFormatter", dateTimeFormatter);
32308 amit.gupta 95
        attributesMap.put("version", "175");
32054 tejbeer 96
        attributesMap.put("cssVersion", "10");
97
        attributesMap.put("isDev", getActiveProfile().equals("dev"));
98
        attributesMap.put("vmUtils", new Utils());
32200 amit.gupta 99
        //attributesMap.put("esc", new EscapeTool());
32054 tejbeer 100
        attributesMap.put("ru", RoundingMode.HALF_UP);
101
        return attributesMap;
102
    }
30470 amit.gupta 103
 
32054 tejbeer 104
    @Bean
105
    public ViewResolver viewResolver() {
106
        VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
107
        bean.setPrefix("");
108
        bean.setSuffix(VELOCITY_PATH_SUFFIX);
109
        bean.setRequestContextAttribute("rc");
110
        bean.setAttributesMap(this.velocityAttributesMap());
111
        return bean;
112
    }
21625 kshitij.so 113
 
31512 amit.gupta 114
//	@Bean
115
//	public ViewResolver freemarkerViewResolver() {
116
//		FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
117
//		resolver.setCache(true);
118
//		resolver.setPrefix("");
119
//		resolver.setSuffix(".ftl");
120
//		return resolver;
121
//	}
122
 
32054 tejbeer 123
    @Bean
124
    public ViewResolver beanNameViewResolver() {
125
        BeanNameViewResolver resolver = new BeanNameViewResolver();
126
        return resolver;
127
    }
24507 amit.gupta 128
 
32054 tejbeer 129
    @Bean
130
    public VelocityConfigurer velocityConfig() {
131
        VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
132
        Properties velocityProperties = new Properties();
133
        velocityProperties.put("directive.set.null.allowed", "true");
134
        velocityConfigurer.setVelocityProperties(velocityProperties);
135
        velocityConfigurer.setResourceLoaderPath(VELOCITY_PATH_PREFIX);
136
        return velocityConfigurer;
137
    }
21555 kshitij.so 138
 
32054 tejbeer 139
    @Bean(name = "messageSource")
140
    public ReloadableResourceBundleMessageSource getReloadableResourceBundleMessageSource() {
141
        LOGGER.debug("creating messageSource bean with message path source name : " + MESSAGE_PATH_SOURCE_NAME);
142
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
143
        messageSource.setBasename(MESSAGE_PATH_SOURCE_NAME);
144
        return messageSource;
145
    }
21555 kshitij.so 146
 
32054 tejbeer 147
    @Bean(name = "multipartResolver")
148
    public CommonsMultipartResolver getCommonsMultipartResolver() {
149
        LOGGER.info("creating common multipart resolver bean");
150
        return new CommonsMultipartResolver();
151
    }
21555 kshitij.so 152
 
32054 tejbeer 153
    @Bean
154
    public PropertySourcesPlaceholderConfigurer propertyConfigurer1() {
155
        String activeProfile = getActiveProfile();
21555 kshitij.so 156
 
32054 tejbeer 157
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
21625 kshitij.so 158
 
32054 tejbeer 159
        if ("prod".equals(activeProfile)) {
160
            resource = new ClassPathResource("/META-INF/prod.properties");
161
        } else if ("staging".equals(activeProfile)) {
162
            resource = new ClassPathResource("/META-INF/staging.properties");
163
        } else {
164
            resource = new ClassPathResource("/META-INF/dev.properties");
165
        }
21555 kshitij.so 166
 
32054 tejbeer 167
        propertySourcesPlaceholderConfigurer.setLocation(resource);
21555 kshitij.so 168
 
32054 tejbeer 169
        return propertySourcesPlaceholderConfigurer;
170
    }
21625 kshitij.so 171
 
32054 tejbeer 172
    private String getActiveProfile() {
173
        Properties properties = new Properties();
174
        try {
175
            properties.load(this.getClass().getClassLoader().getResourceAsStream("META-INF/env.property"));
176
        } catch (IOException e) {
177
            LOGGER.error("Error in reading env property file.Adding default property" + e);
178
            properties.put("profile", "dev");
179
        }
180
        LOGGER.info("Profile is [{}]", properties.get("profile"));
181
        return (String) properties.get("profile");
182
    }
25811 amit.gupta 183
 
32054 tejbeer 184
    @Bean(name = "mailSender")
185
    public JavaMailSender getSendgridMailSender() {
186
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
24639 tejbeer 187
 
32054 tejbeer 188
        // Using gmail
189
        mailSender.setHost("smtp.sendgrid.net");
190
        mailSender.setPort(587);
191
        mailSender.setUsername("apikey");
192
        mailSender.setPassword("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");
24639 tejbeer 193
 
32054 tejbeer 194
        Properties javaMailProperties = new Properties();
195
        javaMailProperties.put("mail.smtp.starttls.enable", "false");
196
        javaMailProperties.put("mail.smtp.auth", "true");
197
        javaMailProperties.put("mail.transport.protocol", "smtp");
198
        javaMailProperties.put("mail.debug", "true");// Prints out everything on
199
        // screen
24639 tejbeer 200
 
32054 tejbeer 201
        mailSender.setJavaMailProperties(javaMailProperties);
202
        return mailSender;
203
    }
24639 tejbeer 204
 
32054 tejbeer 205
    //@Bean(name = "mailSender")
206
    public JavaMailSender googleMailSender() {
207
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
208
        // Using gmail
209
        mailSender.setHost("smtp.gmail.com");
210
        mailSender.setPort(587);
211
        mailSender.setUsername("build@shop2020.in");
212
        mailSender.setPassword("cafe@nes");
31511 amit.gupta 213
 
32054 tejbeer 214
        Properties javaMailProperties = new Properties();
215
        javaMailProperties.put("mail.smtp.starttls.enable", "true");
216
        javaMailProperties.put("mail.smtp.auth", "true");
217
        javaMailProperties.put("mail.transport.protocol", "smtp");
218
        javaMailProperties.put("mail.debug", "true");// Prints out everything on screen
219
        mailSender.setJavaMailProperties(javaMailProperties);
220
        return mailSender;
221
    }
31511 amit.gupta 222
 
32054 tejbeer 223
    @Bean(name = "gson")
224
    public Gson gson() {
25300 tejbeer 225
 
32054 tejbeer 226
        Gson gson = new GsonBuilder().serializeNulls().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
25300 tejbeer 227
 
32054 tejbeer 228
        return gson;
25300 tejbeer 229
 
32054 tejbeer 230
    }
25300 tejbeer 231
 
32054 tejbeer 232
    @Bean(name = "veloctyEngine")
233
    public VelocityEngine velocityEngine() throws VelocityException, IOException {
234
        VelocityEngineFactoryBean factory = new VelocityEngineFactoryBean();
235
        // Properties props = new Properties();
236
        // props.put("resource.loader", "file");
28339 tejbeer 237
 
32054 tejbeer 238
        // props.put("file.resource.loader.description", "Velocity File Resource
239
        // Loader");
240
        // props.put("file.resource.loader.class",
241
        // "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
242
        // props.put("file.resource.loader.cache", true);
243
        // props.put("file.resource.loader.path", ".");
28339 tejbeer 244
 
32054 tejbeer 245
        Properties velocityProperties = new Properties();
246
        velocityProperties.put("resource.loader", "class");
247
        velocityProperties.put("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
248
        velocityProperties.put("file.resource.loader.cache", true);
249
        velocityProperties.put("file.resource.loader.unicode", true);
250
        velocityProperties.put("input.encoding", "UTF-8");
251
        velocityProperties.put("output.encoding", "UTF-8");
252
        velocityProperties.put("overrideLogging", true);
28339 tejbeer 253
 
32054 tejbeer 254
        // velocityProperties.put("file.resource.loader.path", ".");
28339 tejbeer 255
 
32054 tejbeer 256
        factory.setVelocityProperties(velocityProperties);
257
        return factory.createVelocityEngine();
28339 tejbeer 258
 
32054 tejbeer 259
    }
28339 tejbeer 260
 
32054 tejbeer 261
    @Bean
262
    public ObjectMapper objectMapper() {
263
        DateTimeFormatter df = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).optionalStart().appendLiteral('T').optionalEnd().appendLiteral(' ').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
264
        DateTimeFormatter sf = new DateTimeFormatterBuilder().parseCaseInsensitive().append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral('T').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
265
        LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(sf);
266
        LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(df);
267
        JavaTimeModule jtm = new JavaTimeModule();
268
        jtm.addSerializer(LocalDateTime.class, serializer);
269
        jtm.addDeserializer(LocalDateTime.class, deserializer);
32186 amit.gupta 270
        ObjectMapper mapper = new ObjectMapper()
32187 amit.gupta 271
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
32186 amit.gupta 272
                .registerModule(new ParameterNamesModule()).registerModule(new Jdk8Module()).registerModule(jtm); // new module, NOT JSR310Module
32054 tejbeer 273
        return mapper;
274
    }
31282 amit.gupta 275
 
21555 kshitij.so 276
}