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

import lombok.extern.slf4j.Slf4j;
import org.genesys.server.api.v2.facade.RequestApiService;
import org.genesys.server.api.v2.mapper.MapstructMapper;
import org.genesys.server.api.v2.model.impl.MaterialRequestDTO;
import org.genesys.server.api.v2.model.impl.MaterialSubRequestDTO;
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("requestV2APIService")
@Validated
@Transactional(readOnly = true)
@Slf4j
public class RequestApiServiceImpl implements RequestApiService {

	@Autowired
	private MapstructMapper mapper;
	
	@Autowired
	private RequestService service;

	@Autowired
	private MaterialSubRequestRepository subRequestRepository;

	@Autowired
	private InstituteService instituteService;

	@Override
	@Transactional
	public MaterialRequestDTO 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 MaterialRequestDTO 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 MaterialSubRequestDTO relayRequest(UUID uuid) {
		return mapper.map(service.relayRequest(service.getSubrequest(uuid)));
	}

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

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

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

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

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

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

	@Override
	public Page<MaterialSubRequestDTO> list(String instCode, MaterialSubRequestFilter filter, Pageable pageRequest) {
		log.info("Listing requests for {}", instCode);
		final FaoInstitute institute = instituteService.getInstitute(instCode);
		return mapper.map(service.list(institute, filter, pageRequest), mapper::map);
	}

	@Override
	public MaterialRequestDTO get(UUID uuid) {
		return mapper.map(service.get(uuid));
	}

	@Override
	public MaterialSubRequestDTO get(String instCode, UUID uuid) {
		log.info("Loading request for {} uuid={}", instCode, uuid);
		final FaoInstitute institute = instituteService.getInstitute(instCode);
		return mapper.map(service.get(institute, uuid));
	}

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

	@Override
	@Transactional
	public MaterialRequestDTO sendValidationEmail(UUID uuid) {
		return mapper.map(service.sendValidationEmail(service.get(uuid)));
	}

	@Override
	@Transactional
	public MaterialRequestDTO recheckPid(UUID uuid) throws RequestService.NoPidException, EasySMTAException {
		return mapper.map(service.recheckPid(service.get(uuid)));
	}

	@Override
	@Transactional
	public MaterialRequestDTO validateRequest(UUID uuid) throws RequestService.NoPidException, EasySMTAException {
		return mapper.map(service.validateRequest(service.get(uuid)));
	}

	@Override
	@Transactional
	public MaterialSubRequestDTO 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));
	}

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

	@Override
	@Transactional
	public MaterialRequestDTO approveClientRequest(String tokenUuid, String key, boolean approve) throws TokenVerificationService.NoSuchVerificationTokenException, RequestService.NoPidException, TokenVerificationService.TokenExpiredException {
		return mapper.map(service.approveClientRequest(tokenUuid, key, approve));
	}
}