Java 17 Upgrade
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaRequirement.java
index aeb93b3..18d9d00 100644 (file)
@@ -3,7 +3,7 @@
  * ONAP Requirement Model
  * ================================================================================
  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
- * Modifications Copyright (C) 2019-2020 Nordix Foundation.
+ * Modifications Copyright (C) 2019-2021, 2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.policy.models.tosca.simple.concepts;
 
+import jakarta.persistence.Column;
+import jakarta.persistence.ElementCollection;
+import jakarta.persistence.Entity;
+import jakarta.persistence.Inheritance;
+import jakarta.persistence.InheritanceType;
+import jakarta.persistence.Table;
+import java.io.Serial;
 import java.util.ArrayList;
-import java.util.LinkedHashMap;
 import java.util.List;
-import java.util.Map;
-import javax.persistence.Column;
-import javax.persistence.ElementCollection;
-import javax.persistence.Entity;
-import javax.persistence.Inheritance;
-import javax.persistence.InheritanceType;
-import javax.persistence.Lob;
-import javax.persistence.Table;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.onap.policy.common.parameters.annotations.NotNull;
 import org.onap.policy.common.utils.coder.YamlJsonTranslator;
-import org.onap.policy.models.base.PfAuthorative;
 import org.onap.policy.models.base.PfConcept;
 import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfUtils;
@@ -57,12 +54,13 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaRequirement;
 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 @Data
 @EqualsAndHashCode(callSuper = true)
-public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
-        implements PfAuthorative<ToscaRequirement> {
+public class JpaToscaRequirement extends JpaToscaWithTypeAndStringProperties<ToscaRequirement> {
 
+    @Serial
     private static final long serialVersionUID = 2785481541573683089L;
     private static final String AUTHORATIVE_UNBOUNDED_LITERAL = "UNBOUNDED";
     private static final Integer JPA_UNBOUNDED_VALUE = -1;
+    private static final YamlJsonTranslator YAML_TRANSLATOR = new YamlJsonTranslator();
 
     @Column
     private String capability;
@@ -76,10 +74,6 @@ public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
     @ElementCollection
     private List<@NotNull @PfMin(value = 0, allowed = -1) Integer> occurrences;
 
-    @ElementCollection
-    @Lob
-    private Map<@NotNull String, @NotNull String> properties;
-
     /**
      * The Default Constructor creates a {@link JpaToscaRequirement} object with a null key.
      */
@@ -107,7 +101,6 @@ public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
         this.node = copyConcept.node;
         this.relationship = copyConcept.relationship;
         this.occurrences = new ArrayList<>(copyConcept.occurrences);
-        this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
     }
 
     /**
@@ -116,13 +109,12 @@ public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
      * @param authorativeConcept the authorative concept to copy from
      */
     public JpaToscaRequirement(final ToscaRequirement authorativeConcept) {
-        super(new PfConceptKey());
-        this.fromAuthorative(authorativeConcept);
+        super(authorativeConcept);
     }
 
     @Override
     public ToscaRequirement toAuthorative() {
-        ToscaRequirement toscaRequirement = new ToscaRequirement();
+        var toscaRequirement = new ToscaRequirement();
         super.setToscaEntity(toscaRequirement);
         super.toAuthorative();
 
@@ -142,16 +134,6 @@ public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
             toscaRequirement.setOccurrences(occurrencesList);
         }
 
-        if (properties != null) {
-            Map<String, Object> propertiesMap = new LinkedHashMap<>();
-
-            for (Map.Entry<String, String> entry : properties.entrySet()) {
-                propertiesMap.put(entry.getKey(), new YamlJsonTranslator().fromYaml(entry.getValue(), Object.class));
-            }
-
-            toscaRequirement.setProperties(propertiesMap);
-        }
-
         return toscaRequirement;
     }
 
@@ -173,15 +155,16 @@ public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
                 }
             }
         }
+    }
 
-        if (toscaRequirement.getProperties() != null) {
-            properties = new LinkedHashMap<>();
-            for (Map.Entry<String, Object> toscaPropertyEntry : toscaRequirement.getProperties().entrySet()) {
-                String jpaProperty = new YamlJsonTranslator().toYaml(toscaPropertyEntry.getValue());
-                properties.put(toscaPropertyEntry.getKey(), jpaProperty);
-            }
-        }
+    @Override
+    protected Object deserializePropertyValue(String propValue) {
+        return YAML_TRANSLATOR.fromYaml(propValue, Object.class);
+    }
 
+    @Override
+    protected String serializePropertyValue(Object propValue) {
+        return YAML_TRANSLATOR.toYaml(propValue);
     }
 
     @Override
@@ -191,30 +174,21 @@ public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
         capability = capability.trim();
         node = node.trim();
         relationship = relationship.trim();
-
-        properties = PfUtils.mapMap(properties, String::trim);
     }
 
     @Override
     public int compareTo(final PfConcept otherConcept) {
-        if (otherConcept == null) {
-            return -1;
-        }
-
         if (this == otherConcept) {
             return 0;
         }
 
-        if (getClass() != otherConcept.getClass()) {
-            return getClass().getName().compareTo(otherConcept.getClass().getName());
-        }
-
-        final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
-        int result = super.compareTo(other);
+        int result = super.compareTo(otherConcept);
         if (result != 0) {
             return result;
         }
 
+        final JpaToscaRequirement other = (JpaToscaRequirement) otherConcept;
+
         result = capability.compareTo(other.capability);
         if (result != 0) {
             return result;
@@ -230,11 +204,6 @@ public class JpaToscaRequirement extends JpaToscaEntityType<ToscaRequirement>
             return result;
         }
 
-        result = PfUtils.compareCollections(occurrences, other.occurrences);
-        if (result != 0) {
-            return result;
-        }
-
-        return PfUtils.compareMaps(properties, other.properties);
+        return PfUtils.compareCollections(occurrences, other.occurrences);
     }
 }