TranslationService.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.service;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import lombok.Getter;
import lombok.Setter;

import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.genesys.server.model.impl.LangModel;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

@Transactional(readOnly = true)
public interface TranslationService<
	// The entity
	E extends BasicModel,
	// The entityLang
	L extends LangModel<L, E>,
	// Translated entity
	T extends TranslationService.Translation<E, L>,
	// Translated entity filter
	F extends EmptyModelFilter<?, ?>
	> extends CRUDService<L> {

	/**
	 * List.
	 *
	 * @param filter the filter
	 * @param page the page
	 * @return the page
	 */
	Page<T> list(F filter, Pageable page);

	/**
	 * Gets the translated.
	 *
	 * @param entity the entity
	 * @return the translated
	 */
	T getTranslated(E entity);

	/**
	 * Gets the translation in selected locale.
	 *
	 * @param entity the entity
	 * @param locale the requested locale
	 * @return the translated
	 */
	T getTranslated(E entity, Locale locale);

	/**
	 * Gets the translated list.
	 *
	 * @param entityList the list entities
	 * @return the translated list
	 */
	List<T> getTranslated(List<E> entityList);

	/**
	 * Gets the translated list in selected locale.
	 *
	 * @param entityList the list entities
	 * @param locale the requested locale
	 * @return the translated list
	 */
	List<T> getTranslated(List<E> entityList, Locale locale);

	/**
	 * The Class Translation.
	 *
	 * @param <E> the element type
	 * @param <L> the generic type
	 */
	@Setter
	@Getter
	abstract static class Translation<E extends BasicModel, L extends LangModel<L, E>> implements Serializable {
		private static final long serialVersionUID = 7629603657547628388L;

		/** The entity. */
		@JsonUnwrapped
		@NotNull
		@JsonIgnoreProperties("id")
		public E entity;

		/** The translation. */
		@NotNull
		public L translation;

		/**
		 * Instantiates a new translation.
		 */
		protected Translation() {
		}

		/**
		 * Instantiates a new translation.
		 *
		 * @param entity the entity
		 * @param title the title
		 * @param description the description
		 */
		public Translation(E entity, L translation) {
			this.entity = entity;
			this.translation = translation;
		}

		@Override
		public boolean equals(Object o) {
			if (this == o) return true;
			if (o == null || getClass() != o.getClass()) return false;

			Translation<?, ?> that = (Translation<?, ?>) o;

			if (!Objects.equals(entity, that.entity)) return false;
			if (!Objects.equals(translation, that.translation)) return false;
			return true;
		}

		@Override
		public int hashCode() {
			int result = entity != null ? entity.hashCode() : 0;
			result = 31 * result + (translation != null ? translation.hashCode() : 42);
			return result;
		}
	}

	List<L> listTranslations(E e);

	L deleteTranslation(E entity, String languageTag);

	L addLang(E reloaded, L input);

	L upsertLang(E reloaded, L input);

	L getLang(@NotNull E entity, String languageTag);
}