Subversion Repositories SmartDukaan

Rev

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