TaxonomySpecies.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.gringlobal.model;

import java.time.Instant;
import java.util.List;
import java.util.Objects;

import javax.persistence.*;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.compatibility.LookupDisplay;
import org.gringlobal.component.elastic.ElasticLoader;
import org.gringlobal.custom.elasticsearch.SearchField;
import org.gringlobal.custom.validation.javax.CodeValueField;
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.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import static org.gringlobal.model.community.CommunityCodeValues.CODE_VALUE_LENGTH;

/**
 * Auto-generated by:
 * org.apache.openjpa.jdbc.meta.ReverseMappingTool$AnnotatedCodeGenerator
 */
@Entity
@Cacheable
@Table(name = "taxonomy_species")
@Document(indexName = "taxonomyspecies")
@JsonIdentityInfo(scope = TaxonomySpecies.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
public class TaxonomySpecies extends CooperatorOwnedModel implements Copyable<TaxonomySpecies>, ElasticLoader {

	private static final long serialVersionUID = -5958397094175847499L;

	public static final String PREFIX_SUBSPECIES = "subsp. ";
	public static final String PREFIX_NOTHOSUBSPECIES = "nothosubsp. ";
	public static final String PREFIX_VARIETY = "var. ";
	public static final String PREFIX_SUBVARIETY = "subvar. ";
	public static final String PREFIX_FORMA = "f.";

	@Id
	@JsonProperty
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "taxonomy_species_id", columnDefinition = "int")
	private Long id;

	@Column(name = "grin_species_id")
	private Long grinId;

	@Basic
	@Column(name = "alternate_name", length = 2000)
	private String alternateName;

	@Basic
	@Column(name = "common_fertilization_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(value = "TAXONOMY_FERTILIZATION_METHOD", strict = false)
	private String commonFertilizationCode;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "curator1_cooperator_id")
	@Field(type = FieldType.Object)
	@JsonIgnoreProperties({ "ownedBy", "createdBy", "modifiedBy", "note", "geography", "secondaryGeography", "webCooperator" })
	@JsonIdentityReference(alwaysAsId = false)
	private Cooperator curator1Cooperator;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "curator2_cooperator_id")
	@Field(type = FieldType.Object)
	@JsonIgnoreProperties({ "ownedBy", "createdBy", "modifiedBy", "note", "geography", "secondaryGeography", "webCooperator" })
	@JsonIdentityReference(alwaysAsId = false)
	private Cooperator curator2Cooperator;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "current_taxonomy_species_id")
	@JsonIdentityInfo(scope = TaxonomySpecies.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	@JsonIdentityReference(alwaysAsId = true)
	@Field(index = false, type = FieldType.Long)
	private TaxonomySpecies currentTaxonomySpecies;

	@Basic
	@Column(name = "forma_authority", length = 100)
	private String formaAuthority;

	@Basic
	@Column(name = "forma_name", length = 30)
	private String formaName;

	@Basic
	@Column(name = "forma_rank_type", length = 30)
	private String formaRankType;

	@Basic
	@Column(name = "is_forma_hybrid", nullable = false, length = 1)
	private String isFormaHybrid = "N";

	@Basic
	@Column(name = "is_name_pending", nullable = false, length = 1)
	private String isNamePending = "N";

	@Basic
	@Column(name = "is_specific_hybrid", nullable = false, length = 1)
	private String isSpecificHybrid = "N";

	@Basic
	@Column(name = "is_subspecific_hybrid", nullable = false, length = 1)
	private String isSubspecificHybrid = "N";

	@Basic
	@Column(name = "is_subvarietal_hybrid", nullable = false, length = 1)
	private String isSubvarietalHybrid = "N";

	@Basic
	@Column(name = "is_varietal_hybrid", nullable = false, length = 1)
	private String isVarietalHybrid = "N";

	@Basic
	@Column(name = "life_form_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(value = "ACCESSION_LIFE_FORM", strict = false)
	private String lifeFormCode;

	@Basic
	@Column(nullable = false, length = 100)
	@SearchField
	@LookupDisplay
	private String name;

	@Basic
	@Column(name = "name_authority", length = 100)
	private String nameAuthority;

	@Basic
	@Column(name = "name_verified_date")
	private Instant nameVerifiedDate;

	@Basic
	@Column(name = "nomen_number")
	private Integer nomenNumber;

	@Basic
	@Column
	@Lob
	private String note;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "priority1_site_id")
	@Field(type = FieldType.Object)
	@JsonIgnoreProperties({ "ownedBy", "createdBy", "modifiedBy" })
	private Site priority1Site;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "priority2_site_id")
	@Field(type = FieldType.Object)
	@JsonIgnoreProperties({ "ownedBy", "createdBy", "modifiedBy" })
	private Site priority2Site;

	@Basic
	@Column(length = 500)
	private String protologue;

	@Basic
	@Column(name = "protologue_virtual_path")
	private String protologueVirtualPath;

	@Basic
	@Column(name = "restriction_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(value = "TAXONOMY_RESTRICTION", strict = false)
	private String restrictionCode;

	@Basic
	@Column(name = "site_note")
	@Lob
	private String siteNote;

	@Basic
	@Column(name = "species_authority", length = 100)
	private String speciesAuthority;

	@Basic
	@Column(name = "species_name", nullable = false, length = 30)
	private String speciesName;

	@Basic
	@Column(name = "subspecies_authority", length = 100)
	private String subspeciesAuthority;

	@Basic
	@Column(name = "subspecies_name", length = 30)
	private String subspeciesName;

	@Basic
	@Column(name = "subvariety_authority", length = 100)
	private String subvarietyAuthority;

	@Basic
	@Column(name = "subvariety_name", length = 30)
	private String subvarietyName;

	@Basic
	@Column(name = "synonym_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(value = "TAXONOMY_SPECIES_QUALIFIER", strict = false)
	private String synonymCode;

	@ManyToOne(fetch = FetchType.EAGER, cascade = {})
	@JoinColumn(name = "taxonomy_genus_id", nullable = false)
	@Field(type = FieldType.Object)
	@JsonIgnoreProperties({ "ownedBy" })
	private TaxonomyGenus taxonomyGenus;

	@Basic
	@Column(name = "variety_authority", length = 100)
	private String varietyAuthority;

	@Basic
	@Column(name = "variety_name", length = 30)
	private String varietyName;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "verifier_cooperator_id")
	@Field(type = FieldType.Object)
	private Cooperator verifierCooperator;

	@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "taxonomySpecies")
	@JsonIgnore
	private List<TaxonomyCropMap> taxonomyCrops;

	@Basic
	@Column(name = "hybrid_parentage", length = 500)
	private String hybridParentage;

	@Basic
	@Column(name = "is_web_visible", nullable = false, length = 1)
	private String isWebVisible = "N";

	@PrePersist
	@PreUpdate
	private void updateName() {
		if (StringUtils.isBlank(this.name)) { // Update name if not specified
			this.name = this.taxonomyGenus.getName();
			this.name += (Objects.equals(this.isSpecificHybrid, "Y") ? " x " : " ") + this.speciesName;

			var subTaxon = getSubTaxon();
			if (subTaxon != null) {
				this.name += " " + subTaxon;
			}

			this.name = this.name.strip().replaceAll("\\s+", " "); // strip and replace whitespaces with single whitespace
		}

		if (StringUtils.isBlank(this.nameAuthority)) { // Update nameAuthority if not specified
			this.nameAuthority = this.speciesAuthority;

			if (StringUtils.isNotBlank(this.subspeciesName)) {
				this.nameAuthority = this.subspeciesAuthority;
			}
			if (StringUtils.isNotBlank(this.varietyName)) {
				this.nameAuthority = this.varietyAuthority;
			}
			if (StringUtils.isNotBlank(this.subvarietyName)) {
				this.nameAuthority = this.subvarietyAuthority;
			}
			if (StringUtils.isNotBlank(this.formaName)) {
				this.nameAuthority = this.formaAuthority;
			}
		}
	}

	@Transient
	@JsonIgnore
	public String getSubTaxon() {
		StringBuilder sb = new StringBuilder(50);
		if (StringUtils.isNotBlank(this.subspeciesName)) {
			if ("Y".equals(isSubspecificHybrid)) {
				sb.append(" ").append(PREFIX_NOTHOSUBSPECIES).append(subspeciesName);
			} else {
				sb.append(" ").append(PREFIX_SUBSPECIES).append(this.subspeciesName);
			}
		}

		if (StringUtils.isNotBlank(this.varietyName)) {
			sb.append(" ").append(PREFIX_VARIETY).append(this.varietyName);
		}

		if (StringUtils.isNotBlank(this.subvarietyName)) {
			sb.append(" ").append(PREFIX_SUBVARIETY).append(this.subvarietyName);
		}

		if (StringUtils.isNotBlank(this.formaName)) {
			sb.append(" ").append(this.formaRankType).append(" ").append(this.formaName);
		}

		return StringUtils.trimToNull(sb.toString());
	}

	/**
	 * Get specific epithet including × for hybrids. Example: "×acuminata"
	 * 
	 * @return
	 */
	@Transient
	public String getSpecificEpithet() {
		return (Objects.equals(this.isSpecificHybrid, "Y") ? "×" : "") + this.speciesName;
	}

	public TaxonomySpecies(final Long id) {
		this.id = id;
	}

	/**
	 * Find and return the {@code name} of the first {@link Crop} this species is linked
	 * with.
	 * 
	 * @return first {@code crop.name} or {@code null} if not linked to crop.
	 */
	@JsonIgnore
	public String reportCropName() {
		if (this.taxonomyCrops != null) {
			if (this.taxonomyCrops.size() > 0) {
				var first = this.taxonomyCrops.get(0);
				if (first.getCrop() != null) {
					return first.getCrop().getName(); // not null
				}
			}
		}
		return null;
	}

	@Override
	public void lazyLoad() {
		super.lazyLoad();
		lazyLoad(this.getTaxonomyGenus());
		lazyLoad(this.getVerifierCooperator());
		lazyLoad(this.getPriority1Site());
		lazyLoad(this.getPriority2Site());
		lazyLoad(this.getCurator1Cooperator());
		lazyLoad(this.getCurator2Cooperator());
	}

	@Override
	public void prepareForIndexing() {
		this.lazyLoad();
	}

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