aboutsummaryrefslogtreecommitdiffstats
path: root/models-dao/src/main
diff options
context:
space:
mode:
authorrameshiyer27 <ramesh.murugan.iyer@est.tech>2021-03-01 18:46:33 +0000
committerrameshiyer27 <ramesh.murugan.iyer@est.tech>2021-03-03 12:50:47 +0000
commitfcbf3698fa3e3fbc6ea3364d80d4a3f3a8d37650 (patch)
tree20db6b9b856b0a3c6020b2986967c5df0c594401 /models-dao/src/main
parent726bec7cd5e1b0f5c45b3ec58bc64e344240e8c9 (diff)
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 <ramesh.murugan.iyer@est.tech> Change-Id: I5c2e03a5ae98525764a64dd6cce1a1298c5f9816
Diffstat (limited to 'models-dao/src/main')
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java11
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java51
2 files changed, 53 insertions, 9 deletions
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;
/**
@@ -202,6 +203,16 @@ public interface PfDao {
<T extends PfConcept> T get(Class<T> someClass, PfTimestampKey timestampKey);
/**
+ * Get an object from the database, referred to by reference timestamp key.
+ *
+ * @param <T> 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 extends PfConcept> T get(Class<T> someClass, PfReferenceTimestampKey key);
+
+ /**
* Get all the objects in the database of a given type.
*
* @param <T> the type of the objects to get, a subclass of {@link PfConcept}
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 <T extends PfConcept> boolean isRefTimestampKey(final Class<T> 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<String, Object> 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 extends PfConcept> T get(final Class<T> someClass, final PfReferenceTimestampKey key) {
+ return genericGet(someClass, key);
+ }
+
private <T extends PfConcept> T genericGet(final Class<T> 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());
}
}