Fix Null key issue in PfReferenceTimestampKey
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfReferenceKey.java
index 19e8bee..b25d463 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019 Nordix Foundation.
+ *  Copyright (C) 2019,2021 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.
@@ -22,15 +23,14 @@ package org.onap.policy.models.base;
 
 import java.util.ArrayList;
 import java.util.List;
-
 import javax.persistence.Column;
 import javax.persistence.Embeddable;
-
 import lombok.Data;
 import lombok.EqualsAndHashCode;
-
+import lombok.NonNull;
+import org.onap.policy.common.parameters.annotations.NotNull;
+import org.onap.policy.common.parameters.annotations.Pattern;
 import org.onap.policy.common.utils.validation.Assertions;
-import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 
 /**
  * A reference key identifies entities in the system that are contained in other entities. Every contained concept in
@@ -72,16 +72,24 @@ public class PfReferenceKey extends PfKey {
     private static final int PARENT_LOCAL_NAME_FIELD = 2;
     private static final int LOCAL_NAME_FIELD = 3;
 
-    @Column(name = PARENT_KEY_NAME, length = 128)
+    @Column(name = PARENT_KEY_NAME, length = 120)
+    @NotNull
+    @Pattern(regexp = NAME_REGEXP)
     private String parentKeyName;
 
-    @Column(name = PARENT_KEY_VERSION, length = 128)
+    @Column(name = PARENT_KEY_VERSION, length = 15)
+    @NotNull
+    @Pattern(regexp = VERSION_REGEXP)
     private String parentKeyVersion;
 
-    @Column(name = PARENT_LOCAL_NAME, length = 128)
+    @Column(name = PARENT_LOCAL_NAME, length = 120)
+    @NotNull
+    @Pattern(regexp = LOCAL_NAME_REGEXP)
     private String parentLocalName;
 
-    @Column(name = LOCAL_NAME, length = 128)
+    @Column(name = LOCAL_NAME, length = 120)
+    @NotNull
+    @Pattern(regexp = LOCAL_NAME_REGEXP)
     private String localName;
 
     /**
@@ -245,7 +253,9 @@ public class PfReferenceKey extends PfKey {
 
     @Override
     public boolean isNullKey() {
-        return this.equals(PfReferenceKey.getNullKey());
+        return (PfReferenceKey.NULL_KEY_NAME.equals(this.getParentKeyName()) && PfReferenceKey.NULL_KEY_VERSION
+            .equals(this.getParentKeyVersion()) && PfReferenceKey.NULL_KEY_NAME.equals(this.getParentLocalName())
+            && PfReferenceKey.NULL_KEY_NAME.equals(this.getLocalName()));
     }
 
     /**
@@ -305,7 +315,7 @@ public class PfReferenceKey extends PfKey {
     }
 
     @Override
-    public boolean isCompatible(final PfKey otherKey) {
+    public boolean isCompatible(@NonNull final PfKey otherKey) {
         if (!(otherKey instanceof PfReferenceKey)) {
             return false;
         }
@@ -315,36 +325,28 @@ public class PfReferenceKey extends PfKey {
     }
 
     @Override
-    public PfValidationResult validate(final PfValidationResult result) {
-        final String parentNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(PARENT_KEY_NAME,
-                        parentKeyName, NAME_REGEXP);
-        if (parentNameValidationErrorMessage != null) {
-            result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
-                            "parentKeyName invalid-" + parentNameValidationErrorMessage));
-        }
+    public int getMajorVersion() {
+        return this.getParentConceptKey().getMajorVersion();
+    }
 
-        final String parentKeyVersionValidationErrorMessage = Assertions
-                        .getStringParameterValidationMessage(PARENT_KEY_VERSION, parentKeyVersion, VERSION_REGEXP);
-        if (parentKeyVersionValidationErrorMessage != null) {
-            result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
-                            "parentKeyVersion invalid-" + parentKeyVersionValidationErrorMessage));
-        }
+    @Override
+    public int getMinorVersion() {
+        return this.getParentConceptKey().getMinorVersion();
+    }
 
-        final String parentLocalNameValidationErrorMessage = Assertions
-                        .getStringParameterValidationMessage(PARENT_LOCAL_NAME, parentLocalName, LOCAL_NAME_REGEXP);
-        if (parentLocalNameValidationErrorMessage != null) {
-            result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
-                            "parentLocalName invalid-" + parentLocalNameValidationErrorMessage));
-        }
+    @Override
+    public int getPatchVersion() {
+        return this.getParentConceptKey().getPatchVersion();
+    }
 
-        final String localNameValidationErrorMessage = Assertions.getStringParameterValidationMessage(LOCAL_NAME,
-                        localName, LOCAL_NAME_REGEXP);
-        if (localNameValidationErrorMessage != null) {
-            result.addValidationMessage(new PfValidationMessage(this, this.getClass(), ValidationResult.INVALID,
-                            "localName invalid-" + localNameValidationErrorMessage));
-        }
 
-        return result;
+    @Override
+    public boolean isNewerThan(@NonNull final PfKey otherKey) {
+        Assertions.instanceOf(otherKey, PfReferenceKey.class);
+
+        final PfReferenceKey otherReferenceKey = (PfReferenceKey) otherKey;
+
+        return this.getParentConceptKey().isNewerThan(otherReferenceKey.getParentConceptKey());
     }
 
     @Override
@@ -355,22 +357,6 @@ public class PfReferenceKey extends PfKey {
         localName = Assertions.validateStringParameter(LOCAL_NAME, localName, LOCAL_NAME_REGEXP);
     }
 
-    @Override
-    public PfConcept copyTo(final PfConcept target) {
-        Assertions.argumentNotNull(target, "target may not be null");
-
-        final Object copyObject = target;
-        Assertions.instanceOf(copyObject, PfReferenceKey.class);
-
-        final PfReferenceKey copy = ((PfReferenceKey) copyObject);
-        copy.setParentKeyName(parentKeyName);
-        copy.setParentKeyVersion(parentKeyVersion);
-        copy.setLocalName(localName);
-        copy.setParentLocalName(parentLocalName);
-
-        return copy;
-    }
-
     @Override
     public int compareTo(final PfConcept otherObj) {
         Assertions.argumentNotNull(otherObj, "comparison object may not be null");
@@ -379,7 +365,7 @@ public class PfReferenceKey extends PfKey {
             return 0;
         }
         if (getClass() != otherObj.getClass()) {
-            return this.hashCode() - otherObj.hashCode();
+            return getClass().getName().compareTo(otherObj.getClass().getName());
         }
 
         final PfReferenceKey other = (PfReferenceKey) otherObj;