RequestApiServiceImpl.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.server.api.v1.facade.RequestApiService;
import org.genesys.server.api.v1.mapper.APIv1Mapper;
import org.genesys.server.api.v1.model.MaterialRequest;
import org.genesys.server.api.v1.model.MaterialSubRequest;
import org.genesys.server.exception.EasySMTAException;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.server.persistence.MaterialSubRequestRepository;
import org.genesys.server.service.InstituteService;
import org.genesys.server.service.RequestService;
import org.genesys.server.service.TokenVerificationService;
import org.genesys.server.service.filter.MaterialRequestFilter;
import org.genesys.server.service.filter.MaterialSubRequestFilter;
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.util.Set;
import java.util.UUID;

@Service("requestV1APIService")
@Validated
@Transactional(readOnly = true)
public class RequestApiServiceImpl implements RequestApiService {

	@Autowired
	private APIv1Mapper mapper;
	
	@Autowired
	private RequestService service;

	@Autowired
	private MaterialSubRequestRepository subRequestRepository;

	@Autowired
	private InstituteService instituteService;

	@Override
	@Transactional
	public MaterialRequest initiateRequestByUuids(RequestService.RequestInfo requestInfo, Set<UUID> accessionUuids, String origin, String sid, String lang) throws RequestService.RequestException {
		return mapper.map(service.initiateRequestByUuids(requestInfo, accessionUuids, origin, sid, lang));
	}

	@Override
	@Transactional
	public MaterialRequest validateClientRequest(String tokenUuid, String key) throws RequestService.RequestException, TokenVerificationService.NoSuchVerificationTokenException, RequestService.NoPidException, EasySMTAException, TokenVerificationService.TokenExpiredException {
		return mapper.map(service.validateClientRequest(tokenUuid, key));
	}

	@Override
	@Transactional
	public MaterialSubRequest relayRequest(MaterialSubRequest materialSubRequest) {
		return mapper.map(service.relayRequest(mapper.map(materialSubRequest)));
	}

	@Override
	@Transactional
	public MaterialSubRequest validateReceipt(String tokenUuid, String key) throws TokenVerificationService.NoSuchVerificationTokenException, TokenVerificationService.TokenExpiredException {
		return mapper.map(service.validateReceipt(tokenUuid, key));
	}

	@Override
	public MaterialRequest getRequestStatus(UUID uuid) {
		return mapper.map(service.getRequestStatus(uuid));
	}

	@Override
	public Page<MaterialRequest> listMyRequests(MaterialRequestFilter filter, Pageable page) {
		return mapper.map(service.listMyRequests(filter, page), mapper::map);
	}

	@Override
	public Page<MaterialRequest> list(MaterialRequestFilter filter, Pageable pageable) {
		return mapper.map(service.list(filter, pageable), mapper::map);
	}

	@Override
	public Page<MaterialSubRequest> listSubRequests(MaterialSubRequestFilter filter, Pageable pageable) {
		return mapper.map(service.listSubRequests(filter, pageable), mapper::map);
	}

	@Override
	public Page<MaterialSubRequest> listMineSubRequests(MaterialSubRequestFilter filter, Pageable pageable) {
		return mapper.map(service.listMineSubRequests(filter, pageable), mapper::map);
	}

	@Override
	public Page<MaterialSubRequest> list(FaoInstitute institute, MaterialSubRequestFilter filter, Pageable pageRequest) {
		return mapper.map(service.list(institute, filter, pageRequest), mapper::map);
	}

	@Override
	public MaterialRequest get(String uuid) {
		return mapper.map(service.get(uuid));
	}

	@Override
	public MaterialRequest get(Long requestId) {
		return mapper.map(service.get(requestId));
	}

	@Override
	public MaterialSubRequest get(FaoInstitute institute, String uuid) {
		return mapper.map(service.get(institute, uuid));
	}

	@Override
	public MaterialSubRequest getSubrequest(String uuid) {
		return mapper.map(service.getSubrequest(uuid));
	}

	@Override
	@Transactional
	public MaterialRequest remove(String uuid) {
		return mapper.map(service.remove(uuid));
	}

	@Override
	@Transactional
	public MaterialRequest sendValidationEmail(MaterialRequest materialRequest) {
		return mapper.map(service.sendValidationEmail(mapper.map(materialRequest)));
	}

	@Override
	@Transactional
	public MaterialRequest recheckPid(MaterialRequest materialRequest) throws RequestService.NoPidException, EasySMTAException {
		return mapper.map(service.recheckPid(mapper.map(materialRequest)));
	}

	@Override
	@Transactional
	public MaterialRequest validateRequest(MaterialRequest materialRequest) throws RequestService.NoPidException, EasySMTAException {
		return mapper.map(service.validateRequest(mapper.map(materialRequest)));
	}

	@Override
	@Transactional
	public long changeInstitute(FaoInstitute currentInstitute, FaoInstitute newInstitute) {
		return service.changeInstitute(currentInstitute, newInstitute);
	}

	@Override
	@Transactional
	public MaterialSubRequest setProviderInfo(UUID subRequestUuid, RequestService.ProviderInfoRequest info) {
		var materialSubRequest = subRequestRepository.findByUuid(subRequestUuid.toString());
		if (materialSubRequest == null) {
			throw new NotFoundElement("MaterialSubRequest not found by uuid=" + subRequestUuid);
		}
		var institute = instituteService.getInstitute(materialSubRequest.getInstCode());
		if (institute == null) {
			throw new NotFoundElement("Institute not found by code=" + materialSubRequest.getInstCode());
		}
		return mapper.map(service.setProviderInfo(materialSubRequest, institute, info));
	}
}