2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.model;
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;
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;
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)
48 @Table(name = "vid_job_audit_status")
49 public class JobAuditStatus extends VidBaseEntity {
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";
55 public JobAuditStatus(){}
57 public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source){
59 this.jobStatus = jobStatus;
63 public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, Date date){
64 this(jobId, jobStatus, source);
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);
74 public JobAuditStatus(UUID jobId, String jobStatus, SourceStatus source, UUID requestId, String additionalInfo, Date date){
75 this(jobId, jobStatus, source, requestId, additionalInfo);
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;
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;
94 private Date dateStringToDate(String dateAsString){
95 if (StringUtils.isEmpty(dateAsString)) {
99 DateFormat format = new SimpleDateFormat(defaultFormat);
100 format.setTimeZone(TimeZone.getTimeZone("GMT"));
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());
111 public boolean equals(Object o) {
112 if (this == o) return true;
114 if (o == null || getClass() != o.getClass()) return false;
116 JobAuditStatus that = (JobAuditStatus) o;
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)
128 public int hashCode() {
129 return new HashCodeBuilder(17, 37)
134 .append(additionalInfo)
138 public enum SourceStatus {
144 private String instanceName;
145 private String instanceType;
146 private String jobStatus;
147 private SourceStatus source;
148 private UUID requestId;
149 private String additionalInfo;
152 @GeneratedValue(strategy = GenerationType.IDENTITY)
154 @Column(name = "ID", columnDefinition = "INT(11)")
155 public Long getId() {
159 @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
160 @Type(type="org.hibernate.type.UUIDCharType")
161 public UUID getJobId() {
165 public void setJobId(UUID jobId) {
170 @Column(name = "JOB_STATUS")
171 public String getJobStatus() {
175 public void setJobStatus(String jobStatus) {
176 this.jobStatus = jobStatus;
180 @Enumerated(EnumType.STRING)
181 @Column(name = "SOURCE")
182 public SourceStatus getSource() {
186 public void setSource(SourceStatus source) {
187 this.source = source;
190 @Column(name = "REQUEST_ID", columnDefinition = "CHAR(36)")
191 @Type(type="org.hibernate.type.UUIDCharType")
192 public UUID getRequestId() {
196 public void setRequestId(UUID requestId) {
197 this.requestId = requestId;
200 @Column(name = "ADDITIONAL_INFO", columnDefinition = "VARCHAR(2000)")
201 public String getAdditionalInfo() {
202 return additionalInfo;
205 public void setAdditionalInfo(String additionalInfo) {
206 this.additionalInfo = StringUtils.substring(additionalInfo, 0, MAX_ADDITIONAL_INFO_LENGTH);
210 public String getInstanceName() {
214 public void setInstanceName(String instanceName) {
215 this.instanceName = instanceName;
219 public String getInstanceType() {
223 public void setInstanceType(String instanceType) {
224 this.instanceType = instanceType;
228 public Boolean isFinal(){
230 if (getSource() == SourceStatus.VID) {
231 return JobStatus.valueOf(getJobStatus()).isFinal();
234 catch (IllegalArgumentException e){
235 logger.error("JobStatus: " + getJobStatus() + " from vid isn't a value of JobStatus enum" + e.getMessage());
242 public Date getCreatedDate() {