AccessionSource.java

/*
 * Copyright 2020 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.NotNull;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.Copyable;
import org.gringlobal.component.elastic.ElasticTrigger;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.model.community.CommunityCodeValues;
import org.gringlobal.model.community.IWebVisible;
import org.hibernate.Hibernate;
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.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 = "accession_source")
@JsonIdentityInfo(scope = AccessionSource.class, generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Getter
@Setter
@NoArgsConstructor
public class AccessionSource extends CooperatorOwnedModel implements IWebVisible, Copyable<AccessionSource>, ElasticTrigger {
	private static final long serialVersionUID = -4505740539246904505L;

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

	@NotNull
	@ManyToOne(fetch = FetchType.LAZY, cascade = {}, optional = false)
	@JoinColumn(name = "accession_id", nullable = false)
	private Accession accession;

	@Basic
	@NotNull
	@Column(name = "source_type_code", nullable = false, length = CODE_VALUE_LENGTH)
	@CodeValueField(CommunityCodeValues.ACCESSION_SOURCE_TYPE)
	private String sourceTypeCode;

	@Basic
	@Column(name = "acquisition_source_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(CommunityCodeValues.ACCESSION_SOURCE_HABITAT_TYPE)
	private String acquisitionSource;

	@Basic
	@Column(name = "associated_species")
	@Lob
	private String associatedSpecies;

	@Basic
	@Column(name = "collected_form_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(CommunityCodeValues.GERMPLASM_FORM)
	private String collectedFormCode;

	@Basic
	@Column(name = "collector_verbatim_locality")
	@Lob
	private String collectorVerbatimLocality;

	@Basic
	@Column(name = "elevation_meters")
	private Integer elevationMeters;

	@Basic
	@Column(name = "environment_description")
	@Lob
	private String environmentDescription;

	@Basic
	@Column(name = "formatted_locality")
	@Lob
	private String formattedLocality;

	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "geography_id")
	@Field(type = FieldType.Nested)
	private Geography geography;

	@Basic
	@Column(name = "georeference_annotation")
	@Lob
	private String georeferenceAnnotation;

	@Basic
	@Size(max = 10)
	@CodeValueField(CommunityCodeValues.GEOREFERENCE_DATUM)
	@Column(name = "georeference_datum", length = 10)
	private String georeferenceDatum;

	@Basic
	@Column(name = "georeference_protocol_code", length = CODE_VALUE_LENGTH)
	@CodeValueField("GEOREFERENCE_PROTOCOL")
	private String georeferenceProtocolCode;

	@Basic
	@NotNull
	@Size(min = 1, max = 1)
	@Column(name = "is_origin", nullable = false, length = 1)
	private String isOrigin = "N";

	@Basic
	@NotNull
	@Size(min = 1, max = 1)
	@Column(name = "is_web_visible", nullable = false, length = 1)
	private String isWebVisible = "N";

	@Basic
	private Double latitude;

	@Basic
	private Double longitude;

	@Basic
	@Column
	@Lob
	private String note;

	@Basic
	@Column(name = "number_plants_sampled")
	private Integer numberPlantsSampled;

	@Basic
	@Column(name = "quantity_collected")
	private Double quantityCollected;

	@Basic
	@Column(name = "source_date")
	private Date sourceDate;

	@Basic
	@Column(name = "source_date_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(CommunityCodeValues.DATE_FORMAT)
	private String sourceDateCode;

	@Basic
	private Integer uncertainty;

	@Basic
	@Column(name = "unit_quantity_collected_code", length = CODE_VALUE_LENGTH)
	@CodeValueField(CommunityCodeValues.UNIT_OF_QUANTITY)
	private String unitQuantityCollectedCode;

	@JsonIgnore
	@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE })
	@JoinTable(name = "accession_source_map",
			// owner
			joinColumns = @JoinColumn(name = "accession_source_id", referencedColumnName = "accession_source_id"),
			// target
			inverseJoinColumns = @JoinColumn(name = "cooperator_id", referencedColumnName = "cooperator_id"))
	private List<Cooperator> cooperators;

	@OneToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "accessionSource")
	@JsonIgnore
	private List<SourceDescObservation> sourceDescObservations;

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

	/**
	 * @return {@code true} if `isOrigin == 'Y'`
	 */
	@JsonIgnore
	@Transient
	public boolean isOrigin() {
		return StringUtils.equals("Y", this.isOrigin);
	}

	@Override
	public void lazyLoad() {
		super.lazyLoad();
		lazyLoad(this.accession, true);
		lazyLoad(this.geography);
	}

	@Override
	public Object[] reindexedEntities() {
		Hibernate.initialize(this.accession); // assures maybeReindex will work
		return new Object[] { this.accession };
	}

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