Pathogen.java

/*
 * Copyright 2022 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.community;

import java.time.LocalDate;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.SelfCleaning;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.custom.validation.javax.SimpleString;
import org.gringlobal.model.Cooperator;
import org.gringlobal.model.CooperatorOwnedModel;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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

/**
 * {@code Pathogen} captures information about different pathogens affecting
 * plant health.
 */
@Entity
@Table(name = "pathogen", uniqueConstraints = {
	@UniqueConstraint(columnNames = { "name" }),
})
@JsonIdentityInfo(scope = Pathogen.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
public class Pathogen extends CooperatorOwnedModel implements SelfCleaning, Copyable<Pathogen> {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -2845991367380760689L;

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

	@NotNull
	@CodeValueField(CommunityCodeValues.PATHOGEN_TYPE)
	@Column(name = "pathogen_type_code", nullable = false, length = CODE_VALUE_LENGTH)
	@ApiModelProperty(notes = "Type of pathogen. Must be a value in the PATHOGEN_TYPE Code Group.", example = "VIRUS", required = true)
	private String pathogenType;

	@SimpleString
	@NotNull
	@Column(name = "name", nullable = false, length = 100)
	@ApiModelProperty(notes = "Full scientific name of the pathogen", required = true)
	private String name;

	@SimpleString
	@NotNull
	@Column(name = "name_authority", nullable = false, length = 100)
	@ApiModelProperty(notes = "Name authority", required = true)
	private String nameAuthority;

	@SimpleString
	@NotNull
	@Column(name = "name_abbrev", nullable = false, length = 50)
	@ApiModelProperty(notes = "Abbreviated name of the pathogen", required = true)
	private String abbreviatedName;

	@Column(name = "name_verified_date")
	private LocalDate nameVerifiedDate;

	@ManyToOne(cascade = {})
	@JoinColumn(name = "verifier_cooperator_id")
//	@JsonIgnoreProperties({""})
	private Cooperator verifierCooperator;

	@SimpleString
	@Column(name = "alternate_name", length = 100)
	@ApiModelProperty(notes = "Alternative name for the pathogen", required = true)
	private String alternateName;

	@Basic
	@NotNull
	@Size(min = 1, max = 1)
	@Column(name = "is_quarantine", nullable = false, length = 1)
	@ApiModelProperty(notes = "Is the on the FAO Quarantine list?", required = true)
	private String isQuarantine = "N";

	@Basic
	@NotNull
	@Size(min = 1, max = 1)
	@Column(name = "is_extinct", nullable = false, length = 1)
	@ApiModelProperty(notes = "Is the pathogen extinct?", required = true)
	private String isExtinct = "N";

	@SimpleString
	@Column(name = "family_name", length = 30)
	private String familyName;

	@SimpleString
	@Column(name = "family_authority", length = 25)
	private String familyAuthority;

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

	@SimpleString
	@Column(name = "genus_authority", length = 25)
	private String genusAuthority;

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

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

	@SimpleString
	@NotNull
	@Column(name = "species_authority", length = 25)
	private String speciesAuthority;

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

	@SimpleString
	@Column(name = "subspecies_authority", length = 25)
	private String subspeciesAuthority;

	@SimpleString
	@Column(name = "protologue", length = 30)
	private String protologue;

	@Lob
	private String note;

	@JsonIgnore
	@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "pathogen")
	private List<PathogenAttach> attachments;

	@OneToMany(cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "pathogen")
	private List<PathogenSymptomMap> symptoms;
	
	@OneToMany(cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "pathogen")
	private List<PathogenGeographyMap> geographies;

	@OneToMany(cascade = { CascadeType.REMOVE }, fetch = FetchType.LAZY, mappedBy = "pathogen")
	private List<OrderRequestItemPathogenMap> orderRequestItems;

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

	@Override
	public void lazyLoad() {
		super.lazyLoad();
		if (symptoms != null) {
			symptoms.size();
		}
		if (geographies != null) {
			geographies.size();
		}
		if (orderRequestItems != null) {
			orderRequestItems.size();
		}
	}

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