DatasetApiService.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 java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Supplier;

import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.server.api.FilteredPage;
import org.genesys.server.api.v2.model.dataset.DatasetAccessionRefDTO;
import org.genesys.server.api.v2.model.dataset.DatasetCreatorDTO;
import org.genesys.server.api.v2.model.dataset.DatasetDTO;
import org.genesys.server.api.v2.model.dataset.DatasetInfo;
import org.genesys.server.api.v2.model.dataset.DatasetLangDTO;
import org.genesys.server.api.v2.model.dataset.DatasetLocationDTO;
import org.genesys.server.api.v2.model.dataset.TranslatedDatasetDTO;
import org.genesys.server.api.v2.model.dataset.TranslatedDatasetInfo;
import org.genesys.server.api.v2.model.impl.DescriptorDTO;
import org.genesys.server.api.v2.model.impl.RepositoryFileDTO;
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.dataset.Dataset;
import org.genesys.server.model.dataset.DatasetAccessionRef;
import org.genesys.server.model.dataset.DatasetLang;
import org.genesys.server.model.filters.DatasetFilter;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.server.service.AmphibianService.TraitFilters;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.filter.AccessionFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

public interface DatasetApiService extends APIFilteredTranslatedServiceFacade
	<DatasetDTO, TranslatedDatasetDTO, DatasetLangDTO, Dataset, DatasetLang, DatasetFilter> {

	Page<TranslatedDatasetInfo> listInfo(DatasetFilter filter, Pageable page) throws SearchException;

	List<TranslatedDescriptorDTO> loadDescriptors(UUID uuid);

	DatasetDTO setAccessionRefs(UUID uuid, int version, @Valid Collection<DatasetAccessionRef> accessionRefs);

	DatasetDTO uploadAccessions(UUID uuid, int version, char separator, char quotechar, MultipartFile file) throws IOException;

	DatasetDTO addDescriptors(UUID uuid, int version, final Set<UUID> descriptorUuids);

	DatasetDTO removeDescriptors(UUID uuid, int version, Set<UUID> descriptorUuids);

	DatasetDTO updateDescriptors(UUID uuid, int version, final List<UUID> descriptorUuids);

	DatasetDTO loadDataset(DatasetDTO input);

	DatasetDTO loadDataset(UUID uuid, int version);

	Page<DatasetAccessionRefDTO> listAccessions(UUID uuid, Pageable page);

	List<DatasetInfo> listByAccession(Accession accession);

	Map<String, ElasticsearchService.TermResult> getSuggestions(DatasetFilter filter) throws SearchException, IOException;

	Page<DatasetInfo> listDatasetsForCurrentUser(DatasetFilter filter, Pageable page) throws SearchException;

	DatasetDTO loadDataset(UUID uuid) throws NotFoundElement;

	TranslatedDatasetDTO loadTranslatedDataset(UUID uuid) throws NotFoundElement;

	DatasetDTO addDatasetFile(DatasetDTO dataset, MultipartFile file) throws NotFoundElement, IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException;

	DatasetDTO removeDatasetFile(DatasetDTO dataset, UUID fileUuid) throws NotFoundElement, NoSuchRepositoryFileException, IOException;

	List<RepositoryFileDTO> listDatasetFiles(DatasetDTO dataset) throws NotFoundElement;

	DatasetDTO addAccessionRefs(UUID uuid, int version, Set<DatasetAccessionRef> accessionRefs) throws NotFoundElement;

	DatasetDTO reviewDataset(DatasetDTO dataset);

	DatasetDTO rejectDataset(DatasetDTO dataset);

	DatasetDTO approveDataset(DatasetDTO dataset);

	DatasetDTO updateDatasetFile(DatasetDTO dataset, @Valid RepositoryFileDTO metadata) throws NoSuchRepositoryFileException;

	long countDatasets(DatasetFilter filter) throws SearchException;

	void rematchDatasetAccessions();

	DatasetDTO rematchDatasetAccessions(UUID uuid, int version);

	DatasetCreatorDTO createDatasetCreator(UUID datasetUuid, @Valid DatasetCreatorDTO input) throws NotFoundElement;

	DatasetCreatorDTO removeDatasetCreator(UUID datasetUuid, DatasetCreatorDTO input) throws NotFoundElement;

	Page<DatasetCreatorDTO> listDatasetCreators(UUID datasetUuid, Pageable page);

	Page<TranslatedDatasetDTO> list(DatasetFilter filter, Pageable page) throws SearchException;

	DatasetCreatorDTO loadDatasetCreator(UUID datasetCreatorUuid) throws NotFoundElement;

	DatasetCreatorDTO updateDatasetCreator(UUID datasetUuid, @Valid DatasetCreatorDTO datasetCreator) throws NotFoundElement;

	List<DatasetCreatorDTO> autocompleteCreators(String text);

	DatasetLocationDTO createLocation(UUID datasetUuid, @Valid DatasetLocationDTO input) throws NotFoundElement;

	DatasetLocationDTO removeLocation(UUID datasetUuid, DatasetLocationDTO input) throws NotFoundElement;

	Page<DatasetLocationDTO> listLocation(UUID datasetUuid, Pageable page);

	DatasetLocationDTO loadLocation(UUID locationUuid) throws NotFoundElement;

	DatasetLocationDTO updateLocation(UUID datasetUuid, @Valid DatasetLocationDTO input) throws NotFoundElement;

	int clearAccessionRefs(Collection<Accession> accessions);

	DatasetDTO getDataset(UUID uuid);

	DatasetDTO getDataset(UUID uuid, Integer version);

	void writeXlsxMCPD(DatasetDTO dataset, OutputStream outputStream) throws IOException;

	DatasetDTO createNewVersion(@Valid DatasetDTO source);

	class DatasetSuggestionPage extends FilteredPage<TranslatedDatasetInfo, DatasetFilter> {
		public Map<String, ElasticsearchService.TermResult> suggestions;

		public DatasetSuggestionPage(String filterCode, DatasetFilter filter, Page<TranslatedDatasetInfo> data) {
			super(filterCode, filter, data);
		}

		public static DatasetSuggestionPage from(FilteredPage<TranslatedDatasetInfo, DatasetFilter> page, Map<String, ElasticsearchService.TermResult> suggestions) {
			DatasetSuggestionPage res = new DatasetSuggestionPage(page.filterCode, page.filter, page.page);
			res.suggestions = suggestions;

			return res;
		}
	}

	long changeInstitute(FaoInstitute currentInstitute, FaoInstitute newInstitute);

	List<DescriptorDTO> synchronizeDescriptors(UUID uuid);

	void batchRematchAccessionRefs(List<DatasetAccessionRefDTO> accessionRefs);

	Set<UUID> findAccessionsAmphibianDatasets(AccessionFilter accessionFilter);

	void downloadMetadata(UUID datasetUuid, HttpServletResponse response) throws IOException, NoSuchRepositoryFileException;

	void downloadDatasetFile(UUID datasetUuid, long fileId, HttpServletResponse response) throws IOException, NoSuchRepositoryFileException;

	DatasetDTO forceUpdate(@Valid @NotNull DatasetDTO dataset);

	/**
	 * Make core collection based on selected traits
	 * @throws MalformedURLException 
	 */
	void coreCollection(Supplier<OutputStream> outputStreamSupplier, @NotNull UUID datasetUuid, @NotEmpty List<UUID> fields, TraitFilters filters, @NotEmpty Map<?, ?> options) throws MalformedURLException;
	
	/**
	 * Generte dataset clusters based on selected traits
	 * @throws MalformedURLException 
	 */
  void clusterDataset(Supplier<OutputStream> outputStreamSupplier, @NotNull UUID datasetUuid, @NotEmpty List<UUID> fields, TraitFilters filters, @NotEmpty Map<?, ?> options) throws MalformedURLException;

	static final class ClusterRequest {
		public Map<?, ?> options;
		public List<UUID> fields;
		public TraitFilters filters;
	}
}