AuditedModel.java
/*
* Copyright 2020 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gringlobal.model;
import java.time.Instant;
import java.util.function.Function;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.auditlog.annotations.NotAudited;
import org.genesys.blocks.model.EmptyModel;
import org.genesys.blocks.model.EntityId;
import org.genesys.blocks.util.JsonSidConverter;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.swagger.v3.oas.annotations.media.Schema;
/**
* The Class AuditedModel.
*/
@Cacheable
@MappedSuperclass
@Getter
@Setter
public abstract class AuditedModel extends EmptyModel implements DateVersionEntityId {
private static final long serialVersionUID = 1L;
// @ManyToOne(fetch = FetchType.LAZY, cascade = {})
// @JoinColumn(name = "created_by", nullable = false)
@Column(name = "created_by", nullable = false)
@CreatedBy
@JsonSerialize(converter = JsonSidConverter.class)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long createdBy;
@Column(name = "created_date", nullable = false)
@CreatedDate
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Instant createdDate;
// @ManyToOne(fetch = FetchType.LAZY, cascade = {})
// @JoinColumn(name = "modified_by", nullable = true)
@Column(name = "modified_by", nullable = true)
@LastModifiedBy
@JsonSerialize(converter = JsonSidConverter.class)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long modifiedBy;
@Version
@Column(name = "modified_date", precision = 8)
// @LastModifiedDate
// @JsonProperty(access = JsonProperty.Access.READ_ONLY)
@NotAudited
@Schema(required = true, title = "Last modified date", description = "Maintained by the server, original value must be included in all update and delete operations.")
protected Instant modifiedDate;
// @Override
// public int hashCode() {
// final int prime = 31;
// int result = 1;
// result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
// return result;
// }
//
// @Override
// public boolean equals(Object obj) {
// if (this == obj) {
// return true;
// }
// if (obj == null) {
// return false;
// }
// if (getClass() != obj.getClass()) {
// return false;
// }
// AuditedModel other = (AuditedModel) obj;
// if (getId() == null) {
// return super.equals(obj);
// } else if (getId().equals(other.getId())) {
// return true;
// }
// return super.equals(obj);
// }
@Override
public String toString() {
return this.getClass() + "#id=" + getId();
}
protected final void lazyLoad(EntityId entity) {
lazyLoad(entity, false);
}
protected final void lazyLoad(EntityId entity, boolean deepLazyLoad) {
if (entity != null) {
entity.getId();
if (deepLazyLoad && entity instanceof LazyLoading<?>) {
((LazyLoading<?>) entity).lazyLoad();
}
}
}
protected <SOURCE, TARGET> TARGET mapNullable(SOURCE source, @NotNull Function<SOURCE, TARGET> mapper) {
if (source == null) return null;
return mapper.apply(source);
}
}