Merge from ECOMP's repository
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / JobAuditStatus.java
1 package org.onap.vid.model;
2
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;
11
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;
19
20 /*
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)
24  */
25 @DynamicUpdate()
26 @SelectBeforeUpdate()
27 @Entity
28 @Table(name = "vid_job_audit_status")
29 public class JobAuditStatus extends VidBaseEntity {
30
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";
34
35     public JobAuditStatus(){}
36
37     public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source){
38         this.jobId = jobId;
39         this.jobStatus = jobStatus;
40         this.source = source;
41     }
42
43     public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, Date date){
44         this(jobId, jobStatus, source);
45         this.created = date;
46     }
47
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);
52     }
53
54     public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo, Date date){
55         this(jobId, jobStatus, source, requestId, additionalInfo);
56         this.created = date;
57     }
58
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;
64
65     }
66
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;
71     }
72
73
74     private Date dateStringToDate(String dateAsString){
75         if (StringUtils.isEmpty(dateAsString)) {
76             return null;
77         }
78
79         DateFormat format = new SimpleDateFormat(defaultFormat);
80         format.setTimeZone(TimeZone.getTimeZone("GMT"));
81         Date date = null ;
82         try {
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());
86         }
87         return date;
88     }
89
90     @Override
91     public boolean equals(Object o) {
92         if (this == o) return true;
93
94         if (o == null || getClass() != o.getClass()) return false;
95
96         JobAuditStatus that = (JobAuditStatus) o;
97
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)
104                 .isEquals();
105     }
106
107     @Override
108     public int hashCode() {
109         return new HashCodeBuilder(17, 37)
110                 .append(jobId)
111                 .append(jobStatus)
112                 .append(source)
113                 .append(requestId)
114                 .append(additionalInfo)
115                 .toHashCode();
116     }
117
118     public enum SourceStatus {
119         MSO,
120         VID
121     }
122
123     private UUID jobId;
124     private String instanceName;
125     private String instanceType;
126     private String jobStatus;
127     private SourceStatus source;
128     private UUID requestId;
129     private String additionalInfo;
130
131     @Id
132     @GeneratedValue(strategy = GenerationType.IDENTITY)
133     @Override
134     @Column(name = "ID", columnDefinition = "INT(11)")
135     public Long getId() {
136         return this.id;
137     }
138
139     @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
140     @Type(type="org.hibernate.type.UUIDCharType")
141     public UUID getJobId() {
142         return jobId;
143     }
144
145     public void setJobId(UUID jobId) {
146         this.jobId = jobId;
147     }
148
149
150     @Column(name = "JOB_STATUS")
151     public String getJobStatus() {
152         return jobStatus;
153     }
154
155     public void setJobStatus(String jobStatus) {
156         this.jobStatus = jobStatus;
157     }
158
159
160     @Enumerated(EnumType.STRING)
161     @Column(name = "SOURCE")
162     public SourceStatus getSource() {
163         return source;
164     }
165
166     public void setSource(SourceStatus source) {
167         this.source = source;
168     }
169
170     @Column(name = "REQUEST_ID", columnDefinition = "CHAR(36)")
171     @Type(type="org.hibernate.type.UUIDCharType")
172     public UUID getRequestId() {
173         return requestId;
174     }
175
176     public void setRequestId(UUID requestId) {
177         this.requestId = requestId;
178     }
179
180     @Column(name = "ADDITIONAL_INFO", columnDefinition = "VARCHAR(2000)")
181     public String getAdditionalInfo() {
182         return additionalInfo;
183     }
184
185     public void setAdditionalInfo(String additionalInfo) {
186         this.additionalInfo = StringUtils.substring(additionalInfo, 0, MAX_ADDITIONAL_INFO_LENGTH);
187     }
188
189     @Transient
190     public String getInstanceName() {
191         return instanceName;
192     }
193
194     public void setInstanceName(String instanceName) {
195         this.instanceName = instanceName;
196     }
197
198     @Transient
199     public String getInstanceType() {
200         return instanceType;
201     }
202
203     public void setInstanceType(String instanceType) {
204         this.instanceType = instanceType;
205     }
206
207     @Transient
208     public Boolean isFinal(){
209         try {
210             if (getSource() == SourceStatus.VID) {
211                 return JobStatus.valueOf(getJobStatus()).isFinal();
212             }
213         }
214         catch (IllegalArgumentException e){
215             logger.error("JobStatus: " + getJobStatus() + " from vid isn't a value of JobStatus enum" + e.getMessage());
216             return false;
217         }
218         return false;
219     }
220
221     @Transient
222     public Date getCreatedDate() {
223         return getCreated();
224     }
225
226 }