TranslatedAuditedVersionedModel.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.impl;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.custom.elasticsearch.IgnoreField;
import org.genesys.server.component.elastic.ElasticLoader;
import org.genesys.spring.validation.javax.SupportedLanguage;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.validation.constraints.Size;
import java.util.List;

@MappedSuperclass
@Getter
@Setter
public abstract class TranslatedAuditedVersionedModel<L extends LangModel<L, E>, E extends AuditedVersionedModel> extends AuditedVersionedModel implements ITranslatedModel, ElasticLoader {

	@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, mappedBy = "entity")
	@JsonIgnore
	@IgnoreField(value = false)
	@Field(type = FieldType.Nested)
	@JsonIgnoreProperties({ "ownedBy", "entity" })
	protected List<L> langs;

	/**
	 * The original language in which title (description, etc.) were provided to Genesys.
	 */
	@Column(length = LangModel.LANGUAGETAG_MAX_LENGTH, name = "lang", nullable = false)
	@Size(min = LangModel.LANGUAGETAG_MIN_LENGTH, max = LangModel.LANGUAGETAG_MAX_LENGTH)
	@SupportedLanguage
	protected String originalLanguageTag;

	public List<L> getLangs() {
		return langs;
	}

	public void setLangs(List<L> langs) {
		this.langs = langs;
	}

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