ControlledVocabulary.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.vocab;

import com.fasterxml.jackson.annotation.*;
import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.Publishable;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.blocks.model.UuidModel;
import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.server.model.Partner;
import org.hibernate.annotations.ColumnDefault;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * A controlled vocabulary groups {@link VocabularyTerm} to a list.
 *
 * Example: The ISO-3166 3 alpha is a controlled vocabulary of 3-letter country
 * codes.
 *
 *
 * @author Matija Obreza
 */
@Entity
@Table(name = "vocabulary")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid", scope = ControlledVocabulary.class)
@Document(indexName = "controlledvocabulary")
@Getter
@Setter
public class ControlledVocabulary extends UuidModel implements Publishable, SelfCleaning, AclAwareModel {

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

	/**
	 * User-specified version tag. E.g. "1.0", "1.1"
	 */
	private String versionTag;

	/** Publish by default!. */
	@Column()
	private boolean published;

	/**
	 * Trait title in English.
	 */
	@Size(max = 200)
	@Column(nullable = true, length = 200)
	private String publisher;

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

	/**
	 * Trait description in English.
	 */
	@Lob
	private String description;

	/** URL to definition of the vocabulary. */
	private String url;

	/** Term URL prefix. */
	private String termUrlPrefix;

	/** The terms. */
	@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REFRESH })
	@JoinTable(name = "vocabulary_term", joinColumns = @JoinColumn(name = "vocabularyId"),
			// other side
			inverseJoinColumns = @JoinColumn(name = "termId", unique = true),
			// unique constraints
			uniqueConstraints = { @UniqueConstraint(columnNames = { "vocabularyId", "termId" }) })
	@OrderColumn(name = "idx")
	@JsonIgnoreProperties({ "vocabulary", "descriptor" })
	@JsonView({ JsonViews.Minimal.class })
	@Field(type = FieldType.Nested)
	private List<VocabularyTerm> terms;

	/** Number of terms in the vocabulary */
	@ColumnDefault("0")
	private int termCount;

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

	/**
	 * Pre-persist, pre-update
	 */
	@PrePersist
	@PreUpdate
	private void preupdate() {
		trimStringsToNull();
		
		this.termCount = this.terms == null ? 0 : this.terms.size();
	}

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