SeedInventoryExtra.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;

import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

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

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import lombok.Getter;
import lombok.Setter;

import org.genesys.blocks.annotations.NotCopyable;
import org.gringlobal.api.exception.InvalidApiUsageException;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.model.community.CommunityCodeValues;
import org.hibernate.annotations.Formula;

@Entity
@DiscriminatorValue(SeedInventoryExtra.EXTRA_TYPE)
@Getter
@Setter
public class SeedInventoryExtra extends InventoryExtra {

	private static final long serialVersionUID = -2987354304920359738L;
	
	public static final String EXTRA_TYPE = "SeedInventoryExtra";
	
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "inventory_last_viability_id")
	@JsonProperty(access = JsonProperty.Access.READ_ONLY)
	@JsonIgnoreProperties("inventory")
	@JsonIdentityReference(alwaysAsId = true)
	@NotCopyable
	private InventoryViability lastViability;

	@Min(value = 0)
	@Max(value = 100)
	@Column(name = "last_viability")
	private Double lastViabilityResult;

	@Column(name = "last_viability_date")
	private Date lastViabilityDate;

	@Column(name = "last_viability_date_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(CommunityCodeValues.DATE_FORMAT)
	private String lastViabilityDateCode;

	@Min(value = 0)
	@Max(value = 100)
	@Column(name = "initial_viability", nullable = true)
	private Double initialViability;

	@Column(name = "initial_viability_date", nullable = true)
	private Date initialViabilityDate;

	@Column(name = "initial_viability_date_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(CommunityCodeValues.DATE_FORMAT)
	private String initialViabilityDateCode;

	@Min(0) @Max(100)
	@Column(name = "moisture_content")
	private Float moistureContent;

	@Column(name = "moisture_content_date")
	private Date moistureContentDate;

	@CodeValueField(CommunityCodeValues.DATE_FORMAT)
	@Column(name = "moisture_content_date_code", length = CODE_VALUE_LENGTH)
	private String moistureContentDateCode;

	@PrePersist
	@PreUpdate
	protected void prePersist() {
		super.prePersist();
		validateInitialViability();
		validateMoistureContent();
	}
	
	private void validateInitialViability() {
		if (this.initialViability == null) {
			this.initialViabilityDate = null;
			this.initialViabilityDateCode = null;
		} else if (this.initialViabilityDate != null && this.initialViabilityDateCode == null) {
			throw new InvalidApiUsageException("initialViabilityDateCode not specified");
		}
	}

	private void validateMoistureContent() {
		if (this.moistureContent == null) {
			this.moistureContentDate = null;
			this.moistureContentDateCode = null;
		} else if (this.moistureContentDate != null && this.moistureContentDateCode == null) {
			throw new InvalidApiUsageException("moistureContentDateCode not specified");
		}
	}
	
	@Override
	public InventoryExtra apply(InventoryExtra source) {
		super.apply(source);
		return this;
	}

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

	/**
	 * Copy initial viability data
	 */
	@Override
	public SeedInventoryExtra copy() {
		var copy = new SeedInventoryExtra();
		copy.apply(this);
		copy.setInventory(null); // Clear
		return copy;
	}
}