Inventory.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.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Optional;
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.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.annotations.NotCopyable;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.compatibility.LookupDisplay;
import org.gringlobal.compatibility.SysTableFieldInfo;
import org.gringlobal.component.elastic.ElasticLoader;
import org.gringlobal.custom.elasticsearch.IgnoreField;
import org.gringlobal.custom.elasticsearch.SearchField;
import org.gringlobal.custom.json.IgnoreEntityRefDeserializer;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.custom.validation.javax.OneLine;
import org.gringlobal.custom.validation.javax.SimpleString;
import org.gringlobal.model.community.CommunityCodeValues;
import org.hibernate.Hibernate;
import org.hibernate.annotations.Formula;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
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 com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import static org.gringlobal.model.community.CommunityCodeValues.CODE_VALUE_LENGTH;
/**
* Auto-generated by:
* org.apache.openjpa.jdbc.meta.ReverseMappingTool$AnnotatedCodeGenerator
*/
@Entity
@Table(name = "inventory", uniqueConstraints = {
@UniqueConstraint(columnNames = { "barcode" }),
@UniqueConstraint(columnNames = { "inventory_number" }),
@UniqueConstraint(columnNames = { "inventory_number_part1", "inventory_number_part2", "inventory_number_part3", "form_type_code" })
})
@JsonIdentityInfo(scope = Inventory.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
public class Inventory extends CooperatorOwnedModel implements Copyable<Inventory>, ElasticLoader {
private static final long serialVersionUID = -3649373841894075497L;
public static final String SYSTEM_INVENTORY_FTC = "**";
@Id
@JsonProperty
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "inventory_id", columnDefinition = "int")
private Long id;
@NotNull
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "accession_id", nullable = false)
@Field(type = FieldType.Object)
@JsonIgnoreProperties({ "ownedBy", "names" })
private Accession accession;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "site_id", nullable = false)
private Site site;
@Basic
@Column(name = "availability_end_date")
private Date availabilityEndDate;
@Basic
@Column(name = "availability_start_date")
private Date availabilityStartDate;
@Basic
@NotNull
@Size(max = CODE_VALUE_LENGTH)
@Column(name = "availability_status_code", nullable = false, length = CODE_VALUE_LENGTH)
@CodeValueField(CommunityCodeValues.INVENTORY_AVAILABILITY_STATUS)
private String availabilityStatusCode;
@Basic
@Column(name = "availability_status_note")
@Lob
private String availabilityStatusNote;
@Basic
@Size(max = CODE_VALUE_LENGTH)
@CodeValueField("INVENTORY_AVAILABILITY_REASON")
@Column(name = "availability_reason_code", length = CODE_VALUE_LENGTH)
private String availabilityReasonCode;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "backup_inventory_id")
@JsonSerialize(using = MinimalInventorySerializer.class)
@IgnoreField // this excludes the field in JSON serialization for ES
@JsonIgnoreProperties({ "parentInventory", "backupInventory" })
private Inventory backupInventory;
@Basic
@Column(name = "distribution_critical_quantity")
@Min(0)
private Double distributionCriticalQuantity;
@Basic
@Size(max = CODE_VALUE_LENGTH)
@Column(name = "distribution_default_form_code", length = CODE_VALUE_LENGTH)
@CodeValueField(CommunityCodeValues.GERMPLASM_FORM)
private String distributionDefaultFormCode;
@Basic
@Column(name = "distribution_default_quantity")
@Min(0)
private Double distributionDefaultQuantity;
@Basic
@Size(max = CODE_VALUE_LENGTH)
@Column(name = "distribution_unit_code", length = CODE_VALUE_LENGTH)
@CodeValueField(CommunityCodeValues.UNIT_OF_QUANTITY)
private String distributionUnitCode;
@Basic
@NotNull
@Size(max = CODE_VALUE_LENGTH)
@CodeValueField(CommunityCodeValues.GERMPLASM_FORM)
@Column(name = "form_type_code", nullable = false, length = CODE_VALUE_LENGTH)
private String formTypeCode;
@Basic
@Column(name = "hundred_seed_weight")
@Positive
private Double hundredSeedWeight;
@NotNull
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "inventory_maint_policy_id", nullable = false)
@JsonIgnoreProperties({ "ownedBy" })
@Field(type = FieldType.Object)
private InventoryMaintenancePolicy inventoryMaintenancePolicy;
@SearchField
@Size(max = 128)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
@Column(name = "inventory_number", length = 128)
@SysTableFieldInfo(readonly = true)
@LookupDisplay
private String inventoryNumber;
@Basic
@NotNull
// Handled by @SimpleString -- @Pattern(regexp = "(^$)|(^\\S+(?:\\s\\S+)*$)") // Blank string or a trimmed string that may contain single white space characters
@Size(max = 50)
@Column(name = "inventory_number_part1", nullable = false, length = 50)
@OneLine
@SimpleString
private String inventoryNumberPart1;
@Basic
@Column(name = "inventory_number_part2")
private Long inventoryNumberPart2;
@Basic
@Size(max = 50)
@Column(name = "inventory_number_part3", length = 50)
@OneLine
@SimpleString
private String inventoryNumberPart3;
@Basic
@NotNull
@Size(max = 1)
@Pattern(regexp = "^Y|N")
@Column(name = "is_auto_deducted", nullable = false, length = 1)
private String isAutoDeducted = "N";
@Basic
@NotNull
@Size(max = 1)
@Column(name = "is_available", nullable = false, length = 1)
@SysTableFieldInfo(readonly = true)
private String isAvailable = "N";
@Basic
@NotNull
@Size(max = 1)
@Column(name = "is_distributable", nullable = false, length = 1)
private String isDistributable = "N";
@Basic
private Double latitude;
@Basic
private Double longitude;
@Basic
@Column
@Lob
private String note;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "parent_inventory_id")
@JsonSerialize(using = MinimalInventorySerializer.class)
@IgnoreField // this excludes the field in JSON serialization for ES
private Inventory parentInventory;
@Basic
@Size(max = CODE_VALUE_LENGTH)
@Column(name = "pathogen_status_code", length = CODE_VALUE_LENGTH)
@CodeValueField(value = CommunityCodeValues.PATHOGEN_STATUS, indexed = true)
private String pathogenStatusCode;
@Basic
@Size(max = CODE_VALUE_LENGTH)
@Column(name = "plant_sex_code", length = CODE_VALUE_LENGTH)
@CodeValueField("INVENTORY_PLANT_SEX")
private String plantSexCode;
@Basic
@Size(max = CODE_VALUE_LENGTH)
@Column(name = "pollination_method_code", length = CODE_VALUE_LENGTH)
@CodeValueField("INVENTORY_POLLINATION_METHOD")
private String pollinationMethodCode;
@Basic
@Size(max = CODE_VALUE_LENGTH)
@Column(name = "pollination_vector_code", length = CODE_VALUE_LENGTH)
@CodeValueField("INVENTORY_POLLINATION_VECTOR")
private String pollinationVectorCode;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "preservation_method_id")
private Method preservationMethod;
@Basic
@Column(name = "propagation_date")
private Date propagationDate;
@Basic
@Size(max = CODE_VALUE_LENGTH)
@Column(name = "propagation_date_code", length = CODE_VALUE_LENGTH)
@CodeValueField(CommunityCodeValues.DATE_FORMAT)
private String propagationDateCode;
@Basic
@Column(name = "quantity_on_hand")
@Min(0)
private Double quantityOnHand;
@Basic
@Size(max = CODE_VALUE_LENGTH)
@Column(name = "quantity_on_hand_unit_code", length = CODE_VALUE_LENGTH)
@CodeValueField(CommunityCodeValues.UNIT_OF_QUANTITY)
private String quantityOnHandUnitCode;
@Basic
@Column(name = "regeneration_critical_quantity")
@Min(0)
private Double regenerationCriticalQuantity;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "regeneration_method_id")
private Method regenerationMethod;
@Basic
@Column(length = 200)
private String rootstock;
@Size(max = 100)
@Column(length = 100)
private String barcode;
@Basic
@Size(max = 20)
@Column(name = "storage_location_part1", length = 20)
private String storageLocationPart1;
@Basic
@Size(max = 20)
@Column(name = "storage_location_part2", length = 20)
private String storageLocationPart2;
@Basic
@Size(max = 20)
@Column(name = "storage_location_part3", length = 20)
private String storageLocationPart3;
@Basic
@Size(max = 20)
@Column(name = "storage_location_part4", length = 20)
private String storageLocationPart4;
@Basic
@Column(name = "web_availability_note")
@Lob
private String webAvailabilityNote;
@Basic
@Size(max = CODE_VALUE_LENGTH)
@Column(name = "container_type_code", length = CODE_VALUE_LENGTH)
@CodeValueField(CommunityCodeValues.CONTAINER_TYPE)
private String containerTypeCode;
@Formula("(SELECT name.plant_name FROM accession_inv_name name WHERE name.inventory_id = inventory_id ORDER BY name.plant_name_rank OFFSET 0 ROWS FETCH FIRST 1 ROWS ONLY)")
private String preferredName;
@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "inventory")
@JsonIgnore
private List<InventoryAction> actions;
@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "inventory")
@JsonIgnore
private List<InventoryQualityStatus> quality;
@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "inventory")
@JsonIgnore
@OrderBy("testedDate DESC")
private List<InventoryViability> viability;
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, mappedBy = "inventory")
@JsonIgnore
private List<AccessionInvAnnotation> annotations;
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, mappedBy = "inventory")
@IgnoreField(false) // this includes the field in JSON serialization for ES
@Field(type = FieldType.Nested)
@JsonIgnoreProperties({ "ownedBy", "inventory" })
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private List<AccessionInvName> names;
@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "inventory")
@JsonIgnore
private List<AccessionInvGroupMap> accessionInvGroupMaps;
@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "inventory")
@JsonIgnore
private List<AccessionInvAttach> attachments;
@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, mappedBy = "inventory")
@JsonIgnoreProperties({ "inventory" })
@NotCopyable
@IgnoreEntityRefDeserializer
private InventoryExtra extra;
@Basic
@Column(length=20)
@OneLine
@Pattern(regexp = "^10\\.[0-9]+(\\.[0-9]+)*/.+")
private String doi;
@Basic
private Long generation;
@Basic
@Column(name = "distribution_rank")
private Integer distributionRank = null;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "production_location_geography_id")
private Geography productionLocationGeography;
@Override
@PrePersist
protected void prePersist() {
super.prePersist();
preUpdate();
}
@PreUpdate
protected void preUpdate() {
adjustValues();
distributionRankSetup();
}
/**
* Adjust values according to business logic
*/
private void adjustValues() {
this.inventoryNumberPart1 = StringUtils.trimToEmpty(this.inventoryNumberPart1);
this.inventoryNumberPart3 = StringUtils.trimToNull(this.inventoryNumberPart3);
}
private void distributionRankSetup() {
if (isDistributable.equals("N")) {
distributionRank = null;
}
}
public Inventory(final Long id) {
this.id = id;
}
public boolean isSystemInventory() {
return Inventory.SYSTEM_INVENTORY_FTC.equals(this.formTypeCode);
}
public List<AccessionInvName> getNames() {
return names;
}
public void applyInventoryMaintenancePolicy(final InventoryMaintenancePolicy maintenancePolicy) {
this.inventoryMaintenancePolicy = maintenancePolicy;
this.setFormTypeCode(maintenancePolicy.getFormTypeCode());
this.setQuantityOnHandUnitCode(maintenancePolicy.getQuantityOnHandUnitCode());
this.setWebAvailabilityNote(maintenancePolicy.getWebAvailabilityNote());
this.setIsAutoDeducted(maintenancePolicy.getIsAutoDeducted());
this.setDistributionDefaultFormCode(maintenancePolicy.getDistributionDefaultFormCode());
this.setDistributionDefaultQuantity(maintenancePolicy.getDistributionDefaultQuantity());
if (Optional.ofNullable(maintenancePolicy.getDistributionDefaultQuantity()).orElse(0d) > 0) {
this.setIsDistributable("Y");
} else {
this.setIsDistributable("N");
}
this.setDistributionUnitCode(maintenancePolicy.getDistributionUnitCode());
this.setDistributionCriticalQuantity(maintenancePolicy.getDistributionCriticalQuantity());
this.setRegenerationCriticalQuantity(maintenancePolicy.getRegenerationCriticalQuantity());
}
public String reportAccessionPedigree() {
var pedigree = this.accession.getAccessionPedigree();
if (pedigree != null) {
return pedigree.getDescription();
}
return null;
}
public String reportOriginCountry() {
var sources = this.accession.getAccessionSources();
if (CollectionUtils.isNotEmpty(sources)) {
var originSource = sources.stream().filter(AccessionSource::isOrigin).findFirst().orElse(null);
if (originSource != null && originSource.getGeography() != null) {
return originSource.getGeography().getCountryCode();
}
}
return null;
}
public String reportCropName() {
return this.accession.getTaxonomySpecies().reportCropName();
}
public String reportInventoryName() {
return reportInventoryName(null, null);
}
public String reportInventoryName(String categoryCode, Integer plantNameRank) {
if (CollectionUtils.isNotEmpty(this.names)) {
if (StringUtils.isBlank(categoryCode) && plantNameRank == null) {
return this.names.get(0).getPlantName();
} else {
var invName = this.names.stream()
.filter(name -> StringUtils.isBlank(categoryCode) || categoryCode.equals(name.getCategoryCode()))
.filter(name -> plantNameRank == null || plantNameRank.equals(name.getPlantNameRank()))
.findFirst().orElse(null);
if (invName != null) {
return invName.getPlantName();
}
}
}
var acceInventories = this.accession.getInventories();
if (CollectionUtils.isNotEmpty(acceInventories)) {
var sysInventory = acceInventories.stream().filter(Inventory::isSystemInventory).findFirst().orElse(null);
if (sysInventory != null && CollectionUtils.isNotEmpty(sysInventory.getNames())) {
return sysInventory.getNames().get(0).getPlantName();
}
}
return null;
}
@Override
public void lazyLoad() {
super.lazyLoad();
lazyLoad(this.site);
lazyLoad(this.accession, true);
lazyLoad(this.backupInventory);
lazyLoad(this.inventoryMaintenancePolicy);
lazyLoad(this.preservationMethod);
lazyLoad(this.regenerationMethod);
lazyLoad(this.extra, true);
lazyLoad(this.productionLocationGeography);
}
@Override
public void prepareForIndexing() {
this.names.size();
if (this.extra != null) {
this.extra.getId();
}
if (this.parentInventory == this) {
this.parentInventory = null;
}
if (this.backupInventory == this) {
this.backupInventory = null;
}
}
public static class MinimalInventorySerializer extends JsonSerializer<Inventory> {
@Override
public void serialize(Inventory inventory, JsonGenerator jgen, SerializerProvider sp) throws IOException {
if (inventory == null) {
jgen.writeNull();
} else {
jgen.writeStartObject();
jgen.writeObjectField("id", inventory.getId());
if (Hibernate.isInitialized(inventory)) {
jgen.writeObjectField("inventoryNumber", inventory.getInventoryNumber());
}
jgen.writeEndObject();
}
}
}
/**
* Get the {@code generation} number for propagated material
* @return {@code null} if generation number is not defined, {@code generation + 1} otherwise.
*/
public Long nextGeneration() {
return generation == null ? null : 1l + generation;
}
@Override
public boolean canEqual(Object other) {
return other instanceof Inventory;
}
}