From fcbf3698fa3e3fbc6ea3364d80d4a3f3a8d37650 Mon Sep 17 00:00:00 2001 From: rameshiyer27 Date: Mon, 1 Mar 2021 18:46:33 +0000 Subject: Fix Reference Key columns persistence issue in db Problem: When PfReferenceTimestamp Key is persisted in db, the parent class fields are ignored which causes inconsistent data while saving and fetching in db. Fix: JPA is not supporting inheritance in @Embeddable classes. Hence included the super class as @Embedded property in PfReferenceTimestamp key. @Embedded properties are unable to be queried directly. Slightly modified Dao methods to support querying @embedded fields. Issue-ID: POLICY-3049 Signed-off-by: zrrmmua Change-Id: I5c2e03a5ae98525764a64dd6cce1a1298c5f9816 --- .../java/org/onap/policy/models/dao/PfDao.java | 11 +++++ .../onap/policy/models/dao/impl/DefaultPfDao.java | 51 ++++++++++++++++++---- 2 files changed, 53 insertions(+), 9 deletions(-) (limited to 'models-dao') 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 05f907fdd..eb77be9c0 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 @@ -29,6 +29,7 @@ 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.PfReferenceKey; +import org.onap.policy.models.base.PfReferenceTimestampKey; import org.onap.policy.models.base.PfTimestampKey; /** @@ -201,6 +202,16 @@ public interface PfDao { */ T get(Class someClass, PfTimestampKey timestampKey); + /** + * Get an object from the database, referred to by reference timestamp key. + * + * @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 key the PfReferenceTimestampKey of the object to get + * @return the object that was retrieved from the database or null if the object was not retrieved + */ + T get(Class someClass, PfReferenceTimestampKey key); + /** * 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 c29c4bf2d..ef86c177b 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 @@ -37,6 +37,7 @@ 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.PfReferenceTimestampKey; import org.onap.policy.models.base.PfTimestampKey; import org.onap.policy.models.base.PfUtils; import org.onap.policy.models.dao.DaoParameters; @@ -78,6 +79,8 @@ public class DefaultPfDao implements PfDao { 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 PARENT_NAME_REF_FILTER = "c.key.referenceKey.parentKeyName = :parentKeyName"; + private static final String CLONE_ERR_MSG = "Could not clone object of class \"{}\""; private static final String DELETE_BY_CONCEPT_KEY = @@ -366,7 +369,8 @@ public class DefaultPfDao implements PfDao { if (filterMap != null) { filterQueryString = buildFilter(filterMap, filterQueryString); } - filterQueryString = addKeyFilterString(filterQueryString, name, startTime, endTime); + filterQueryString = addKeyFilterString(filterQueryString, name, startTime, endTime, + isRefTimestampKey(someClass)); if (getRecordNum > 0) { filterQueryString += ORDER + " c.key.timeStamp " + sortOrder; } @@ -378,7 +382,11 @@ public class DefaultPfDao implements PfDao { } } if (name != null) { - query.setParameter("name", name); + if (isRefTimestampKey(someClass)) { + query.setParameter("parentKeyName", name); + } else { + query.setParameter("name", name); + } } if (startTime != null) { if (endTime != null) { @@ -398,11 +406,25 @@ public class DefaultPfDao implements PfDao { LOGGER.debug("filterQueryString is \"{}\"", filterQueryString); return query.getResultList(); - } finally { + } finally { mg.close(); } } + /** + * This method checks if the class invoking the DAO is using PfReferenceTimestamp Key. + * @param someClass class that invoked Dao + * @return true if the key is PfReferenceTimestampKey. + */ + private boolean isRefTimestampKey(final Class someClass) { + try { + return PfReferenceTimestampKey.class.isAssignableFrom(someClass.getDeclaredField("key").getType()); + } catch (NoSuchFieldException e) { + LOGGER.error("Error verifying the key for reference timestamp:", e); + return false; + } + } + private String buildFilter(final Map filterMap, String filterQueryString) { StringBuilder bld = new StringBuilder(filterQueryString); for (String key : filterMap.keySet()) { @@ -426,6 +448,11 @@ public class DefaultPfDao implements PfDao { return genericGet(someClass, key); } + @Override + public T get(final Class someClass, final PfReferenceTimestampKey key) { + return genericGet(someClass, key); + } + private T genericGet(final Class someClass, final Object key) { if (someClass == null) { return null; @@ -619,25 +646,31 @@ public class DefaultPfDao implements PfDao { * 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 + * @param isRefTimestampKey boolean value, set to true if the query invoked for pfReferenceTimestampKey * @return the filter string to query database */ private String addKeyFilterString(String inputFilterString, final String name, final Instant startTime, - final Instant endTime) { + final Instant endTime, final boolean isRefTimestampKey) { String filterQueryString; + String inputFilter = inputFilterString; if (name != null) { - inputFilterString += NAME_FILTER + AND; + if (isRefTimestampKey) { + inputFilter += PARENT_NAME_REF_FILTER + AND; + } else { + inputFilter += NAME_FILTER + AND; + } } if (startTime != null) { if (endTime != null) { - filterQueryString = inputFilterString + TIMESTAMP_START_FILTER + AND + TIMESTAMP_END_FILTER; + filterQueryString = inputFilter + TIMESTAMP_START_FILTER + AND + TIMESTAMP_END_FILTER; } else { - filterQueryString = inputFilterString + TIMESTAMP_START_FILTER; + filterQueryString = inputFilter + TIMESTAMP_START_FILTER; } } else { if (endTime != null) { - filterQueryString = inputFilterString + TIMESTAMP_END_FILTER; + filterQueryString = inputFilter + TIMESTAMP_END_FILTER; } else { - filterQueryString = inputFilterString.substring(0, inputFilterString.length() - AND.length()); + filterQueryString = inputFilter.substring(0, inputFilter.length() - AND.length()); } } -- cgit 1.2.3-korg