Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / main / java / org / onap / vid / dao / JobRequest.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.dao;
22
23 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
24
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import java.io.IOException;
27 import java.util.Objects;
28 import java.util.UUID;
29 import javax.persistence.Column;
30 import javax.persistence.Entity;
31 import javax.persistence.Id;
32 import javax.persistence.Table;
33 import javax.persistence.Transient;
34 import org.hibernate.annotations.DynamicUpdate;
35 import org.hibernate.annotations.SelectBeforeUpdate;
36 import org.hibernate.annotations.Type;
37 import org.onap.vid.model.VidBaseEntity;
38 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
39
40
41 @DynamicUpdate
42 @SelectBeforeUpdate
43 @Entity
44 @Table(name = "vid_job_request")
45 public class JobRequest extends VidBaseEntity {
46
47     private UUID jobId;
48     private ServiceInstantiation request;
49
50
51     public JobRequest(UUID jobId, ServiceInstantiation request) {
52         this.jobId = jobId;
53         this.request = request;
54     }
55
56     public JobRequest() {
57     }
58
59     @Id
60     @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
61     @Type(type = "org.hibernate.type.UUIDCharType")
62     public UUID getJobId() {
63         return jobId;
64     }
65
66     public void setJobId(UUID jobId) {
67         this.jobId = jobId;
68     }
69
70     @Transient
71     public ServiceInstantiation getRequest() {
72         return request;
73     }
74
75     public void setRequest(ServiceInstantiation request) {
76         this.request = request;
77     }
78
79     //the columnDefinition is used only in UT
80     @Column(name = "REQUEST", columnDefinition = "VARCHAR(30000)")
81     public String getRequestRaw() {
82         try {
83             return JACKSON_OBJECT_MAPPER.writeValueAsString(request);
84         } catch (JsonProcessingException e) {
85             throw new RuntimeException(e);
86         }
87     }
88
89     public void setRequestRaw(String raw) {
90         try {
91             this.request = JACKSON_OBJECT_MAPPER.readValue(raw, ServiceInstantiation.class);
92         } catch (IOException e) {
93             throw new RuntimeException(e);
94         }
95     }
96
97     @Override
98     public boolean equals(Object o) {
99         if (this == o) return true;
100         if (!(o instanceof JobRequest)) return false;
101         JobRequest that = (JobRequest) o;
102         return Objects.equals(getJobId(), that.getJobId()) &&
103                 Objects.equals(getRequest(), that.getRequest());
104     }
105
106     @Override
107     public int hashCode() {
108         return Objects.hash(getJobId(), getRequest());
109     }
110 }
111