DescriptorApiService.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;

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.v2.facade.impl.DescriptorApiServiceImpl;
import org.genesys.server.api.v2.model.dataset.DatasetInfo;
import org.genesys.server.api.v2.model.impl.DescriptorDTO;
import org.genesys.server.api.v2.model.impl.DescriptorLangDTO;
import org.genesys.server.api.v2.model.impl.DescriptorListInfo;
import org.genesys.server.api.v2.model.impl.TranslatedDescriptorDTO;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.filters.DescriptorFilter;
import org.genesys.server.model.traits.Descriptor;
import org.genesys.server.model.traits.DescriptorLang;
import org.genesys.server.service.TranslatorService;
import org.genesys.server.service.worker.dupe.DuplicateFinder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

public interface DescriptorApiService extends APIFilteredTranslatedServiceFacade
	<DescriptorDTO, TranslatedDescriptorDTO, DescriptorLangDTO, Descriptor, DescriptorLang, DescriptorFilter> {

	/**
	 * Search matching descriptor.
	 *
	 * @param descriptor the descriptor
	 * @return matching descriptor
	 */
	List<DescriptorDTO> searchMatchingDescriptor(DescriptorDTO descriptor);

	/**
	 * Updates the image of descriptor.
	 *
	 * @param descriptor the descriptor
	 * @param file image file that be added to Db and descriptor
	 * @param imageMetadata the updated image metadata
	 * @return updated descriptor
	 * @throws IOException IOException
	 * @throws InvalidRepositoryPathException InvalidRepositoryPathException
	 * @throws InvalidRepositoryFileDataException InvalidRepositoryFileDataException
	 * @throws NoSuchRepositoryFileException the no such repository file exception
	 */
	DescriptorDTO updateImage(DescriptorDTO descriptor, MultipartFile file, RepositoryImage imageMetadata) throws IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException, NoSuchRepositoryFileException;

	/**
	 * Removes the image of descriptor.
	 *
	 * @param descriptor the descriptor
	 * @return updated descriptor
	 * @throws NoSuchRepositoryFileException NoSuchRepositoryFileException
	 * @throws IOException IOException
	 * @throws InvalidRepositoryPathException
	 */
	DescriptorDTO removeImage(DescriptorDTO descriptor) throws NoSuchRepositoryFileException, IOException, InvalidRepositoryPathException;

	/**
	 * Force update descriptor. This is an editorial action, only allowed for Administrators.
	 *
	 * @param descriptor updatable descriptor
	 * @return updated descriptor
	 */
	DescriptorDTO forceUpdateDescriptor(@Valid DescriptorDTO descriptor);

	/**
	 * Get descriptor by UUID.
	 *
	 * @param uuid uuid of descriptor
	 * @return loaded descriptor
	 * @throws NotFoundElement if descriptor is not found
	 */
	DescriptorDTO loadDescriptor(UUID uuid) throws NotFoundElement;

	/**
	 * Get translated descriptor by UUID.
	 *
	 * @param uuid uuid of descriptor
	 * @return loaded descriptor
	 * @throws NotFoundElement if descriptor is not found
	 */
	TranslatedDescriptorDTO loadTranslatedDescriptor(UUID uuid) throws NotFoundElement;

	/**
	 * Gets the descriptor by uuid and version.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return the descriptor
	 * @throws NotFoundElement if descriptor is not found
	 */
	DescriptorDTO loadDescriptor(UUID uuid, int version) throws NotFoundElement;

	/**
	 * Page with descriptor list by user.
	 *
	 * @param filter the filter
	 * @param page parameters
	 * @return Page with descriptor list
	 */
	Page<DescriptorDTO> listDescriptorsForCurrentUser(DescriptorFilter filter, Pageable page) throws IOException, SearchException;


	/**
	 * List descriptors accessible to the user (both published by others and owned descriptors).
	 *
	 * @param filter the filter
	 * @param page the page
	 * @return the page
	 */
	Page<DescriptorDTO> listAccessibleDescriptors(DescriptorFilter filter, Pageable page) throws IOException, SearchException;

	/**
	 * List published descriptors.
	 *
	 * @param descriptorFilter the descriptor filter
	 * @param page parameters
	 * @return Page with descriptor list
	 */
	Page<DescriptorDTO> listDescriptors(DescriptorFilter descriptorFilter, Pageable page) throws SearchException;

	/**
	 * List published translated descriptors with details.
	 *
	 * @param filter the descriptor filter
	 * @param page parameters
	 * @return Page with descriptors
	 */
	Page<TranslatedDescriptorDTO> listDescriptorsDetails(DescriptorFilter filter, Pageable page) throws SearchException;

	/**
	 * Method for delete Descriptor.
	 *
	 * @param descriptor descriptor
	 * @return removed Descriptor
	 * @throws IOException
	 * @throws InvalidRepositoryPathException
	 */
	DescriptorDTO removeDescriptor(DescriptorDTO descriptor) throws InvalidRepositoryPathException, IOException;

	/**
	 * Insert or update all Descriptor records.
	 *
	 * @param source the source
	 * @return list of updated descriptors
	 */
	List<DescriptorDTO> upsertDescriptors(@Valid List<DescriptorDTO> source);

	/**
	 * Insert or update Descriptor record.
	 *
	 * @param source the source
	 * @return updated descriptor
	 */
	DescriptorDTO upsertDescriptor(@Valid DescriptorDTO source);

	/**
	 * Validates and publishes an unpublished descriptor. Admin - only.
	 *
	 * @param descriptor the descriptor
	 * @return published descriptor
	 */
	DescriptorDTO approveDescriptor(DescriptorDTO descriptor);

	/**
	 * Puts the descriptor into the Review state to be reviewed by admin.
	 * Admin and the user with MANAGE permission - only.
	 *
	 * @param descriptor the descriptor
	 * @return descriptor in REVIEWING state
	 */
	DescriptorDTO reviewDescriptor(DescriptorDTO descriptor);

	/**
	 * Unpublishes published descriptor.
	 * Admin and the user with MANAGE permission - only.
	 *
	 * @param descriptor the descriptor
	 * @return unpublished descriptor
	 */
	DescriptorDTO rejectDescriptor(DescriptorDTO descriptor);

	/**
	 * Gets the descriptor lists using specified descriptor.
	 *
	 * @param uuid the descriptor uuid
	 * @return the descriptor lists
	 */
	List<DescriptorListInfo> getDescriptorLists(UUID uuid);

	/**
	 * Gets the datasets where descriptor is used.
	 *
	 * @param uuid the descriptor uuid
	 * @return the datasets
	 */
	List<DatasetInfo> getDatasets(UUID uuid);

	/**
	 * Create next version of the descriptor.
	 *
	 * @param descriptor the source descriptor
	 * @param major if a new major version should be created
	 * @return a new descriptor entity with updated versions
	 */
	DescriptorDTO nextVersion(DescriptorDTO descriptor, boolean major);

	void exportDescriptors(DescriptorFilter filter, OutputStream outputStream) throws IOException;

	/**
	 * Count published descriptors.
	 *
	 * @param filter the filter
	 * @return the number of published descriptors
	 */
	long countDescriptors(DescriptorFilter filter) throws SearchException;

	/**
	 * Get descriptor for UUID. No lazy-loading.
	 *
	 * @param uuid
	 * @return
	 */
	DescriptorDTO getDescriptor(UUID uuid);

	DescriptorLangDTO machineTranslate(UUID uuid, String targetLang) throws TranslatorService.TranslatorException;

	List<DuplicateFinder.Hit<DescriptorDTO>> findSimilar(DescriptorApiServiceImpl.SimilarRequest similarRequest);

	class SimilarRequest {
		public DescriptorDTO select;
		public DescriptorFilter target;
	}

	DescriptorDetails getDescriptorDetails(UUID uuid);
	
	class DescriptorDetails {
		public TranslatedDescriptorDTO descriptor;
		public List<DescriptorListInfo> descriptorLists;
		public List<DatasetInfo> datasets;
	}
}