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

import java.util.List;

import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.gringlobal.api.v2.facade.APIFilteredTranslatedServiceFacade;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.CooperatorOwnedLang;
import org.gringlobal.model.SysLang;
import org.gringlobal.model.TranslatedCooperatorOwnedModel;
import org.gringlobal.service.FilteredTranslatedCRUDService;
import org.gringlobal.service.TranslationService;
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;

public abstract class APIFilteredTranslatedServiceFacadeImpl<S extends FilteredTranslatedCRUDService<E, L, T, F>, DTO, TDTO, LDTO, E extends TranslatedCooperatorOwnedModel<L, E>, L extends CooperatorOwnedLang<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 T convertTranslation(TDTO source);

	protected abstract TDTO convertTranslation(T source);
	
	protected abstract L convertLang(LDTO source);

	protected abstract LDTO convertLang(L source);
	
	@Override
	@Transactional
	public TDTO createTranslated(TDTO source) {
		return convertTranslation(translationService.create(convertTranslation(source)));
	}

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

	@Override
	@Transactional(readOnly = true)
	public TDTO reloadTranslated(TDTO source) {
		return convertTranslation(translationService.reload(convertTranslation(source)));
	}

	@Override
	@Transactional(readOnly = true)
	public DTO reload(DTO source) {
		return convert(translationService.reload(convert(source)));
	}

	@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 remove(DTO entity, SysLang sysLang) {
		return convertLang(translationService.remove(convert(entity), sysLang));
	}

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

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