APIFilteredTranslatedServiceFacadeImpl.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.api.v2.facade.impl;

import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.genesys.server.api.v2.facade.APIFilteredTranslatedServiceFacade;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.impl.LangModel;
import org.genesys.server.service.FilteredTranslatedCRUDService;
import org.genesys.server.service.TranslationService;
import org.genesys.server.service.TranslatorService.TranslatorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public abstract class APIFilteredTranslatedServiceFacadeImpl
	<S extends FilteredTranslatedCRUDService<E, L, T, F>, DTO, TDTO, LDTO, E extends BasicModel, L extends LangModel<L, E>, T extends TranslationService.Translation<E, L>, F extends EmptyModelFilter<F, E>>
	extends APIServiceFacadeImpl<S, DTO, E> implements APIFilteredTranslatedServiceFacade<DTO, TDTO, LDTO, E, L, F> {

	@Autowired
	protected S translationService;

	protected abstract TDTO convertTranslation(T source);

	protected abstract L convertLang(LDTO source);

	protected abstract LDTO convertLang(L source);

	@Override
	@Transactional(readOnly = true)
	public TDTO loadTranslated(long id) {
		return convertTranslation(translationService.loadTranslated(id));
	}

	@Override
	@Transactional(readOnly = true)
	public TDTO loadTranslated(E entity) {
		return convertTranslation(translationService.loadTranslated(entity.getId()));
	}

	@Override
	@Transactional(readOnly = true)
	public Page<TDTO> listFiltered(F filter, Pageable page) throws SearchException {
		return mapper.map(translationService.listFiltered(filter, page), this::convertTranslation);
	}

	@Override
	@Transactional
	public LDTO removeTranslation(DTO entity, String languageTag) {
		return convertLang(translationService.removeTranslation(convert(entity), languageTag));
	}

	@Override
	@Transactional
	public LDTO upsertTranslation(DTO entity, LDTO input) {
		return convertLang(translationService.upsertTranslation(convert(entity), convertLang(input)));
	}

	@Override
	@Transactional(readOnly = true)
	public List<LDTO> listTranslations(long entityId) {
		var l = translationService.getLangs(entityId);
		return mapper.map(l, this::convertLang);
	}

	@Override
	public LDTO machineTranslate(long entityId, String targetLanguage) throws TranslatorException {
		return convertLang(translationService.machineTranslate(service.get(entityId), targetLanguage));
	}

	@Override
	public LDTO machineTranslate(E entity, String targetLanguage) throws TranslatorException {
		return convertLang(translationService.machineTranslate(entity, targetLanguage));
	}

}