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.v1.facade.impl;

import com.google.common.collect.Lists;
import org.genesys.server.api.v1.facade.DescriptorListApiService;
import org.genesys.server.api.v1.mapper.APIv1Mapper;
import org.genesys.server.api.v1.model.Descriptor;
import org.genesys.server.api.v1.model.DescriptorList;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.filters.DescriptorListFilter;
import org.genesys.server.service.DescriptorListService;
import org.genesys.server.service.ElasticsearchService;
import org.springframework.beans.factory.annotation.Autowired;
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("descriptorListV1APIService")
@Validated
public class DescriptorListApiServiceImpl implements DescriptorListApiService {

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

	@Override
	@Transactional
	public DescriptorList removeDescriptors(DescriptorList descriptorList, Descriptor... descriptors) {
		var mappedDescriptors = mapper.map(Lists.newArrayList(descriptors), mapper::map).toArray(new org.genesys.server.model.traits.Descriptor[]{});
		return mapper.map(service.removeDescriptors(mapper.map(descriptorList), mappedDescriptors));
	}

	@Override
	@Transactional
	public DescriptorList addDescriptors(DescriptorList descriptorList, Descriptor... descriptors) {
		var mappedDescriptors = mapper.map(Lists.newArrayList(descriptors), mapper::map).toArray(new org.genesys.server.model.traits.Descriptor[]{});
		return mapper.map(service.addDescriptors(mapper.map(descriptorList), mappedDescriptors));
	}

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

	@Override
	@Transactional(readOnly = true)
	public List<Descriptor> loadDescriptors(DescriptorList descriptorList) {
		return mapper.map(service.loadDescriptors(mapper.map(descriptorList)), mapper::map);
	}

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

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

	@Override
	@Transactional(readOnly = true)
	public Page<DescriptorList> listDescriptorLists(DescriptorListFilter filters, Pageable page) throws SearchException {
		return mapper.map(service.list(filters, page), mapper::map);
	}

	@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<DescriptorList> listDescriptorListsForCurrentUser(DescriptorListFilter descriptorListFilter, Pageable page) throws SearchException {
		return mapper.map(service.listDescriptorListsForCurrentUser(descriptorListFilter, page), mapper::map);
	}

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

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

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

	@Override
	@Transactional
	public DescriptorList setDescriptors(DescriptorList descriptorList, Descriptor[] descriptors) {
		var mappedDescriptors = mapper.map(Lists.newArrayList(descriptors), mapper::map).toArray(new org.genesys.server.model.traits.Descriptor[]{});
		return mapper.map(service.setDescriptors(mapper.map(descriptorList), mappedDescriptors));
	}

	@Override
	@Transactional
	public List<DescriptorList> autocompleteDescriptorLists(String text) {
		return mapper.map(service.autocompleteDescriptorLists(text), mapper::map);
	}

	@Override
	@Transactional(readOnly = true)
	public void exportDescriptorList(DescriptorList descriptorList, OutputStream outputStream) throws IOException {
		service.exportDescriptorList(mapper.map(descriptorList), outputStream);
	}

	@Override
	@Transactional
	public DescriptorList remove(DescriptorList descriptorList) {
		return mapper.map(service.remove(mapper.map(descriptorList)));
	}

	@Override
	@Transactional
	public DescriptorList update(DescriptorList descriptorList) {
		return mapper.map(service.update(mapper.map(descriptorList)));
	}

	@Override
	@Transactional
	public DescriptorList create(DescriptorList descriptorList) {
		return mapper.map(service.create(mapper.map(descriptorList)));
	}

}