2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.basicmodel.dao.impl;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
27 import javax.persistence.EntityManager;
28 import javax.persistence.EntityManagerFactory;
29 import javax.persistence.Persistence;
31 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
36 import org.onap.policy.apex.model.basicmodel.dao.ApexDao;
37 import org.onap.policy.apex.model.basicmodel.dao.DaoParameters;
38 import org.slf4j.ext.XLogger;
39 import org.slf4j.ext.XLoggerFactory;
42 * The Class DefaultApexDao is an JPA implementation of the {@link ApexDao} class for Apex concepts ({@link AxConcept}).
43 * It uses the default JPA implementation in the javax {@link Persistence} class.
46 * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
48 public class DefaultApexDao implements ApexDao {
49 private static final XLogger LOGGER = XLoggerFactory.getXLogger(DefaultApexDao.class);
51 private static final String SELECT_C_FROM = "SELECT c FROM ";
52 private static final String AND_C_KEY_LOCAL_NAME = "' AND c.key.localName='";
53 private static final String AND_C_KEY_PARENT_KEY_VERSION = "' AND c.key.parentKeyVersion='";
54 private static final String C_WHERE_C_KEY_PARENT_KEY_NAME = " c WHERE c.key.parentKeyName='";
55 private static final String AND_C_KEY_VERSION = "' AND c.key.version='";
56 private static final String C_WHERE_C_KEY_NAME = " c WHERE c.key.name='";
57 private static final String DELETE_FROM = "DELETE FROM ";
59 // Entity manager for JPA
60 private EntityManagerFactory emf = null;
66 public void init(final DaoParameters daoParameters) throws ApexException {
67 if (daoParameters == null || daoParameters.getPersistenceUnit() == null) {
68 LOGGER.error("Apex persistence unit parameter not set");
69 throw new ApexException("Apex persistence unit parameter not set");
72 LOGGER.debug("Creating Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" . . .");
74 emf = Persistence.createEntityManagerFactory(daoParameters.getPersistenceUnit(),
75 daoParameters.getJdbcProperties());
76 } catch (final Exception e) {
77 LOGGER.warn("Creation of Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed", e);
78 throw new ApexException(
79 "Creation of Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed",
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) {
239 .createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME
240 + key.getName() + AND_C_KEY_VERSION + key.getVersion() + "'", someClass)
243 mg.getTransaction().commit();
254 public <T extends AxConcept> int deleteByReferenceKey(final Class<T> someClass,
255 final Collection<AxReferenceKey> keys) {
256 if (keys == null || keys.isEmpty()) {
259 int deletedCount = 0;
260 final EntityManager mg = getEntityManager();
262 mg.getTransaction().begin();
263 for (final AxReferenceKey key : keys) {
264 deletedCount += mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
265 + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion()
266 + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).executeUpdate();
268 mg.getTransaction().commit();
279 public <T extends AxConcept> void deleteAll(final Class<T> someClass) {
280 final EntityManager mg = getEntityManager();
282 mg.getTransaction().begin();
283 mg.createQuery(DELETE_FROM + someClass.getSimpleName() + " c ", someClass).executeUpdate();
284 mg.getTransaction().commit();
294 public <T extends AxConcept> T get(final Class<T> someClass, final AxArtifactKey key) {
295 if (someClass == null) {
298 final EntityManager mg = getEntityManager();
300 final T t = mg.find(someClass, key);
302 // This clone is created to force the JPA DAO to recurse down through the object
304 final T clonedT = someClass.newInstance();
307 } catch (final Exception e) {
308 LOGGER.warn("Could not clone object of class \"" + someClass.getCanonicalName() + "\"", e);
323 public <T extends AxConcept> T get(final Class<T> someClass, final AxReferenceKey key) {
324 if (someClass == null) {
327 final EntityManager mg = getEntityManager();
329 final T t = mg.find(someClass, key);
332 final T clonedT = someClass.newInstance();
335 } catch (final Exception e) {
336 LOGGER.warn("Could not clone object of class \"" + someClass.getCanonicalName() + "\"", e);
351 public <T extends AxConcept> List<T> getAll(final Class<T> someClass) {
352 if (someClass == null) {
353 return Collections.emptyList();
355 final EntityManager mg = getEntityManager();
357 return mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + " c", someClass).getResultList();
367 public <T extends AxConcept> List<T> getAll(final Class<T> someClass, final AxArtifactKey parentKey) {
368 if (someClass == null) {
369 return Collections.emptyList();
371 final EntityManager mg = getEntityManager();
373 return mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
374 + parentKey.getName() + AND_C_KEY_PARENT_KEY_VERSION + parentKey.getVersion() + "'",
375 someClass).getResultList();
385 public <T extends AxConcept> T getArtifact(final Class<T> someClass, final AxArtifactKey key) {
386 if (someClass == null || key == null) {
389 final EntityManager mg = getEntityManager();
392 ret = mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME + key.getName()
393 + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).getResultList();
397 if (ret == null || ret.isEmpty()) {
400 if (ret.size() > 1) {
401 throw new IllegalArgumentException("More than one result was returned for search for " + someClass
402 + " with key " + key.getId() + ": " + ret);
411 public <T extends AxConcept> T getArtifact(final Class<T> someClass, final AxReferenceKey key) {
412 if (someClass == null || key == null) {
415 final EntityManager mg = getEntityManager();
418 ret = mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
419 + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion()
420 + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).getResultList();
424 if (ret == null || ret.isEmpty()) {
427 if (ret.size() > 1) {
428 throw new IllegalArgumentException("More than one result was returned for search for " + someClass
429 + " with key " + key.getId() + ": " + ret);
438 public <T extends AxConcept> T update(final T obj) {
439 final EntityManager mg = getEntityManager();
442 mg.getTransaction().begin();
445 mg.getTransaction().commit();
456 public <T extends AxConcept> long size(final Class<T> someClass) {
457 if (someClass == null) {
460 final EntityManager mg = getEntityManager();
463 size = mg.createQuery("SELECT COUNT(c) FROM " + someClass.getSimpleName() + " c", Long.class)