DescriptorApiServiceImpl.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 org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryImage;
import org.genesys.server.api.v1.facade.DescriptorApiService;
import org.genesys.server.api.v1.mapper.APIv1Mapper;
import org.genesys.server.api.v1.model.DatasetInfo;
import org.genesys.server.api.v1.model.Descriptor;
import org.genesys.server.api.v1.model.DescriptorList;
import org.genesys.server.api.v1.model.DescriptorListInfo;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.dataset.Dataset;
import org.genesys.server.model.filters.DescriptorFilter;
import org.genesys.server.service.DescriptorService;
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 org.springframework.web.multipart.MultipartFile;

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

@Service("descriptorV1APIService")
@Validated
public class DescriptorApiServiceImpl implements DescriptorApiService {

	@Autowired
	private APIv1Mapper mapper;
	
	@Autowired
	private DescriptorService service;
	
	@Override
	@Transactional(readOnly = true)
	public List<Descriptor> searchMatchingDescriptor(Descriptor descriptor) {
		return mapper.map(service.searchMatchingDescriptor(mapper.map(descriptor)), mapper::map);
	}

	@Override
	@Transactional
	public Descriptor updateImage(Descriptor descriptor, MultipartFile file, RepositoryImage imageMetadata) throws IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException, NoSuchRepositoryFileException {
		return mapper.map(service.updateImage(mapper.map(descriptor), file, imageMetadata));
	}

	@Override
	@Transactional
	public Descriptor removeImage(Descriptor descriptor) throws NoSuchRepositoryFileException, IOException, InvalidRepositoryPathException {
		return mapper.map(service.removeImage(mapper.map(descriptor)));
	}

	@Override
	@Transactional
	public Descriptor forceUpdateDescriptor(Descriptor descriptor) {
		return mapper.map(service.forceUpdateDescriptor(mapper.map(descriptor)));
	}

	@Override
	@Transactional(readOnly = true)
	public Descriptor loadDescriptor(UUID uuid) throws NotFoundElement {
		return mapper.map(service.loadDescriptor(uuid));
	}

	@Override
	@Transactional(readOnly = true)
	public Descriptor loadDescriptor(UUID uuid, int version) throws NotFoundElement {
		return mapper.map(service.loadDescriptor(uuid, version));
	}

	@Override
	@Transactional(readOnly = true)
	public Page<Descriptor> listDescriptorsForCurrentUser(DescriptorFilter filter, Pageable page) throws IOException, SearchException {
		return mapper.map(service.listDescriptorsForCurrentUser(filter, page), mapper::map);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<Descriptor> listAccessibleDescriptors(DescriptorFilter filter, Pageable page) throws IOException, SearchException {
		return mapper.map(service.listAccessibleDescriptors(filter, page), mapper::map);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<Descriptor> listDescriptors(DescriptorFilter descriptorFilter, Pageable page) throws SearchException {
		return mapper.map(service.listDescriptors(descriptorFilter, page), mapper::map);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<Descriptor> listDescriptorsDetails(DescriptorFilter filter, Pageable page) throws SearchException {
		return mapper.map(service.listDescriptorsDetails(filter, page), mapper::map);
	}

	@Override
	@Transactional
	public Descriptor removeDescriptor(Descriptor descriptor) throws InvalidRepositoryPathException, IOException {
		return mapper.map(service.remove(mapper.map(descriptor)));
	}

	@Override
	@Transactional
	public List<Descriptor> upsertDescriptors(List<Descriptor> source) {
		return mapper.map(service.upsertDescriptors(mapper.map(source, mapper::map)), mapper::map);
	}

	@Override
	@Transactional
	public Descriptor upsertDescriptor(Descriptor source) {
		return mapper.map(service.upsertDescriptor(mapper.map(source)));
	}

	@Override
	@Transactional
	public Descriptor approveDescriptor(Descriptor descriptor) {
		return mapper.map(service.approveDescriptor(mapper.map(descriptor)));
	}

	@Override
	@Transactional
	public Descriptor reviewDescriptor(Descriptor descriptor) {
		return mapper.map(service.reviewDescriptor(mapper.map(descriptor)));
	}

	@Override
	@Transactional
	public Descriptor rejectDescriptor(Descriptor descriptor) {
		return mapper.map(service.rejectDescriptor(mapper.map(descriptor)));
	}

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

	@Override
	@Transactional(readOnly = true)
	public List<DatasetInfo> getDatasets(Descriptor descriptor) {
		return mapper.map(service.getDatasets(mapper.map(descriptor)), mapper::mapInfo);
	}

	@Override
	@Transactional
	public Descriptor nextVersion(Descriptor descriptor, boolean major) {
		return mapper.map(service.nextVersion(mapper.map(descriptor), major));
	}

	@Override
	@Transactional(readOnly = true)
	public void exportDescriptors(DescriptorFilter filter, OutputStream outputStream) throws IOException {
		service.exportDescriptors(filter, outputStream);
	}

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

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

	@Override
	public Descriptor create(Descriptor descriptor) {
		return mapper.map(service.create(mapper.map(descriptor)));
	}

	@Override
	public Descriptor update(Descriptor descriptor) {
		return mapper.map(service.update(mapper.map(descriptor)));
	}
}