Genotype.java

/*
 * Copyright 2025 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 java.util.Objects;

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.UniqueConstraint;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import org.genesys.blocks.model.Copyable;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.genesys.AccessionRef;
import org.genesys.spring.validation.javax.SimpleString;
import org.genesys.spring.validation.javax.ValidUrl;

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

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

/**
 * Genotypic data for an accession is available on a remote server {@code #serverUrl}
 * as {@code #genotypeId}.
 */
@Entity
@Table(
  name = "genotype",
  uniqueConstraints = @UniqueConstraint(columnNames = { "serverUrl", "genotypeId" }, name = "UK_lydr62lyq3upe0ml43b2heecx"),
  indexes = { @Index(columnList = "accessionId", name = "IDX_wc5vyw5x88gtfqsye9so5i6s"), @Index(columnList = "instCode, acceNumb", name = "IDX_ngecbg7atvogpy16wydlbb25a") }
)
@Getter
@Setter
@NoArgsConstructor
public class Genotype extends AccessionRef<FaoInstitute> implements Copyable<Genotype> {

	/**
	 * Institute is used to group the genotype records and manage permissions.
	 */
	@ManyToOne(cascade = {}, optional = false, fetch = FetchType.LAZY)
	@JoinColumn(name = "instituteId", updatable = false)
	@JsonIgnore
	protected FaoInstitute list;

	/**
	 * The URL of the remote system where genotypic data of the accession is found
	 * as {@code genotypeId}.
	 * Note that this should be the <b>base URL of the server</b> and not a precise
	 * URL to the actual genotype.
	 */
	@Column(length = 100)
	@ValidUrl
	@Size(max = 100)
	@CsvBindByName(column = "serverUrl")
	private String serverUrl;

	/** ID of the accession genotype as used on the remote @{code serverUrl} */
	@Column(length = 100)
	@NotBlank
	@SimpleString
	@Size(max = 100)
	@CsvBindByName(column = "genotypeId")
	private String genotypeId;

	public Genotype(Accession accession) {
		super(accession);
	}

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

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

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

}