OrderRequestItem.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.*;
import javax.validation.constraints.Size;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.api.exception.InvalidApiUsageException;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.model.community.CommunityCodeValues;
import com.fasterxml.jackson.annotation.JsonGetter;
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 = "order_request_item")
@JsonIdentityInfo(scope = OrderRequestItem.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
public class OrderRequestItem extends CooperatorOwnedModel implements Copyable<OrderRequestItem> {
private static final long serialVersionUID = -8842216779863242481L;
@Basic
@Column(name = "distribution_form_code", length = CODE_VALUE_LENGTH)
@CodeValueField(CommunityCodeValues.GERMPLASM_FORM)
private String distributionFormCode;
@Basic
@Column(name = "external_taxonomy", length = 100)
private String externalTaxonomy;
@Id
@JsonProperty
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "order_request_item_id", columnDefinition = "int")
private Long id;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "inventory_id", nullable = false)
@JsonIgnoreProperties({ "parentInventory", "backupInventory", "inventoryMaintenancePolicy", "preservationMethod", "regenerationMethod" })
private Inventory inventory;
@OneToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "withdrawn_inventory_id")
@JsonIgnoreProperties({ "parentInventory", "backupInventory", "inventoryMaintenancePolicy", "preservationMethod", "regenerationMethod" })
private Inventory withdrawnInventory;
@Basic
@Column(length = 200)
private String name;
@Basic
@Column
@Lob
private String note;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "order_request_id", nullable = false)
@JsonIgnore
private OrderRequest orderRequest;
@Basic
@Column(name = "quantity_shipped")
private Double quantityShipped;
@Basic
@Column(name = "quantity_shipped_unit_code", length = CODE_VALUE_LENGTH)
@CodeValueField(CommunityCodeValues.UNIT_OF_QUANTITY)
private String quantityShippedUnitCode;
@Basic
@Column(name = "sequence_number")
private Integer sequenceNumber;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "source_cooperator_id")
private Cooperator sourceCooperator;
@Basic
@Column(name = "status_code", nullable = false, length = CODE_VALUE_LENGTH)
@CodeValueField(CommunityCodeValues.ORDER_REQUEST_ITEM_STATUS)
private String statusCode;
@Basic
@Column(name = "status_date")
private Date statusDate;
@ManyToOne(fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "web_order_request_item_id")
private WebOrderRequestItem webOrderRequestItem;
@Basic
@Column(name = "web_user_note")
@Lob
private String webUserNote;
@Basic
@Size(max = CODE_VALUE_LENGTH)
@Column(name = "container_type_code", length = CODE_VALUE_LENGTH)
@CodeValueField("CONTAINER_TYPE")
private String containerTypeCode;
@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "orderRequestItem")
@JsonIgnore
private List<OrderRequestItemAction> actions;
@PrePersist
@PreUpdate
private void checkOrderRequestItem() {
if (withdrawnInventory != null) {
if (inventory == null) {
throw new InvalidApiUsageException("Cannot set withdrawnInventory without source inventory");
} else if (! inventory.getAccession().getId().equals(withdrawnInventory.getAccession().getId())) {
throw new InvalidApiUsageException("WithdrawnInventory accession does not match source inventory accession");
} else if (withdrawnInventory.getParentInventory() == null) {
throw new InvalidApiUsageException("WithdrawnInventory does not specify parent inventory");
} else if (! withdrawnInventory.getParentInventory().getId().equals(inventory.getId())) {
throw new InvalidApiUsageException("WithdrawnInventory does not specify source inventory as parent inventory");
}
}
}
public OrderRequestItem(final Long id) {
this.id = id;
}
@JsonGetter
public Long getOrderRequestId() {
return this.orderRequest == null ? null : this.orderRequest.getId();
}
public String reportAccessionNumber() {
var accession = this.inventory.getAccession();
if (accession != null) {
return accession.getAccessionNumber();
}
return null;
}
public String reportAccessionName() {
var accession = this.inventory.getAccession();
if (accession != null) {
return accession.getPreferredName();
}
return null;
}
public String reportAccessionCollectCountryCode() {
var accession = this.inventory.getAccession();
if (accession != null) {
var sources = accession.getAccessionSources();
if (CollectionUtils.isNotEmpty(sources)) {
var collectedSource = sources.stream()
.filter(source -> CommunityCodeValues.ACCESSION_SOURCE_TYPE_COLLECTED.value.equals(source.getSourceTypeCode()) && source.isOrigin())
.findFirst().orElse(null);
if (collectedSource != null && collectedSource.getGeography() != null) {
return collectedSource.getGeography().getCountryCode();
}
}
}
return null;
}
public String reportSourceInventoryNumber() {
return this.inventory.getInventoryNumber();
}
public String reportWithdrawnInventoryNumber() {
if (this.withdrawnInventory != null) {
return withdrawnInventory.getInventoryNumber();
}
return null;
}
@Override
public void lazyLoad() {
super.lazyLoad();
lazyLoad(this.inventory);
lazyLoad(this.inventory.getAccession());
if (this.inventory.getAccession() != null) {
lazyLoad(this.inventory.getAccession().getTaxonomySpecies());
}
if (this.withdrawnInventory != null) {
lazyLoad(this.withdrawnInventory);
lazyLoad(this.withdrawnInventory.getAccession());
if (this.withdrawnInventory.getAccession() != null) {
lazyLoad(this.withdrawnInventory.getAccession().getTaxonomySpecies());
}
}
lazyLoad(this.webOrderRequestItem);
lazyLoad(this.sourceCooperator);
}
@Override
public boolean canEqual(Object other) {
return other instanceof OrderRequestItem;
}
}