290b74e3add5d72a3b779aed69f05fa9bd5ea5bd
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.config;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import javax.persistence.EntityManagerFactory;
26 import javax.sql.DataSource;
27 import org.eclipse.persistence.config.BatchWriting;
28 import org.eclipse.persistence.config.PersistenceUnitProperties;
29 import org.eclipse.persistence.logging.SessionLog;
30 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterGroup;
31 import org.springframework.beans.factory.ObjectProvider;
32 import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration;
33 import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
34 import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
35 import org.springframework.context.annotation.Bean;
36 import org.springframework.context.annotation.Configuration;
37 import org.springframework.context.annotation.Primary;
38 import org.springframework.orm.jpa.JpaTransactionManager;
39 import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
40 import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
41 import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter;
42 import org.springframework.transaction.PlatformTransactionManager;
43 import org.springframework.transaction.annotation.EnableTransactionManagement;
44 import org.springframework.transaction.jta.JtaTransactionManager;
45
46 @Configuration
47 @EnableTransactionManagement
48 public class JpaConfiguration extends JpaBaseConfiguration {
49
50     protected JpaConfiguration(DataSource dataSource, JpaProperties properties,
51             ObjectProvider<JtaTransactionManager> jtaTransactionManager) {
52         super(dataSource, properties, jtaTransactionManager);
53     }
54
55     @Override
56     protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
57         return new EclipseLinkJpaVendorAdapter();
58     }
59
60     @Override
61     protected Map<String, Object> getVendorProperties() {
62         return Map.of(PersistenceUnitProperties.BATCH_WRITING, BatchWriting.JDBC);
63     }
64
65     /**
66      * Create EntityManagerFactory.
67      *
68      * @param builder EntityManagerFactoryBuilder
69      * @param dataSource DataSource
70      * @return LocalContainerEntityManagerFactoryBean
71      */
72     @Bean("entityManagerFactory")
73     public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactory(
74             EntityManagerFactoryBuilder builder, DataSource dataSource,
75             ClRuntimeParameterGroup clRuntimeParameterGroup) {
76
77         return builder.dataSource(dataSource)
78                 .persistenceUnit(clRuntimeParameterGroup.getDatabaseProviderParameters().getPersistenceUnit())
79                 .properties(initJpaProperties()).build();
80     }
81
82     /**
83      * create a PlatformTransactionManager.
84      *
85      * @param emf EntityManagerFactory
86      * @return PlatformTransactionManager
87      */
88     @Bean
89     public static PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
90         final var transactionManager = new JpaTransactionManager();
91         transactionManager.setEntityManagerFactory(emf);
92         return transactionManager;
93     }
94
95     /**
96      * create Jpa Properties.
97      *
98      * @return JpaProperties
99      */
100     @Bean
101     @Primary
102     public static JpaProperties properties(ClRuntimeParameterGroup clRuntimeParameterGroup) {
103         final var jpaProperties = new JpaProperties();
104         jpaProperties.setShowSql(clRuntimeParameterGroup.isShowSql());
105         jpaProperties.setDatabasePlatform(clRuntimeParameterGroup.getDatabasePlatform());
106         return jpaProperties;
107     }
108
109     private static Map<String, ?> initJpaProperties() {
110         final Map<String, Object> ret = new HashMap<>();
111         // Add any JpaProperty you are interested in and is supported by your Database and JPA implementation
112         ret.put(PersistenceUnitProperties.BATCH_WRITING, BatchWriting.JDBC);
113         ret.put(PersistenceUnitProperties.LOGGING_LEVEL, SessionLog.INFO_LABEL);
114         ret.put(PersistenceUnitProperties.WEAVING, "false");
115         ret.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_ONLY);
116         ret.put(PersistenceUnitProperties.DDL_GENERATION_MODE, PersistenceUnitProperties.DDL_DATABASE_GENERATION);
117         return ret;
118     }
119 }