Accession.java

/**
 * Copyright 2014 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.genesys.server.model.genesys;

import java.util.List;
import java.util.UUID;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.JsonViews;
import org.genesys.server.model.dataset.Dataset;
import org.genesys.server.model.impl.DiversityTree;
import org.genesys.server.model.impl.Subset;
import org.springframework.data.elasticsearch.annotations.Document;
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.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

/**
 * Active accession records with all possible constraints.
 *
 * @author mobreza
 *
 */
@Entity
@Table(name = "accession",
		// Unique constraints
		uniqueConstraints = { @UniqueConstraint(name = "UQ_accession_genus_inst", columnNames = { "instituteId", "genus", "acceNumb" }),
				@UniqueConstraint(name = "UQ_accession_dataProviderId", columnNames = { "dataProviderId" }),
				@UniqueConstraint(name = "UQ_accession_doi", columnNames = { "doi" }) },
		// Indexes
		indexes = { 
				@Index(name = "IX_id3", columnList = "instCode,genus,acceNumb"), 
				@Index(name = "IX_acceNumb", columnList = "acceNumb"), 
				@Index(name = "IX_inst_seqNo", columnList = "instituteId,seqNo"), 
				@Index(name = "IX_origcty_seqNo", columnList = "orgCtyId,seqNo"), 
				@Index(name = "IX_taxa_seq", columnList = "taxonomyId2,seqNo"), 
				@Index(name = "IX_seqNo", columnList = "seqNo"), 
				@Index(name = "IX_accession_lastModifiedDate", columnList = "lastModifiedDate"),
				@Index(name = "IX_accession_lastChangedDate", columnList = "lastChangedDate"),
				@Index(name = "IX_accession_createdDate", columnList = "createdDate")})
@Audited
@Document(indexName = "accession")
@Getter
@Setter
@NoArgsConstructor
public class Accession extends AccessionData {

	/**
	 *
	 */
	private static final long serialVersionUID = -4847875217506974494L;
	
	public Accession(UUID uuid) {
		super(uuid);
	}

	@ManyToMany(fetch = FetchType.LAZY, cascade = {})
	@JoinTable(name = "dataset_accessions", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "datasetId", referencedColumnName = "id"))
	@JsonView({ JsonViews.Indexed.class })
	@JsonIdentityReference(alwaysAsId = true)
	@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid", scope = Dataset.class)
	@Field(type = FieldType.Keyword)
	private List<Dataset> datasets;
	
	@ManyToMany(fetch = FetchType.LAZY, cascade = {})
	@JoinTable(name = "subset_accessions", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "subsetId", referencedColumnName = "id"))
	@JsonView({ JsonViews.Indexed.class })
	@JsonIdentityReference(alwaysAsId = true)
	@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid", scope = Subset.class)
	@Field(type = FieldType.Keyword)
	private List<Subset> subsets;

	@ManyToMany(fetch = FetchType.LAZY, cascade = {})
	@JoinTable(name = "diversitytree_accessions", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "diversitytreeId", referencedColumnName = "id"))
	@JsonView({ JsonViews.Indexed.class })
	@JsonIdentityReference(alwaysAsId = true)
	@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid", scope = DiversityTree.class)
	@Field(type = FieldType.Keyword)
	private List<DiversityTree> diversityTrees;

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