Add DAO Enabled Tosca Model
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / PfKeyUse.java
index 0eb55a7..57141c2 100644 (file)
@@ -22,16 +22,20 @@ package org.onap.policy.models.base;
 
 import java.util.List;
 
+import javax.ws.rs.core.Response;
+
 import lombok.EqualsAndHashCode;
+import lombok.NonNull;
 import lombok.ToString;
 
 import org.onap.policy.common.utils.validation.Assertions;
 import org.onap.policy.models.base.PfValidationResult.ValidationResult;
 
 /**
- * This class records a usage of a key in the system. When the list of keys being used by a concept is built using the
- * getKeys() method of the {@link PfConcept} class, an instance of this class is created for every key occurrence. The
- * list of keys returned by the getKeys() method is a list of {@link PfKeyUse} objects.
+ * This class records a usage of a key in the system. When the list of keys being used by a concept
+ * is built using the getKeys() method of the {@link PfConcept} class, an instance of this class is
+ * created for every key occurrence. The list of keys returned by the getKeys() method is a list of
+ * {@link PfKeyUse} objects.
  *
  * <p>Validation checks that each key is valid.
  */
@@ -50,22 +54,21 @@ public class PfKeyUse extends PfKey {
     }
 
     /**
-     * Copy constructor.
+     * This constructor creates an instance of this class, and holds a reference to a used key.
      *
-     * @param copyConcept the concept to copy from
+     * @param usedKey a used key
      */
-    public PfKeyUse(final PfKeyUse copyConcept) {
-        super(copyConcept);
+    public PfKeyUse(@NonNull final PfKey usedKey) {
+        this.usedKey = usedKey;
     }
 
     /**
-     * This constructor creates an instance of this class, and holds a reference to a used key.
+     * Copy constructor.
      *
-     * @param usedKey a used key
+     * @param copyConcept the concept to copy from
      */
-    public PfKeyUse(final PfKey usedKey) {
-        Assertions.argumentNotNull(usedKey, "usedKey may not be null");
-        this.usedKey = usedKey;
+    public PfKeyUse(@NonNull final PfKeyUse copyConcept) {
+        super(copyConcept);
     }
 
     @Override
@@ -83,56 +86,42 @@ public class PfKeyUse extends PfKey {
         return usedKey.getId();
     }
 
+    @Override
+    public boolean isNullKey() {
+        return usedKey.isNullKey();
+    }
+
     /**
      * Sets the key.
      *
      * @param key the key
      */
-    public void setKey(final PfKey key) {
-        Assertions.argumentNotNull(key, "usedKey may not be null");
+    public void setKey(@NonNull final PfKey key) {
         this.usedKey = key;
     }
 
     @Override
-    public PfKey.Compatibility getCompatibility(final PfKey otherKey) {
+    public PfKey.Compatibility getCompatibility(@NonNull final PfKey otherKey) {
         return usedKey.getCompatibility(otherKey);
     }
 
     @Override
-    public boolean isCompatible(final PfKey otherKey) {
+    public boolean isCompatible(@NonNull final PfKey otherKey) {
         return usedKey.isCompatible(otherKey);
     }
 
-    @Override
-    public PfValidationResult validate(final PfValidationResult result) {
-        if (usedKey.equals(PfConceptKey.getNullKey())) {
-            result.addValidationMessage(new PfValidationMessage(usedKey, this.getClass(), ValidationResult.INVALID,
-                    "usedKey is a null key"));
-        }
-        return usedKey.validate(result);
-    }
-
     @Override
     public void clean() {
         usedKey.clean();
     }
 
     @Override
-    public PfConcept copyTo(final PfConcept target) {
-        Assertions.argumentNotNull(target, "target may not be null");
-
-        final Object copyObject = target;
-        Assertions.instanceOf(copyObject, PfKeyUse.class);
-
-        final PfKeyUse copy = ((PfKeyUse) copyObject);
-        try {
-            copy.usedKey = usedKey.getClass().newInstance();
-        } catch (final Exception e) {
-            throw new PfModelRuntimeException("error copying concept key: " + e.getMessage(), e);
+    public PfValidationResult validate(@NonNull final PfValidationResult result) {
+        if (usedKey.isNullKey()) {
+            result.addValidationMessage(new PfValidationMessage(usedKey, this.getClass(), ValidationResult.INVALID,
+                    "usedKey is a null key"));
         }
-        usedKey.copyTo(copy.usedKey);
-
-        return copy;
+        return usedKey.validate(result);
     }
 
     @Override
@@ -150,4 +139,21 @@ public class PfKeyUse extends PfKey {
 
         return usedKey.compareTo(other.usedKey);
     }
+
+    @Override
+    public PfConcept copyTo(@NonNull final PfConcept target) {
+        final Object copyObject = target;
+        Assertions.instanceOf(copyObject, PfKeyUse.class);
+
+        final PfKeyUse copy = ((PfKeyUse) copyObject);
+        try {
+            copy.usedKey = usedKey.getClass().newInstance();
+        } catch (final Exception e) {
+            throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR,
+                    "error copying concept key: " + e.getMessage(), e);
+        }
+        usedKey.copyTo(copy.usedKey);
+
+        return copy;
+    }
 }