Citation.java

/*
 * Copyright 2024 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.bib;

import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OrderColumn;
import javax.persistence.Table;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;

import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.UuidModel;
import org.genesys.custom.elasticsearch.IgnoreField;
import org.genesys.custom.elasticsearch.SearchField;
import org.genesys.server.component.elastic.ElasticLoader;
import org.genesys.server.model.Partner;
import org.genesys.server.model.PublishState;
import org.genesys.spring.validation.javax.SimpleString;
import org.genesys.spring.validation.javax.ValidUrl;
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.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

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

@Entity
@Table(name = "bib_citation")
@Getter
@Setter
@Document(indexName = "bib_citation")
@NoArgsConstructor
public class Citation extends UuidModel implements Copyable<Citation>, ElasticLoader {

	@Basic
	@Size(max = 500)
	@Column(length = 500, nullable = false)
	@SimpleString
	@SearchField
	private String title;

	@Basic
	private Integer publicationYear;

	@ElementCollection
	@CollectionTable(name = "bib_citation_keyword", joinColumns = @JoinColumn(name = "citationId"))
	@Column(name = "keyword", nullable = false, length = 200)
	@OrderColumn(name = "position")
	@SimpleString
	@SearchField
	private List<String> keywords;

	@ElementCollection
	@CollectionTable(name = "bib_citation_collection", joinColumns = @JoinColumn(name = "citationId"))
	@Column(name = "collection", nullable = false, length = 200)
	@OrderColumn(name = "position")
	@SimpleString
	@Field(type = FieldType.Keyword)
	@SearchField
	private List<String> collections;

	@ElementCollection
	@CollectionTable(name = "bib_taxonomy_families", joinColumns = @JoinColumn(name = "citationId"))
	@Column(name = "taxonomy_family", nullable = false, length = 100)
	@OrderColumn(name = "position")
	@SimpleString
	private List<String> taxonomyFamilies;

	@ElementCollection
	@CollectionTable(name = "bib_taxonomy_species", joinColumns = @JoinColumn(name = "citationId"))
	@Column(name = "taxonomy_species", nullable = false, length = 100)
	@OrderColumn(name = "position")
	@SimpleString
	private List<String> taxonomySpecies;

	@ElementCollection
	@CollectionTable(name = "bib_institute_codes", joinColumns = @JoinColumn(name = "citationId"))
	@Column(name = "institute_code", nullable = false, length = 10)
	@OrderColumn(name = "position")
	@SimpleString
	private List<@Pattern(regexp = "[A-Z]{3}\\d{3,4}") String> instituteCodes;

	@Basic
	@Size(max = 100)
	@Pattern(message = "Invalid DOI", regexp = "^10\\.\\d+/.+$")
	private String doi;

	@ElementCollection
	@CollectionTable(name = "bib_citation_url", joinColumns = @JoinColumn(name = "citationId"))
	@Column(name = "url", nullable = false, length = 500)
	@OrderColumn(name = "position")
	@ValidUrl
	private List<String> urls;

	@ElementCollection
	@CollectionTable(name = "bib_citation_author", joinColumns = @JoinColumn(name = "citationId"))
	@OrderColumn(name = "position")
	@Valid
	@Field(type = FieldType.Nested)
	private List<Author> authors;

	@Basic
	@Size(max = 500)
	@Column(length = 500)
	@SimpleString
	private String publication;

	@Basic
	@Size(max = 500)
	@Column(length = 500)
	@SimpleString
	private String publisher;

	@Basic
	@Size(max = 10)
	@Column(length = 10)
	@SimpleString
	private String language;

	@Basic
	@Size(max = 50)
	@Column(length = 50)
	@SimpleString
	private String volume;

	@Basic
	@Size(max = 50)
	@Column(length = 50)
	@SimpleString
	private String issue;

	@Basic
	@Size(max = 50)
	@Column(length = 50)
	@SimpleString
	private String pages;

	@Basic
	@Size(max = 50)
	@Column(length = 50)
	@SimpleString
	private String serialNumber;

	@Enumerated(EnumType.STRING)
	@Column(name = "type", nullable = false, length = 50)
	private CitationType type;

	@Basic
	@Size(max = 500)
	@Column(length = 500)
	@ValidUrl
	private String downloadUrl;

	/**
	 * The abstract of the publication.
	 */
	@Basic
	@Lob
	@SearchField
	private String abstractText;

	@Enumerated(EnumType.STRING)
	@Column(name = "sourceType", length = 50)
	private SourceType sourceType;

	@Enumerated(EnumType.STRING)
	@Column(name = "relevance", length = 50)
	private RelevanceType relevance; // TODO

	@ManyToOne(cascade = {}, optional = false)
	@JoinColumn(name = "partnerId", updatable = false)
	@NotNull
	@Field(type = FieldType.Object)
	private Partner owner;

	@Enumerated(EnumType.STRING)
	@Column(name = "state", nullable = false, length = 10)
	private PublishState state;


	@Basic
	@Size(max = 500)
	@Column
	private String statusText;

	@ManyToOne()
	@JoinColumn(name = "linkedCitationId")
	@JsonIgnoreProperties({ "linkedCitation" })
	@IgnoreField
	private Citation linkedCitation;

	/**
	 * The original/complete source data in RIS (or other) format
	 */
	@Lob
	@JsonIgnore
	@IgnoreField
	private String sourceData;

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

	@Override
	public void prepareForIndexing() {
		if (this.authors != null) {
			this.authors.size();
		}
	}
}