Subset.java
/*
* Copyright 2018 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 java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
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 lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.annotations.NotCopyable;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.Publishable;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.server.model.Partner;
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;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
/**
* The Class Subset.
*/
@Entity
@Table(name = "subset", uniqueConstraints = @UniqueConstraint(name = "UQ_current_subs_version", columnNames={"versionsId", "current"}))
@Audited
@Document(indexName = "subset")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid", scope = Subset.class)
@Getter
@Setter
@NoArgsConstructor
public class Subset extends TranslatedUuidModel<SubsetLang, Subset> implements AclAwareModel, Publishable, SelfCleaning {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 7021405309572916429L;
/** The versions. */
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.REFRESH }, optional = false, fetch = FetchType.LAZY)
@JoinColumn(name = "versionsId", updatable = false)
@JsonIgnore
private SubsetVersions versions;
private Boolean current;
/** The title. */
@NotNull
@Size(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 wiews code. */
@Size(max = 9)
@Column(name = "wiews_code", length = 9, nullable = true)
private String wiewsCode;
/** The institute. */
@ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "institute_id", updatable = false)
@JsonView({ JsonViews.Public.class })
@JsonIgnoreProperties({"owner"})
@Field(type = FieldType.Object)
private FaoInstitute institute;
@ManyToOne(cascade = {}, optional = false)
@JoinColumn(name = "owner_id", updatable = false)
@JsonView({ JsonViews.Public.class })
@Field(type = FieldType.Object)
@NotNull
private Partner owner;
/** The accessionRefs. */
@OneToMany(mappedBy = "list", cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY)
@Field(type = FieldType.Object)
@JsonIgnore
private List<SubsetAccessionRef> 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 = "subset", orphanRemoval = true, fetch = FetchType.LAZY)
@Field(type = FieldType.Object)
@JsonView({ JsonViews.Public.class })
@OrderBy("position")
private List<SubsetCreator> creators;
/** The crops. */
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "subset_crops", joinColumns = @JoinColumn(name = "subsetId"), indexes = { @Index(columnList = "subsetId, crop") })
@Column(name = "crop", nullable = false, length = Crop.CROP_SHORTNAME_LENGTH)
@JsonView({ JsonViews.Minimal.class })
private Set<String> crops;
/** The date when subset was authored. */
@Size(max = 8)
@Column(length = 8)
private String date;
/** The source. */
@Size(max = 200)
@Column(length = 200)
private String source;
public static enum SubsetType {
REPRESENTATIVE,
SELECTIVE
}
@Enumerated(EnumType.STRING)
@Column(length = 20, nullable = true)
private SubsetType subsetType;
@Column(length = 200, nullable = true)
private String selectionMethod;
@Column(name = "firstPublishedDate")
@NotCopyable
private Instant firstPublishedDate;
@Transient
private UUID currentVersion;
public Subset(Long id) {
super.setId(id);
}
@PostLoad
protected void postLoad() {
if (this.versions != null && versions.getCurrentVersion() != null && !this.uuid.equals(versions.getCurrentVersion().getUuid())) {
this.currentVersion = versions.getCurrentVersion().getUuid();
}
}
/**
* Gets the UUID of current subset
*
* @return the UUID of current subset
*/
@JsonGetter
public UUID getCurrentVersion() {
return currentVersion;
}
/**
* Generate UUID if missing.
*/
@PrePersist
@PreUpdate
protected void prepersist() {
trimStringsToNull();
if (this.institute != null) {
this.wiewsCode = this.institute.getCode();
}
if (Objects.isNull(firstPublishedDate) && Objects.equals(state, PublishState.PUBLISHED)) {
firstPublishedDate = Instant.now();
}
// if (this.getAccessionRefs() != null) {
// this.accessionCount = this.getAccessionRefs().size();
// }
}
/*
* (non-Javadoc)
* @see org.genesys.blocks.security.model.AclAwareModel#aclParentObject()
*/
@Override
public AclAwareModel aclParentObject() {
return null;
}
/**
* Gets the version tag.
*
* @return the versionTag
*/
@Override
@JsonIgnore
public String getVersionTag() {
return getCreatedDate() != null ? new SimpleDateFormat("yyyy-MM-dd").format(getCreatedDate()) : null;
}
/**
* Checks if published.
*
* @return the published
*/
@Override
public boolean isPublished() {
return this.state == PublishState.PUBLISHED;
}
/**
* Sets the date when subset was authored
*
* @param date the new date
*/
public void setDate(String date) {
if (MCPDUtil.isMcpdDate(date)) {
this.date = date;
}
}
@Override
public boolean canEqual(Object other) {
return other instanceof Subset;
}
}