Aharoni, Pavel (pa0916) | 8c70f07 | 2018-11-18 00:07:12 +0200 | [diff] [blame] | 1 | /*-
|
| 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 | */
|
| 20 | package org.onap.osam.job.dao.job;
|
| 21 |
|
| 22 |
|
| 23 | import com.fasterxml.jackson.core.JsonProcessingException;
|
| 24 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 25 | import com.google.common.base.MoreObjects;
|
| 26 | import lombok.Getter;
|
| 27 | import lombok.Setter;
|
| 28 | import lombok.extern.slf4j.Slf4j;
|
| 29 | import org.hibernate.annotations.DynamicUpdate;
|
| 30 | import org.hibernate.annotations.SelectBeforeUpdate;
|
| 31 | import org.hibernate.annotations.Type;
|
| 32 | import org.onap.osam.job.JobType;
|
| 33 | import org.onap.osam.job.impl.JobData;
|
| 34 | import org.onap.osam.job.impl.JobSharedData;
|
| 35 | import org.onap.osam.model.dao.BaseEntity;
|
| 36 | import org.springframework.data.annotation.CreatedDate;
|
| 37 | import org.springframework.data.annotation.LastModifiedDate;
|
| 38 |
|
| 39 | import javax.persistence.AttributeConverter;
|
| 40 | import javax.persistence.Column;
|
| 41 | import javax.persistence.Converter;
|
| 42 | import javax.persistence.Entity;
|
| 43 | import javax.persistence.EnumType;
|
| 44 | import javax.persistence.Enumerated;
|
| 45 | import javax.persistence.Lob;
|
| 46 | import java.io.IOException;
|
| 47 | import java.util.Date;
|
| 48 | import java.util.Map;
|
| 49 | import java.util.Objects;
|
| 50 | import 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
|
| 63 | public 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 | }
|