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;
27 import javax.persistence.EntityManager;
28 import javax.persistence.EntityManagerFactory;
29 import javax.persistence.Persistence;
30 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
35 import org.onap.policy.apex.model.basicmodel.dao.ApexDao;
36 import org.onap.policy.apex.model.basicmodel.dao.DaoParameters;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
41 * The Class DefaultApexDao is an JPA implementation of the {@link ApexDao} class for Apex concepts ({@link AxConcept}).
42 * It uses the default JPA implementation in the javax {@link Persistence} class.
45 * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
47 public class DefaultApexDao implements ApexDao {
48 private static final XLogger LOGGER = XLoggerFactory.getXLogger(DefaultApexDao.class);
50 private static final String SELECT_C_FROM = "SELECT c FROM ";
51 private static final String AND_C_KEY_LOCAL_NAME = "' AND c.key.localName='";
52 private static final String AND_C_KEY_PARENT_KEY_VERSION = "' AND c.key.parentKeyVersion='";
53 private static final String C_WHERE_C_KEY_PARENT_KEY_NAME = " c WHERE c.key.parentKeyName='";
54 private static final String AND_C_KEY_VERSION = "' AND c.key.version='";
55 private static final String C_WHERE_C_KEY_NAME = " c WHERE c.key.name='";
56 private static final String DELETE_FROM = "DELETE FROM ";
58 // Entity manager for JPA
59 private EntityManagerFactory emf = null;
65 public void init(final DaoParameters daoParameters) throws ApexException {
66 if (daoParameters == null || daoParameters.getPersistenceUnit() == null) {
67 LOGGER.error("Apex persistence unit parameter not set");
68 throw new ApexException("Apex persistence unit parameter not set");
71 LOGGER.debug("Creating Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" . . .");
73 emf = Persistence.createEntityManagerFactory(daoParameters.getPersistenceUnit(),
74 daoParameters.getJdbcProperties());
75 } catch (final Exception e) {
76 LOGGER.warn("Creation of Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed", e);
77 throw new ApexException(
78 "Creation of Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed", e);
80 LOGGER.debug("Created Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\"");
84 * Gets the entity manager for this DAO.
86 * @return the entity manager
88 protected final synchronized EntityManager getEntityManager() {
90 LOGGER.warn("Apex DAO has not been initialized");
91 throw new ApexRuntimeException("Apex DAO has not been initialized");
94 return emf.createEntityManager();
101 public final void close() {
111 public <T extends AxConcept> void create(final T obj) {
115 final EntityManager mg = getEntityManager();
117 mg.getTransaction().begin();
119 mg.getTransaction().commit();
129 public <T extends AxConcept> void delete(final T obj) {
133 final EntityManager mg = getEntityManager();
135 mg.getTransaction().begin();
136 mg.remove(mg.contains(obj) ? obj : mg.merge(obj));
137 mg.getTransaction().commit();
147 public <T extends AxConcept> void delete(final Class<T> someClass, final AxArtifactKey key) {
151 final EntityManager mg = getEntityManager();
153 mg.getTransaction().begin();
154 mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME + key.getName()
155 + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).executeUpdate();
156 mg.getTransaction().commit();
166 public <T extends AxConcept> void delete(final Class<T> someClass, final AxReferenceKey key) {
170 final EntityManager mg = getEntityManager();
172 mg.getTransaction().begin();
173 mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
174 + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion()
175 + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).executeUpdate();
176 mg.getTransaction().commit();
186 public <T extends AxConcept> void createCollection(final Collection<T> objs) {
187 if (objs == null || objs.isEmpty()) {
190 final EntityManager mg = getEntityManager();
192 mg.getTransaction().begin();
193 for (final T t : objs) {
196 mg.getTransaction().commit();
206 public <T extends AxConcept> void deleteCollection(final Collection<T> objs) {
207 if (objs == null || objs.isEmpty()) {
210 final EntityManager mg = getEntityManager();
212 mg.getTransaction().begin();
213 for (final T t : objs) {
214 mg.remove(mg.contains(t) ? t : mg.merge(t));
216 mg.getTransaction().commit();
226 public <T extends AxConcept> int deleteByArtifactKey(final Class<T> someClass,
227 final Collection<AxArtifactKey> keys) {
228 if (keys == null || keys.isEmpty()) {
231 int deletedCount = 0;
232 final EntityManager mg = getEntityManager();
234 mg.getTransaction().begin();
235 for (final AxArtifactKey key : keys) {
236 deletedCount += mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME
237 + key.getName() + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).executeUpdate();
239 mg.getTransaction().commit();
250 public <T extends AxConcept> int deleteByReferenceKey(final Class<T> someClass,
251 final Collection<AxReferenceKey> keys) {
252 if (keys == null || keys.isEmpty()) {
255 int deletedCount = 0;
256 final EntityManager mg = getEntityManager();
258 mg.getTransaction().begin();
259 for (final AxReferenceKey key : keys) {
262 DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
263 + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION
264 + key.getParentKeyVersion() + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'",
265 someClass).executeUpdate();
267 mg.getTransaction().commit();
278 public <T extends AxConcept> void deleteAll(final Class<T> someClass) {
279 final EntityManager mg = getEntityManager();
281 mg.getTransaction().begin();
282 mg.createQuery(DELETE_FROM + someClass.getSimpleName() + " c ", someClass).executeUpdate();
283 mg.getTransaction().commit();
293 public <T extends AxConcept> T get(final Class<T> someClass, final AxArtifactKey key) {
294 if (someClass == null) {
297 final EntityManager mg = getEntityManager();
299 final T t = mg.find(someClass, key);
301 // This clone is created to force the JPA DAO to recurse down through the object
303 final T clonedT = someClass.getDeclaredConstructor().newInstance();
306 } catch (final Exception e) {
307 LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e);
322 public <T extends AxConcept> T get(final Class<T> someClass, final AxReferenceKey key) {
323 if (someClass == null) {
326 final EntityManager mg = getEntityManager();
328 final T t = mg.find(someClass, key);
331 final T clonedT = someClass.getDeclaredConstructor().newInstance();
334 } catch (final Exception e) {
335 LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e);
350 public <T extends AxConcept> List<T> getAll(final Class<T> someClass) {
351 if (someClass == null) {
352 return Collections.emptyList();
354 final EntityManager mg = getEntityManager();
356 return mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + " c", someClass).getResultList();
366 public <T extends AxConcept> List<T> getAll(final Class<T> someClass, final AxArtifactKey parentKey) {
367 if (someClass == null) {
368 return Collections.emptyList();
370 final EntityManager mg = getEntityManager();
374 SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
375 + parentKey.getName() + AND_C_KEY_PARENT_KEY_VERSION + parentKey.getVersion() + "'",
387 public <T extends AxConcept> T getArtifact(final Class<T> someClass, final AxArtifactKey key) {
388 if (someClass == null || key == null) {
391 final EntityManager mg = getEntityManager();
394 ret = mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME + key.getName()
395 + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).getResultList();
399 if (ret == null || ret.isEmpty()) {
402 if (ret.size() > 1) {
403 throw new IllegalArgumentException("More than one result was returned for search for " + someClass
404 + " with key " + key.getId() + ": " + ret);
413 public <T extends AxConcept> T getArtifact(final Class<T> someClass, final AxReferenceKey key) {
414 if (someClass == null || key == null) {
417 final EntityManager mg = getEntityManager();
420 ret = mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
421 + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion()
422 + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).getResultList();
426 if (ret == null || ret.isEmpty()) {
429 if (ret.size() > 1) {
430 throw new IllegalArgumentException("More than one result was returned for search for " + someClass
431 + " with key " + key.getId() + ": " + ret);
440 public <T extends AxConcept> T update(final T obj) {
441 final EntityManager mg = getEntityManager();
444 mg.getTransaction().begin();
447 mg.getTransaction().commit();
458 public <T extends AxConcept> long size(final Class<T> someClass) {
459 if (someClass == null) {
462 final EntityManager mg = getEntityManager();
465 size = mg.createQuery("SELECT COUNT(c) FROM " + someClass.getSimpleName() + " c", Long.class)