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