AccessionApiService.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.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;

import org.genesys.server.api.Slice;
import org.genesys.server.api.v2.model.dataset.DatasetInfo;
import org.genesys.server.api.v2.model.impl.AccessionDTO;
import org.genesys.server.api.v2.model.impl.FaoInstituteDTO;
import org.genesys.server.api.v2.model.impl.GenotypeInfo;
import org.genesys.server.api.v2.model.impl.ImageGalleryDTO;
import org.genesys.server.api.v2.model.impl.PDCIInfo;
import org.genesys.server.api.v2.model.impl.RepositoryFileInfo;
import org.genesys.server.api.v2.model.impl.SubsetInfo;
import org.genesys.server.api.v2.model.impl.Taxonomy2Info;
import org.genesys.server.api.v2.model.impl.TileClimateInfo;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.impl.AccessionIdentifier3;
import org.genesys.server.service.AccessionService;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.filter.AccessionFilter;
import org.genesys.server.service.worker.dupe.DuplicateFinder;
import org.genesys.server.service.worker.dupe.DuplicateFinder.SimilarityHit;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface AccessionApiService {

	/**
	 * Count accessions. Uses Elasticsearch, but counts from database when number is
	 * small enough.
	 *
	 * @param filter the filter
	 * @return the count
	 */
	long countAccessions(AccessionFilter filter) throws SearchException;

	/**
	 * Get accession#uuid from id.
	 *
	 * @param id the id
	 * @return the uuid
	 */
	UUID uuidFromId(long id);

	/**
	 * Get accession#uuids from ids
	 *
	 * @param ids the ids
	 * @return the list
	 */
	List<UUID> uuidsFromIds(List<Long> ids);

	/**
	 * Get accession#uuid from accession number and instcode.
	 *
	 * @param acceNumber the accession number
	 * @param instCode code of holder institute (can be null)
	 * @return the uuid
	 */
	UUID uuidFromAcceNumber(String instCode, String acceNumber);

	/**
	 * Get accession#uuids from ids
	 *
	 * @param acceNumbers accession numbers
	 * @param instCode code of holder institute (can be null)
	 * @return the list of uuids
	 */
	List<UUID> uuidsFromAcceNumbers(String instCode, List<String> acceNumbers);

	/**
	 * List accessions by filter
	 *
	 * @param filter the filter
	 * @param page the page
	 * @return the page
	 */
	Page<AccessionDTO> list(AccessionFilter filter, Pageable page) throws SearchException;

	/**
	 * List accessions with images by filter
	 *
	 * @param filter the filter
	 * @param page the page
	 * @return the page
	 */
	Page<AccessionDetails> withImages(AccessionFilter filter, Pageable page) throws SearchException;

	/**
	 * List accessions with images by filter
	 *
	 * @param filter the filter
	 * @param page the page
	 * @return the page
	 */
	Slice<AccessionDetails> withImagesSlice(AccessionFilter filter, Pageable page) throws SearchException;

	/**
	 * Count accession images by filter
	 *
	 * @param filter the filter
	 * @return the number of images
	 * @throws Exception
	 * @throws SearchException
	 */
	int countAccessionsImages(AccessionFilter filter) throws SearchException, Exception;

	/**
	 * Filtering autocompleter with suggestions
	 *
	 * @param filter currently applied accession filters
	 * @param field autocompleting field
	 * @param text typed text
	 * @return list of suggestions
	 */
	List<AccessionService.LabelValue<String>> autocomplete(AccessionFilter filter, String field, String text) throws IOException;

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

	/**
	 * Gets the geo bounds of maximum and minimum accession localities
	 *
	 * @param filter the filter
	 * @return the bounds [ [ a,b ], [ c, d] ]
	 */
	Number[][] getGeoBounds(AccessionFilter filter);

	/**
	 * Gets accession by uuid
	 *
	 * @param uuid the uuid
	 * @return the accession
	 */
	AccessionDTO getByUuid(UUID uuid);

	/**
	 * Gets accession by doi.
	 *
	 * @param doi the doi
	 * @return the by doi
	 */
	AccessionDTO getByDoi(String doi);

	/**
	 * Gets accession details
	 *
	 * @param accession the accession
	 * @return the accession details
	 */
	AccessionDetails getAccessionDetails(AccessionDTO accession);

	/**
	 * Gets accession audit log, requires authentication
	 *
	 * @param accession the accession
	 * @return the accession audit log
	 */
	AccessionService.AccessionAuditLog getAuditLog(AccessionDTO accession);

	/**
	 * Converts AccessionIdentifiers to UUID
	 *
	 * @param identifiers accession identifiers to lookup in DB
	 * @return map with UUIDs and related AccessionIdentifiers
	 */
	Map<UUID, AccessionIdentifier3> toUUID(List<? extends AccessionIdentifier3> identifiers);

	List<AccessionDTO> forUuids(Set<UUID> uuids);

	AccessionDetails getAccessionDetailsByDoi(String doi);

	AccessionDetails getAccessionDetailsByUuid(UUID uuid);

	List<DuplicateFinder.Hit<AccessionDTO>> getSimilarAccessionsForUUID(UUID uuid);

	List<DuplicateFinder.Hit<AccessionDTO>> getSimilarAccessionsForUUID(UUID uuid, AccessionFilter filter);

  List<SimilarityHit<AccessionDTO>> findSimilar(SimilaritySearchParams params) throws Exception;

	@Accessors(fluent = true, chain = true)
	@Getter
	@Setter
	public static class SimilaritySearchParams {
		public AccessionFilter select; // Which accessions to process
		public AccessionFilter target; // What target filter to apply
	}

	AccessionService.AccessionAuditLog getAccessionAuditLogByDoi(String doi);

	AccessionService.AccessionAuditLog getAccessionAuditLogByUUID(UUID uuid);

	@Data
	class AccessionDetails {
		public AccessionDTO details;
		public PDCIInfo pdci;
		public List<DatasetInfo> datasets;
		public List<SubsetInfo> subsets;
		public ImageGalleryDTO imageGallery;
		public List<RepositoryFileInfo> files;
		public List<GenotypeInfo> genotypes;
	}

	@Data
	class AccessionGeo {
		public Double longitude;
		public Double latitude;
		public Double elevation;
		public Double uncertainty;
		public String datum;
		public String method;
		public Long tileIndex;
		public TileClimateInfo climate;
		public boolean referenced;
	}

	/**
	 * List taxonomic records for filtered accession.
	 *
	 * @param filter the filter
	 * @return the list
	 */
	List<Taxonomy2Info> listSpecies(AccessionFilter filter);

	/**
	 * Update existing accessions from old FAO WIEWS Institute to the new Institute.
	 * This updates accession.instCode and accession.institute in the database.
	 *
	 * @param currentInstitute current {@link FaoInstituteDTO}
	 * @param newInstitute new institute
	 * @return number of updated {@link DatasetAccessionRefe} records
	 */
	long changeInstitute(FaoInstituteDTO currentInstitute, FaoInstituteDTO newInstitute);

	/**
	 * Get all distinct tileIndex3deg for filtered accessions 
	 *
	 * @param filter
	 * @return distinct list of tileIndex3deg
	 * @throws Exception
	 * @throws SearchException
	 */
	Set<Integer> listTileIndex3min(AccessionFilter filter) throws SearchException, Exception;

	/**
	 * Returns a map of {@link #listTileIndex3min(AccessionFilter)} grouped by crop.shortName.
	 * When the filter for crops is empty (filter.crop) then the map key is "NOCROP".
	 *
	 * @param filter
	 * @return distinct list of tileIndex3deg by crop.shortName
	 * @throws SearchException
	 * @throws Exception
	 */
	Map<String, Set<Integer>> listTileIndex3minByCrop(AccessionFilter filter) throws SearchException, Exception;

	/**
	 * Query accession data for selected fields
	 *
	 * @param filter The Filter
	 * @param select List of fields to query
	 * @param page Your page request
	 * @param useMcpdStyle Merge arrays with ";"
	 * @return Requested paged data
	 * @throws Exception
	 */
	Page<Map<String, ?>> query(AccessionFilter filter, List<String> select, Pageable page, boolean useMcpdStyle) throws Exception;

	/**
	 * Query accession data for selected fields and do with individual records what you want in your {@code consumer}
	 *
	 * @param filter The Filter
	 * @param select List of fields to query
	 * @param page Your page request
	 * @param useMcpdStyle Merge arrays with ";"
	 * @param consumer Do what you want!
	 * @throws Exception
	 */
	void query(AccessionFilter filter, List<String> select, Pageable page, boolean useMcpdStyle, Consumer<Map<String, ?>> consumer) throws Exception;

}