Subversion Repositories SmartDukaan

Rev

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