1 package org.onap.vid.model;
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.commons.lang3.builder.EqualsBuilder;
5 import org.apache.commons.lang3.builder.HashCodeBuilder;
6 import org.hibernate.annotations.DynamicUpdate;
7 import org.hibernate.annotations.SelectBeforeUpdate;
8 import org.hibernate.annotations.Type;
9 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
10 import org.onap.vid.job.Job.JobStatus;
12 import javax.persistence.*;
13 import java.text.DateFormat;
14 import java.text.ParseException;
15 import java.text.SimpleDateFormat;
16 import java.util.Date;
17 import java.util.TimeZone;
18 import java.util.UUID;
21 The following 2 annotations let hibernate to update only fields that actually have been changed.
22 DynamicUpdate tell hibernate to update only dirty fields.
23 SelectBeforeUpdate is needed since during update the entity is detached (get and update are in different sessions)
28 @Table(name = "vid_job_audit_status")
29 public class JobAuditStatus extends VidBaseEntity {
31 public static final int MAX_ADDITIONAL_INFO_LENGTH = 2000;
32 static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobAuditStatus.class);
33 private static final String defaultFormat = "E, dd MMM yyyy HH:mm:ss z";
35 public JobAuditStatus(){}
37 public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source){
39 this.jobStatus = jobStatus;
43 public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, Date date){
44 this(jobId, jobStatus, source);
48 public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo) {
49 this(jobId, jobStatus, source);
50 this.requestId = requestId;
51 setAdditionalInfo(additionalInfo);
54 public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo, Date date){
55 this(jobId, jobStatus, source, requestId, additionalInfo);
59 public JobAuditStatus(String instanceName, String jobStatus, UUID requestId, String additionalInfo) {
60 this.instanceName = instanceName;
61 this.jobStatus = jobStatus;
62 this.requestId = requestId;
63 this.additionalInfo = additionalInfo;
67 public JobAuditStatus(String instanceName, String jobStatus, UUID requestId, String additionalInfo, String date, String instanceType) {
68 this(instanceName, jobStatus, requestId, additionalInfo);
69 this.created = dateStringToDate(date);
70 this.instanceType = instanceType;
74 private Date dateStringToDate(String dateAsString){
75 if (StringUtils.isEmpty(dateAsString)) {
79 DateFormat format = new SimpleDateFormat(defaultFormat);
80 format.setTimeZone(TimeZone.getTimeZone("GMT"));
83 date = format.parse(dateAsString);
84 } catch (ParseException e) {
85 logger.error("There was an error to parse the string "+ dateAsString +" to date ", e.getMessage());
91 public boolean equals(Object o) {
92 if (this == o) return true;
94 if (o == null || getClass() != o.getClass()) return false;
96 JobAuditStatus that = (JobAuditStatus) o;
98 return new EqualsBuilder()
99 .append(jobId, that.jobId)
100 .append(jobStatus, that.jobStatus)
101 .append(source, that.source)
102 .append(requestId, that.requestId)
103 .append(additionalInfo, that.additionalInfo)
108 public int hashCode() {
109 return new HashCodeBuilder(17, 37)
114 .append(additionalInfo)
118 public enum SourceStatus {
124 private String instanceName;
125 private String instanceType;
126 private String jobStatus;
127 private SourceStatus source;
128 private UUID requestId;
129 private String additionalInfo;
132 @GeneratedValue(strategy = GenerationType.IDENTITY)
134 @Column(name = "ID", columnDefinition = "INT(11)")
135 public Long getId() {
139 @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
140 @Type(type="org.hibernate.type.UUIDCharType")
141 public UUID getJobId() {
145 public void setJobId(UUID jobId) {
150 @Column(name = "JOB_STATUS")
151 public String getJobStatus() {
155 public void setJobStatus(String jobStatus) {
156 this.jobStatus = jobStatus;
160 @Enumerated(EnumType.STRING)
161 @Column(name = "SOURCE")
162 public SourceStatus getSource() {
166 public void setSource(SourceStatus source) {
167 this.source = source;
170 @Column(name = "REQUEST_ID", columnDefinition = "CHAR(36)")
171 @Type(type="org.hibernate.type.UUIDCharType")
172 public UUID getRequestId() {
176 public void setRequestId(UUID requestId) {
177 this.requestId = requestId;
180 @Column(name = "ADDITIONAL_INFO", columnDefinition = "VARCHAR(2000)")
181 public String getAdditionalInfo() {
182 return additionalInfo;
185 public void setAdditionalInfo(String additionalInfo) {
186 this.additionalInfo = StringUtils.substring(additionalInfo, 0, MAX_ADDITIONAL_INFO_LENGTH);
190 public String getInstanceName() {
194 public void setInstanceName(String instanceName) {
195 this.instanceName = instanceName;
199 public String getInstanceType() {
203 public void setInstanceType(String instanceType) {
204 this.instanceType = instanceType;
208 public Boolean isFinal(){
210 if (getSource() == SourceStatus.VID) {
211 return JobStatus.valueOf(getJobStatus()).isFinal();
214 catch (IllegalArgumentException e){
215 logger.error("JobStatus: " + getJobStatus() + " from vid isn't a value of JobStatus enum" + e.getMessage());
222 public Date getCreatedDate() {