Formatting Code base with ktlint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / db-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / db / primary / PrimaryDatabaseConfiguration.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.db.primary
18
19 import org.onap.ccsdk.cds.blueprintsprocessor.db.PrimaryDataSourceProperties
20 import org.slf4j.LoggerFactory
21 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
22 import org.springframework.context.annotation.Bean
23 import org.springframework.context.annotation.ComponentScan
24 import org.springframework.context.annotation.Configuration
25 import org.springframework.data.jpa.repository.config.EnableJpaAuditing
26 import org.springframework.data.jpa.repository.config.EnableJpaRepositories
27 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
28 import org.springframework.jdbc.datasource.DriverManagerDataSource
29 import org.springframework.orm.jpa.JpaTransactionManager
30 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
31 import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter
32 import org.springframework.transaction.PlatformTransactionManager
33 import java.util.HashMap
34 import javax.sql.DataSource
35
36 @Configuration
37 @ConditionalOnProperty(
38     name = ["blueprintsprocessor.db.primary.defaultConfig"], havingValue = "true",
39     matchIfMissing = true
40 )
41 @ComponentScan
42 @EnableJpaRepositories(
43     basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor.*",
44         "org.onap.ccsdk.cds.controllerblueprints.*"],
45     entityManagerFactoryRef = "primaryEntityManager",
46     transactionManagerRef = "primaryTransactionManager"
47 )
48 @EnableJpaAuditing
49 open class PrimaryDatabaseConfiguration(private val primaryDataSourceProperties: PrimaryDataSourceProperties) {
50
51     private val log = LoggerFactory.getLogger(PrimaryDatabaseConfiguration::class.java)!!
52
53     @Bean("primaryEntityManager")
54     open fun primaryEntityManager(): LocalContainerEntityManagerFactoryBean {
55         val em = LocalContainerEntityManagerFactoryBean()
56         em.dataSource = primaryDataSource()
57         em.setPackagesToScan(
58             "org.onap.ccsdk.cds.blueprintsprocessor.*",
59             "org.onap.ccsdk.cds.controllerblueprints.*"
60         )
61         em.jpaVendorAdapter = HibernateJpaVendorAdapter()
62         val properties = HashMap<String, Any>()
63         properties["hibernate.hbm2ddl.auto"] = primaryDataSourceProperties.hibernateHbm2ddlAuto
64         properties["hibernate.dialect"] = primaryDataSourceProperties.hibernateDialect
65         em.jpaPropertyMap = properties
66         return em
67     }
68
69     @Bean("primaryDataSource")
70     open fun primaryDataSource(): DataSource {
71         val dataSource = DriverManagerDataSource()
72         dataSource.setDriverClassName(primaryDataSourceProperties.driverClassName)
73         dataSource.url = primaryDataSourceProperties.url
74         dataSource.username = primaryDataSourceProperties.username
75         dataSource.password = primaryDataSourceProperties.password
76         return dataSource
77     }
78
79     @Bean("primaryTransactionManager")
80     open fun primaryTransactionManager(): PlatformTransactionManager {
81         val transactionManager = JpaTransactionManager()
82         transactionManager.entityManagerFactory = primaryEntityManager().getObject()
83         log.info("Initialised Primary Transaction Manager for url ${primaryDataSourceProperties.url}")
84         return transactionManager
85     }
86
87     @Bean("primaryNamedParameterJdbcTemplate")
88     open fun primaryNamedParameterJdbcTemplate(primaryDataSource: DataSource): NamedParameterJdbcTemplate {
89         return NamedParameterJdbcTemplate(primaryDataSource)
90     }
91 }