blob: 807221fcad988036f5a72a866e1bce3b24565883 [file] [log] [blame]
Aharoni, Pavel (pa0916)8c70f072018-11-18 00:07:12 +02001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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=========================================================
19 */
20package org.onap.osam.job.dao.job;
21
22
23import com.fasterxml.jackson.core.JsonProcessingException;
24import com.fasterxml.jackson.databind.ObjectMapper;
25import com.google.common.base.MoreObjects;
26import lombok.Getter;
27import lombok.Setter;
28import lombok.extern.slf4j.Slf4j;
29import org.hibernate.annotations.DynamicUpdate;
30import org.hibernate.annotations.SelectBeforeUpdate;
31import org.hibernate.annotations.Type;
32import org.onap.osam.job.JobType;
33import org.onap.osam.job.impl.JobData;
34import org.onap.osam.job.impl.JobSharedData;
35import org.onap.osam.model.dao.BaseEntity;
36import org.springframework.data.annotation.CreatedDate;
37import org.springframework.data.annotation.LastModifiedDate;
38
39import javax.persistence.AttributeConverter;
40import javax.persistence.Column;
41import javax.persistence.Converter;
42import javax.persistence.Entity;
43import javax.persistence.EnumType;
44import javax.persistence.Enumerated;
45import javax.persistence.Lob;
46import java.io.IOException;
47import java.util.Date;
48import java.util.Map;
49import java.util.Objects;
50import java.util.UUID;
51
52/*
53 The following 2 annotations let hibernate to update only fields that actually have been changed.
54 DynamicUpdate tell hibernate to update only dirty fields.
55 SelectBeforeUpdate is needed since during update the entity is detached (get and update are in different sessions)
56*/
57@DynamicUpdate()
58@SelectBeforeUpdate()
59@Entity
60@Getter
61@Setter
62@Slf4j
63public class OsamJob extends BaseEntity {
64
65 @Column(unique = true, nullable = false, columnDefinition = "CHAR(36)")
66 @Type(type="org.hibernate.type.UUIDCharType")
67 private UUID uuid;
68
69 @CreatedDate
70 private Date createdDate;
71
72 @LastModifiedDate
73 private Date modifiedDate;
74
75 @Enumerated(value = EnumType.STRING)
76 private JobStatus status;
77
78 @Enumerated(value = EnumType.STRING)
79 private JobType type;
80
81 @Lob
82 @Column
83 private JobData data = new JobData();
84
85 @Column
86 private String takenBy;
87
88 @Column
89 private String userId;
90
91 @Column(nullable = false)
92 private Integer age = 0;
93
94 @Column(nullable = false)
95 private Integer indexInBulk = 0;
96
97 @Column
98 private Date deletedAt;
99
100 public Map<String, Object> getDataMap() {
101 return data.getCommandData().get(getType());
102 }
103
104 public void setTypeAndData(JobType jobType, Map<String, Object> data) {
105 // *add* the data to map,
106 // then change state to given type
107 this.type = jobType;
108 this.data.getCommandData().put(jobType, data);
109 }
110
111 public void setSharedData(JobSharedData sharedData) {
112 this.data.setSharedData(sharedData);
113 }
114
115 public JobSharedData getSharedData(){
116 return this.data.getSharedData();
117 };
118
119 @Override
120 public boolean equals(Object o) {
121 if (this == o) return true;
122 if (!(o instanceof OsamJob)) return false;
123 OsamJob daoJob = (OsamJob) o;
124 return Objects.equals(getUuid(), daoJob.getUuid());
125 }
126
127 @Override
128 public int hashCode() {
129 return Objects.hash(getUuid());
130 }
131
132 @Override
133 public String toString() {
134 return MoreObjects.toStringHelper(this)
135 .add("status", status)
136 .add("type", type)
137 .add("uuid", uuid)
138 .add("takenBy", takenBy)
139 .add("userId", userId)
140 .add("age", age)
141 .add("createdDate", createdDate)
142 .add("modifiedDate", modifiedDate)
143 .add("deletedAt", deletedAt)
144 .add("data", data)
145 .toString();
146 }
147
148 @Converter(autoApply = true)
149 public static class JobDataConverter implements AttributeConverter<JobData, String> {
150
151 @Override
152 public String convertToDatabaseColumn(JobData jobData) {
153 if( jobData == null )
154 return null;
155
156 ObjectMapper mapper = new ObjectMapper();
157
158 try {
159 return mapper.writeValueAsString(jobData);
160 } catch (JsonProcessingException e) {
161 log.error("Couldn't persist JobData object {}, error: {}. Persisting null", jobData, e);
162 return null;
163 }
164 }
165
166 @Override
167 public JobData convertToEntityAttribute(String s) {
168 if( s == null )
169 return null;
170
171 ObjectMapper mapper = new ObjectMapper();
172
173 try {
174 return mapper.readValue(s, JobData.class);
175 } catch (IOException e) {
176 log.error("Couldn't deserialize {} to JobData object, error: {}. Returning null", s, e);
177 return null;
178 }
179 }
180 }
181}