DescriptorListApiServiceImpl.java

/*
 * Copyright 2024 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 com.google.common.collect.Lists;
import org.genesys.server.api.v2.facade.DescriptorListApiService;
import org.genesys.server.api.v2.model.impl.DescriptorDTO;
import org.genesys.server.api.v2.model.impl.DescriptorListDTO;
import org.genesys.server.api.v2.model.impl.DescriptorListInfo;
import org.genesys.server.api.v2.model.impl.DescriptorListLangDTO;
import org.genesys.server.api.v2.model.impl.TranslatedDescriptorDTO;
import org.genesys.server.api.v2.model.impl.TranslatedDescriptorListDTO;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.filters.DescriptorListFilter;
import org.genesys.server.model.traits.Descriptor;
import org.genesys.server.model.traits.DescriptorList;
import org.genesys.server.model.traits.DescriptorListLang;
import org.genesys.server.service.DescriptorListService;
import org.genesys.server.service.DescriptorListTranslationService;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.TranslatorService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service("descriptorListV2APIService")
@Validated
public class DescriptorListApiServiceImpl extends APIFilteredTranslatedServiceFacadeImpl<DescriptorListService, DescriptorListDTO,
	TranslatedDescriptorListDTO, DescriptorListLangDTO, DescriptorList, DescriptorListLang,
	DescriptorListTranslationService.TranslatedDescriptorList, DescriptorListFilter> implements DescriptorListApiService {

	@Override
	protected TranslatedDescriptorListDTO convertTranslation(DescriptorListTranslationService.TranslatedDescriptorList source) {
		return mapper.map(source);
	}

	@Override
	protected DescriptorListLang convertLang(DescriptorListLangDTO source) {
		return mapper.map(source);
	}

	@Override
	protected DescriptorListLangDTO convertLang(DescriptorListLang source) {
		return mapper.map(source);
	}

	@Override
	protected DescriptorList convert(DescriptorListDTO source) {
		return mapper.map(source);
	}

	@Override
	protected DescriptorListDTO convert(DescriptorList source) {
		return mapper.map(source);
	}

	@Override
	@Transactional(readOnly = true)
	public long countDescriptorLists(DescriptorListFilter filter) throws SearchException {
		return service.countDescriptorLists(filter);
	}

	@Override
	@Transactional
	public DescriptorListDTO removeDescriptors(DescriptorListDTO descriptorList, DescriptorDTO... descriptors) {
		var mappedDescriptors = mapper.map(Lists.newArrayList(descriptors), mapper::map).toArray(new Descriptor[] {});
		return mapper.map(service.removeDescriptors(mapper.map(descriptorList), mappedDescriptors));
	}

	@Override
	@Transactional
	public DescriptorListDTO addDescriptors(DescriptorListDTO descriptorList, DescriptorDTO... descriptors) {
		var mappedDescriptors = mapper.map(Lists.newArrayList(descriptors), mapper::map).toArray(new Descriptor[] {});
		return mapper.map(service.addDescriptors(mapper.map(descriptorList), mappedDescriptors));
	}

	@Override
	@Transactional(readOnly = true)
	public DescriptorListDTO loadDescriptorList(UUID uuid) {
		return mapper.map(service.loadDescriptorList(uuid));
	}

	@Override
	public TranslatedDescriptorListDTO loadTranslatedDescriptorList(UUID uuid) {
		return mapper.map(service.loadTranslatedDescriptorList(uuid));
	}

	@Override
	@Transactional(readOnly = true)
	public List<TranslatedDescriptorDTO> loadDescriptors(UUID uuid) {
		return mapper.map(service.loadTranslatedDescriptors(service.get(uuid)), mapper::map);
	}

	@Override
	@Transactional(readOnly = true)
	public DescriptorListDTO loadDescriptorList(UUID uuid, int version) {
		return mapper.map(service.loadDescriptorList(uuid, version));
	}

	@Override
	@Transactional(readOnly = true)
	public DescriptorListDTO loadDescriptorList(DescriptorListDTO input) {
		return mapper.map(service.loadDescriptorList(mapper.map(input)));
	}

	@Override
	@Transactional(readOnly = true)
	public Map<String, ElasticsearchService.TermResult> getSuggestions(DescriptorListFilter filter) throws SearchException, IOException {
		return service.getSuggestions(filter);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<DescriptorListDTO> listDescriptorListsForCurrentUser(DescriptorListFilter descriptorListFilter, Pageable page) throws SearchException {
		return mapper.map(service.listDescriptorListsForCurrentUser(descriptorListFilter, page), mapper::map);
	}

	@Override
	@Transactional
	public DescriptorListDTO reviewDescriptorList(DescriptorListDTO descriptorList) {
		return mapper.map(service.reviewDescriptorList(mapper.map(descriptorList)));
	}

	@Override
	@Transactional
	public DescriptorListDTO rejectDescriptorList(DescriptorListDTO descriptorList) {
		return mapper.map(service.rejectDescriptorList(mapper.map(descriptorList)));
	}

	@Override
	@Transactional
	public DescriptorListDTO approveDescriptorList(DescriptorListDTO descriptorList) {
		return mapper.map(service.approveDescriptorList(mapper.map(descriptorList)));
	}

	@Override
	@Transactional
	public DescriptorListDTO setDescriptors(DescriptorListDTO descriptorList, DescriptorDTO[] descriptors) {
		var mappedDescriptors = mapper.map(Lists.newArrayList(descriptors), mapper::map).toArray(new Descriptor[] {});
		return mapper.map(service.setDescriptors(mapper.map(descriptorList), mappedDescriptors));
	}

	@Override
	@Transactional(readOnly = true)
	public List<DescriptorListInfo> autocompleteDescriptorLists(String text) {
		return mapper.map(service.autocompleteDescriptorLists(text), mapper::mapInfo);
	}

	@Override
	@Transactional(readOnly = true)
	public void exportDescriptorList(UUID descriptorListUuid, OutputStream outputStream) throws IOException {
		service.exportDescriptorList(service.get(descriptorListUuid), outputStream);
	}

	@Override
	public DescriptorListLangDTO machineTranslate(UUID uuid, String targetLang) throws TranslatorService.TranslatorException {
		return mapper.map(service.machineTranslate(service.loadDescriptorList(uuid), targetLang));
	}
}