DiversityTree.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.genesys.server.model.impl;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PostLoad;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.UUID;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.blocks.model.UuidModel;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.blocks.util.ClassAclOid;
import org.genesys.custom.elasticsearch.IgnoreField;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.server.model.PublishState;
import org.genesys.util.MCPDUtil;
import org.hibernate.annotations.Type;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
/**
* The Class DiversityTree.
*
* @author Maxym Borodenko
*/
@Entity
@Table(name = "diversitytree", uniqueConstraints = @UniqueConstraint(name = "UQ_current_tree_version", columnNames={"versionsId", "current"}))
@Audited
@Document(indexName = "diversitytree")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid", scope = DiversityTree.class)
@Getter
@Setter
public class DiversityTree extends UuidModel implements AclAwareModel, SelfCleaning {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 7275026147867434825L;
private static final ClassAclOid<DiversityTree> PARENT_OID = ClassAclOid.forClass(DiversityTree.class);
/** The versions. */
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "versionsId", updatable = false)
@JsonIgnore
private DiversityTreeVersions versions;
/** The current. */
private Boolean current;
/** The title. */
@NotNull
@Size(min = 1, max = 250)
@Column(length = 250, nullable = false)
protected String title;
/** The description. */
@Column
@Lob
@Type(type = "org.hibernate.type.TextType")
protected String description;
/** The publisher. */
@Size(max = 250)
@Column(length = 250)
protected String publisher;
/** The date created. */
@Size(max = 100)
@Column(length = 100)
protected String dateCreated;
/** The accessionRefs. */
@OneToMany(mappedBy = "list", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
@Field(type = FieldType.Object)
@JsonIgnore
private List<DiversityTreeAccessionRef> accessionRefs;
/** The accession count. */
@NotNull
@Column(name = "accession_count", nullable = false)
private int accessionCount = 0;
/** The publish state. */
@Enumerated(EnumType.ORDINAL)
private PublishState state = PublishState.DRAFT;
/** The creators. */
@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "diversityTree", orphanRemoval = true, fetch = FetchType.LAZY)
@Field(type = FieldType.Object)
@JsonView({ JsonViews.Public.class })
private List<DiversityTreeCreator> creators;
/** The crop. */
@NotNull
@Size(max = Crop.CROP_SHORTNAME_LENGTH)
@Column(name = "crop", length = Crop.CROP_SHORTNAME_LENGTH, nullable = false)
private String crop;
/** The date when DiversityTree was authored. */
@Size(max = 8)
@Column(length = 8)
private String date;
/** The source. */
@Size(max = 200)
@Column(length = 200)
private String source;
/** The treeFile. */
@OneToOne(fetch = FetchType.LAZY, cascade = {}, optional = true, orphanRemoval = false)
@JoinColumn(name = "treeFileId", unique = true)
@IgnoreField
private RepositoryFile treeFile;
@Transient
private UUID currentVersion;
@PostLoad
protected void postLoad() {
if (this.versions != null && versions.getCurrentVersion() != null && !this.uuid.equals(versions.getCurrentVersion().getUuid())) {
this.currentVersion = versions.getCurrentVersion().getUuid();
}
}
@PrePersist
@PreUpdate
protected void prepersist() {
trimStringsToNull();
}
/*
* (non-Javadoc)
* @see org.genesys.blocks.security.model.AclAwareModel#aclParentObject()
*/
@Override
public AclAwareModel aclParentObject() {
return PARENT_OID;
}
/**
* Sets the date when DiversityTree was authored
*
* @param date the new date
*/
public void setDate(String date) {
if (MCPDUtil.isMcpdDate(date)) {
this.date = date;
}
}
/**
* Gets the UUID of current subset
*
* @return the UUID of current subset
*/
@JsonGetter
public UUID getCurrentVersion() {
return currentVersion;
}
/**
* Checks if published.
*
* @return the published
*/
public boolean isPublished() {
return this.state == PublishState.PUBLISHED;
}
@Override
public boolean canEqual(Object other) {
return other instanceof DiversityTree;
}
}