aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/main/java/org/onap/vid/job/impl/JobDaoImpl.java
blob: 1ff1296c7c799ac703089fff5231db90e8972c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package org.onap.vid.job.impl;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.MoreObjects;
import org.hibernate.annotations.Type;
import org.onap.vid.exceptions.GenericUncheckedException;
import org.onap.vid.job.Job;
import org.onap.vid.job.JobType;
import org.onap.vid.model.VidBaseEntity;

import javax.persistence.*;
import java.io.IOException;
import java.util.*;

@Entity
@Table(name = "vid_job")
public class JobDaoImpl extends VidBaseEntity implements Job {

    private static ObjectMapper objectMapper = new ObjectMapper();
    private Job.JobStatus status;
    private JobType type;
    private Map<JobType, Map<String, Object>> data = new TreeMap<>();
    private UUID templateId;
    private UUID uuid;
    private String takenBy;
    private String userId;
    private Integer age = 0;
    private Integer indexInBulk = 0;
    private Date deletedAt;

    @Id
    @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
    @Type(type="org.hibernate.type.UUIDCharType")
    public UUID getUuid() {
        return uuid;
    }

    public void setUuid(UUID uuid) {
        this.uuid = uuid;
    }

    //we use uuid instead id. So making id Transient
    @Override
    @Transient
    @JsonIgnore
    public Long getId() {
        return this.getUuid().getLeastSignificantBits();
    }

    @Column(name = "JOB_STATUS")
    @Enumerated(EnumType.STRING)
    public Job.JobStatus getStatus() {
        return status;
    }

    public void setStatus(Job.JobStatus status) {
        this.status = status;
    }

    @Enumerated(EnumType.STRING)
    @Column(name = "JOB_TYPE")
    public JobType getType() {
        return type;
    }

    public void setType(JobType type) {
        this.type = type;
    }

    //the columnDefinition is relevant only for UT
    @Column(name = "JOB_DATA", columnDefinition = "VARCHAR(30000)")
    public String getDataRaw() {
        try {
            return objectMapper.writeValueAsString(data);
        } catch (JsonProcessingException e) {
            throw new GenericUncheckedException(e);
        }
    }

    public void setDataRaw(String data) {
        try {
            this.data = objectMapper.readValue(data, new TypeReference<Map<JobType, Map<String, Object>>>() {
            });
        } catch (IOException e) {
            throw new GenericUncheckedException(e);
        }
    }

    @Transient
    public Map<String, Object> getData() {
        return data.get(getType());
    }

    @Override
    public void setTypeAndData(JobType jobType, Map<String, Object> data) {
        // *add* the data to map,
        // then change state to given type
        this.type = jobType;
        this.data.put(jobType, data);
    }

    @Column(name = "TAKEN_BY")
    public String getTakenBy() {
        return takenBy;
    }

    public void setTakenBy(String takenBy) {
        this.takenBy = takenBy;
    }

    @Type(type="org.hibernate.type.UUIDCharType")
    @Column(name = "TEMPLATE_ID", columnDefinition = "CHAR(36)")
    public UUID getTemplateId() {
        return templateId;
    }

    @Override
    public void setTemplateId(UUID templateId) {
        this.templateId = templateId;
    }

    @Column(name="INDEX_IN_BULK")
    public Integer getIndexInBulk() {
        return indexInBulk;
    }

    @Override
    public void setIndexInBulk(Integer indexInBulk) {
        this.indexInBulk = indexInBulk;
    }

    @Column(name="USER_ID")
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Column(name="AGE")
    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Column(name="DELETED_AT")
    public Date getDeletedAt() {
        return deletedAt;
    }

    public void setDeletedAt(Date deletedAt) {
        this.deletedAt = deletedAt;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof JobDaoImpl)) return false;
        JobDaoImpl daoJob = (JobDaoImpl) o;
        return Objects.equals(getUuid(), daoJob.getUuid());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getUuid());
    }

    @Override
    public String toString() {
        return MoreObjects.toStringHelper(this)
                .add("status", status)
                .add("type", type)
                .add("templateId", templateId)
                .add("uuid", uuid)
                .add("takenBy", takenBy)
                .add("userId", userId)
                .add("age", age)
                .add("created", created)
                .add("modified", modified)
                .add("deletedAt", deletedAt)
                .add("data", data)
                .toString();
    }
}