From 09e2bc57b947fb71c4dde9a8c0ded52f695abda1 Mon Sep 17 00:00:00 2001 From: "adheli.tavares" Date: Tue, 11 May 2021 14:35:20 +0100 Subject: Handling Policy deploy/undeploy audit models Added a new Entity to store information when a Pdp Policy is deployed or undeployed and its create/retrieve methods. Change-Id: I35b3608c878bbfbbee0a99a124d100a48fe08131 Issue-ID: POLICY-2899 Signed-off-by: adheli.tavares --- .../java/org/onap/policy/models/dao/PfDao.java | 11 +++++++ .../onap/policy/models/dao/impl/DefaultPfDao.java | 35 +++++++++++++++++----- 2 files changed, 39 insertions(+), 7 deletions(-) (limited to 'models-dao/src/main/java') diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java index 844759367..378ae59b6 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java @@ -251,6 +251,17 @@ public interface PfDao { */ List getAll(Class someClass, PfConceptKey parentKey); + /** + * Get all the objects in the database of a given type. + * + * @param the type of the objects to get, a subclass of {@link PfConcept} + * @param someClass the class of the objects to get, a subclass of {@link PfConcept} + * @param orderBy field from class to order results by + * @param numRecords number of records to be retrieved + * @return the objects or null if no objects were retrieved + */ + List getAll(Class someClass, String orderBy, Integer numRecords); + /** * Get all the objects in the database of a given type. * diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java index d1e32935c..009563078 100644 --- a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java +++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java @@ -32,6 +32,7 @@ import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; import javax.persistence.TypedQuery; import javax.ws.rs.core.Response; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfGeneratedIdKey; @@ -72,6 +73,7 @@ public class DefaultPfDao implements PfDao { private static final String WHERE = " WHERE "; private static final String AND = " AND "; + private static final String ORDER_BY = " ORDER BY c."; private static final String NAME_FILTER = "c.key.name = :name"; private static final String VERSION_FILTER = "c.key.version = :version"; @@ -395,9 +397,8 @@ public class DefaultPfDao implements PfDao { try { PfFilter timeStampFilter = new PfFilterFactory().createFilter(someClass); - filterQueryString = timeStampFilter.addFilter(filterQueryString, - name, startTime, endTime, filterMap, sortOrder, getRecordNum); - + filterQueryString = timeStampFilter.addFilter(filterQueryString, name, startTime, endTime, filterMap, + sortOrder, getRecordNum); TypedQuery query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass); @@ -427,7 +428,7 @@ public class DefaultPfDao implements PfDao { LOGGER.debug("filterQueryString is \"{}\"", filterQueryString); return query.getResultList(); - } finally { + } finally { mg.close(); } } @@ -504,6 +505,27 @@ public class DefaultPfDao implements PfDao { } } + @Override + public List getAll(Class someClass, String orderBy, Integer numRecords) { + + if (someClass == null) { + return Collections.emptyList(); + } + final var mg = getEntityManager(); + try { + String query = setQueryTable(SELECT_FROM_TABLE, someClass); + + if (StringUtils.isNotBlank(orderBy)) { + query = query.concat(ORDER_BY).concat(orderBy); + } + + return mg.createQuery(query, someClass).setMaxResults(numRecords) + .getResultList(); + } finally { + mg.close(); + } + } + @Override public List getAllVersionsByParent(final Class someClass, final String parentKeyName) { if (someClass == null || parentKeyName == null) { @@ -539,9 +561,8 @@ public class DefaultPfDao implements PfDao { } @Override - public List getByTimestamp(final Class someClass, - final PfGeneratedIdKey key, - final Instant timeStamp) { + public List getByTimestamp(final Class someClass, final PfGeneratedIdKey key, + final Instant timeStamp) { if (someClass == null || key == null || timeStamp == null) { return Collections.emptyList(); } -- cgit 1.2.3-korg