DiversityTreeAccessionRef.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.genesys.server.model.impl;

import javax.persistence.Column;
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 javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.genesys.AccessionRef;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.opencsv.bean.CsvBindByName;

/**
 * The Class DiversityTreeAccessionRef.
 *
 * @author Maxym Borodenko
 */
@Entity
@Table(name = "diversitytree_accessions", indexes = { @Index(columnList = "diversitytreeId, instCode, acceNumb"), @Index(columnList = "diversitytreeId, genus") }, uniqueConstraints = {
		@UniqueConstraint(columnNames = { "diversitytreeId", "instCode", "acceNumb", "genus", "nodeKey" }), @UniqueConstraint(columnNames = { "diversitytreeId", "doi" }) })
@Getter
@Setter
@NoArgsConstructor
public class DiversityTreeAccessionRef extends AccessionRef<DiversityTree> {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 6883364512762777104L;

	/** The full node path. */
	@NotNull
	@Size(min = 1, max = 100)
	@Column(nullable = false, length = 100)
	@CsvBindByName(column = "nodeKey")
	protected String nodeKey;

	@ManyToOne(cascade = {}, optional = false, fetch = FetchType.LAZY)
	@JoinColumn(name = "diversitytreeId")
	@JsonIgnore
	protected DiversityTree list;

	/**
	 * Instantiates a new DiversityTree accession ref.
	 *
	 * @param accession the accession
	 * @param nodeKey the nodeKey
	 */
	public DiversityTreeAccessionRef(Accession accession, String nodeKey) {
		super(accession);
		this.nodeKey = nodeKey;
	}

	/**
	 * Instantiates a new DiversityTree accession ref.
	 *
	 * @param instCode the instCode
	 * @param acceNumb the acceNumb
	 * @param genus the genus
	 * @param doi the doi
	 * @param nodeKey the nodeKey
	 */
	public DiversityTreeAccessionRef(String instCode, String acceNumb, String genus, String doi, String nodeKey) {
		super(instCode, acceNumb, genus, doi);
		this.nodeKey = nodeKey;
	}

	/**
	 * Instantiates a new DiversityTree accession ref.
	 *
	 * @param instCode the instCode
	 * @param acceNumb the acceNumb
	 * @param genus the genus
	 * @param species the species
	 * @param doi the doi
	 * @param nodeKey the nodeKey
	 */
	public DiversityTreeAccessionRef(String instCode, String acceNumb, String genus, String species, String doi, String nodeKey) {
		this(instCode, acceNumb, genus, doi, nodeKey);
		this.species = species;
	}

	/**
	 * Gets the DiversityTree.
	 *
	 * @return the DiversityTree
	 */
	@Transient
	@JsonIgnore
	public DiversityTree getList() {
		return list;
	}
	
//	@Override
//	public int hashCode() {
//		int result = super.hashCode();
//		result = 31 * result + (nodeKey != null ? nodeKey.hashCode() : 0);
//		result = 31 * result + (list != null ? list.hashCode() : 0);
//		return result;
//	}
//
//	@Override
//	public boolean equals(Object obj) {
//		if (this == obj)
//			return true;
//		if (!super.equals(obj))
//			return false;
//		if (getClass() != obj.getClass())
//			return false;
//		DiversityTreeAccessionRef other = (DiversityTreeAccessionRef) obj;
//		if (nodeKey == null) {
//			if (other.nodeKey != null)
//				return false;
//		} else if (!nodeKey.equals(other.nodeKey))
//			return false;
//		if (list == null) {
//			return other.list == null;
//		} else return list.equals(other.list);
//	}

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