AccessionId.java

/*
 * Copyright 2019 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.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import javax.persistence.CascadeType;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.Index;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.IdUUID;
import org.genesys.blocks.model.JsonViews;
import org.genesys.custom.elasticsearch.IgnoreField;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.server.model.impl.TileClimate;
import org.genesys.server.service.AccessionService;
import org.genesys.spring.validation.javax.EachIntegerOneOf;
import org.genesys.util.TileIndexCalculator;
import org.genesys.worldclim.WorldClimUtil;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Type;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

/**
 * Entity holds the assigned accession identifiers regardless of active or
 * historic records.
 */
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "acce", indexes = { 
		@Index(name = "IX_imageCount", columnList = "imageCount"),
		@Index(name = "IX_acce_tile3", columnList = "tileIndex3min") })
@Audited
@Getter
@Setter
@NoArgsConstructor
public class AccessionId extends AuditedVersionedModel implements IdUUID {

	/**
	 *
	 */
	private static final long serialVersionUID = -6224417080504343264L;

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

//	@JsonIgnore
//	@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, orphanRemoval = false, mappedBy = "accessionId")
//	private Accession accession;
//
//	@JsonIgnore
//	@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, orphanRemoval = false, mappedBy = "accessionId")
//	private AccessionHistoric accessionH;
	
	@Column(updatable = false)
	@Type(type = "uuid-binary")
	protected UUID uuid;

	@ManyToMany(cascade = {}, fetch = FetchType.LAZY)
	@JoinTable(name = "accession_listitem", joinColumns = @JoinColumn(name = "acceid"), inverseJoinColumns = @JoinColumn(name = "listid"))
	@JsonView({ JsonViews.Indexed.class })
	@JsonIdentityReference(alwaysAsId = true)
	@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid", scope = AccessionList.class)
	@Field(type = FieldType.Keyword)
	private Set<AccessionList> lists;

