2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.common.sitemanager.data.service;
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;
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.Properties;
35 import javax.persistence.EntityManager;
36 import javax.persistence.EntityManagerFactory;
37 import javax.persistence.Persistence;
38 import javax.persistence.TypedQuery;
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;
44 public class DatabaseAccessServiceImpl implements DatabaseAccessService {
46 private final EntityManagerFactory entityManagerFactory;
47 private EntityManager entityManager;
49 public DatabaseAccessServiceImpl(final String persistenceUnitName, final Properties properties) {
50 this.entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnitName, properties);
51 this.entityManager = entityManagerFactory.createEntityManager();
54 public DatabaseAccessServiceImpl(final EntityManagerFactory entityManagerFactory) {
55 this.entityManagerFactory = entityManagerFactory;
56 this.entityManager = entityManagerFactory.createEntityManager();
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();
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();
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);
83 return execute(StateManagementEntity.class, STATE_MANAGEMENT_QUERY);
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);
97 return execute(ResourceRegistrationEntity.class, RESOURCE_REGISTRATION_QUERY);
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);
106 entityManager.flush();
112 public <T> void refreshEntity(final T enity) {
113 entityManager.refresh(enity);
118 public void close() {
119 if (entityManager.isOpen()) {
120 entityManager.close();