TaxonomyGenus.java

/*
 * Copyright 2026 Global Crop Diversity Trust
 * Licensed under the Apache License, Version 2.0
 * See LICENSE file in project root folder or http://www.apache.org/licenses/LICENSE-2.0
 */

package org.gringlobal.model;

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

import javax.persistence.*;

import org.genesys.blocks.model.Copyable;

import org.gringlobal.compatibility.LookupDisplay;
import org.gringlobal.component.elastic.ElasticLoader;
import org.gringlobal.custom.validation.javax.CodeValueField;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
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;

/**
 * Auto-generated by:
 * org.apache.openjpa.jdbc.meta.ReverseMappingTool$AnnotatedCodeGenerator
 */
@Entity
@Cacheable
@Table(name = "taxonomy_genus")
@Document(indexName = "taxonomygenus")
@JsonIdentityInfo(scope = TaxonomyGenus.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
@LookupDisplay(template = "concat(genusName, case when(subgenusName = null) then '' else concat(' subg. ', subgenusName) end," +
	" case when(sectionName = null) then '' else concat(' sect. ', sectionName) end, case when(subsectionName = null) then '' else concat(' subsect. ', subsectionName) end," +
	" case when(seriesName = null) then '' else concat(' ser. ', seriesName) end, case when(subseriesName = null) then '' else concat(' subser. ', subseriesName) end," +
	" case when(genusAuthority = null) then '' when(genusAuthority = '') then '' else concat(' ', genusAuthority) end)"
)
public class TaxonomyGenus extends CooperatorOwnedModel implements Copyable<TaxonomyGenus>, ElasticLoader {

	private static final long serialVersionUID = 3054321300424882004L;

	/** Taxonomy Genus ID. */
	@Id
	@JsonProperty
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "taxonomy_genus_id", columnDefinition = "int")
	private Long id;

	/** GRIN Taxonomy Genus ID: The id of this name in USDA GRIN Taxonomy. */
	@Column(name = "grin_genus_id")
	private Long grinId;

	/** TaxonomyFamily: Reference to the accepted extended family described by this taxonomy genus. */
	@ManyToOne(fetch = FetchType.EAGER, cascade = {})
	@JoinColumn(name = "taxonomy_family_id", nullable = false)
	@Field(type = FieldType.Object)
	@JsonIgnoreProperties({ "ownedBy", "createdBy", "modifiedBy", "typeTaxonomyGenus", "currentTaxonomyFamily", "note" })
	private TaxonomyFamily taxonomyFamily;

	/** Hybrid Code: The hybrid code for this genus. Uses GENUS_HYBRID vocabulary. */
	@Basic
	@Column(name = "hybrid_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(value = "GENUS_HYBRID", strict = false)
	private String hybridCode;

	/** Genus: The taxonomic genus name. */
	@Basic
	@Column(name = "genus_name", nullable = false, length = 30)
	private String genusName;

	/** Genus Authority: The author for the genus name. */
	@Basic
	@Column(name = "genus_authority", length = 100)
	private String genusAuthority;

	/** Subgenus: The name for a subgenus division of the genus. */
	@Basic
	@Column(name = "subgenus_name", length = 30)
	private String subgenusName;

	/** Section: The name for a section division of the genus. */
	@Basic
	@Column(name = "section_name", length = 30)
	private String sectionName;

	/** Subsection: The name for a subsection division of the genus. */
	@Basic
	@Column(name = "subsection_name", length = 30)
	private String subsectionName;

	/** Series: The name for a series division of the genus. */
	@Basic
	@Column(name = "series_name", length = 30)
	private String seriesName;

	/** Subseries: The name for a subseries division of the genus. */
	@Basic
	@Column(name = "subseries_name", length = 30)
	private String subseriesName;

	/** Synonym Qualifier: A code qualifying the genus name if the name is a synonym. Uses TAXONOMY_GENUS_QUALIFIER vocabulary. */
	@Basic
	@Column(name = "qualifying_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(value = "TAXONOMY_GENUS_QUALIFIER", strict = false)
	private String qualifyingCode;

	/** TaxonomyGenus: Reference to the accepted extended genus described by this taxonomy genus. */
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "current_taxonomy_genus_id")
	@JsonIgnore
	@JsonIdentityInfo(scope = TaxonomyGenus.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	@JsonIdentityReference(alwaysAsId = true)
	private TaxonomyGenus currentTaxonomyGenus;

	/** Is Web Visible?: Whether this taxonomy genus is visible on the web. */
	@Basic
	@Column(name = "is_web_visible", nullable = false, length = 1)
	private String isWebVisible = "N";

	/** Note: Generic comments about the genus. */
	@Basic
	@Column
	@Lob
	private String note;

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

	@Override
	public void lazyLoad() {
		super.lazyLoad();
		lazyLoad(this.getCurrentTaxonomyGenus());
		lazyLoad(this.getTaxonomyFamily());
	}

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

	private String genusPrefix(String hybridCode) {
		if (hybridCode == null) return "";
		switch (hybridCode) {
			case "+": return "+";
			case "X": return "x";
			default: return "";
		}
	}
	/**
	 * The complete genus name, including hybrid code
	 *
	 * @return full genus name
	 */
	public String getName() {
		return genusPrefix(this.hybridCode) + this.genusName;
	}

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