	@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, optional = true, orphanRemoval = true)
	@JoinColumn(name = "pdciId", unique = true)
	@JsonIgnoreProperties({ "accession" })
	@JsonView({ JsonViews.Internal.class })
	@IgnoreField
	private PDCI pdci;

	@Column(name = "storage", nullable = false)
	@EachIntegerOneOf(value = {10, 11, 12, 13, 20, 30, 40, 50, 99})
	@ElementCollection(fetch = FetchType.LAZY)
	@CollectionTable(name = "accession_storage", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"))
	@OrderBy("storage")
	@Field(type = FieldType.Integer)
	private Set<Integer> storage;

	@Column(name = "duplSite", nullable = false, length = 7)
	@ElementCollection(fetch = FetchType.LAZY)
	@CollectionTable(name = "accession_duplsite", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"))
	@OrderBy("duplSite")
	@Field(type = FieldType.Keyword)
	private Set<@Pattern(regexp = "[A-Z]{3}\\d{3,4}") @Size(max = 7) String> duplSite;

	@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, optional = true, orphanRemoval = true)
	@JoinColumn(name = "collId", unique = true)
	@JsonIgnoreProperties({ "accession" })
	@Field(type = FieldType.Object)
	private AccessionCollect coll;

	@Valid
	@OneToMany(mappedBy = "accession", cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true)
	@JsonIgnoreProperties({ "accession" })
	@Field(type = FieldType.Nested)
	@OrderBy("id")
	private List<AccessionAlias> aliases;

	@OneToMany(mappedBy = "accession", cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true)
	@JsonIgnoreProperties({ "accession" })
	@Field(type = FieldType.Nested)
	@OrderBy("id")
	private List<AccessionRemark> remarks;

	@Column(name = "breederCode", nullable = false, length = 7)
	@ElementCollection(fetch = FetchType.LAZY)
	@CollectionTable(name = "accession_breedercode", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"))
	@Field(type = FieldType.Keyword)
	private Set<@Pattern(regexp = "[A-Z]{3}\\d{3,4}") @Size(max = 7) String> breederCode;

	@Column(name = "breederName", nullable = false, length = 250)
	@ElementCollection(fetch = FetchType.LAZY)
	@CollectionTable(name = "accession_breedername", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"))
	@Field(type = FieldType.Text)
	private Set<@Size(max = 250) String> breederName;

	/** Number of images in the accession */
	@ColumnDefault("0")
	private int imageCount;

	/** Number of associated published subsets. */
	private long subsetCount = 0;

	/** Number of associated published datasets. */
	private long datasetCount = 0;

	@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, optional = true, orphanRemoval = true)
	@JoinColumn(name = "folderId", unique = true)
	@JsonIgnore
	private RepositoryFolder repositoryFolder;

	// Geo
	@Field(store = true)
	@Column(name = "longitude")
	private Double longitude;

	@Field(store = true)
	@Column(name = "latitude")
	private Double latitude;

	@Column(name = "elevation")
	private Double elevation;

	@Column(name = "coordUncertainty")
	private Double coordinateUncertainty;

	@Column(name = "coordDatum", length = 100)
	private String coordinateDatum;

	@Column(name = "georefMeth", length = 100)
	private String georeferenceMethod;

	private Long tileIndex;

	@Column(name = "tileIndex3min")
	private Integer tileIndex3min;

	@ManyToOne(fetch = FetchType.LAZY, optional = true, cascade = { })
	@NotFound(action = NotFoundAction.IGNORE)
	@JoinColumn(name = "tileIndex", nullable = true, insertable = false, updatable = false, foreignKey = @ForeignKey(name = "FKNOTILECONSTRAINT", value = ConstraintMode.NO_CONSTRAINT))
	@JsonView({ JsonViews.Protected.class })
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	@JsonIgnoreProperties({ "tileIndex" })
	@Field(type = FieldType.Object)
	private TileClimate climate;

	@PrePersist
	@PreUpdate
	private void prepersist() {
		if (uuid == null) {
			uuid = UUID.randomUUID();
		}
		
//		// Remove empty AccessionGeo
//		if (this.geo != null && this.geo.isEmpty()) {
//			this.geo = null;
//		}
		
		// Remove empty AccessionCollect
		if (this.coll != null && this.coll.isEmpty()) {
			this.coll = null;
		}
		
		// Remove empty AccessionAlias
		if (this.aliases != null && !this.aliases.isEmpty()) {
			List<AccessionAlias> empties = this.aliases.stream().filter(alias -> alias == null || alias.isEmpty()).collect(Collectors.toList());
			this.aliases.removeAll(empties);
		}

		// Remove empty AccessionRemark
		if (this.remarks != null && !this.remarks.isEmpty()) {
			List<AccessionRemark> empties = this.remarks.stream().filter(remark -> remark == null || remark.isEmpty()).collect(Collectors.toList());
			this.remarks.removeAll(empties);
		}

		this.storage = removeEmpty(this.storage);
		this.duplSite = removeEmpty(this.duplSite);
		this.breederCode = removeEmpty(this.breederCode);
		this.breederName = removeEmpty(this.breederName);

		this.coordinateDatum = StringUtils.trimToNull(this.coordinateDatum);
		this.georeferenceMethod = StringUtils.trimToNull(this.georeferenceMethod);

		// Both must be provided
		if ((this.longitude == null && this.latitude != null) || (this.longitude != null && this.latitude == null)) {
			this.latitude = null;
			this.longitude = null;
		}

		// Treat (0, 0) as null
		if ((this.longitude != null && this.longitude == 0) && (this.latitude != null && this.latitude == 0)) {
			this.longitude = null;
			this.latitude = null;
		}

		if (this.longitude == null && this.latitude == null) {
			// Free to clear useless data
			this.coordinateDatum = null;
			this.coordinateUncertainty = null;
			this.georeferenceMethod = null;
			this.tileIndex = null;
			this.tileIndex3min = null;
		
		} else {
			// Update tile indexes
			tileIndex = WorldClimUtil.getWorldclim25Tile(this.longitude, this.latitude);
			tileIndex3min = TileIndexCalculator.get3MinuteTileIndex(this.longitude, this.latitude);
		}
	}

	@PreRemove
	private void preRemove() {
		// Just so that TileClimate isn't touched!
		this.tileIndex = null;
		this.climate = null;
	}

	private static <T> Set<T> removeEmpty(Set<T> set) {
		if (set == null || set.isEmpty()) {
			return set;
		}
		set.removeIf(s -> {
			if (s instanceof String) {
				return StringUtils.isBlank((String) s);
			} else {
				return s == null;
			}
		});

		return set;
	}

	@IgnoreField
	public PDCI getPdci() {
		return pdci;
	}
	
	public AccessionService.AccessionGeo getGeo() {
		var geo = new AccessionService.AccessionGeo();
		geo.accession = this;
		geo.longitude = this.longitude;
		geo.latitude = this.latitude;
		geo.referenced = this.latitude != null && this.longitude != null;
		geo.elevation = this.elevation;
		geo.uncertainty = this.coordinateUncertainty;
		geo.datum = this.coordinateDatum;
		geo.method = this.georeferenceMethod;
		geo.tileIndex = this.tileIndex;
		geo.climate = this.climate;
		return geo;
	}

	@Override
	public boolean canEqual(Object other) {
		return other instanceof AccessionId;
	}
}