diff options
author | rameshiyer27 <ramesh.murugan.iyer@est.tech> | 2021-03-01 18:46:33 +0000 |
---|---|---|
committer | rameshiyer27 <ramesh.murugan.iyer@est.tech> | 2021-03-03 12:50:47 +0000 |
commit | fcbf3698fa3e3fbc6ea3364d80d4a3f3a8d37650 (patch) | |
tree | 20db6b9b856b0a3c6020b2986967c5df0c594401 /models-base/src/main/java | |
parent | 726bec7cd5e1b0f5c45b3ec58bc64e344240e8c9 (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-base/src/main/java')
-rw-r--r-- | models-base/src/main/java/org/onap/policy/models/base/PfReferenceTimestampKey.java | 128 |
1 files changed, 100 insertions, 28 deletions
diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfReferenceTimestampKey.java b/models-base/src/main/java/org/onap/policy/models/base/PfReferenceTimestampKey.java index 47a72e8cd..06af688b0 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfReferenceTimestampKey.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfReferenceTimestampKey.java @@ -22,12 +22,14 @@ package org.onap.policy.models.base; import java.sql.Timestamp; import java.time.Instant; +import java.util.Collections; +import java.util.List; import javax.persistence.Column; import javax.persistence.Embeddable; +import javax.persistence.Embedded; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; -import lombok.ToString; import org.onap.policy.common.parameters.annotations.NotNull; import org.onap.policy.common.utils.validation.Assertions; @@ -36,11 +38,11 @@ import org.onap.policy.common.utils.validation.Assertions; * additional option to have timestamp as a parameter. * */ + @Embeddable @Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -public class PfReferenceTimestampKey extends PfReferenceKey { +@EqualsAndHashCode +public class PfReferenceTimestampKey extends PfKey { private static final long serialVersionUID = 1130918285832617215L; private static final String TIMESTAMP_TOKEN = "timeStamp"; @@ -49,24 +51,27 @@ public class PfReferenceTimestampKey extends PfReferenceKey { @NotNull private Timestamp timeStamp; + @Embedded + @Column + private PfReferenceKey referenceKey; /** * The default constructor creates a null reference timestamp key. */ public PfReferenceTimestampKey() { - super(); + this.referenceKey = new PfReferenceKey(); this.timeStamp = new Timestamp(0); } /** * The Copy Constructor creates a key by copying another key. * - * @param referenceKey + * @param referenceTimestampKey * the reference key to copy from */ - public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceKey) { - super(referenceKey); - this.timeStamp = referenceKey.getTimeStamp(); + public PfReferenceTimestampKey(final PfReferenceTimestampKey referenceTimestampKey) { + this.referenceKey = referenceTimestampKey.getReferenceKey(); + this.timeStamp = referenceTimestampKey.getTimeStamp(); } /** @@ -76,7 +81,7 @@ public class PfReferenceTimestampKey extends PfReferenceKey { * the parent concept key of this reference key */ public PfReferenceTimestampKey(final PfConceptKey pfConceptKey) { - super(pfConceptKey); + this.referenceKey = new PfReferenceKey(pfConceptKey); this.timeStamp = new Timestamp(0); } @@ -91,7 +96,7 @@ public class PfReferenceTimestampKey extends PfReferenceKey { * the time stamp for this reference key */ public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String localName, final Instant instant) { - super(pfConceptKey, localName); + this.referenceKey = new PfReferenceKey(pfConceptKey, localName); this.timeStamp = Timestamp.from(instant); } @@ -107,7 +112,7 @@ public class PfReferenceTimestampKey extends PfReferenceKey { */ public PfReferenceTimestampKey(final PfReferenceKey parentReferenceKey, final String localName, final Instant instant) { - super(parentReferenceKey, localName); + this.referenceKey = new PfReferenceKey(parentReferenceKey, localName); this.timeStamp = Timestamp.from(instant); } @@ -126,7 +131,7 @@ public class PfReferenceTimestampKey extends PfReferenceKey { */ public PfReferenceTimestampKey(final PfConceptKey pfConceptKey, final String parentLocalName, final String localName, final Instant instant) { - super(pfConceptKey, parentLocalName, localName); + this.referenceKey = new PfReferenceKey(pfConceptKey, parentLocalName, localName); this.timeStamp = Timestamp.from(instant); } @@ -145,7 +150,7 @@ public class PfReferenceTimestampKey extends PfReferenceKey { */ public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion, final String localName, final Instant instant) { - super(parentKeyName, parentKeyVersion, NULL_KEY_NAME, localName); + this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, PfKey.NULL_KEY_NAME, localName); this.timeStamp = Timestamp.from(instant); } @@ -166,7 +171,7 @@ public class PfReferenceTimestampKey extends PfReferenceKey { */ public PfReferenceTimestampKey(final String parentKeyName, final String parentKeyVersion, final String parentLocalName, final String localName, final Instant instant) { - super(parentKeyName, parentKeyVersion, parentLocalName, localName); + this.referenceKey = new PfReferenceKey(parentKeyName, parentKeyVersion, parentLocalName, localName); this.timeStamp = Timestamp.from(instant); } @@ -177,7 +182,7 @@ public class PfReferenceTimestampKey extends PfReferenceKey { * @param id the key ID in a format that respects the KEY_ID_REGEXP */ public PfReferenceTimestampKey(final String id) { - super(id.substring(0, id.lastIndexOf(':'))); + this.referenceKey = new PfReferenceKey(id.substring(0, id.lastIndexOf(':'))); this.timeStamp = new Timestamp(Long.parseLong(id.substring(id.lastIndexOf(':') + 1))); } @@ -200,32 +205,99 @@ public class PfReferenceTimestampKey extends PfReferenceKey { setTimeStamp(Timestamp.from(instant)); } - @Override + /** + * Get the key of this reference. + * + * @return the pfReferenceTimestamp key + */ public PfReferenceTimestampKey getKey() { return this; } - @Override + /** + * Get the key as a string. + * @return pfReferenceTimestamp key. + */ public String getId() { - return super.getId() + ':' + getTimeStamp().getTime(); + return getReferenceKey().getId() + ':' + getTimeStamp().getTime(); + } + + + /** + * Check if this key is a newer version than the other key. + * + * @param otherKey the key to check against + * @return true, if this key is newer than the other key + */ + @Override + public boolean isNewerThan(@NonNull PfKey otherKey) { + Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class); + final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey; + if (!getTimeStamp().equals(otherReferenceKey.timeStamp)) { + return timeStamp.after(otherReferenceKey.timeStamp); + } + return getReferenceKey().isNewerThan(otherReferenceKey.getReferenceKey()); } @Override public boolean isNullKey() { - return super.isNullKey() && getTimeStamp().getTime() == 0; + return getReferenceKey().isNullKey() && getTimeStamp().getTime() == 0; } @Override - public boolean isNewerThan(@NonNull PfKey otherKey) { - Assertions.instanceOf(otherKey, PfReferenceTimestampKey.class); - final PfReferenceTimestampKey otherConceptKey = (PfReferenceTimestampKey) otherKey; + public int getMajorVersion() { + return getReferenceKey().getMajorVersion(); + } - if (this.equals(otherConceptKey)) { - return false; + @Override + public int getMinorVersion() { + return getReferenceKey().getMinorVersion(); + } + + @Override + public int getPatchVersion() { + return getReferenceKey().getPatchVersion(); + } + + + @Override + public int compareTo(@NonNull final PfConcept otherObj) { + if (this == otherObj) { + return 0; + } + if (getClass() != otherObj.getClass()) { + return getClass().getName().compareTo(otherObj.getClass().getName()); } - if (!getTimeStamp().equals(otherConceptKey.timeStamp)) { - return timeStamp.after(otherConceptKey.timeStamp); + int result = getReferenceKey().compareTo(((PfReferenceTimestampKey) otherObj).getReferenceKey()); + if (0 == result) { + return getTimeStamp().compareTo(((PfReferenceTimestampKey) otherObj).timeStamp); } - return super.isNewerThan(otherKey); + return result; + } + + @Override + public List<PfKey> getKeys() { + return Collections.singletonList(getKey()); + } + + @Override + public void clean() { + getReferenceKey().clean(); + } + + @Override + public Compatibility getCompatibility(@NonNull PfKey otherKey) { + return getReferenceKey().getCompatibility(otherKey); + } + + @Override + public boolean isCompatible(@NonNull PfKey otherKey) { + if (!(otherKey instanceof PfReferenceTimestampKey)) { + return false; + } + final PfReferenceTimestampKey otherReferenceKey = (PfReferenceTimestampKey) otherKey; + + return this.getReferenceKey().getParentConceptKey().isCompatible(otherReferenceKey.getReferenceKey() + .getParentConceptKey()); } } |