DatasetAccessionRef.java

/*
 * Copyright 2018 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.dataset;

import java.util.Objects;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

import lombok.NoArgsConstructor;

import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.genesys.AccessionRef;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * The Class DatasetAccessionRef.
 */
@Entity
@Table(name = "dataset_accessions", indexes = { @Index(columnList = "datasetId, instCode, acceNumb"), @Index(columnList = "datasetId, genus") }, uniqueConstraints = {
		@UniqueConstraint(columnNames = { "datasetId", "instCode", "acceNumb", "genus" }), @UniqueConstraint(columnNames = { "datasetId", "doi" }) })
@NoArgsConstructor
public class DatasetAccessionRef extends AccessionRef<Dataset> {

	private static final long serialVersionUID = -8155179462312813571L;

	@ManyToOne(cascade = {}, optional = false, fetch = FetchType.LAZY)
	@JoinColumn(name = "datasetId")
	@JsonIgnore
	protected Dataset list;
	
	/**
	 * Instantiates a new dataset accession ref.
	 *
	 * @param ref the ref
	 */
	public DatasetAccessionRef(AccessionRef<?> ref) {
		this.instCode=ref.getInstCode();
		this.acceNumb=ref.getAcceNumb();
		this.genus=ref.getGenus();
		this.species=ref.getSpecies();
		this.doi=ref.getDoi();
		this.accession = ref.getAccession();
	}

	/**
	 * Instantiates a new dataset accession ref.
	 *
	 * @param accession the accession
	 */
	public DatasetAccessionRef(Accession accession) {
		super(accession);
	}

	public DatasetAccessionRef(String instCode, String acceNumb, String genus, String doi) {
		super(instCode, acceNumb, genus, doi);
	}

	/**
	 * Gets the dataset.
	 *
	 * @return the dataset
	 */
	@Transient
	@JsonIgnore
	public Dataset getDataset() {
		return list;
	}

	/**
	 * Sets the dataset.
	 *
	 * @param dataset the new dataset
	 */
	public void setDataset(Dataset dataset) {
		this.list = dataset;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((list == null) ? 0 : list.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		DatasetAccessionRef other = (DatasetAccessionRef) obj;
		if (list == null) {
			if (other.list != null)
				return false;
		} else if (!Objects.equals(list.getId(), other.list.getId()))
			return false;
		return true;
	}

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