FilteredTranslatedCRUDServiceImpl.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.impl;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.model.filters.EmptyModelFilter;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.impl.LangModel;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.FilteredTranslatedCRUDService;
import org.genesys.server.service.TranslationService;
import org.genesys.server.service.filter.IFullTextFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.transaction.annotation.Transactional;
import java.lang.reflect.ParameterizedType;
import java.util.List;
/**
* The Class FilteredTranslatedCRUDServiceImpl.
*
* @param <E> the Entity type
* @param <L> the EntityLang type
* @param <T> the TranslatedEntity type
* @param <F> the EntityFilter type
* @param <R> the Repository for Entity
*/
@Transactional(readOnly = true)
public abstract class FilteredTranslatedCRUDServiceImpl<E extends BasicModel, L extends LangModel<L, E>, T extends TranslationService.Translation<E, L>, F extends EmptyModelFilter<F, E>, R extends JpaRepository<E, Long> & QuerydslPredicateExecutor<E>>
extends FilteredCRUDService2Impl<E, F, R> implements FilteredTranslatedCRUDService<E, L, T, F> {
@Autowired(required = false)
protected ElasticsearchService elasticsearchService;
@Autowired
protected TranslationService<E, L, T, F> translationSupport;
/** The runtime Java type of the entity E */
private final Class<E> targetType;
@SuppressWarnings("unchecked")
public FilteredTranslatedCRUDServiceImpl() {
this.targetType = ((Class<E>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]);
}
// @Override
// @Transactional
// public T create(T source) {
// assert(source.entity != null);
// return upsert(source.entity, LocaleContextHolder.getLocale().toLanguageTag(), source);
// }
@Override
public T loadTranslated(long id) {
return translationSupport.getTranslated(load(id));
}
@Override
public T reload(T source) {
return translate(reload(source.entity));
}
protected final T translate(E source) {
return translationSupport.getTranslated(source);
}
@Override
public Page<T> listFiltered(F filter, Pageable page) throws SearchException {
return listFiltered(targetType, filter, page);
}
protected Page<T> listFiltered(Class<E> clazz, F filter, Pageable page, String... boostFields) throws SearchException {
if (filter instanceof IFullTextFilter) {
IFullTextFilter ftf = (IFullTextFilter) filter;
if (ftf.isFulltextQuery()) {
var entityPage = elasticsearchService.findAll(clazz, filter, null, page, this::list, boostFields);
var content = entityPage.getContent();
if (content.isEmpty()) {
return Page.empty(page);
} else {
return new PageImpl<>(translationSupport.getTranslated(entityPage.getContent()), page, entityPage.getTotalElements());
}
}
}
return translationSupport.list(filter, page);
}
@Override
public List<L> getLangs(long entityId) {
return translationSupport.listTranslations(get(entityId));
}
@Override
@Transactional
public L removeTranslation(E entity, String langTag) {
return translationSupport.deleteTranslation(entity, langTag);
}
@Override
@Transactional
public L upsertTranslation(E entity, L input) {
assert(!entity.isNew());
assert(input.getEntity() == null || input.getEntity().getId().equals(entity.getId()));
assert(StringUtils.isNotBlank(input.getLanguageTag()));
return translationSupport.upsertLang(entity, input);
}
// NOTE: GGCE does not have the concept of original translation and always relies on Translation<X>
// NOTE: Genesys keeps the original texts in the entity E so we do not use this approach.
// private T upsert(@NotNull E entity, String langTag, T source) {
// if (entity.isNew()) {
// E reloaded = create(entity);
// var lang = source.updateLang(translationSupport.newLang());
// lang.setLanguageTag(langTag);
// translationSupport.addLang(reloaded, lang);
// return translate(reloaded);
// } else {
// E reloaded = update(entity);
// translationSupport.upsertLang(reloaded, source.updateLang(translationSupport.getLang(entity, langTag)));
// return translate(reloaded);
// }
// }
}