DescriptorList.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.traits;

import java.time.Instant;
import java.util.List;
import java.util.Objects;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderColumn;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.annotations.NotCopyable;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.Publishable;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.server.model.Partner;
import org.genesys.server.model.PublishState;
import org.genesys.server.model.impl.Crop;
import org.genesys.server.model.impl.TranslatedUuidModel;
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.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

/**
 * The Class DescriptorList.
 *
 * @author Andrey Lugovskoy
 */
@Entity
@Table(name="descriptorlist")
@Audited
@Cacheable
@Document(indexName = "descriptorlist")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid", scope = DescriptorList.class)
@Getter
@Setter
public class DescriptorList extends TranslatedUuidModel<DescriptorListLang, DescriptorList> implements Publishable, SelfCleaning, AclAwareModel {

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

	/**
	 * Descriptor List title in English.
	 */
	@NotNull
	@Column(nullable = false)
	private String title;

	/**
	 * Descriptor List description in English.
	 */
	@Lob
	private String description;

	/** Not published by default. */
	@Enumerated(EnumType.ORDINAL)
	private PublishState state = PublishState.DRAFT;

	/** Descriptor List URL. */
	private String url;

	/**
	 * User-specified version tag. E.g. "1.0", "1.1"
	 */
	@NotNull
	@Column(nullable = false, updatable = false)
	private String versionTag;

	/** The bibliographic citation. */
	@Column(name = "bibliographicCitation")
	@Lob
	private String bibliographicCitation;

	/** The descriptors. */
	@ManyToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.LAZY)
	@JoinTable(name = "descriptorlist_descriptor", joinColumns = @JoinColumn(name = "descriptorListId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "descriptorId", referencedColumnName = "id"))
	@OrderColumn(name = "position")
	@JsonIgnoreProperties(value = { "descriptorLists", "datasets", "owner", "vocabulary" })
	@JsonView(JsonViews.Indexed.class)
	@Field(type = FieldType.Nested)
	List<Descriptor> descriptors;

	/** The owner. */
	@NotNull
	@ManyToOne(cascade = {}, optional = false)
	@JoinColumn(name = "partnerId", updatable = false)
	@JsonView({ JsonViews.Public.class })
	@Field(type = FieldType.Object)
	private Partner owner;

	/** The publisher. */
	private String publisher;

	/** The crop. */
	@Size(max = Crop.CROP_SHORTNAME_LENGTH)
	@Column(name = "crop", length = Crop.CROP_SHORTNAME_LENGTH, nullable = true)
	private String crop;

	@Column(name = "firstPublishedDate")
	@NotCopyable
	private Instant firstPublishedDate;

	/**
	 * Pre-persist, pre-update
	 */
	@PrePersist
	@PreUpdate
	private void preupdate() {
		if (Objects.isNull(firstPublishedDate) && Objects.equals(state, PublishState.PUBLISHED)) {
			firstPublishedDate = Instant.now();
		}
		trimStringsToNull();
	}

	/**
	 * Owner is the ACL parent object for the descriptor list
	 */
	@Override
	public AclAwareModel aclParentObject() {
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.genesys.blocks.model.Publishable#isPublished()
	 */
	@Override
	public boolean isPublished() {
		return this.state == PublishState.PUBLISHED;
	}

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