2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.job.impl;
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;
37 import javax.persistence.*;
38 import java.io.IOException;
39 import java.util.Date;
41 import java.util.Objects;
42 import java.util.UUID;
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)
52 @Table(name = "vid_job")
53 public class JobDaoImpl extends VidBaseEntity implements Job {
55 private static ObjectMapper objectMapper = new ObjectMapper();
56 private Job.JobStatus status;
58 private JobData data = new JobData();
59 private UUID templateId;
61 private String takenBy;
62 private String userId;
63 private Integer age = 0;
64 private Integer indexInBulk = 0;
65 private Date deletedAt;
68 @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
69 @Type(type="org.hibernate.type.UUIDCharType")
70 public UUID getUuid() {
74 public void setUuid(UUID uuid) {
78 //we use uuid instead id. So making id Transient
83 return this.getUuid().getLeastSignificantBits();
86 @Column(name = "JOB_STATUS")
87 @Enumerated(EnumType.STRING)
88 public Job.JobStatus getStatus() {
92 public void setStatus(Job.JobStatus status) {
96 @Enumerated(EnumType.STRING)
97 @Column(name = "JOB_TYPE")
98 public JobType getType() {
102 public void setType(JobType type) {
106 //the columnDefinition is relevant only for UT
107 @Column(name = "JOB_DATA", columnDefinition = "VARCHAR(30000)")
108 public String getDataRaw() {
110 return objectMapper.writeValueAsString(data);
111 } catch (JsonProcessingException e) {
112 throw new GenericUncheckedException(e);
116 public void setDataRaw(String data) {
118 this.data = objectMapper.readValue(data, JobData.class);
119 } catch (IOException e) {
120 throw new JobException("Error parsing job's data", uuid, e);
125 public Map<String, Object> getData() {
126 return data.getCommandData().get(getType());
129 public void setSharedData(JobSharedData sharedData) {
130 this.data.setSharedData(sharedData);
135 public JobSharedData getSharedData() {
136 return this.data.getSharedData();
140 public void setTypeAndData(JobType jobType, Map<String, Object> data) {
141 // *add* the data to map,
142 // then change state to given type
144 this.data.getCommandData().put(jobType, data);
147 @Column(name = "TAKEN_BY")
148 public String getTakenBy() {
152 public void setTakenBy(String takenBy) {
153 this.takenBy = takenBy;
156 @Type(type="org.hibernate.type.UUIDCharType")
157 @Column(name = "TEMPLATE_ID", columnDefinition = "CHAR(36)")
158 public UUID getTemplateId() {
163 public void setTemplateId(UUID templateId) {
164 this.templateId = templateId;
168 @Column(name="INDEX_IN_BULK")
169 public Integer getIndexInBulk() {
174 public void setIndexInBulk(Integer indexInBulk) {
175 this.indexInBulk = indexInBulk;
178 @Column(name="USER_ID")
179 public String getUserId() {
183 public void setUserId(String userId) {
184 this.userId = userId;
188 public Integer getAge() {
192 public void setAge(Integer age) {
196 @Column(name="DELETED_AT")
197 public Date getDeletedAt() {
201 public void setDeletedAt(Date deletedAt) {
202 this.deletedAt = deletedAt;
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());
214 public int hashCode() {
215 return Objects.hash(getUuid());
219 public String toString() {
220 return MoreObjects.toStringHelper(this)
221 .add("status", status)
223 .add("templateId", templateId)
225 .add("takenBy", takenBy)
226 .add("userId", userId)
228 .add("created", created)
229 .add("modified", modified)
230 .add("deletedAt", deletedAt)