Upgrade the H2 database to the latest version
[policy/models.git] / models-pap / src / main / java / org / onap / policy / models / pap / persistence / concepts / JpaPolicyAudit.java
index b31af8e..e2f8972 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2021 Nordix Foundation.
+ *  Modifications Copyright (C) 2021-2022 Bell Canada. 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.
 package org.onap.policy.models.pap.persistence.concepts;
 
 import java.time.Instant;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import javax.persistence.Column;
-import javax.persistence.EmbeddedId;
 import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
 import javax.persistence.Index;
 import javax.persistence.Inheritance;
 import javax.persistence.InheritanceType;
 import javax.persistence.Table;
+import javax.persistence.TableGenerator;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
 import javax.validation.constraints.NotNull;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
+import lombok.NonNull;
 import org.apache.commons.lang3.builder.CompareToBuilder;
+import org.onap.policy.common.parameters.BeanValidationResult;
+import org.onap.policy.common.parameters.ValidationStatus;
+import org.onap.policy.common.parameters.annotations.Pattern;
 import org.onap.policy.common.utils.validation.Assertions;
 import org.onap.policy.models.base.PfAuthorative;
 import org.onap.policy.models.base.PfConcept;
-import org.onap.policy.models.base.PfGeneratedIdKey;
+import org.onap.policy.models.base.PfConceptKey;
 import org.onap.policy.models.base.PfKey;
 import org.onap.policy.models.base.PfReferenceKey;
-import org.onap.policy.models.base.validation.annotations.VerifyKey;
+import org.onap.policy.models.base.Validated;
 import org.onap.policy.models.pap.concepts.PolicyAudit;
 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
@@ -54,18 +63,33 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
  *
  */
 @Entity
-@Table(name = "JpaPolicyAudit", indexes = {@Index(name = "JpaPolicyAuditIndex_timestamp", columnList = "timeStamp")})
+@Table(name = "JpaPolicyAudit", indexes = {
+    @Index(name = "JpaPolicyAuditIndex_timestamp", columnList = "timeStamp")
+})
 @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
 @Data
 @EqualsAndHashCode(callSuper = false)
 public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAudit> {
     private static final long serialVersionUID = -2935734300607322191L;
 
-    @EmbeddedId
-    @Column
-    @NotNull
-    @VerifyKey(versionNotNull = true)
-    private PfGeneratedIdKey key;
+    @Id
+    @Column(name = "ID")
+    @GeneratedValue(strategy = GenerationType.TABLE, generator = "auditIdGen")
+    @TableGenerator(
+        name = "auditIdGen",
+        table = "sequence",
+        pkColumnName = "SEQ_NAME",
+        valueColumnName = "SEQ_COUNT",
+        pkColumnValue = "SEQ_GEN")
+    private Long generatedId;
+
+    @Column(name = "name", length = 120)
+    @Pattern(regexp = PfKey.NAME_REGEXP)
+    private String name;
+
+    @Column(name = "version", length = 20)
+    @Pattern(regexp = PfKey.VERSION_REGEXP)
+    private String version;
 
     @Column
     private String pdpGroup;
@@ -83,13 +107,14 @@ public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAud
     private Date timeStamp;
 
     @Column
-    private String user;
+    private String userName;
 
     /**
      * Default constructor.
      */
     public JpaPolicyAudit() {
-        key = new PfGeneratedIdKey();
+        this.setName(PfKey.NULL_KEY_NAME);
+        this.setVersion(PfKey.NULL_KEY_VERSION);
     }
 
     /**
@@ -107,12 +132,14 @@ public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAud
      * @param copyConcept original entity to be copied
      */
     public JpaPolicyAudit(JpaPolicyAudit copyConcept) {
-        this.key = new PfGeneratedIdKey(copyConcept.getKey());
+        this.name = copyConcept.name;
+        this.version = copyConcept.version;
+        this.generatedId = copyConcept.generatedId;
         this.pdpGroup = copyConcept.getPdpGroup();
         this.pdpType = copyConcept.getPdpType();
         this.action = copyConcept.getAction();
         this.timeStamp = copyConcept.getTimeStamp();
-        this.user = copyConcept.getUser();
+        this.userName = copyConcept.getUserName();
     }
 
     @Override
@@ -131,29 +158,31 @@ public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAud
 
         // @formatter:off
         return new CompareToBuilder()
-                        .append(key, other.key)
+                        .append(name, other.name)
+                        .append(version, other.version)
+                        .append(generatedId, other.generatedId)
                         .append(pdpGroup, other.pdpGroup)
                         .append(pdpType, other.pdpType)
                         .append(action, other.action)
                         .append(timeStamp, other.timeStamp)
-                        .append(user, other.user)
+                        .append(userName, other.userName)
                         .toComparison();
         // @formatter:on
     }
 
     @Override
     public PolicyAudit toAuthorative() {
-        ToscaConceptIdentifier policyIdent = new ToscaConceptIdentifier(key.getName(), key.getVersion());
+        var policyIdent = new ToscaConceptIdentifier(name, version);
 
         // @formatter:off
         return PolicyAudit.builder()
-                        .auditId(key.getGeneratedId())
+                        .auditId(generatedId)
                         .pdpGroup(pdpGroup)
                         .pdpType(pdpType)
                         .policy(policyIdent)
                         .action(action)
                         .timestamp(timeStamp == null ? null : timeStamp.toInstant())
-                        .user(user)
+                        .user(userName)
                         .build();
         // @formatter:on
     }
