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