Replace Eclipselink with Hibernate
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaNodeTemplate.java
index 9a07ea1..0a5f0c6 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2020 Nordix Foundation.
+ * Copyright (C) 2020-2021,2023 Nordix Foundation.
  * Modifications Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,15 +26,12 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import javax.persistence.CascadeType;
-import javax.persistence.Column;
-import javax.persistence.ElementCollection;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
 import javax.persistence.Inheritance;
 import javax.persistence.InheritanceType;
 import javax.persistence.JoinColumn;
 import javax.persistence.JoinColumns;
-import javax.persistence.Lob;
 import javax.persistence.OneToOne;
 import javax.persistence.Table;
 import javax.ws.rs.core.Response;
@@ -42,17 +39,13 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.NonNull;
 import org.apache.commons.lang3.ObjectUtils;
-import org.onap.policy.common.parameters.annotations.NotBlank;
-import org.onap.policy.common.parameters.annotations.NotNull;
 import org.onap.policy.common.parameters.annotations.Valid;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
-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.PfKey;
 import org.onap.policy.models.base.PfModelRuntimeException;
-import org.onap.policy.models.base.PfUtils;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaCapabilityAssignment;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
 
@@ -64,31 +57,25 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 @Data
 @EqualsAndHashCode(callSuper = false)
-public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
-        implements PfAuthorative<ToscaNodeTemplate> {
+public class JpaToscaNodeTemplate extends JpaToscaWithTypeAndStringProperties<ToscaNodeTemplate> {
     private static final long serialVersionUID = 1675770231921107988L;
 
     private static final StandardCoder STANDARD_CODER = new StandardCoder();
 
-    @Column
-    @NotNull
-    @NotBlank
-    private String type;
-
-    @ElementCollection
-    @Lob
-    private Map<@NotNull String, @NotNull String> properties;
-
     // formatter:off
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
-    @JoinColumns({@JoinColumn(name = "requirementsName", referencedColumnName = "name"),
-        @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")})
+    @JoinColumns({
+        @JoinColumn(name = "requirementsName", referencedColumnName = "name"),
+        @JoinColumn(name = "requirementsVersion", referencedColumnName = "version")
+    })
     @Valid
     private JpaToscaRequirements requirements;
 
     @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
-    @JoinColumns({@JoinColumn(name = "capabilitiesName", referencedColumnName = "name"),
-        @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")})
+    @JoinColumns({
+        @JoinColumn(name = "capabilitiesName", referencedColumnName = "name"),
+        @JoinColumn(name = "capabilitiesVersion", referencedColumnName = "version")
+    })
     @Valid
     private JpaToscaCapabilityAssignments capabilities;
     // @formatter:on
@@ -116,8 +103,7 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
      */
     public JpaToscaNodeTemplate(final JpaToscaNodeTemplate copyConcept) {
         super(copyConcept);
-        this.type = copyConcept.type;
-        this.properties = PfUtils.mapMap(copyConcept.properties, String::new);
+
         this.requirements =
                 (copyConcept.requirements != null ? new JpaToscaRequirements(copyConcept.requirements) : null);
         this.capabilities =
@@ -132,7 +118,6 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
      */
     public JpaToscaNodeTemplate(@NonNull final PfConceptKey key, final String type) {
         super(key);
-        this.type = type;
     }
 
     /**
@@ -146,21 +131,10 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
 
     @Override
     public ToscaNodeTemplate toAuthorative() {
-        ToscaNodeTemplate toscaNodeTemplate = new ToscaNodeTemplate();
+        var toscaNodeTemplate = new ToscaNodeTemplate();
         super.setToscaEntity(toscaNodeTemplate);
         super.toAuthorative();
 
-        toscaNodeTemplate.setType(type);
-
-        toscaNodeTemplate.setProperties(PfUtils.mapMap(properties, property -> {
-            try {
-                return STANDARD_CODER.decode(property, Object.class);
-            } catch (CoderException ce) {
-                String errorMessage = "error decoding property JSON value read from database: " + property;
-                throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
-            }
-        }));
-
         if (requirements != null) {
             toscaNodeTemplate.setRequirements(requirements.toAuthorative());
         }
@@ -180,17 +154,6 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
     public void fromAuthorative(ToscaNodeTemplate toscaNodeTemplate) {
         super.fromAuthorative(toscaNodeTemplate);
 
-        type = toscaNodeTemplate.getType();
-
-        properties = PfUtils.mapMap(toscaNodeTemplate.getProperties(), property -> {
-            try {
-                return STANDARD_CODER.encode(property);
-            } catch (CoderException ce) {
-                String errorMessage = "error encoding property JSON value for database: " + property;
-                throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
-            }
-        });
-
         if (toscaNodeTemplate.getRequirements() != null) {
             requirements = new JpaToscaRequirements();
             requirements.fromAuthorative(toscaNodeTemplate.getRequirements());
@@ -202,6 +165,26 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
         }
     }
 
+    @Override
+    protected Object deserializePropertyValue(String propValue) {
+        try {
+            return STANDARD_CODER.decode(propValue, Object.class);
+        } catch (CoderException ce) {
+            String errorMessage = "error decoding property JSON value read from database: " + propValue;
+            throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
+        }
+    }
+
+    @Override
+    protected String serializePropertyValue(Object propValue) {
+        try {
+            return STANDARD_CODER.encode(propValue);
+        } catch (CoderException ce) {
+            String errorMessage = "error encoding property JSON value for database: " + propValue;
+            throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ce);
+        }
+    }
+
     @Override
     public List<PfKey> getKeys() {
         final List<PfKey> keyList = super.getKeys();
@@ -221,10 +204,6 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
     public void clean() {
         super.clean();
 
-        type = type.trim();
-
-        properties = PfUtils.mapMap(properties, String::trim);
-
         if (requirements != null) {
             requirements.clean();
         }
@@ -236,31 +215,16 @@ public class JpaToscaNodeTemplate extends JpaToscaEntityType<ToscaNodeTemplate>
 
     @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 JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
-        int result = super.compareTo(other);
+        int result = super.compareTo(otherConcept);
         if (result != 0) {
             return result;
         }
 
-        result = type.compareTo(other.type);
-        if (result != 0) {
-            return result;
-        }
-
-        result = PfUtils.compareMaps(properties, other.properties);
-        if (result != 0) {
-            return result;
-        }
+        final JpaToscaNodeTemplate other = (JpaToscaNodeTemplate) otherConcept;
 
         result = ObjectUtils.compare(requirements, other.requirements);
         if (result != 0) {