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