TaxonomyGenus.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.grin;

import javax.persistence.Basic;
import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.Copyable;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.springframework.data.elasticsearch.annotations.Document;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

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

	private static final long serialVersionUID = -6571857859488674729L;

	@Id
	@JsonProperty
	@Column(name = "taxonomy_genus_id")
	private Long id;

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

	@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)
	@NotFound(action = NotFoundAction.IGNORE)
	private TaxonomyGenus currentTaxonomyGenus;

	@Basic
	@Column(name = "genus_authority", length = 100)
	private String genusAuthority;

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

	@Basic
	@Column(name = "hybrid_code", length = 20)
	private String hybridCode;

	@Basic
	@Column
	@Lob
	@JsonIgnore
	private String note;

	@Basic
	@Column(name = "qualifying_code", length = 20)
	private String qualifyingCode;

	@Basic
	@Column(name = "section_name", length = 30)
	private String sectionName;

	@Basic
	@Column(name = "series_name", length = 30)
	private String seriesName;

	@Basic
	@Column(name = "subgenus_name", length = 30)
	private String subgenusName;

	@Basic
	@Column(name = "subsection_name", length = 30)
	private String subsectionName;

	@Basic
	@Column(name = "subseries_name", length = 30)
	private String subseriesName;

	@ManyToOne(fetch = FetchType.EAGER, cascade = {})
	@JoinColumn(name = "taxonomy_family_id", nullable = false)
	@Field(type = FieldType.Object)
	@JsonIgnoreProperties({ "ownedBy", "createdBy", "modifiedBy", "currentTaxonomyFamily", "note" })
	private TaxonomyFamily taxonomyFamily;

	@PrePersist
	@PreUpdate
	private void updateName() {
		this.name = StringUtils.defaultIfBlank(this.hybridCode, "") + this.genusName;
	}

	public TaxonomyGenus(final Long id) {
		this.id = id;
	}
	
	/**
	 * Utility to create an object reference to specified {@link TaxonomyGenus#id}.
	 * 
	 * @param taxonomyGenusId may be {@code null}
	 * @return {@code null} if incoming id is {@code null}, reference object otherwise
	 */
	public static TaxonomyGenus withId(Long taxonomyGenusId) {
		return taxonomyGenusId == null ? null : new TaxonomyGenus(taxonomyGenusId);
	}

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