2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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.models.dao.impl;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.List;
29 import javax.persistence.EntityManager;
30 import javax.persistence.EntityManagerFactory;
31 import javax.persistence.Persistence;
32 import javax.persistence.TypedQuery;
33 import javax.ws.rs.core.Response;
34 import org.onap.policy.models.base.PfConcept;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.base.PfModelException;
37 import org.onap.policy.models.base.PfModelRuntimeException;
38 import org.onap.policy.models.base.PfReferenceKey;
39 import org.onap.policy.models.base.PfTimestampKey;
40 import org.onap.policy.models.base.PfUtils;
41 import org.onap.policy.models.dao.DaoParameters;
42 import org.onap.policy.models.dao.PfDao;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * The Class DefaultPfDao is an JPA implementation of the {@link PfDao} class for Policy Framework concepts
48 * ({@link PfConcept}). It uses the default JPA implementation in the javax {@link Persistence} class.
50 public class DefaultPfDao implements PfDao {
51 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
54 private static final String NAME = "name";
55 private static final String VERSION = "version";
56 private static final String TIMESTAMP = "timeStamp";
57 private static final String PARENT_NAME = "parentname";
58 private static final String PARENT_VERSION = "parentversion";
59 private static final String LOCAL_NAME = "localname";
61 private static final String TABLE_TOKEN = "__TABLE__";
63 private static final String DELETE_FROM_TABLE = "DELETE FROM __TABLE__ c";
65 private static final String SELECT_FROM_TABLE = "SELECT c FROM __TABLE__ c";
67 private static final String WHERE = " WHERE ";
68 private static final String AND = " AND ";
69 private static final String ORDER = " ORDER BY ";
71 private static final String NAME_FILTER = "c.key.name = :name";
72 private static final String VERSION_FILTER = "c.key.version = :version";
73 private static final String TIMESTAMP_FILTER = "c.key.timeStamp = :timeStamp";
74 private static final String TIMESTAMP_START_FILTER = "c.key.timeStamp >= :startTime";
75 private static final String TIMESTAMP_END_FILTER = "c.key.timeStamp <= :endTime";
76 private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname";
77 private static final String PARENT_VERSION_FILTER = "c.key.parentKeyVersion = :parentversion";
78 private static final String LOCAL_NAME_FILTER = "c.key.localName = :localname";
80 private static final String CLONE_ERR_MSG = "Could not clone object of class \"{}\"";
82 private static final String DELETE_BY_CONCEPT_KEY =
83 DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER;
85 private static final String DELETE_BY_TIMESTAMP_KEY =
86 DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER + AND + TIMESTAMP_FILTER;
88 private static final String DELETE_BY_REFERENCE_KEY =
89 DELETE_FROM_TABLE + WHERE + PARENT_NAME_FILTER + AND + PARENT_VERSION_FILTER + AND + LOCAL_NAME_FILTER;
91 private static final String SELECT_ALL_FOR_PARENT =
92 SELECT_FROM_TABLE + WHERE + PARENT_NAME_FILTER + AND + PARENT_VERSION_FILTER;
94 private static final String SELECT_ALL_VERSIONS = SELECT_FROM_TABLE + WHERE + NAME_FILTER;
96 private static final String SELECT_BY_CONCEPT_KEY =
97 SELECT_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER;
99 private static final String SELECT_BY_REFERENCE_KEY =
100 SELECT_FROM_TABLE + WHERE + PARENT_NAME_FILTER + AND + PARENT_VERSION_FILTER + AND + LOCAL_NAME_FILTER;
103 // Entity manager for JPA
104 private EntityManagerFactory emf = null;
107 public void init(final DaoParameters daoParameters) throws PfModelException {
108 if (daoParameters == null || daoParameters.getPersistenceUnit() == null) {
109 LOGGER.error("Policy Framework persistence unit parameter not set");
110 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR,
111 "Policy Framework persistence unit parameter not set");
114 LOGGER.debug("Creating Policy Framework persistence unit \"{}\" . . .", daoParameters.getPersistenceUnit());
116 emf = Persistence.createEntityManagerFactory(daoParameters.getPersistenceUnit(),
117 daoParameters.getJdbcProperties());
118 } catch (final Exception ex) {
119 String errorMessage = "Creation of Policy Framework persistence unit \""
120 + daoParameters.getPersistenceUnit() + "\" failed";
121 LOGGER.warn(errorMessage, ex);
122 throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ex);
124 LOGGER.debug("Created Policy Framework persistence unit \"{}\"", daoParameters.getPersistenceUnit());
128 * Gets the entity manager for this DAO.
130 * @return the entity manager
132 protected final synchronized EntityManager getEntityManager() {
134 LOGGER.warn("Policy Framework DAO has not been initialized");
135 throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
136 "Policy Framework DAO has not been initialized");
139 return emf.createEntityManager();
143 public final void close() {
150 public <T extends PfConcept> void create(final T obj) {
154 final EntityManager mg = getEntityManager();
156 mg.getTransaction().begin();
158 mg.getTransaction().commit();
165 public <T extends PfConcept> void delete(final T obj) {
169 final EntityManager mg = getEntityManager();
171 mg.getTransaction().begin();
172 mg.remove(mg.contains(obj) ? obj : mg.merge(obj));
173 mg.getTransaction().commit();
180 public <T extends PfConcept> void delete(final Class<T> someClass, final PfConceptKey key) {
184 final EntityManager mg = getEntityManager();
187 mg.getTransaction().begin();
188 mg.createQuery(setQueryTable(DELETE_BY_CONCEPT_KEY, someClass), someClass)
189 .setParameter(NAME, key.getName())
190 .setParameter(VERSION, key.getVersion())
192 mg.getTransaction().commit();
200 public <T extends PfConcept> void delete(final Class<T> someClass, final PfReferenceKey key) {
204 final EntityManager mg = getEntityManager();
207 mg.getTransaction().begin();
208 mg.createQuery(setQueryTable(DELETE_BY_REFERENCE_KEY, someClass), someClass)
209 .setParameter(PARENT_NAME, key.getParentKeyName())
210 .setParameter(PARENT_VERSION, key.getParentKeyVersion())
211 .setParameter(LOCAL_NAME, key.getLocalName())
213 mg.getTransaction().commit();
221 public <T extends PfConcept> void delete(final Class<T> someClass, final PfTimestampKey key) {
225 final EntityManager mg = getEntityManager();
228 mg.getTransaction().begin();
229 mg.createQuery(setQueryTable(DELETE_BY_TIMESTAMP_KEY, someClass), someClass)
230 .setParameter(NAME, key.getName())
231 .setParameter(VERSION, key.getVersion())
232 .setParameter(TIMESTAMP, key.getTimeStamp())
234 mg.getTransaction().commit();
242 public <T extends PfConcept> void createCollection(final Collection<T> objs) {
243 if (objs == null || objs.isEmpty()) {
246 final EntityManager mg = getEntityManager();
248 mg.getTransaction().begin();
249 for (final T t : objs) {
252 mg.getTransaction().commit();
259 public <T extends PfConcept> void deleteCollection(final Collection<T> objs) {
260 if (objs == null || objs.isEmpty()) {
263 final EntityManager mg = getEntityManager();
265 mg.getTransaction().begin();
266 for (final T t : objs) {
267 mg.remove(mg.contains(t) ? t : mg.merge(t));
269 mg.getTransaction().commit();
276 public <T extends PfConcept> int deleteByConceptKey(final Class<T> someClass, final Collection<PfConceptKey> keys) {
277 if (keys == null || keys.isEmpty()) {
280 int deletedCount = 0;
281 final EntityManager mg = getEntityManager();
284 mg.getTransaction().begin();
285 for (final PfConceptKey key : keys) {
286 deletedCount += mg.createQuery(setQueryTable(DELETE_BY_CONCEPT_KEY, someClass), someClass)
287 .setParameter(NAME, key.getName())
288 .setParameter(VERSION, key.getVersion())
291 mg.getTransaction().commit();
300 public <T extends PfConcept> int deleteByReferenceKey(final Class<T> someClass,
301 final Collection<PfReferenceKey> keys) {
302 if (keys == null || keys.isEmpty()) {
305 int deletedCount = 0;
306 final EntityManager mg = getEntityManager();
309 mg.getTransaction().begin();
310 for (final PfReferenceKey key : keys) {
311 deletedCount += mg.createQuery(setQueryTable(DELETE_BY_REFERENCE_KEY, someClass), someClass)
312 .setParameter(PARENT_NAME, key.getParentKeyName())
313 .setParameter(PARENT_VERSION, key.getParentKeyVersion())
314 .setParameter(LOCAL_NAME, key.getLocalName())
317 mg.getTransaction().commit();
326 public <T extends PfConcept> void deleteAll(final Class<T> someClass) {
327 final EntityManager mg = getEntityManager();
329 mg.getTransaction().begin();
330 mg.createQuery(setQueryTable(DELETE_FROM_TABLE, someClass), someClass).executeUpdate();
331 mg.getTransaction().commit();
338 public <T extends PfConcept> List<T> getFiltered(final Class<T> someClass, final String name,
339 final String version) {
341 return getAll(someClass);
344 if (version == null) {
345 return getAllVersions(someClass, name);
348 T foundConcept = get(someClass, new PfConceptKey(name, version));
350 return (foundConcept == null ? Collections.emptyList() : Collections.singletonList(foundConcept));
354 public <T extends PfConcept> List<T> getFiltered(final Class<T> someClass, final String name, final String version,
355 final Date startTime, final Date endTime, final Map<String, Object> filterMap, final String sortOrder,
356 final int getRecordNum) {
357 final EntityManager mg = getEntityManager();
359 String filterQueryString = SELECT_FROM_TABLE + WHERE;
362 if (filterMap != null) {
363 StringBuilder bld = new StringBuilder(filterQueryString);
364 for (String key : filterMap.keySet()) {
365 bld.append("c." + key + "= :" + key + AND);
367 filterQueryString = bld.toString();
369 filterQueryString = addKeyFilterString(filterQueryString, name, startTime, endTime);
370 if (getRecordNum > 0) {
371 filterQueryString += ORDER + " c.key.timeStamp " + sortOrder;
373 TypedQuery<T> query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass);
375 if (filterMap != null) {
376 for (Map.Entry<String, Object> entry : filterMap.entrySet()) {
377 query.setParameter(entry.getKey(), entry.getValue());
381 query.setParameter("name", name);
383 if (startTime != null) {
384 if (endTime != null) {
385 query.setParameter("startTime", startTime);
386 query.setParameter("endTime", endTime);
388 query.setParameter("startTime", startTime);
391 if (endTime != null) {
392 query.setParameter("endTime", endTime);
395 if (getRecordNum > 0) {
396 query.setMaxResults(getRecordNum);
399 LOGGER.error("filterQueryString is \"{}\"", filterQueryString);
400 return query.getResultList();
407 public <T extends PfConcept> T get(final Class<T> someClass, final PfConceptKey key) {
408 if (someClass == null) {
411 final EntityManager mg = getEntityManager();
413 final T t = mg.find(someClass, key);
414 return checkAndReturn(someClass, t);
421 public <T extends PfConcept> T get(final Class<T> someClass, final PfReferenceKey key) {
422 if (someClass == null) {
425 final EntityManager mg = getEntityManager();
427 final T t = mg.find(someClass, key);
428 return checkAndReturn(someClass, t);
435 public <T extends PfConcept> T get(final Class<T> someClass, final PfTimestampKey key) {
436 if (someClass == null) {
439 final EntityManager mg = getEntityManager();
441 final T t = mg.find(someClass, key);
442 return checkAndReturn(someClass, t);
449 public <T extends PfConcept> List<T> getAll(final Class<T> someClass) {
450 if (someClass == null) {
451 return Collections.emptyList();
453 final EntityManager mg = getEntityManager();
455 return mg.createQuery(setQueryTable(SELECT_FROM_TABLE, someClass), someClass).getResultList();
462 public <T extends PfConcept> List<T> getAll(final Class<T> someClass, final PfConceptKey parentKey) {
463 if (someClass == null) {
464 return Collections.emptyList();
466 final EntityManager mg = getEntityManager();
469 return mg.createQuery(setQueryTable(SELECT_ALL_FOR_PARENT, someClass), someClass)
470 .setParameter(PARENT_NAME, parentKey.getName())
471 .setParameter(PARENT_VERSION, parentKey.getVersion())
480 public <T extends PfConcept> List<T> getAllVersions(final Class<T> someClass, final String conceptName) {
481 if (someClass == null || conceptName == null) {
482 return Collections.emptyList();
484 final EntityManager mg = getEntityManager();
487 return mg.createQuery(setQueryTable(SELECT_ALL_VERSIONS, someClass), someClass)
488 .setParameter(NAME, conceptName)
497 public <T extends PfConcept> T getConcept(final Class<T> someClass, final PfConceptKey key) {
498 if (someClass == null || key == null) {
501 final EntityManager mg = getEntityManager();
505 ret = mg.createQuery(setQueryTable(SELECT_BY_CONCEPT_KEY, someClass), someClass)
506 .setParameter(NAME, key.getName())
507 .setParameter(VERSION, key.getVersion())
514 return getSingleResult(someClass, key.getId(), ret);
518 public <T extends PfConcept> T getConcept(final Class<T> someClass, final PfReferenceKey key) {
519 if (someClass == null || key == null) {
522 final EntityManager mg = getEntityManager();
526 ret = mg.createQuery(setQueryTable(SELECT_BY_REFERENCE_KEY, someClass), someClass)
527 .setParameter(PARENT_NAME, key.getParentKeyName())
528 .setParameter(PARENT_VERSION, key.getParentKeyVersion())
529 .setParameter(LOCAL_NAME, key.getLocalName())
536 return getSingleResult(someClass, key.getId(), ret);
540 public <T extends PfConcept> T update(final T obj) {
541 final EntityManager mg = getEntityManager();
544 mg.getTransaction().begin();
547 mg.getTransaction().commit();
555 public <T extends PfConcept> long size(final Class<T> someClass) {
556 if (someClass == null) {
559 final EntityManager mg = getEntityManager();
562 size = mg.createQuery("SELECT COUNT(c) FROM " + someClass.getSimpleName() + " c", Long.class)
571 * Add the table to a query string.
573 * @param queryString the query string
574 * @param tableClass the class name of the table
575 * @return the updated query string
577 private <T extends PfConcept> String setQueryTable(final String queryString, final Class<T> tableClass) {
578 return queryString.replaceAll(TABLE_TOKEN, tableClass.getSimpleName());
582 * Check that a query returned one and only one entry and return that entry.
584 * @param someClass the class being searched for
585 * @param conceptName the concept name being searched for
586 * @param resultList the result list returned by the query
587 * @return the single unique result
589 private <T extends PfConcept> T getSingleResult(final Class<T> someClass, final String searchFilter, List<T> ret) {
590 if (ret == null || ret.isEmpty()) {
593 if (ret.size() > 1) {
594 throw new IllegalArgumentException("More than one result was returned query on " + someClass
595 + " with filter " + searchFilter + ": " + ret);
601 * generate filter string with the filter value in TimestampKey.
603 * @param inputFilterString current filterString generated from FilterMap
604 * @param name the pdp name the start timeStamp to filter from database, filter rule: startTime <= filteredRecord
605 * timeStamp <= endTime. null for ignore start time.
606 * @param endTime the end timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp <=
607 * endTime. null for ignore end time
608 * @return the filter string to query database
610 private String addKeyFilterString(String inputFilterString, final String name, final Date startTime,
611 final Date endTime) {
612 String filterQueryString;
614 inputFilterString += NAME_FILTER + AND;
616 if (startTime != null) {
617 if (endTime != null) {
618 filterQueryString = inputFilterString + TIMESTAMP_START_FILTER + AND + TIMESTAMP_END_FILTER;
620 filterQueryString = inputFilterString + TIMESTAMP_START_FILTER;
623 if (endTime != null) {
624 filterQueryString = inputFilterString + TIMESTAMP_END_FILTER;
626 filterQueryString = inputFilterString.substring(0, inputFilterString.length() - AND.length());
630 return filterQueryString;
634 * check the result get from database and return the object.
636 * @param <T> the type of the object to get, a subclass of {@link PfConcept}
637 * @param someClass the class of the object to get, a subclass of {@link PfConcept}
638 * @param t the object that was retrieved from the database
639 * @return the checked object or null
641 private <T extends PfConcept> T checkAndReturn(final Class<T> someClass, final T objToCheck) {
642 if (objToCheck != null) {
644 return PfUtils.makeCopy(objToCheck);
645 } catch (final Exception e) {
646 LOGGER.warn(CLONE_ERR_MSG, someClass.getName(), e);