AccessionData.java

/*
 * Copyright 2014 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.genesys;

import java.io.Serializable;
import java.text.MessageFormat;
import java.time.Instant;
import java.util.UUID;

import javax.persistence.CascadeType;
import javax.persistence.Column;
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.MappedSuperclass;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.auditlog.annotations.NotAudited;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.IdUUID;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.server.model.impl.AccessionIdentifier3;
import org.genesys.server.model.impl.Country;
import org.genesys.server.model.impl.Crop;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.spring.validation.javax.IntegerOneOf;
import org.genesys.util.NumberUtils;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.Type;
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.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonView;
import com.querydsl.core.annotations.QueryInit;

@MappedSuperclass
@Getter
@Setter
@NoArgsConstructor
public abstract class AccessionData extends AuditedVersionedModel implements IdUUID, AccessionIdentifier3, Serializable, SelfCleaning {

	private static final long serialVersionUID = -3428918862058441943L;

	public AccessionData(UUID uuid) {
		this.accessionId = new AccessionId(uuid);
	}


	@Valid
	@MapsId
	@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER, optional = false, orphanRemoval = false)
	@JoinColumn(name = "id")
	@JsonUnwrapped
	@JsonIgnoreProperties({ "id", "version", "active", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate", "_class" })
	@QueryInit({ "coll.*", "pdci.*", "lists.*", "climate.*" })
	@Field(type = FieldType.Auto)
	private AccessionId accessionId;

	@Pattern.List({
		@Pattern(regexp = "^10\\.[0-9]+(\\.[0-9]+)*/.+"),
		@Pattern(regexp = "^10\\.(?!0155).+", message = "GLIS test DOI is not allowed")
	})
	@Column(name = "doi", length = 100)
	private String doi;

	@Size(max = 100)
	@Column(name = "dataProviderId", unique = true, length = 100)
	private String dataProviderId;

	@Size(max = 10)
	@Pattern(regexp = "[A-Z]{3}\\d{3,4}")
	@NotNull
	@Column(name = "instCode", length = 10, nullable = false)
	private String instituteCode;

	@Size(max = 100)
	@Column(nullable = false, length = 100)
	private String genus;

	@NotNull
	@ManyToOne(cascade = {}, optional = false)
	@JoinColumn(name = "instituteId")
	@JsonView({ JsonViews.Minimal.class })
	@Field(type = FieldType.Object)
	@JsonIgnoreProperties({"settings", "pdciHistogram", "statisticsPDCI"})
	@QueryInit({ "country.*", "owner.*"})
	private FaoInstitute institute;

	@Size(max = 128)
	@NotNull
	@Column(name = "acceNumb", nullable = false, length = 128)
	private String accessionNumber;

	@Column(name = "seqNo", nullable = false)
	private double seqNo = 0;

	@Size(max = 100)
	@Column(name = "cropName", nullable = true, length = 100)
	private String cropName;

	@ManyToOne(cascade = {}, optional = true, fetch = FetchType.LAZY)
	@JoinColumn(name = "cropId")
	@Field(type = FieldType.Object)
	private Crop crop;

	@NotNull
	@ManyToOne(cascade = {}, optional = false)
	@JoinColumn(name = "taxonomyId2")
	@JsonView({ JsonViews.Minimal.class })
	@Field(type = FieldType.Object)
	@QueryInit({ "currentTaxonomySpecies.*", "grinTaxonomySpecies.*" })
	private Taxonomy2 taxonomy;

	// FIXME: Remove?
	@Size(max = 3)
	@Column(name = "acqSrc", length = 3)
	private String acquisitionSource;

	@Size(max = 12)
	@Column(name = "acqDate", length = 12)
	private String acquisitionDate;

	@Size(max = 3)
	@Column(name = "origCty", length = 3)
	private String origCty;

	@ManyToOne(cascade = {}, optional = true)
	@JoinColumn(name = "orgCtyId", nullable = true)
	@JsonView({ JsonViews.Public.class })
	@Field(type = FieldType.Object)
