VocabularyTerm.java
/*
* Copyright 2017 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.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonView;
import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.server.model.impl.TranslatedBasicModel;
import org.genesys.server.model.traits.Descriptor;
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.text.MessageFormat;
/**
* A single vocabulary term, belonging to a {@link ControlledVocabulary}.
*
* Example: terms below belong to ISO-3166 controlled vocabulary
*
* <pre>
* DEU, Germany
* ITA, Italy
* NGA, Nigeria
* </pre>
*
* @author Matija Obreza
*/
@Entity
@Table(name = "term")
@Document(indexName = "vocabularyterm")
@Getter
@Setter
public class VocabularyTerm extends TranslatedBasicModel<VocabularyTermLang, VocabularyTerm> implements SelfCleaning, Copyable<VocabularyTerm> {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 6568957772654494563L;
/** Term code. */
@NotNull
@Size(max = 50)
@Column(nullable = false, length = 50)
private String code;
/**
* Term title in English.
*/
@NotNull
@Column(nullable = false)
private String title;
/**
* Link back to Descriptor
*/
@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.REFRESH })
@JoinTable(name = "descriptor_term", joinColumns = @JoinColumn(name = "termId"),
// other side
inverseJoinColumns = @JoinColumn(name = "descriptorId"))
@JsonView({ JsonViews.Indexed.class, JsonViews.Minimal.class })
@JsonIgnoreProperties(value = { "terms", "owner" })
@JsonIdentityReference(alwaysAsId = false)
@Field(type = FieldType.Nested)
private Descriptor descriptor;
/**
* Link back to Descriptor
*/
@ManyToOne(fetch = FetchType.LAZY, cascade = { CascadeType.REFRESH })
@JoinTable(name = "vocabulary_term", joinColumns = @JoinColumn(name = "termId"),
// other side
inverseJoinColumns = @JoinColumn(name = "vocabularyId"))
@JsonView({ JsonViews.Indexed.class, JsonViews.Minimal.class })
@JsonIgnoreProperties(value = { "terms", "owner" })
@JsonIdentityReference(alwaysAsId = false)
@Field(type = FieldType.Nested)
private ControlledVocabulary vocabulary;
/**
* Term description in English.
*/
@Lob
private String description;
/**
* Pre-persist, pre-update
*/
@PrePersist
@PreUpdate
private void preupdate() {
trimStringsToNull();
}
/**
* Create {@link VocabularyTerm} instance with specified parameters.
*
* @param code term code
* @param title term title
* @return updated term
*/
public static VocabularyTerm fromData(final String code, final String title) {
final VocabularyTerm term = new VocabularyTerm();
term.setCode(code);
term.setTitle(title);
term.setOriginalLanguageTag("en");
return term;
}
/**
* Create {@link VocabularyTerm} instance with specified parameters.
*
* @param code term code
* @param title term title
* @param lang term lang
* @return updated term
*/
public static VocabularyTerm fromData(final String code, final String title, final String lang) {
final VocabularyTerm term = new VocabularyTerm();
term.setCode(code);
term.setTitle(title);
term.setOriginalLanguageTag(lang);
return term;
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return MessageFormat.format("code={0} term={1}", code, title);
}
@Override
public boolean canEqual(Object other) {
return other instanceof VocabularyTerm;
}
}