@@ -162,30 +191,46 @@ public class JpaPolicyAudit extends PfConcept implements PfAuthorative<PolicyAud
     public void fromAuthorative(PolicyAudit authorativeConcept) {
         if (authorativeConcept.getPolicy() != null) {
             final ToscaConceptIdentifier policy = authorativeConcept.getPolicy();
-            key = new PfGeneratedIdKey(policy.getName(), policy.getVersion(), authorativeConcept.getAuditId());
+            this.setName(policy.getName());
+            this.setVersion(policy.getVersion());
         } else {
-            key = new PfGeneratedIdKey();
+            this.setName(PfKey.NULL_KEY_NAME);
+            this.setVersion(PfKey.NULL_KEY_VERSION);
         }
-
+        this.setGeneratedId(authorativeConcept.getAuditId());
         pdpGroup = authorativeConcept.getPdpGroup();
         pdpType = authorativeConcept.getPdpType();
         action = authorativeConcept.getAction();
         timeStamp = authorativeConcept.getTimestamp() == null ? Date.from(Instant.now())
-                : Date.from(authorativeConcept.getTimestamp());
-        user = authorativeConcept.getUser();
+            : Date.from(authorativeConcept.getTimestamp());
+        userName = authorativeConcept.getUser();
     }
 
     @Override
     public List<PfKey> getKeys() {
-        return getKey().getKeys();
+        final List<PfKey> keyList = new ArrayList<>();
+        keyList.add(getKey());
+        return keyList;
     }
 
     @Override
-    public void clean() {
-        key.clean();
+    public PfKey getKey() {
+        return new PfConceptKey(name, version);
+    }
 
+    @Override
+    public void clean() {
         pdpGroup = Assertions.validateStringParameter("pdpGroup", pdpGroup, PfReferenceKey.LOCAL_NAME_REGEXP);
         pdpType = Assertions.validateStringParameter("pdpType", pdpType, PfReferenceKey.LOCAL_NAME_REGEXP);
-        user = Assertions.validateStringParameter("user", user, PfReferenceKey.LOCAL_NAME_REGEXP);
+        userName = Assertions.validateStringParameter("user", userName, PfReferenceKey.LOCAL_NAME_REGEXP);
+    }
+
+    @Override
+    public BeanValidationResult validate(@NonNull String fieldName) {
+        BeanValidationResult result = super.validate(fieldName);
+        if (PfKey.NULL_KEY_NAME.equals(name)) {
+            result.addResult("name", name, ValidationStatus.INVALID, Validated.IS_NULL);
+        }
+        return result;
     }
 }