ee98fc298db11cab8c590e4823a0e9bb1c23beb5
[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 import java.util.Collection;
31 import java.util.List;
32 import java.util.Properties;
33 import javax.persistence.EntityManager;
34 import javax.persistence.EntityManagerFactory;
35 import javax.persistence.Persistence;
36 import javax.persistence.TypedQuery;
37 import org.onap.policy.common.im.jpa.ResourceRegistrationEntity;
38 import org.onap.policy.common.im.jpa.StateManagementEntity;
39 import org.onap.policy.common.utils.jpa.EntityTransCloser;
40
41 public class DatabaseAccessServiceImpl implements DatabaseAccessService {
42
43     private final EntityManagerFactory entityManagerFactory;
44     private EntityManager entityManager;
45
46     public DatabaseAccessServiceImpl(final String persistenceUnitName, final Properties properties) {
47         this.entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName, properties);
48         this.entityManager = entityManagerFactory.createEntityManager();
49     }
50     
51     public DatabaseAccessServiceImpl(final EntityManagerFactory entityManagerFactory) {
52         this.entityManagerFactory = entityManagerFactory;
53         this.entityManager = entityManagerFactory.createEntityManager();
54     }
55
56     @Override
57     public <T> List<T> execute(final Class<T> clazz, final String query, final String paramName,
58             final String paramValue) {
59         final TypedQuery<T> typedQuery = entityManager.createQuery(query, clazz);
60         typedQuery.setParameter(paramName, paramValue);
61         return typedQuery.getResultList();
62
63     }
64
65     @Override
66     public <T> List<T> execute(final Class<T> clazz, final String query) {
67         final TypedQuery<T> typedQuery = entityManager.createQuery(query, clazz);
68         return typedQuery.getResultList();
69     }
70
71     @Override
72     public List<StateManagementEntity> getStateManagementEntities(final String rOption, final String sOption) {
73         if (rOption != null) {
74             final String query = STATE_MANAGEMENT_QUERY + WHERE_S_RESOURCE_NAME + RESOURCE_NAME;
75             return execute(StateManagementEntity.class, query, RESOURCE_NAME, rOption);
76         } else if (sOption != null) {
77             return execute(StateManagementEntity.class, STATE_MANAGEMENT_QUERY);
78         }
79         return execute(StateManagementEntity.class, STATE_MANAGEMENT_QUERY);
80
81     }
82
83     @Override
84     public List<ResourceRegistrationEntity> getResourceRegistrationEntities(final String rOption,
85             final String sOption) {
86         if (rOption != null) {
87             final String query = RESOURCE_REGISTRATION_QUERY + WHERE_R_RESOURCE_NAME + RESOURCE_NAME;
88             return execute(ResourceRegistrationEntity.class, query, RESOURCE_NAME, rOption);
89         } else if (sOption != null) {
90             final String query = RESOURCE_REGISTRATION_QUERY + WHERE_R_SITE_NAME + SITE_NAME;
91             return execute(ResourceRegistrationEntity.class, query, SITE_NAME, sOption);
92         }
93         return execute(ResourceRegistrationEntity.class, RESOURCE_REGISTRATION_QUERY);
94     }
95
96     @Override
97     public <T> void persist(final Collection<T> entities) {
98         try (final EntityTransCloser et = new EntityTransCloser(entityManager.getTransaction())) {
99             for (final T entity : entities) {
100                 entityManager.persist(entity);
101             }
102             entityManager.flush();
103             et.commit();
104         }
105     }
106
107     @Override
108     public <T> void refreshEntity(final T enity) {
109         entityManager.refresh(enity);
110
111     }
112
113     @Override
114     public void close() {
115         if (entityManager.isOpen()) {
116             entityManager.close();
117         }
118     }
119
120 }