AccessionRef.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.genesys;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.server.model.impl.AccessionIdentifier3;

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

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * Identifies an accession by it's INSTCODE, ACCENUMB and GENUS. Use of DOI
 * remains optional.
 *
 * @author Andrey Lugovskoy
 * @author Matija Obreza
 * @author Viacheslav Pavlov
 */
@MappedSuperclass
@Getter
@Setter
@NoArgsConstructor
public abstract class AccessionRef<T> extends BasicModel implements SelfCleaning, AccessionIdentifier3, Serializable {

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

	/** The DOI. */
	@Pattern(regexp = "^$|^10\\.[0-9]+(\\.[0-9]+)*/.+")
	@Size(max = 100)
	@Column(length = 100)
	@CsvBindByName(column = "doi")
	protected String doi;

	/** The inst code. */
	@NotNull
	@Size(max = 10)
	@Pattern(regexp = "[A-Z]{3}\\d{3,4}")
	@Column(length = 10, nullable = false)
	@CsvBindByName(column = "instCode")
	protected String instCode;

	/** The acce numb. */
	@NotNull
	@Size(min = 1, max = 50)
	@Column(nullable = false, length = 50)
	@CsvBindByName(column = "acceNumb")
	protected String acceNumb;

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

	/** The species. */
	@Size(max = 100)
	@Column(nullable = true, length = 100)
	@CsvBindByName(column = "species")
	protected String species;

	/** The accession. */
	@ManyToOne(optional = true, fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "accessionId", nullable = true)
	@JsonView({ JsonViews.Public.class })
	protected Accession accession;

	public AccessionRef(Accession accession) {
		this.instCode = accession.getInstitute().getCode();
		this.acceNumb = accession.getAccessionNumber();
		this.genus = accession.getTaxonomy().getGenus();
		this.species = accession.getTaxonomy().getSpecies();
		this.doi = accession.getDoi();
		this.accession = accession;
	}

	public AccessionRef(String instCode, String acceNumb, String genus, String doi) {
		this.instCode = instCode;
		this.acceNumb = acceNumb;
		this.genus = genus;
		this.doi = doi;
	}
	
	@PrePersist
	@PreUpdate
	private void preupdate() {
		trimStringsToNull();
	}
	
	/**
	 * Gets holding institute code.
	 *
	 * @return the instCode
	 */
	@Override
	@Transient
	public String getHoldingInstitute() {
		return instCode;
	}

	/**
	 * Gets accession number.
	 *
	 * @return the acceNumb
	 */
	@Override
	@Transient
	public String getAccessionNumber() {
		return acceNumb;
	}
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((acceNumb == null) ? 0 : acceNumb.hashCode());
//		result = prime * result + ((doi == null) ? 0 : doi.hashCode());
		result = prime * result + ((genus == null) ? 0 : genus.hashCode());
		result = prime * result + ((instCode == null) ? 0 : instCode.hashCode());
//		result = prime * result + ((species == null) ? 0 : species.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!this.canEqual(obj))
			return false;
		AccessionRef<?> other = (AccessionRef<?>) obj;
		if (acceNumb == null) {
			if (other.acceNumb != null)
				return false;
		} else if (!acceNumb.equals(other.acceNumb))
			return false;
////		if (doi == null) {
////			if (other.doi != null)
////				return false;
//		} else if (!doi.equals(other.doi))
//			return false;
		if (instCode == null) {
			if (other.instCode != null)
				return false;
		} else if (!instCode.equals(other.instCode))
			return false;
		if (genus == null) {
			if (other.genus != null)
				return false;
		} else if (!genus.equals(other.genus))
			return false;
//		if (species == null) {
//			if (other.species != null)
//				return false;
//		} else if (!species.equals(other.species))
//			return false;
		return true;
	}

	@Override
	public String toString() {
		return getClass().getSimpleName() + " " + instCode + " " + acceNumb + " " + genus + " " + doi;
	}
}