LangModel.java

/*
 * Copyright 2023 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 lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import org.genesys.blocks.annotations.NotCopyable;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.Copyable;
import org.genesys.blocks.model.SelfCleaning;
import org.genesys.custom.elasticsearch.IgnoreField;
import org.genesys.custom.elasticsearch.SearchField;
import org.genesys.server.component.elastic.ElasticTrigger;
import org.genesys.spring.validation.javax.SupportedLanguage;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@MappedSuperclass
@Getter
@Setter
@NoArgsConstructor
@Audited
public abstract class LangModel<L extends LangModel<L, E>, E extends BasicModel> extends AuditedVersionedModel implements Copyable<L>, SelfCleaning, ElasticTrigger {
	private static final long serialVersionUID = -8074420732594755502L;

	public static final int LANGUAGETAG_MIN_LENGTH = 2;
	public static final int LANGUAGETAG_MAX_LENGTH = 10;

	@NotNull
	@ManyToOne(fetch = FetchType.LAZY, cascade = {})
	@JoinColumn(name = "entityId", updatable = false, nullable = false)
	@IgnoreField
	protected E entity;

	@Column(length = LANGUAGETAG_MAX_LENGTH, name = "lang", nullable = false, updatable = false)
	@Size(min = LANGUAGETAG_MIN_LENGTH, max = LANGUAGETAG_MAX_LENGTH)
	@NotCopyable
	@SupportedLanguage
	@Field(type = FieldType.Keyword)
	protected String languageTag;

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

	@Column(nullable = false, name = "mt")
	protected boolean machineTranslated = false;

	@PrePersist
	@PreUpdate
	protected void prepersist() {
		trimStringsToNull();
	}

	/**
	 * Reindex entity when this is saved!
	 */
	@Override
	public Object[] reindexedEntities() {
		return new Object[] { this.entity };
	}
}