49e25ecb0fa899ed8f8223efa3f4e22f5ee71656
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / JobAuditStatus.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.model;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.apache.commons.lang3.builder.EqualsBuilder;
25 import org.apache.commons.lang3.builder.HashCodeBuilder;
26 import org.hibernate.annotations.DynamicUpdate;
27 import org.hibernate.annotations.SelectBeforeUpdate;
28 import org.hibernate.annotations.Type;
29 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
30 import org.onap.vid.job.Job.JobStatus;
31
32 import javax.persistence.*;
33 import java.text.DateFormat;
34 import java.text.ParseException;
35 import java.text.SimpleDateFormat;
36 import java.util.Date;
37 import java.util.TimeZone;
38 import java.util.UUID;
39
40 /*
41  The following 2 annotations let hibernate to update only fields that actually have been changed.
42  DynamicUpdate tell hibernate to update only dirty fields.
43  SelectBeforeUpdate is needed since during update the entity is detached (get and update are in different sessions)
44  */
45 @DynamicUpdate()
46 @SelectBeforeUpdate()
47 @Entity
48 @Table(name = "vid_job_audit_status")
49 public class JobAuditStatus extends VidBaseEntity {
50
51     public static final int MAX_ADDITIONAL_INFO_LENGTH = 2000;
52     static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JobAuditStatus.class);
53     private static final String defaultFormat = "E, dd MMM yyyy HH:mm:ss z";
54
55     public JobAuditStatus(){}
56
57     public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source){
58         this.jobId = jobId;
59         this.jobStatus = jobStatus;
60         this.source = source;
61     }
62
63     public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, Date date){
64         this(jobId, jobStatus, source);
65         this.created = date;
66     }
67
68     public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo) {
69         this(jobId, jobStatus, source);
70         this.requestId = requestId;
71         setAdditionalInfo(additionalInfo);
72     }
73
74     public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo, Date date){
75         this(jobId, jobStatus, source, requestId, additionalInfo);
76         this.created = date;
77     }
78
79     public JobAuditStatus(String instanceName, String jobStatus, UUID requestId, String additionalInfo) {
80         this.instanceName = instanceName;
81         this.jobStatus = jobStatus;
82         this.requestId = requestId;
83         this.additionalInfo = additionalInfo;
84
85     }
86
87     public JobAuditStatus(String instanceName, String jobStatus, UUID requestId, String additionalInfo, String date, String instanceType) {
88        this(instanceName, jobStatus, requestId, additionalInfo);
89        this.created = dateStringToDate(date);
90        this.instanceType = instanceType;
91     }
92
93
94     private Date dateStringToDate(String dateAsString){
95         if (StringUtils.isEmpty(dateAsString)) {
96             return null;
97         }
98
99         DateFormat format = new SimpleDateFormat(defaultFormat);
100         format.setTimeZone(TimeZone.getTimeZone("GMT"));
101         Date date = null ;
102         try {
103             date = format.parse(dateAsString);
104         } catch (ParseException e) {
105             logger.error("There was an error to parse the string "+ dateAsString +" to date ", e.getMessage());
106         }
107         return date;
108     }
109
110     @Override
111     public boolean equals(Object o) {
112         if (this == o) return true;
113
114         if (o == null || getClass() != o.getClass()) return false;
115
116         JobAuditStatus that = (JobAuditStatus) o;
117
118         return new EqualsBuilder()
119                 .append(jobId, that.jobId)
120                 .append(jobStatus, that.jobStatus)
121                 .append(source, that.source)
122                 .append(requestId, that.requestId)
123                 .append(additionalInfo, that.additionalInfo)
124                 .isEquals();
125     }
126
127     @Override
128     public int hashCode() {
129         return new HashCodeBuilder(17, 37)
130                 .append(jobId)
131                 .append(jobStatus)
132                 .append(source)
133                 .append(requestId)
134                 .append(additionalInfo)
135                 .toHashCode();
136     }
137
138     public enum SourceStatus {
139         MSO,
140         VID
141     }
142
143     private UUID jobId;
144     private String instanceName;
145     private String instanceType;
146     private String jobStatus;
147     private SourceStatus source;
148     private UUID requestId;
149     private String additionalInfo;
150
151     @Id
152     @GeneratedValue(strategy = GenerationType.IDENTITY)
153     @Override
154     @Column(name = "ID", columnDefinition = "INT(11)")
155     public Long getId() {
156         return this.id;
157     }
158
159     @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
160     @Type(type="org.hibernate.type.UUIDCharType")
161     public UUID getJobId() {
162         return jobId;
163     }
164
165     public void setJobId(UUID jobId) {
166         this.jobId = jobId;
167     }
168
169
170     @Column(name = "JOB_STATUS")
171     public String getJobStatus() {
172         return jobStatus;
173     }
174
175     public void setJobStatus(String jobStatus) {
176         this.jobStatus = jobStatus;
177     }
178
179
180     @Enumerated(EnumType.STRING)
181     @Column(name = "SOURCE")
182     public SourceStatus getSource() {
183         return source;
184     }
185
186     public void setSource(SourceStatus source) {
187         this.source = source;
188     }
189
190     @Column(name = "REQUEST_ID", columnDefinition = "CHAR(36)")
191     @Type(type="org.hibernate.type.UUIDCharType")
192     public UUID getRequestId() {
193         return requestId;
194     }
195
196     public void setRequestId(UUID requestId) {
197         this.requestId = requestId;
198     }
199
200     @Column(name = "ADDITIONAL_INFO", columnDefinition = "VARCHAR(2000)")
201     public String getAdditionalInfo() {
202         return additionalInfo;
203     }
204
205     public void setAdditionalInfo(String additionalInfo) {
206         this.additionalInfo = StringUtils.substring(additionalInfo, 0, MAX_ADDITIONAL_INFO_LENGTH);
207     }
208
209     @Transient
210     public String getInstanceName() {
211         return instanceName;
212     }
213
214     public void setInstanceName(String instanceName) {
215         this.instanceName = instanceName;
216     }
217
218     @Transient
219     public String getInstanceType() {
220         return instanceType;
221     }
222
223     public void setInstanceType(String instanceType) {
224         this.instanceType = instanceType;
225     }
226
227     @Transient
228     public Boolean isFinal(){
229         try {
230             if (getSource() == SourceStatus.VID) {
231                 return JobStatus.valueOf(getJobStatus()).isFinal();
232             }
233         }
234         catch (IllegalArgumentException e){
235             logger.error("JobStatus: " + getJobStatus() + " from vid isn't a value of JobStatus enum" + e.getMessage());
236             return false;
237         }
238         return false;
239     }
240
241     @Transient
242     public Date getCreatedDate() {
243         return getCreated();
244     }
245
246 }