| 23723 |
amit.gupta |
1 |
package com.smartdukaan.cron.config;
|
|
|
2 |
|
| 24433 |
amit.gupta |
3 |
import java.time.LocalDateTime;
|
|
|
4 |
import java.time.format.DateTimeFormatter;
|
|
|
5 |
import java.time.format.DateTimeFormatterBuilder;
|
| 23723 |
amit.gupta |
6 |
import java.util.Properties;
|
|
|
7 |
|
|
|
8 |
import javax.sql.DataSource;
|
|
|
9 |
|
| 23755 |
amit.gupta |
10 |
import org.apache.logging.log4j.LogManager;
|
|
|
11 |
import org.apache.logging.log4j.Logger;
|
| 23723 |
amit.gupta |
12 |
import org.hibernate.SessionFactory;
|
|
|
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
14 |
import org.springframework.beans.factory.annotation.Value;
|
|
|
15 |
import org.springframework.context.annotation.Bean;
|
| 23755 |
amit.gupta |
16 |
import org.springframework.context.annotation.ComponentScan;
|
| 23723 |
amit.gupta |
17 |
import org.springframework.context.annotation.Configuration;
|
| 23898 |
amit.gupta |
18 |
import org.springframework.context.annotation.Primary;
|
| 23723 |
amit.gupta |
19 |
import org.springframework.context.annotation.PropertySource;
|
|
|
20 |
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
|
|
21 |
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
|
|
22 |
import org.springframework.orm.hibernate5.LocalSessionFactoryBuilder;
|
|
|
23 |
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
24 |
|
| 24433 |
amit.gupta |
25 |
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
26 |
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
|
|
27 |
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
|
28 |
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
|
|
29 |
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
|
|
30 |
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
|
| 23738 |
amit.gupta |
31 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
|
|
32 |
|
| 23723 |
amit.gupta |
33 |
@Configuration
|
|
|
34 |
@EnableTransactionManagement
|
| 23755 |
amit.gupta |
35 |
@ComponentScan("com.spice.profitmandi.*")
|
| 23723 |
amit.gupta |
36 |
@PropertySource("classpath:META-INF/env.properties")
|
|
|
37 |
public class DBConfig {
|
|
|
38 |
|
| 23755 |
amit.gupta |
39 |
private static final Logger LOGGER=LogManager.getLogger(DBConfig.class);
|
| 23723 |
amit.gupta |
40 |
|
| 23755 |
amit.gupta |
41 |
|
|
|
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;
|
| 23738 |
amit.gupta |
98 |
|
|
|
99 |
@Value("${mongo.host}")
|
|
|
100 |
private String mongoHost;
|
| 23723 |
amit.gupta |
101 |
|
| 23738 |
amit.gupta |
102 |
@Value("${content.mongo.host}")
|
|
|
103 |
private String contentMongoHost;
|
|
|
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 |
}
|
| 24433 |
amit.gupta |
116 |
|
|
|
117 |
@Bean
|
|
|
118 |
public ObjectMapper objectMapper() {
|
|
|
119 |
DateTimeFormatter df = new DateTimeFormatterBuilder()
|
|
|
120 |
.parseCaseInsensitive()
|
|
|
121 |
.append(DateTimeFormatter.ISO_LOCAL_DATE)
|
|
|
122 |
.optionalStart()
|
|
|
123 |
.appendLiteral('T')
|
|
|
124 |
.optionalEnd()
|
|
|
125 |
.appendLiteral(' ')
|
|
|
126 |
.append(DateTimeFormatter.ISO_LOCAL_TIME)
|
|
|
127 |
.toFormatter();
|
|
|
128 |
DateTimeFormatter sf = new DateTimeFormatterBuilder()
|
|
|
129 |
.parseCaseInsensitive()
|
|
|
130 |
.append(DateTimeFormatter.ISO_LOCAL_DATE)
|
|
|
131 |
.appendLiteral('T')
|
|
|
132 |
.append(DateTimeFormatter.ISO_LOCAL_TIME)
|
|
|
133 |
.toFormatter();
|
|
|
134 |
LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(sf);
|
|
|
135 |
LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(df);
|
|
|
136 |
JavaTimeModule jtm = new JavaTimeModule();
|
|
|
137 |
jtm.addSerializer(LocalDateTime.class, serializer);
|
|
|
138 |
jtm.addDeserializer(LocalDateTime.class, deserializer);
|
|
|
139 |
ObjectMapper mapper = new ObjectMapper()
|
|
|
140 |
.registerModule(new ParameterNamesModule())
|
|
|
141 |
.registerModule(new Jdk8Module())
|
|
|
142 |
.registerModule(jtm); // new module, NOT JSR310Module
|
|
|
143 |
return mapper;
|
|
|
144 |
}
|
| 23723 |
amit.gupta |
145 |
|
|
|
146 |
@Bean
|
|
|
147 |
public Properties getHibernateProperties() {
|
|
|
148 |
Properties dbProperties = new Properties();
|
|
|
149 |
dbProperties.put(HIBERNATE_DIALECT, hibernateDialect);
|
|
|
150 |
dbProperties.put(HIBERNATE_SHOW_SQL, hibernateShowSql);
|
|
|
151 |
dbProperties.put(HIBERNATE_FORMAT_SQL, hibernateFormatSql);
|
|
|
152 |
dbProperties.put(HIBERNATE_JDBC_BATCH_SIZE, hibernateBatchSize);
|
|
|
153 |
dbProperties.put(HIBERNATE_C3P0_MIN_SIZE, hibernateMinSize);
|
|
|
154 |
dbProperties.put(HIBERNATE_C3P0_MAX_SIZE, hibernateMaxSize);
|
|
|
155 |
dbProperties.put(HIBERNATE_C3P0_TIMEOUT, hibernateTimeout);
|
|
|
156 |
dbProperties.put(HIBERNATE_C3P0_MAX_STATEMENTS, hibernateMaxStatements);
|
|
|
157 |
dbProperties.put(HIBERNATE_C3P0_IDLE_TEST_PERIOD, hibernateIdleTestPeriod);
|
|
|
158 |
return dbProperties;
|
|
|
159 |
}
|
| 23898 |
amit.gupta |
160 |
|
|
|
161 |
@Primary
|
| 23723 |
amit.gupta |
162 |
@Autowired
|
|
|
163 |
@Bean(name = "sessionFactory")
|
|
|
164 |
public SessionFactory getSessionFactory(DataSource dataSource) {
|
|
|
165 |
LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);
|
|
|
166 |
sessionBuilder.addProperties(getHibernateProperties());
|
|
|
167 |
sessionBuilder.scanPackages("com.spice.profitmandi.dao.*");
|
|
|
168 |
return sessionBuilder.buildSessionFactory();
|
|
|
169 |
}
|
| 23898 |
amit.gupta |
170 |
|
|
|
171 |
@Primary
|
| 23723 |
amit.gupta |
172 |
@Autowired
|
|
|
173 |
@Bean(name = "transactionManager")
|
|
|
174 |
public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {
|
|
|
175 |
HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);
|
|
|
176 |
return transactionManager;
|
|
|
177 |
}
|
| 23738 |
amit.gupta |
178 |
|
|
|
179 |
@Bean
|
|
|
180 |
public Mongo mongoClient(SessionFactory sessionFactory) {
|
|
|
181 |
return new Mongo(mongoHost, contentMongoHost);
|
|
|
182 |
}
|
| 23723 |
amit.gupta |
183 |
}
|