2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.basicmodel.dao.impl;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.Persistence;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
37 import org.onap.policy.apex.model.basicmodel.dao.ApexDao;
38 import org.onap.policy.apex.model.basicmodel.dao.DaoParameters;
39 import org.slf4j.ext.XLogger;
40 import org.slf4j.ext.XLoggerFactory;
43 * The Class DefaultApexDao is an JPA implementation of the {@link ApexDao} class for Apex concepts ({@link AxConcept}).
44 * It uses the default JPA implementation in the javax {@link Persistence} class.
47 * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
49 public class DefaultApexDao implements ApexDao {
50 private static final XLogger LOGGER = XLoggerFactory.getXLogger(DefaultApexDao.class);
52 private static final String SELECT_C_FROM = "SELECT c FROM ";
53 private static final String AND_C_KEY_LOCAL_NAME = "' AND c.key.localName='";
54 private static final String AND_C_KEY_PARENT_KEY_VERSION = "' AND c.key.parentKeyVersion='";
55 private static final String C_WHERE_C_KEY_PARENT_KEY_NAME = " c WHERE c.key.parentKeyName='";
56 private static final String AND_C_KEY_VERSION = "' AND c.key.version='";
57 private static final String C_WHERE_C_KEY_NAME = " c WHERE c.key.name='";
58 private static final String DELETE_FROM = "DELETE FROM ";
60 // Entity manager for JPA
61 private EntityManagerFactory emf = null;
67 public void init(final DaoParameters daoParameters) throws ApexException {
68 if (daoParameters == null || daoParameters.getPersistenceUnit() == null) {
69 LOGGER.error("Apex persistence unit parameter not set");
70 throw new ApexException("Apex persistence unit parameter not set");
73 LOGGER.debug("Creating Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" . . .");
75 emf = Persistence.createEntityManagerFactory(daoParameters.getPersistenceUnit(),
76 daoParameters.getJdbcProperties());
77 } catch (final Exception e) {
78 LOGGER.warn("Creation of Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed", e);
79 throw new ApexException(
80 "Creation of Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed", e);
82 LOGGER.debug("Created Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\"");
86 * Gets the entity manager for this DAO.
88 * @return the entity manager
90 protected final synchronized EntityManager getEntityManager() {
92 LOGGER.warn("Apex DAO has not been initialized");
93 throw new ApexRuntimeException("Apex DAO has not been initialized");
96 return emf.createEntityManager();
103 public final void close() {
113 public <T extends AxConcept> void create(final T obj) {
117 final EntityManager mg = getEntityManager();
119 mg.getTransaction().begin();
121 mg.getTransaction().commit();
131 public <T extends AxConcept> void delete(final T obj) {
135 final EntityManager mg = getEntityManager();
137 mg.getTransaction().begin();
138 mg.remove(mg.contains(obj) ? obj : mg.merge(obj));
139 mg.getTransaction().commit();
149 public <T extends AxConcept> void delete(final Class<T> someClass, final AxArtifactKey key) {
153 final EntityManager mg = getEntityManager();
155 mg.getTransaction().begin();
156 mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME + key.getName()
157 + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).executeUpdate();
158 mg.getTransaction().commit();
168 public <T extends AxConcept> void delete(final Class<T> someClass, final AxReferenceKey key) {
172 final EntityManager mg = getEntityManager();
174 mg.getTransaction().begin();
175 mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
176 + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion()
177 + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).executeUpdate();
178 mg.getTransaction().commit();
188 public <T extends AxConcept> void createCollection(final Collection<T> objs) {
189 if (objs == null || objs.isEmpty()) {
192 final EntityManager mg = getEntityManager();
194 mg.getTransaction().begin();
195 for (final T t : objs) {
198 mg.getTransaction().commit();
208 public <T extends AxConcept> void deleteCollection(final Collection<T> objs) {
209 if (objs == null || objs.isEmpty()) {
212 final EntityManager mg = getEntityManager();
214 mg.getTransaction().begin();
215 for (final T t : objs) {
216 mg.remove(mg.contains(t) ? t : mg.merge(t));
218 mg.getTransaction().commit();
228 public <T extends AxConcept> int deleteByArtifactKey(final Class<T> someClass,
229 final Collection<AxArtifactKey> keys) {
230 if (keys == null || keys.isEmpty()) {
233 int deletedCount = 0;
234 final EntityManager mg = getEntityManager();
236 mg.getTransaction().begin();
237 for (final AxArtifactKey key : keys) {
238 deletedCount += mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME
239 + key.getName() + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).executeUpdate();
241 mg.getTransaction().commit();
252 public <T extends AxConcept> int deleteByReferenceKey(final Class<T> someClass,
253 final Collection<AxReferenceKey> keys) {
254 if (keys == null || keys.isEmpty()) {
257 int deletedCount = 0;
258 final EntityManager mg = getEntityManager();
260 mg.getTransaction().begin();
261 for (final AxReferenceKey key : keys) {
264 DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
265 + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION
266 + key.getParentKeyVersion() + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'",
267 someClass).executeUpdate();
269 mg.getTransaction().commit();
280 public <T extends AxConcept> void deleteAll(final Class<T> someClass) {
281 final EntityManager mg = getEntityManager();
283 mg.getTransaction().begin();
284 mg.createQuery(DELETE_FROM + someClass.getSimpleName() + " c ", someClass).executeUpdate();
285 mg.getTransaction().commit();
295 public <T extends AxConcept> T get(final Class<T> someClass, final AxArtifactKey key) {
296 if (someClass == null) {
299 final EntityManager mg = getEntityManager();
301 final T t = mg.find(someClass, key);
303 // This clone is created to force the JPA DAO to recurse down through the object
305 final T clonedT = someClass.getDeclaredConstructor().newInstance();
308 } catch (final Exception e) {
309 LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e);
324 public <T extends AxConcept> T get(final Class<T> someClass, final AxReferenceKey key) {
325 if (someClass == null) {
328 final EntityManager mg = getEntityManager();
330 final T t = mg.find(someClass, key);
333 final T clonedT = someClass.getDeclaredConstructor().newInstance();
336 } catch (final Exception e) {
337 LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e);
352 public <T extends AxConcept> List<T> getAll(final Class<T> someClass) {
353 if (someClass == null) {
354 return Collections.emptyList();
356 final EntityManager mg = getEntityManager();
358 return mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + " c", someClass).getResultList();
368 public <T extends AxConcept> List<T> getAll(final Class<T> someClass, final AxArtifactKey parentKey) {
369 if (someClass == null) {
370 return Collections.emptyList();
372 final EntityManager mg = getEntityManager();
376 SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
377 + parentKey.getName() + AND_C_KEY_PARENT_KEY_VERSION + parentKey.getVersion() + "'",
389 public <T extends AxConcept> T getArtifact(final Class<T> someClass, final AxArtifactKey key) {
390 if (someClass == null || key == null) {
393 final EntityManager mg = getEntityManager();
396 ret = mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME + key.getName()
397 + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).getResultList();
401 if (ret == null || ret.isEmpty()) {
404 if (ret.size() > 1) {
405 throw new IllegalArgumentException("More than one result was returned for search for " + someClass
406 + " with key " + key.getId() + ": " + ret);
415 public <T extends AxConcept> T getArtifact(final Class<T> someClass, final AxReferenceKey key) {
416 if (someClass == null || key == null) {
419 final EntityManager mg = getEntityManager();
422 ret = mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
423 + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion()
424 + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).getResultList();
428 if (ret == null || ret.isEmpty()) {
431 if (ret.size() > 1) {
432 throw new IllegalArgumentException("More than one result was returned for search for " + someClass
433 + " with key " + key.getId() + ": " + ret);
442 public <T extends AxConcept> T update(final T obj) {
443 final EntityManager mg = getEntityManager();
446 mg.getTransaction().begin();
449 mg.getTransaction().commit();
460 public <T extends AxConcept> long size(final Class<T> someClass) {
461 if (someClass == null) {
464 final EntityManager mg = getEntityManager();
467 size = mg.createQuery("SELECT COUNT(c) FROM " + someClass.getSimpleName() + " c", Long.class)