//	@QueryInit({ "region.*" })
	private Country countryOfOrigin;

	@Column(name = "sampStat", length = 3)
	@IntegerOneOf(value = {100,110,120,130,200,300,400,410,411,412,413,414,415,416,420,421,422,423,500,600,999})
	private Integer sampStat;

	@Column(name = "inSGSV")
	private boolean inSvalbard;

	@Column(name = "inTrust")
	private Boolean inTrust;

	@Column(name = "available")
	private Boolean available;

	@NotNull
	@Column(name = "historic", nullable = false)
	private boolean historic = false;
	
	@Column(name = "aegis")
	private Boolean aegis = false;

	@Column(name = "mlsStat")
	private Boolean mlsStatus;

	@Size(max = 300)
	@Column(name = "acceurl", length = 300, nullable = true)
	private String acceUrl;

	@Column(name = "names")
	@Lob
	@Type(type = "org.hibernate.type.TextType")
	private String accNames;

	@Column(name = "otherIds")
	@Lob
	@Type(type = "org.hibernate.type.TextType")
	private String otherIds;

	@Size(max = 7)
	@Pattern(regexp = "[A-Z]{3}\\d{3,4}")
	@Column(name = "donorCode", length = 7)
	private String donorCode;

	@Size(max = 300)
	@Column(name = "donorName", length = 300)
	private String donorName;

	@Size(max = 200)
	@Column(name = "donorNumb", length = 200)
	private String donorNumb;

	@Column(name = "ancest")
	@Lob
	@Type(type = "org.hibernate.type.TextType")
	private String ancest;

	@Column(length = 10)
	@Enumerated(EnumType.STRING)
	@Field(type = FieldType.Keyword)
	private CurationType curationType;

	@JsonIgnore
	@NotAudited
	@Column(name = "lastChangedDate")
	private Instant lastChangedDate;
	
	// This is a fake field for ES indexing
	@Transient
	@JsonInclude
	private boolean sgsv;


	@Formula("(SELECT alias.name FROM accession_alias alias WHERE alias.accessionId = id AND alias.aliasType = 0 order by alias.id limit 1)")
	private String accessionName;

	public enum CurationType {
		FULL,
		PARTIAL,
		ARCHIVED,
		HISTORICAL,
	}

	/**
	 * Update MCPD {@link #storageStr}
	 */
	@PrePersist
	@PreUpdate
	private void prePersist() {
		trimStringsToNull();

		if (this.curationType != null) {
			this.historic = this.curationType == CurationType.HISTORICAL;
		}
		this.seqNo = NumberUtils.numericValue(this.accessionNumber);

		if (this.countryOfOrigin != null) {
			this.origCty = this.countryOfOrigin.getCode3();
		} else {
			this.origCty = null;
		}
		this.genus = getTaxonomy().getGenus();
		this.instituteCode = getInstitute().getCode();
		this.inSvalbard = this.accessionId.getDuplSite() != null && this.accessionId.getDuplSite().contains("NOR051");
		if (this.lastChangedDate == null) {
			this.lastChangedDate = this.getCreatedDate();
		}
	}
	
	@Override
	@JsonIgnore
	public String getHoldingInstitute() {
		// We use this for incoming JSONs that don't have instituteCode (yet), but have institute
		if (instituteCode == null && institute != null) {
			return institute.getCode();
		}
		return instituteCode;
	}

	public String getGenus() {
		// We use this for incoming JSONs that don't have genus (yet), but have taxonomy
		if (genus == null && taxonomy != null) {
			return taxonomy.getGenus();
		}
		return genus;
	}

	/**
	 * @deprecated We're not using this anywhere!
	 */
	@Deprecated
	public String getAcquisitionSource() {
		return this.acquisitionSource;
	}

	/**
	 * @deprecated We're not using this anywhere!
	 */
	@Deprecated
	public void setAcquisitionSource(final String acquisitionSource) {
		this.acquisitionSource = acquisitionSource;
	}

	@Override
	@Transient
	@JsonIgnore
	public UUID getUuid() {
		return accessionId.getUuid();
	}

	@Override
	public String toString() {
		return MessageFormat.format("Accession id={0,number,##########} UUID={1} {2} {3} {4}", accessionId.getId(), getUuid(), instituteCode, accessionNumber, genus);
	}

	@JsonGetter
	public String getAccessionName() {
		return this.accessionName;
	}

	@Transient
	@JsonIgnore
	public Integer getSampleStatus() {
		return this.sampStat;
	}

	@Transient
	@JsonIgnore
	// For EL
	public Boolean getAvailability() {
		return this.available;
	}

	@Transient
	@JsonIgnore
	// For EL
	public String getInstCode() {
		return this.instituteCode;
	}

	// TODO Obsolete this or #getInSvalbard()
	public boolean getSgsv() {
		return this.inSvalbard;
	}

	@JsonInclude
	public Boolean getImages() {
		return this.accessionId != null && this.accessionId.getImageCount() > 0;
	}
}