769b164bdbd9ceb54c8cbee889748d794d439e33
[policy/common.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * site-manager
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.common.sitemanager.data.service;
22
23 import static org.onap.policy.common.sitemanager.utils.Constants.RESOURCE_NAME;
24 import static org.onap.policy.common.sitemanager.utils.Constants.RESOURCE_REGISTRATION_QUERY;
25 import static org.onap.policy.common.sitemanager.utils.Constants.SITE_NAME;
26 import static org.onap.policy.common.sitemanager.utils.Constants.STATE_MANAGEMENT_QUERY;
27 import static org.onap.policy.common.sitemanager.utils.Constants.WHERE_R_RESOURCE_NAME;
28 import static org.onap.policy.common.sitemanager.utils.Constants.WHERE_R_SITE_NAME;
29 import static org.onap.policy.common.sitemanager.utils.Constants.WHERE_S_RESOURCE_NAME;
30
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.Properties;
34
35 import javax.persistence.EntityManager;
36 import javax.persistence.EntityManagerFactory;
37 import javax.persistence.Persistence;
38 import javax.persistence.TypedQuery;
39
40 import org.onap.policy.common.im.jpa.ResourceRegistrationEntity;
41 import org.onap.policy.common.im.jpa.StateManagementEntity;
42 import org.onap.policy.common.utils.jpa.EntityTransCloser;
43
44 public class DatabaseAccessServiceImpl implements DatabaseAccessService {
45
46     private final EntityManagerFactory entityManagerFactory;
47     private EntityManager entityManager;
48
49     public DatabaseAccessServiceImpl(final String persistenceUnitName, final Properties properties) {
50         this.entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName, properties);
51         this.entityManager = entityManagerFactory.createEntityManager();
52     }
53
54     public DatabaseAccessServiceImpl(final EntityManagerFactory entityManagerFactory) {
55         this.entityManagerFactory = entityManagerFactory;
56         this.entityManager = entityManagerFactory.createEntityManager();
57     }
58
59     @Override
60     public <T> List<T> execute(final Class<T> clazz, final String query, final String paramName,
61             final String paramValue) {
62         final TypedQuery<T> typedQuery = entityManager.createQuery(query, clazz);
63         typedQuery.setParameter(paramName, paramValue);
64         return typedQuery.getResultList();
65
66     }
67
68     @Override
69     public <T> List<T> execute(final Class<T> clazz, final String query) {
70         final TypedQuery<T> typedQuery = entityManager.createQuery(query, clazz);
71         return typedQuery.getResultList();
72     }
73
74     @Override
75     public List<StateManagementEntity> getStateManagementEntities(final String resourceOption,
76             final String stateOption) {
77         if (resourceOption != null) {
78             final String query = STATE_MANAGEMENT_QUERY + WHERE_S_RESOURCE_NAME + RESOURCE_NAME;
79             return execute(StateManagementEntity.class, query, RESOURCE_NAME, resourceOption);
80         } else if (stateOption != null) {
81             return execute(StateManagementEntity.class, STATE_MANAGEMENT_QUERY);
82         }
83         return execute(StateManagementEntity.class, STATE_MANAGEMENT_QUERY);
84
85     }
86
87     @Override
88     public List<ResourceRegistrationEntity> getResourceRegistrationEntities(final String resourceOption,
89             final String stateOption) {
90         if (resourceOption != null) {
91             final String query = RESOURCE_REGISTRATION_QUERY + WHERE_R_RESOURCE_NAME + RESOURCE_NAME;
92             return execute(ResourceRegistrationEntity.class, query, RESOURCE_NAME, resourceOption);
93         } else if (stateOption != null) {
94             final String query = RESOURCE_REGISTRATION_QUERY + WHERE_R_SITE_NAME + SITE_NAME;
95             return execute(ResourceRegistrationEntity.class, query, SITE_NAME, stateOption);
96         }
97         return execute(ResourceRegistrationEntity.class, RESOURCE_REGISTRATION_QUERY);
98     }
99
100     @Override
101     public <T> void persist(final Collection<T> entities) {
102         try (final EntityTransCloser et = new EntityTransCloser(entityManager.getTransaction())) {
103             for (final T entity : entities) {
104                 entityManager.persist(entity);
105             }
106             entityManager.flush();
107             et.commit();
108         }
109     }
110
111     @Override
112     public <T> void refreshEntity(final T enity) {
113         entityManager.refresh(enity);
114
115     }
116
117     @Override
118     public void close() {
119         if (entityManager.isOpen()) {
120             entityManager.close();
121         }
122     }
123
124 }