InventoryViabilityData.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 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 lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.annotations.NotCopyable;
import org.genesys.blocks.model.Copyable;

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;

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

	@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;

	@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;

	@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "inventoryViabilityData")
	@JsonIgnoreProperties({ "inventoryViabilityData" })
	private List<InventoryViabilityDataEnvironmentMap> dataEnvironmentMaps;

	@Basic
	@Column
	@Lob
	private String note;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "counter_cooperator_id")
	@JsonIdentityInfo(scope = Cooperator.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
	private Cooperator counterCooperator;

	/**
	 * The replication number distinguishes between the replicates of one test. This
	 * is a 1-based index, less than the total number of replicates
	 * {@link InventoryViability#getReplicationCount()}.
	 */
	@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;

	/**
	 * The sequential number of an observation of the replicate with
	 * {@link #replicationNumber}. Goes with {@link #countDate} and is incremented.
	 */
	@Basic
	@Column(name = "count_number", nullable = false)
	@Min(1)
	private int countNumber;

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

	/**
	 * Total number of propagules of the inventory sample used in the germination
	 * test
	 */
	@Basic
	@Column(name = "replication_count")
	private Integer replicationCount;

	@Basic
	@Column(name = "normal_count")
	@Min(0)
	private Integer normalCount;

	@Basic
	@Column(name = "abnormal_count")
	@Min(0)
	private Integer abnormalCount;

	@Basic
	@Column(name = "confirmed_dormant_count")
	@Min(0)
	private Integer confirmedDormantCount;

	@Basic
	@Column(name = "dead_count")
	@Min(0)
	private Integer deadCount;

	@Basic
	@Column(name = "dormant_count")
	@Min(0)
	private Integer dormantCount;

	@Basic
	@Column(name = "empty_count")
	@Min(0)
	private Integer emptyCount;

	@Basic
	@Column(name = "estimated_dormant_count")
	@Min(0)
	private Integer estimatedDormantCount;

	@Basic
	@Column(name = "hard_count")
	@Min(0)
	private Integer hardCount;

	@Basic
	@Column(name = "infested_count")
	@Min(0)
	private Integer infestedCount;

	@Basic
	@Column(name = "treated_dormant_count")
	@Min(0)
	private Integer treatedDormantCount;

	@Basic
	@Column(name = "unknown_count")
	@Min(0)
	private Integer unknownCount;

	/**
	 * Number of propagules in the inventory sample subjected to Tetrazolium test
	 * that displayed a positive result
	 */
	@Basic
	@Column(name = "tzpositive_count")
	@Min(0)
	private Integer tzPositiveCount;

	/**
	 * Number of propagules in the inventory sample subjected to Tetrazolium test
	 * that displayed a negative result
	 */
	@Basic
	@Column(name = "tznegative_count")
	@Min(0)
	private Integer tzNegativeCount;

	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;
	}
}