InventoryViabilityData.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 java.util.Date;
import java.util.List;
import java.util.Optional;
import javax.persistence.Basic;
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.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

import org.genesys.blocks.annotations.NotCopyable;
import org.genesys.blocks.model.Copyable;

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

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
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
@Table(name = "inventory_viability_data", uniqueConstraints = {
		@UniqueConstraint(name = "ndx_uniq_ivd", columnNames = { "inventory_viability_id", "replication_number", "count_number" })
})
@JsonIdentityInfo(scope = InventoryViabilityData.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
public class InventoryViabilityData extends CooperatorOwnedModel implements Copyable<InventoryViabilityData> {

	private static final long serialVersionUID = -3279935610821060724L;

	/** Inventory Viability Data ID. */
	@Id
	@JsonProperty
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "inventory_viability_data_id", columnDefinition = "int")
	private Long id;

	/** Inventory Viability: Reference to the viability test described by this data record. */
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "inventory_viability_id", nullable = false, updatable = false)
	@JsonIdentityReference(alwaysAsId = true)
	@JsonIdentityInfo(scope = InventoryViability.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	@NotCopyable
	private InventoryViability inventoryViability;

	/** Replication Number: Replication number for this observation. No decimals; whole numbers only. */
	@Basic
	@Column(name = "replication_number", nullable = false)
	@Min(0) // Using 0 just in case it was used, but 1 is the right value
	private int replicationNumber;

	/** Count Number: Sequential number of the observation within the replicate. No decimals; whole numbers only. */
	@Basic
	@Column(name = "count_number", nullable = false)
	@Min(1) private int countNumber;

	/** Count Date: Date when the observation was made. */
	@Basic
	@Column(name = "count_date", nullable = false)
	private Date countDate;

	/** Order Request Item: Reference to the order request item associated with this data record. */
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "order_request_item_id", updatable = false)
	@JsonIdentityReference(alwaysAsId = true)
	@JsonIdentityInfo(scope = OrderRequestItem.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	@NotCopyable
	private OrderRequestItem orderRequestItem;

	/** Counter Cooperator: Reference to the cooperator who counted the results. */
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "counter_cooperator_id")
	@JsonIdentityInfo(scope = Cooperator.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	private Cooperator counterCooperator;

	/** Replication Count: Total number of propagules used in the germination test. No decimals; whole numbers only. */
	@Basic
	@Column(name = "replication_count")
	private Integer replicationCount;

	/** Normal Count: Number of propagules with normal germination. No decimals; whole numbers only. */
	@Basic
	@Column(name = "normal_count")
	@Min(0) private Integer normalCount;

	/** Abnormal Count: Number of propagules with abnormal germination. No decimals; whole numbers only. */
	@Basic
	@Column(name = "abnormal_count")
	@Min(0) private Integer abnormalCount;

	/** Confirmed Dormant Count: Number of confirmed dormant propagules. No decimals; whole numbers only. */
	@Basic
	@Column(name = "confirmed_dormant_count")
	@Min(0) private Integer confirmedDormantCount;

	/** Dead Count: Number of dead propagules. No decimals; whole numbers only. */
	@Basic
	@Column(name = "dead_count")
	@Min(0) private Integer deadCount;

	/** Dormant Count: Number of dormant propagules. No decimals; whole numbers only. */
	@Basic
	@Column(name = "dormant_count")
	@Min(0) private Integer dormantCount;

	/** Empty Count: Number of empty propagules. No decimals; whole numbers only. */
	@Basic
	@Column(name = "empty_count")
	@Min(0) private Integer emptyCount;

	/** Estimated Dormant Count: Number of estimated dormant propagules. No decimals; whole numbers only. */
	@Basic
	@Column(name = "estimated_dormant_count")
	@Min(0) private Integer estimatedDormantCount;

	/** Hard Count: Number of hard propagules. No decimals; whole numbers only. */
	@Basic
	@Column(name = "hard_count")
	@Min(0) private Integer hardCount;

	/** Infested Count: Number of infested propagules. No decimals; whole numbers only. */
	@Basic
	@Column(name = "infested_count")
	@Min(0) private Integer infestedCount;

	/** Treated Dormant Count: Number of treated dormant propagules. No decimals; whole numbers only. */
	@Basic
	@Column(name = "treated_dormant_count")
	@Min(0) private Integer treatedDormantCount;

	/** Unknown Count: Number of propagules not determined. No decimals; whole numbers only. */
	@Basic
	@Column(name = "unknown_count")
	@Min(0) private Integer unknownCount;

	/** TZ Positive Count: Number of tetrazolium-positive propagules. No decimals; whole numbers only. */
	@Basic
	@Column(name = "tzpositive_count")
	@Min(0) private Integer tzPositiveCount;

	/** TZ Negative Count: Number of tetrazolium-negative propagules. No decimals; whole numbers only. */
	@Basic
	@Column(name = "tznegative_count")
	@Min(0) private Integer tzNegativeCount;

	/** Is Included?: Indicates whether this data record is included in calculations. */
	@Basic
	@Column(name = "is_included", nullable = false, length = 1)
	@NotNull @Pattern(regexp = "^Y|N$") private String isIncluded = "Y";

	/** Note: General remarks about the inventory viability data. */
	@Basic
	@Column
	@Lob
	private String note;

	/** Environment Maps: Environment conditions for this data record. */
	@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "inventoryViabilityData")
	@JsonIgnoreProperties({ "inventoryViabilityData" })
	private List<InventoryViabilityDataEnvironmentMap> dataEnvironmentMaps;

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

	@Override
	public void lazyLoad() {
		lazyLoad(this.inventoryViability, true);
		lazyLoad(this.counterCooperator);
		if (dataEnvironmentMaps != null) {
			dataEnvironmentMaps.size();
		}
	}

	/**
	 * Summarize all counts
	 * @return the sum of counts
	 */
	public int sumOfCounts() {
		int sumOfCount = normalCount;
		sumOfCount += Optional.ofNullable(abnormalCount).orElse(0);
		sumOfCount += Optional.ofNullable(unknownCount).orElse(0);

		sumOfCount += Optional.ofNullable(dormantCount).orElse(0);
		sumOfCount += Optional.ofNullable(confirmedDormantCount).orElse(0);
		sumOfCount += Optional.ofNullable(estimatedDormantCount).orElse(0);

		sumOfCount += Optional.ofNullable(deadCount).orElse(0);
		sumOfCount += Optional.ofNullable(emptyCount).orElse(0);
		sumOfCount += Optional.ofNullable(hardCount).orElse(0);
		sumOfCount += Optional.ofNullable(infestedCount).orElse(0);
		sumOfCount += Optional.ofNullable(treatedDormantCount).orElse(0);

		sumOfCount += Optional.ofNullable(tzPositiveCount).orElse(0);
		sumOfCount += Optional.ofNullable(tzNegativeCount).orElse(0);
		return sumOfCount;
	}

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