| 23723 |
amit.gupta |
1 |
package com.smartdukaan.cron.config;
|
|
|
2 |
|
| 24433 |
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;
|
| 29859 |
amit.gupta |
6 |
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
|
| 24433 |
amit.gupta |
7 |
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
| 29859 |
amit.gupta |
8 |
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
|
| 24433 |
amit.gupta |
9 |
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
|
|
10 |
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
|
| 25721 |
tejbeer |
11 |
import com.google.gson.Gson;
|
|
|
12 |
import com.google.gson.GsonBuilder;
|
| 29838 |
tejbeer |
13 |
import com.spice.profitmandi.dao.convertor.LocalDateJsonConverter;
|
| 25721 |
tejbeer |
14 |
import com.spice.profitmandi.dao.convertor.LocalDateTimeJsonConverter;
|
| 23738 |
amit.gupta |
15 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
| 30307 |
amit.gupta |
16 |
import org.apache.logging.log4j.LogManager;
|
|
|
17 |
import org.apache.logging.log4j.Logger;
|
|
|
18 |
import org.hibernate.SessionFactory;
|
|
|
19 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
20 |
import org.springframework.beans.factory.annotation.Value;
|
|
|
21 |
import org.springframework.context.annotation.*;
|
|
|
22 |
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
|
|
23 |
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
|
|
24 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
|
|
|
25 |
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
| 23738 |
amit.gupta |
26 |
|
| 30307 |
amit.gupta |
27 |
import javax.sql.DataSource;
|
|
|
28 |
import java.time.LocalDate;
|
|
|
29 |
import java.time.LocalDateTime;
|
|
|
30 |
import java.time.format.DateTimeFormatter;
|
|
|
31 |
import java.time.format.DateTimeFormatterBuilder;
|
|
|
32 |
import java.util.Properties;
|
|
|
33 |
|
| 23723 |
amit.gupta |
34 |
@Configuration
|
|
|
35 |
@EnableTransactionManagement
|
| 23755 |
amit.gupta |
36 |
@ComponentScan("com.spice.profitmandi.*")
|
| 23723 |
amit.gupta |
37 |
@PropertySource("classpath:META-INF/env.properties")
|
|
|
38 |
public class DBConfig {
|
|
|
39 |
|
| 25721 |
tejbeer |
40 |
private static final Logger LOGGER = LogManager.getLogger(DBConfig.class);
|
| 23723 |
amit.gupta |
41 |
|
| 23755 |
amit.gupta |
42 |
private static final String HIBERNATE_DIALECT = "hibernate.dialect";
|
| 23723 |
amit.gupta |
43 |
|
| 23755 |
amit.gupta |
44 |
private static final String HIBERNATE_SHOW_SQL = "hibernate.show_sql";
|
| 23723 |
amit.gupta |
45 |
|
| 23755 |
amit.gupta |
46 |
private static final String HIBERNATE_FORMAT_SQL = "hibernate.format_sql";
|
| 23723 |
amit.gupta |
47 |
|
| 23755 |
amit.gupta |
48 |
private static final String HIBERNATE_JDBC_BATCH_SIZE = "hibernate.jdbc.batch_size";
|
| 23723 |
amit.gupta |
49 |
|
| 23755 |
amit.gupta |
50 |
private static final String HIBERNATE_C3P0_MIN_SIZE = "hibernate.c3p0.min_size";
|
| 23723 |
amit.gupta |
51 |
|
| 23755 |
amit.gupta |
52 |
private static final String HIBERNATE_C3P0_MAX_SIZE = "hibernate.c3p0.max_size";
|
| 23723 |
amit.gupta |
53 |
|
| 23755 |
amit.gupta |
54 |
private static final String HIBERNATE_C3P0_TIMEOUT = "hibernate.c3p0.timeout";
|
| 23723 |
amit.gupta |
55 |
|
| 23755 |
amit.gupta |
56 |
private static final String HIBERNATE_C3P0_MAX_STATEMENTS = "hibernate.c3p0.max_statements";
|
| 23723 |
amit.gupta |
57 |
|
| 23755 |
amit.gupta |
58 |
private static final String HIBERNATE_C3P0_IDLE_TEST_PERIOD = "hibernate.c3p0.idle_test_period";
|
|
|
59 |
|
| 23723 |
amit.gupta |
60 |
@Value("${hibernate.driver.class}")
|
|
|
61 |
private String hibernateDriverClass;
|
|
|
62 |
|
|
|
63 |
@Value("${hibernate.url}")
|
|
|
64 |
private String hibernateUrl;
|
|
|
65 |
|
|
|
66 |
@Value("${hibernate.user.name}")
|
|
|
67 |
private String hibernateUserName;
|
|
|
68 |
|
|
|
69 |
@Value("${hibernate.password}")
|
|
|
70 |
private String hibernatePassword;
|
|
|
71 |
|
|
|
72 |
@Value("${hibernate.dialect}")
|
|
|
73 |
private String hibernateDialect;
|
|
|
74 |
|
|
|
75 |
@Value("${hibernate.show_sql}")
|
|
|
76 |
private String hibernateShowSql;
|
|
|
77 |
|
|
|
78 |
@Value("${hibernate.format_sql}")
|
|
|
79 |
private String hibernateFormatSql;
|
|
|
80 |
|
|
|
81 |
@Value("${hibernate.jdbc.batch_size}")
|
|
|
82 |
private String hibernateBatchSize;
|
|
|
83 |
|
|
|
84 |
@Value("${hibernate.c3p0.min_size}")
|
|
|
85 |
private String hibernateMinSize;
|
|
|
86 |
|
|
|
87 |
@Value("${hibernate.c3p0.max_size}")
|
|
|
88 |
private String hibernateMaxSize;
|
|
|
89 |
|
|
|
90 |
@Value("${hibernate.c3p0.timeout}")
|
|
|
91 |
private String hibernateTimeout;
|
|
|
92 |
|
|
|
93 |
@Value("${hibernate.c3p0.max_statements}")
|
|
|
94 |
private String hibernateMaxStatements;
|
|
|
95 |
|
|
|
96 |
@Value("${hibernate.c3p0.idle_test_period}")
|
|
|
97 |
private String hibernateIdleTestPeriod;
|
| 25721 |
tejbeer |
98 |
|
| 23738 |
amit.gupta |
99 |
@Value("${mongo.host}")
|
|
|
100 |
private String mongoHost;
|
| 23723 |
amit.gupta |
101 |
|
| 23738 |
amit.gupta |
102 |
@Value("${content.mongo.host}")
|
|
|
103 |
private String contentMongoHost;
|
| 25721 |
tejbeer |
104 |
|
| 23898 |
amit.gupta |
105 |
@Primary
|
| 23723 |
amit.gupta |
106 |
@Bean(name = "dataSource")
|
|
|
107 |
public DataSource dataSource() {
|
|
|
108 |
DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
|
|
109 |
dataSource.setDriverClassName(hibernateDriverClass);
|
|
|
110 |
dataSource.setUrl(hibernateUrl);
|
|
|
111 |
dataSource.setUsername(hibernateUserName);
|
|
|
112 |
dataSource.setPassword(hibernatePassword);
|
| 23755 |
amit.gupta |
113 |
LOGGER.info("DataSource returned {}", dataSource);
|
| 23723 |
amit.gupta |
114 |
return dataSource;
|
|
|
115 |
}
|
| 25721 |
tejbeer |
116 |
|
| 24433 |
amit.gupta |
117 |
@Bean
|
|
|
118 |
public ObjectMapper objectMapper() {
|
| 25721 |
tejbeer |
119 |
DateTimeFormatter df = new DateTimeFormatterBuilder().parseCaseInsensitive()
|
|
|
120 |
.append(DateTimeFormatter.ISO_LOCAL_DATE).optionalStart().appendLiteral('T').optionalEnd()
|
|
|
121 |
.appendLiteral(' ').append(DateTimeFormatter.ISO_LOCAL_TIME).toFormatter();
|
|
|
122 |
DateTimeFormatter sf = new DateTimeFormatterBuilder().parseCaseInsensitive()
|
|
|
123 |
.append(DateTimeFormatter.ISO_LOCAL_DATE).appendLiteral('T').append(DateTimeFormatter.ISO_LOCAL_TIME)
|
| 24433 |
amit.gupta |
124 |
.toFormatter();
|
|
|
125 |
LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(sf);
|
|
|
126 |
LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(df);
|
| 29859 |
amit.gupta |
127 |
LocalDateSerializer serializer1 = new LocalDateSerializer(DateTimeFormatter.ISO_LOCAL_DATE);
|
|
|
128 |
LocalDateDeserializer deserializer1 = new LocalDateDeserializer(DateTimeFormatter.ISO_LOCAL_DATE);
|
| 24433 |
amit.gupta |
129 |
JavaTimeModule jtm = new JavaTimeModule();
|
|
|
130 |
jtm.addSerializer(LocalDateTime.class, serializer);
|
| 29859 |
amit.gupta |
131 |
jtm.addSerializer(LocalDate.class, serializer1);
|
| 24433 |
amit.gupta |
132 |
jtm.addDeserializer(LocalDateTime.class, deserializer);
|
| 29859 |
amit.gupta |
133 |
jtm.addDeserializer(LocalDate.class, deserializer1);
|
| 25721 |
tejbeer |
134 |
ObjectMapper mapper = new ObjectMapper().registerModule(new ParameterNamesModule())
|
|
|
135 |
.registerModule(new Jdk8Module()).registerModule(jtm); // new module, NOT JSR310Module
|
| 30307 |
amit.gupta |
136 |
LOGGER.info("ObjectMapper returned {}", objectMapper());
|
| 24433 |
amit.gupta |
137 |
return mapper;
|
|
|
138 |
}
|
| 23723 |
amit.gupta |
139 |
|
|
|
140 |
@Bean
|
|
|
141 |
public Properties getHibernateProperties() {
|
|
|
142 |
Properties dbProperties = new Properties();
|
|
|
143 |
dbProperties.put(HIBERNATE_DIALECT, hibernateDialect);
|
|
|
144 |
dbProperties.put(HIBERNATE_SHOW_SQL, hibernateShowSql);
|
|
|
145 |
dbProperties.put(HIBERNATE_FORMAT_SQL, hibernateFormatSql);
|
|
|
146 |
dbProperties.put(HIBERNATE_JDBC_BATCH_SIZE, hibernateBatchSize);
|
|
|
147 |
dbProperties.put(HIBERNATE_C3P0_MIN_SIZE, hibernateMinSize);
|
|
|
148 |
dbProperties.put(HIBERNATE_C3P0_MAX_SIZE, hibernateMaxSize);
|
|
|
149 |
dbProperties.put(HIBERNATE_C3P0_TIMEOUT, hibernateTimeout);
|
|
|
150 |
dbProperties.put(HIBERNATE_C3P0_MAX_STATEMENTS, hibernateMaxStatements);
|
|
|
151 |
dbProperties.put(HIBERNATE_C3P0_IDLE_TEST_PERIOD, hibernateIdleTestPeriod);
|
|
|
152 |
return dbProperties;
|
|
|
153 |
}
|
| 25721 |
tejbeer |
154 |
|
| 23898 |
amit.gupta |
155 |
@Primary
|
| 23723 |
amit.gupta |
156 |
@Autowired
|
|
|
157 |
@Bean(name = "sessionFactory")
|
|
|
158 |
public SessionFactory getSessionFactory(DataSource dataSource) {
|
|
|
159 |
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
|
|
|
160 |
sessionBuilder.addProperties(getHibernateProperties());
|
|
|
161 |
sessionBuilder.scanPackages("com.spice.profitmandi.dao.*");
|
| 30307 |
amit.gupta |
162 |
LOGGER.info("Session Factory returned {}", "sessionFactory");
|
| 23723 |
amit.gupta |
163 |
return sessionBuilder.buildSessionFactory();
|
|
|
164 |
}
|
| 25721 |
tejbeer |
165 |
|
| 23898 |
amit.gupta |
166 |
@Primary
|
| 23723 |
amit.gupta |
167 |
@Autowired
|
|
|
168 |
@Bean(name = "transactionManager")
|
|
|
169 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
|
|
170 |
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
|
| 30307 |
amit.gupta |
171 |
LOGGER.info("Transaction Manager returned {}", transactionManager);
|
| 23723 |
amit.gupta |
172 |
return transactionManager;
|
|
|
173 |
}
|
| 25721 |
tejbeer |
174 |
|
| 23738 |
amit.gupta |
175 |
@Bean
|
|
|
176 |
public Mongo mongoClient(SessionFactory sessionFactory) {
|
|
|
177 |
return new Mongo(mongoHost, contentMongoHost);
|
|
|
178 |
}
|
| 25721 |
tejbeer |
179 |
|
|
|
180 |
@Bean(name = "gson")
|
|
|
181 |
public Gson gson() {
|
| 27999 |
amit.gupta |
182 |
Gson gson = new GsonBuilder().serializeNulls()
|
| 29838 |
tejbeer |
183 |
.registerTypeAdapter(LocalDate.class, new LocalDateJsonConverter())
|
| 25721 |
tejbeer |
184 |
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeJsonConverter()).create();
|
| 30307 |
amit.gupta |
185 |
LOGGER.info("Gson returned {}", gson);
|
| 25721 |
tejbeer |
186 |
return gson;
|
|
|
187 |
|
|
|
188 |
}
|
| 23723 |
amit.gupta |
189 |
}
|