X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-dao%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Fdao%2Fimpl%2FDefaultPfDao.java;h=ad9ef1215ee4fa78072db86bd23df1276cb742d2;hb=68a60a45f27287a4a523c82ef466cbeec655f641;hp=b5739a610eeb7dbd8826d3bd50f478ede7d0c7b7;hpb=1cdad9b78056103a4ca1d1b71d996489787ed702;p=policy%2Fmodels.git 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 b5739a610..ad9ef1215 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 @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,18 +23,21 @@ package org.onap.policy.models.dao.impl; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.List; - +import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; +import javax.persistence.TypedQuery; import javax.ws.rs.core.Response; - import org.onap.policy.models.base.PfConcept; import org.onap.policy.models.base.PfConceptKey; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.base.PfReferenceKey; +import org.onap.policy.models.base.PfTimestampKey; +import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.dao.DaoParameters; import org.onap.policy.models.dao.PfDao; import org.slf4j.Logger; @@ -50,6 +53,7 @@ public class DefaultPfDao implements PfDao { // @formatter:off private static final String NAME = "name"; private static final String VERSION = "version"; + private static final String TIMESTAMP = "timeStamp"; private static final String PARENT_NAME = "parentname"; private static final String PARENT_VERSION = "parentversion"; private static final String LOCAL_NAME = "localname"; @@ -62,16 +66,25 @@ public class DefaultPfDao implements PfDao { private static final String WHERE = " WHERE "; private static final String AND = " AND "; + private static final String ORDER = " ORDER BY "; + + private static final String NAME_FILTER = "c.key.name = :name"; + private static final String VERSION_FILTER = "c.key.version = :version"; + private static final String TIMESTAMP_FILTER = "c.key.timeStamp = :timeStamp"; + private static final String TIMESTAMP_START_FILTER = "c.key.timeStamp >= :startTime"; + private static final String TIMESTAMP_END_FILTER = "c.key.timeStamp <= :endTime"; + private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname"; + private static final String PARENT_VERSION_FILTER = "c.key.parentKeyVersion = :parentversion"; + private static final String LOCAL_NAME_FILTER = "c.key.localName = :localname"; - private static final String NAME_FILTER = "c.key.name = :name"; - private static final String VERSION_FILTER = "c.key.version = :version"; - private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname"; - private static final String PARENT_VERSION_FILTER = "c.key.parentKeyVersion = :parentversion"; - private static final String LOCAL_NAME_FILTER = "c.key.localName = :localname"; + private static final String CLONE_ERR_MSG = "Could not clone object of class \"{}\""; private static final String DELETE_BY_CONCEPT_KEY = DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER; + private static final String DELETE_BY_TIMESTAMP_KEY = + DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER + AND + TIMESTAMP_FILTER; + private static final String DELETE_BY_REFERENCE_KEY = DELETE_FROM_TABLE + WHERE + PARENT_NAME_FILTER + AND + PARENT_VERSION_FILTER + AND + LOCAL_NAME_FILTER; @@ -105,7 +118,7 @@ public class DefaultPfDao implements PfDao { } catch (final Exception ex) { String errorMessage = "Creation of Policy Framework persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed"; - LOGGER.warn(errorMessage, ex); + LOGGER.warn(errorMessage); throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ex); } LOGGER.debug("Created Policy Framework persistence unit \"{}\"", daoParameters.getPersistenceUnit()); @@ -204,6 +217,27 @@ public class DefaultPfDao implements PfDao { } } + @Override + public void delete(final Class someClass, final PfTimestampKey key) { + if (key == null) { + return; + } + final EntityManager mg = getEntityManager(); + try { + // @formatter:off + mg.getTransaction().begin(); + mg.createQuery(setQueryTable(DELETE_BY_TIMESTAMP_KEY, someClass), someClass) + .setParameter(NAME, key.getName()) + .setParameter(VERSION, key.getVersion()) + .setParameter(TIMESTAMP, key.getTimeStamp()) + .executeUpdate(); + mg.getTransaction().commit(); + // @formatter:on + } finally { + mg.close(); + } + } + @Override public void createCollection(final Collection objs) { if (objs == null || objs.isEmpty()) { @@ -317,33 +351,78 @@ public class DefaultPfDao implements PfDao { } @Override - public T get(final Class someClass, final PfConceptKey key) { - if (someClass == null) { - return null; - } + public List getFiltered(final Class someClass, final String name, final String version, + final Date startTime, final Date endTime, final Map filterMap, final String sortOrder, + final int getRecordNum) { final EntityManager mg = getEntityManager(); + + String filterQueryString = SELECT_FROM_TABLE + WHERE; + try { - final T t = mg.find(someClass, key); - if (t != null) { - // This clone is created to force the JPA DAO to recurse down through the object - try { - final T clonedT = someClass.newInstance(); - t.copyTo(clonedT); - return clonedT; - } catch (final Exception e) { - LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e); - return null; + if (filterMap != null) { + filterQueryString = buildFilter(filterMap, filterQueryString); + } + filterQueryString = addKeyFilterString(filterQueryString, name, startTime, endTime); + if (getRecordNum > 0) { + filterQueryString += ORDER + " c.key.timeStamp " + sortOrder; + } + TypedQuery query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass); + + if (filterMap != null) { + for (Map.Entry entry : filterMap.entrySet()) { + query.setParameter(entry.getKey(), entry.getValue()); + } + } + if (name != null) { + query.setParameter("name", name); + } + if (startTime != null) { + if (endTime != null) { + query.setParameter("startTime", startTime); + query.setParameter("endTime", endTime); + } else { + query.setParameter("startTime", startTime); } } else { - return null; + if (endTime != null) { + query.setParameter("endTime", endTime); + } + } + if (getRecordNum > 0) { + query.setMaxResults(getRecordNum); } + + LOGGER.error("filterQueryString is \"{}\"", filterQueryString); + return query.getResultList(); } finally { mg.close(); } } + private String buildFilter(final Map filterMap, String filterQueryString) { + StringBuilder bld = new StringBuilder(filterQueryString); + for (String key : filterMap.keySet()) { + bld.append("c." + key + "= :" + key + AND); + } + return bld.toString(); + } + + @Override + public T get(final Class someClass, final PfConceptKey key) { + return genericGet(someClass, key); + } + @Override public T get(final Class someClass, final PfReferenceKey key) { + return genericGet(someClass, key); + } + + @Override + public T get(final Class someClass, final PfTimestampKey key) { + return genericGet(someClass, key); + } + + private T genericGet(final Class someClass, final Object key) { if (someClass == null) { return null; } @@ -351,17 +430,9 @@ public class DefaultPfDao implements PfDao { try { final T t = mg.find(someClass, key); if (t != null) { - try { - final T clonedT = someClass.newInstance(); - t.copyTo(clonedT); - return clonedT; - } catch (final Exception e) { - LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e); - return null; - } - } else { - return null; + mg.refresh(t); } + return checkAndReturn(someClass, t); } finally { mg.close(); } @@ -497,7 +568,7 @@ public class DefaultPfDao implements PfDao { * @return the updated query string */ private String setQueryTable(final String queryString, final Class tableClass) { - return queryString.replaceAll(TABLE_TOKEN, tableClass.getSimpleName()); + return queryString.replace(TABLE_TOKEN, tableClass.getSimpleName()); } /** @@ -518,4 +589,56 @@ public class DefaultPfDao implements PfDao { } return ret.get(0); } + + /** + * generate filter string with the filter value in TimestampKey. + * + * @param inputFilterString current filterString generated from FilterMap + * @param name the pdp name the start timeStamp to filter from database, filter rule: startTime <= filteredRecord + * timeStamp <= endTime. null for ignore start time. + * @param endTime the end timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp <= + * endTime. null for ignore end time + * @return the filter string to query database + */ + private String addKeyFilterString(String inputFilterString, final String name, final Date startTime, + final Date endTime) { + String filterQueryString; + if (name != null) { + inputFilterString += NAME_FILTER + AND; + } + if (startTime != null) { + if (endTime != null) { + filterQueryString = inputFilterString + TIMESTAMP_START_FILTER + AND + TIMESTAMP_END_FILTER; + } else { + filterQueryString = inputFilterString + TIMESTAMP_START_FILTER; + } + } else { + if (endTime != null) { + filterQueryString = inputFilterString + TIMESTAMP_END_FILTER; + } else { + filterQueryString = inputFilterString.substring(0, inputFilterString.length() - AND.length()); + } + } + + return filterQueryString; + } + + /** + * check the result get from database and return the object. + * + * @param the type of the object to get, a subclass of {@link PfConcept} + * @param someClass the class of the object to get, a subclass of {@link PfConcept} + * @param t the object that was retrieved from the database + * @return the checked object or null + */ + private T checkAndReturn(final Class someClass, final T objToCheck) { + if (objToCheck != null) { + try { + return PfUtils.makeCopy(objToCheck); + } catch (final Exception e) { + LOGGER.warn(CLONE_ERR_MSG, someClass.getName(), e); + } + } + return null; + } }