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.google.common.base.MoreObjects;
27 import org.hibernate.annotations.DynamicUpdate;
28 import org.hibernate.annotations.SelectBeforeUpdate;
29 import org.hibernate.annotations.Type;
30 import org.onap.vid.exceptions.GenericUncheckedException;
31 import org.onap.vid.job.Job;
32 import org.onap.vid.job.JobException;
33 import org.onap.vid.job.JobType;
34 import org.onap.vid.model.VidBaseEntity;
36 import javax.persistence.*;
37 import java.io.IOException;
38 import java.util.Date;
40 import java.util.Objects;
41 import java.util.UUID;
43 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
46 The following 2 annotations let hibernate to update only fields that actually have been changed.
47 DynamicUpdate tell hibernate to update only dirty fields.
48 SelectBeforeUpdate is needed since during update the entity is detached (get and update are in different sessions)
53 @Table(name = "vid_job")
54 public class JobDaoImpl extends VidBaseEntity implements Job {
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;
69 @Column(name = "JOB_ID", columnDefinition = "CHAR(36)")
70 @Type(type="org.hibernate.type.UUIDCharType")
71 public UUID getUuid() {
75 public void setUuid(UUID uuid) {
79 //we use uuid instead id. So making id Transient
84 return this.getUuid().getLeastSignificantBits();
87 @Column(name = "JOB_STATUS")
88 @Enumerated(EnumType.STRING)
89 public Job.JobStatus getStatus() {
93 public void setStatus(Job.JobStatus status) {
97 @Enumerated(EnumType.STRING)
98 @Column(name = "JOB_TYPE")
99 public JobType getType() {
103 public void setType(JobType type) {
107 //the columnDefinition is relevant only for UT
108 @Column(name = "JOB_DATA", columnDefinition = "VARCHAR(30000)")
109 public String getDataRaw() {
111 return JACKSON_OBJECT_MAPPER.writeValueAsString(data);
112 } catch (JsonProcessingException e) {
113 throw new GenericUncheckedException(e);
117 public void setDataRaw(String data) {
119 this.data = JACKSON_OBJECT_MAPPER.readValue(data, JobData.class);
120 } catch (IOException e) {
121 throw new JobException("Error parsing job's data", uuid, e);
126 public Map<String, Object> getData() {
127 return data.getCommandData().get(getType());
130 public void setSharedData(JobSharedData sharedData) {
131 this.data.setSharedData(sharedData);
136 public JobSharedData getSharedData() {
137 return this.data.getSharedData();
141 public void setTypeAndData(JobType jobType, Map<String, Object> data) {
142 // *add* the data to map,
143 // then change state to given type
145 this.data.getCommandData().put(jobType, data);
148 @Column(name = "TAKEN_BY")
149 public String getTakenBy() {
153 public void setTakenBy(String takenBy) {
154 this.takenBy = takenBy;
157 @Type(type="org.hibernate.type.UUIDCharType")
158 @Column(name = "TEMPLATE_ID", columnDefinition = "CHAR(36)")
159 public UUID getTemplateId() {
164 public void setTemplateId(UUID templateId) {
165 this.templateId = templateId;
169 @Column(name="INDEX_IN_BULK")
170 public Integer getIndexInBulk() {
175 public void setIndexInBulk(Integer indexInBulk) {
176 this.indexInBulk = indexInBulk;
179 @Column(name="USER_ID")
180 public String getUserId() {
184 public void setUserId(String userId) {
185 this.userId = userId;
189 public Integer getAge() {
193 public void setAge(Integer age) {
197 @Column(name="DELETED_AT")
198 public Date getDeletedAt() {
202 public void setDeletedAt(Date deletedAt) {
203 this.deletedAt = deletedAt;
206 @Column(name = "BUILD", columnDefinition = "VARCHAR(100)")
207 public String getBuild() {
211 public void setBuild(String build) {
216 public boolean equals(Object o) {
217 if (this == o) return true;
218 if (!(o instanceof JobDaoImpl)) return false;
219 JobDaoImpl daoJob = (JobDaoImpl) o;
220 return Objects.equals(getUuid(), daoJob.getUuid());
224 public int hashCode() {
225 return Objects.hash(getUuid());
229 public String toString() {
230 return MoreObjects.toStringHelper(this)
231 .add("status", status)
234 .add("templateId", templateId)
236 .add("takenBy", takenBy)
237 .add("userId", userId)
239 .add("indexInBulk", indexInBulk)
240 .add("deletedAt", deletedAt)