ExternalRequestApiService.java

/*
 * Copyright 2025 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;

import lombok.Getter;
import lombok.Setter;
import org.genesys.server.api.v2.model.impl.ExternalRequestDTO;
import org.genesys.server.api.v2.model.impl.ExternalRequestGenesysDTO;
import org.genesys.server.api.v2.model.impl.ExternalRequestItemDTO;
import org.genesys.server.exception.NotFoundElement;

import java.util.List;
import java.util.UUID;

import javax.validation.Valid;

public interface ExternalRequestApiService {

	/**
	 * Register an external request in the system and return it's UUID.
	 *
	 * @param request the external request
	 * @param remoteAddress the IP that made the request
	 * @return UUID of the registered request
	 * @throws ExternalRequestAccessionsException if none of the accessions are found
	 */
	UUID registerExternalRequest(@Valid ExternalRequestDTO request, String remoteAddress) throws ExternalRequestAccessionsException;

	/**
	 * Is an external request still available for processing?
	 *
	 * @return <code>true</code> when available, throws exception otherwise.
	 * @param key UUID of the registered request
	 * @throws NotFoundElement when the request has already been handled, has expired, or doesn't exist at all
	 */
	boolean externalRequestIsAvailable(UUID key) throws NotFoundElement;

	/**
	 * Fetch external request data by key
	 *
	 * @param key UUID if the external request
	 * @return external request details
	 * @throws NotFoundElement when the request has already been handled, has expired, or doesn't exist at all
	 */
	ExternalRequestGenesysDTO getRequest(UUID key) throws NotFoundElement;


	@Getter
	@Setter
	class ExternalRequestAccessionsException extends Exception {
		private final List<ExternalRequestItemDTO> missingAccessions;

		public ExternalRequestAccessionsException(List<ExternalRequestItemDTO> missingAccessions) {
			this.missingAccessions = missingAccessions;
		}
	}
}