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