InventoryViability.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.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
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.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.genesys.blocks.annotations.NotCopyable;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.model.community.CommunityCodeValues;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
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
@Table(name = "inventory_viability", uniqueConstraints = {
	@UniqueConstraint(columnNames = { "inventory_id", "inventory_viability_rule_id", "tested_date", "tested_date_code" })
})
@JsonIdentityInfo(scope = InventoryViability.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
public class InventoryViability extends CooperatorOwnedModel implements Copyable<InventoryViability> {

	private static final long serialVersionUID = -747641506544760622L;

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

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "inventory_id", nullable = false, updatable = false)
	@NotCopyable
	@JsonIgnoreProperties({ "extra" })
	private Inventory inventory;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "inventory_viability_rule_id", updatable = false)
	private InventoryViabilityRule inventoryViabilityRule;

	/**
	 * List of {@link InventoryViabilityData} for this record
	 */
	@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "inventoryViability")
	@OrderBy("countNumber DESC, replicationNumber ASC")
	@JsonIgnore
	private List<InventoryViabilityData> datas;

	@Basic
	@Column(name = "tested_date", nullable = false)
	private Date testedDate;

	@Basic
	@Column(name = "tested_date_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(CommunityCodeValues.DATE_FORMAT)
	private String testedDateCode;

	/**
	 * Total number of replications performed to test the viability of the inventory
	 * sample.
	 */
	@Basic
	@Column(name = "replication_count")
	private Integer replicationCount;

	@Basic
	@Column(name = "total_tested_count")
	private Integer totalTestedCount;

	@Basic
	@Column(name = "vigor_rating_code", length = CODE_VALUE_LENGTH)
	@CodeValueField("INVENTORY_VIGOR")
	private String vigorRatingCode;

	@Basic
	@Column
	@Lob
	private String note;

	/**
	 * Contains the percent viability of the inventory sample. This value is
	 * calculated from the percent normal germination (percent_normal) and the
	 * percent dormant propagules (percent_dormant).
	 */
	@Basic
	@Column(name = "percent_viable")
	@Min(0)
	@Max(100)
	private Double percentViable;

	/**
	 * Percent of propagules in the inventory sample that display normal
	 * germination. This value is required to calculate percent viability
	 */
	@Basic
	@Column(name = "percent_normal")
	@Min(0)
	@Max(100)
	private Double percentNormal;

	/**
	 * Percent of propagules in the inventory sample that are viable, but which did
	 * not germinate. This value is required to calculate percent viability.
	 */
	@Basic
	@Column(name = "percent_dormant")
	@Min(0)
	@Max(100)
	private Double percentDormant;

	@Basic
	@Column(name = "percent_hard")
	@Min(0)
	@Max(100)
	private Double percentHard;

	@Basic
	@Column(name = "percent_abnormal")
	@Min(0)
	@Max(100)
	private Double percentAbnormal;

	@Basic
	@Column(name = "percent_dead")
	@Min(0)
	@Max(100)
	private Double percentDead;

	@Basic
	@Column(name = "percent_empty")
	@Min(0)
	@Max(100)
	private Double percentEmpty;

	@Basic
	@Column(name = "percent_infested")
	@Min(0)
	@Max(100)
	private Double percentInfested;

	@Basic
	@Column(name = "percent_unknown")
	@Min(0)
	@Max(100)
	private Double percentUnknown;

	@Basic
	@Column(name = "percent_tzpositive")
	@Min(0)
	@Max(100)
	private Double percentTzPositive;

	@Basic
	@Column(name = "percent_tznegative")
	@Min(0)
	@Max(100)
	private Double percentTzNegative;
	
	@Column(nullable = false, length = 15)
	@Enumerated(EnumType.STRING)
	private ViabilityStatus status;

	@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "inventoryViability")
	@JsonIgnore
	private List<InventoryViabilityAction> actions;

	public enum ViabilityStatus {
		/** The test is canceled */
		CANCELED,
		/** The test is not yet completed */
		PENDING,
		/** The result of the test is conclusive */
		CONCLUSIVE,
		/** The result of the test is inconclusive */
		INCONCLUSIVE
	}

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

	/**
	 * Update {@link #status} according to {@link #percentViable}.
	 */
	@PrePersist
	@PreUpdate
	private void updateStatus() {
		if (status == ViabilityStatus.CANCELED) {
			// If the test is canceled, set percentViable to null
			percentViable = null;
		} else if (percentViable == null) {
			// If no result, the test is pending
			status = ViabilityStatus.PENDING;
		} else if (status == null) {
			// Assume that completed tests are conclusive unless specified otherwise (original behavior)
			status = ViabilityStatus.CONCLUSIVE;
		} else if (status == ViabilityStatus.PENDING) {
			// If still pending, but have test results, set to conclusive
			status = ViabilityStatus.CONCLUSIVE;
		}
		super.prePersist();
	}

	@Override
	public void lazyLoad() {
		super.lazyLoad();
		lazyLoad(inventory, true);
		lazyLoad(inventoryViabilityRule);
	}

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