Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / impl / JobDaoImpl.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.job.impl;
22
23
24 import com.fasterxml.jackson.annotation.JsonIgnore;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.google.common.base.MoreObjects;
27 import org.hibernate.annotations.DynamicUpdate;
28 import org.hibernate.annotations.SelectBeforeUpdate;
29 import org.hibernate.annotations.Type;
30 import org.onap.vid.exceptions.GenericUncheckedException;
31 import org.onap.vid.job.Job;
32 import org.onap.vid.job.JobException;
33 import org.onap.vid.job.JobType;
34 import org.onap.vid.model.VidBaseEntity;
35
36 import javax.persistence.*;
37 import java.io.IOException;
38 import java.util.Date;
39 import java.util.Map;
40 import java.util.Objects;
41 import java.util.UUID;
42
43 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
44
45 /*
46  The following 2 annotations let hibernate to update only fields that actually have been changed.
47  DynamicUpdate tell hibernate to update only dirty fields.
48  SelectBeforeUpdate is needed since during update the entity is detached (get and update are in different sessions)
49 */
50 @DynamicUpdate()
51 @SelectBeforeUpdate()
52 @Entity
53 @Table(name = "vid_job")
54 public class JobDaoImpl extends VidBaseEntity implements Job {
55
56     private Job.JobStatus status;
57     private JobType type;
58     private JobData data = new JobData();
59     private UUID templateId;
60     private UUID uuid;
61     private String takenBy;
62     private String userId;
63     private Integer age = 0;
64     private Integer indexInBulk = 0;
65     private Date deletedAt;
66     private String build;
67
68     @Id
69     @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
70     @Type(type="org.hibernate.type.UUIDCharType")
71     public UUID getUuid() {
72         return uuid;
73     }
74
75     public void setUuid(UUID uuid) {
76         this.uuid = uuid;
77     }
78
79     //we use uuid instead id. So making id Transient
80     @Override
81     @Transient
82     @JsonIgnore
83     public Long getId() {
84         return this.getUuid().getLeastSignificantBits();
85     }
86
87     @Column(name = "JOB_STATUS")
88     @Enumerated(EnumType.STRING)
89     public Job.JobStatus getStatus() {
90         return status;
91     }
92
93     public void setStatus(Job.JobStatus status) {
94         this.status = status;
95     }
96
97     @Enumerated(EnumType.STRING)
98     @Column(name = "JOB_TYPE")
99     public JobType getType() {
100         return type;
101     }
102
103     public void setType(JobType type) {
104         this.type = type;
105     }
106
107     //the columnDefinition is relevant only for UT
108     @Column(name = "JOB_DATA", columnDefinition = "VARCHAR(30000)")
109     public String getDataRaw() {
110         try {
111             return JACKSON_OBJECT_MAPPER.writeValueAsString(data);
112         } catch (JsonProcessingException e) {
113             throw new GenericUncheckedException(e);
114         }
115     }
116
117     public void setDataRaw(String data) {
118         try {
119             this.data = JACKSON_OBJECT_MAPPER.readValue(data, JobData.class);
120         } catch (IOException e) {
121             throw new JobException("Error parsing job's data", uuid, e);
122         }
123     }
124
125     @Transient
126     public Map<String, Object> getData() {
127         return data.getCommandData().get(getType());
128     }
129
130     public void setSharedData(JobSharedData sharedData) {
131         this.data.setSharedData(sharedData);
132     }
133
134     @Override
135     @Transient
136     public JobSharedData getSharedData() {
137         return this.data.getSharedData();
138     }
139
140     @Override
141     public void setTypeAndData(JobType jobType, Map<String, Object> data) {
142         // *add* the data to map,
143         // then change state to given type
144         this.type = jobType;
145         this.data.getCommandData().put(jobType, data);
146     }
147
148     @Column(name = "TAKEN_BY")
149     public String getTakenBy() {
150         return takenBy;
151     }
152
153     public void setTakenBy(String takenBy) {
154         this.takenBy = takenBy;
155     }
156
157     @Type(type="org.hibernate.type.UUIDCharType")
158     @Column(name = "TEMPLATE_ID", columnDefinition = "CHAR(36)")
159     public UUID getTemplateId() {
160         return templateId;
161     }
162
163     @Override
164     public void setTemplateId(UUID templateId) {
165         this.templateId = templateId;
166     }
167
168     @Override
169     @Column(name="INDEX_IN_BULK")
170     public Integer getIndexInBulk() {
171         return indexInBulk;
172     }
173
174     @Override
175     public void setIndexInBulk(Integer indexInBulk) {
176         this.indexInBulk = indexInBulk;
177     }
178
179     @Column(name="USER_ID")
180     public String getUserId() {
181         return userId;
182     }
183
184     public void setUserId(String userId) {
185         this.userId = userId;
186     }
187
188     @Column(name="AGE")
189     public Integer getAge() {
190         return age;
191     }
192
193     public void setAge(Integer age) {
194         this.age = age;
195     }
196
197     @Column(name="DELETED_AT")
198     public Date getDeletedAt() {
199         return deletedAt;
200     }
201
202     public void setDeletedAt(Date deletedAt) {
203         this.deletedAt = deletedAt;
204     }
205
206     @Column(name = "BUILD", columnDefinition = "VARCHAR(100)")
207     public String getBuild() {
208         return build;
209     }
210
211     public void setBuild(String build) {
212         this.build = build;
213     }
214
215     @Override
216     public boolean equals(Object o) {
217         if (this == o) return true;
218         if (!(o instanceof JobDaoImpl)) return false;
219         JobDaoImpl daoJob = (JobDaoImpl) o;
220         return Objects.equals(getUuid(), daoJob.getUuid());
221     }
222
223     @Override
224     public int hashCode() {
225         return Objects.hash(getUuid());
226     }
227
228     @Override
229     public String toString() {
230         return MoreObjects.toStringHelper(this)
231                 .add("status", status)
232                 .add("type", type)
233                 .add("data", data)
234                 .add("templateId", templateId)
235                 .add("uuid", uuid)
236                 .add("takenBy", takenBy)
237                 .add("userId", userId)
238                 .add("age", age)
239                 .add("indexInBulk", indexInBulk)
240                 .add("deletedAt", deletedAt)
241                 .add("build", build)
242                 .toString();
243     }
244 }