AccessionService.java

/*
 * Copyright 2019 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.service;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.regex.Pattern;

import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.filters.SuperModelFilter;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.server.api.FilteredPage;
import org.genesys.server.api.FilteredSlice;
import org.genesys.server.api.Slice;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.dataset.Dataset;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.genesys.AccessionId;
import org.genesys.server.model.genesys.PDCI;
import org.genesys.server.model.genesys.Taxonomy2;
import org.genesys.server.model.impl.AccessionIdentifier3;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.server.model.impl.Subset;
import org.genesys.server.model.impl.TileClimate;
import org.genesys.server.service.filter.AccessionFilter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonView;

public interface AccessionService {

	/** The Constant ACCESSION_IMAGE_PATTERN. */
	public static final Pattern INSTITUTE_FOLDER_PATTERN = Pattern.compile(InstituteFilesService.REPOSITORY_INSTITUTE_PREFIX + "([A-Z]{3,4}\\d+)");
	public static final Pattern ACCESSION_IMAGE_PATTERN = Pattern.compile(InstituteFilesService.REPOSITORY_INSTITUTE_PREFIX + "([A-Z]{3,4}\\d+)"
			+ InstituteFilesService.REPOSITORY_INSTITUTE_ACCESSIONFILES + "/(.+)");

	public static final Number[][] DEFAULT_GEOBOUNDS = new Number[][] { { -180, 70 }, { 180, -60 } };
	
	/**
	 * 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<Accession> list(AccessionFilter filter, Pageable page) throws SearchException;

	/**
	 * List accessions by filter
	 *
	 * @param filter the filter
	 * @param page the page
	 * @return the page
	 */
  Page<Accession> listFast(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<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
	 */
	Accession getByUuid(UUID uuid);

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

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

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

	/**
	 * Get only available for distribution  accessions id-s.
	 *
	 * @param accessionUuids uuids of accessions
	 * @return id-s of available accessions
	 */
	Set<Long> filterAvailableForDistributionByUuid(Set<UUID> accessionUuids);

	/**
	 * Get only available accessions id-s.
	 *
	 * @param accessionIds ids of accessions
	 * @return id-s of available accessions
	 */
	Set<Long> filterAvailableById(Set<Long> accessionIds);

	/**
	 * 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<Accession> forUuids(Set<UUID> uuids);
	
	/**
	 * Find matching accession.
	 *
	 * @param repositoryFolder the repository folder
	 * @return the accession
	 */
	Accession findMatchingAccession(RepositoryFolder repositoryFolder);

	/**
	 * Scan for published datasets and increase the counts. Admin - only
	 */
	void scanForPublishedDatasets();

	/**
	 * Scan for published subsets and increase the counts. Admin - only
	 */
	void scanForPublishedSubsets();

	/**
	 * Reset subset/dataset counters to 0. Admin - only
	 */
	void resetSubsetAndDatasetCounters();

	/**
	 * Executes the action on a list of accession in a
	 * Spring transaction. Spring security context not supported.
	 *
	 * @param accessionIds List of accession IDs
	 * @param action the action to execute on each accession
	 * @return the list of processed accessions
	 */
	List<Accession> processAccessions(List<Long> accessionIds, IAccessionBatchAction action);
	
	public static interface IBatchAction<T> {
		/**
		 * Run action on batch of Accessions
		 *
		 * @param a the accession
		 * @return must return the resulting {@link Accession}
		 */
		void apply(List<T> a) throws Exception;
	}

	public static interface IAccessionBatchAction extends IBatchAction<Accession> {

	}

	static class AccessionGeo {
		@JsonBackReference
		public AccessionId accession;
		public Double longitude;
		public Double latitude;
		public Double elevation;
		public Double uncertainty;
		public String datum;
		public String method;
		public Long tileIndex;
		@JsonView({JsonViews.Protected.class})
		@JsonInclude(JsonInclude.Include.NON_EMPTY)
		@JsonIgnoreProperties({"tileIndex"})
		public TileClimate climate;
		public boolean referenced;
	}

	static class AccessionDetails {
		public Accession details;
		public PDCI pdci;
		public List<Dataset> datasets;
		public List<Subset> subsets;
		public ImageGallery imageGallery;
		public List<RepositoryFile> files;
	}

	static class AccessionAuditLog {
		public Map<String, List<AuditLog>> auditAccession;
		public Map<String, List<AuditLog>> auditAccessionId;
		public Map<String, List<AuditLog>> auditAccessionCollect;
		public Map<String, List<AuditLog>> auditAccessionGeo;
	}

	public static class AccessionMapInfo {
		public String filterCode;
		public AccessionFilter filter;

		public Number[][] bounds;
		public long accessionCount;
		public String[] tileServers;
		public Map<String, ElasticsearchService.TermResult> suggestions;
	}

	public static class AccessionOverview {
		public String filterCode;
		public AccessionFilter filter;
		public long accessionCount;
		public Map<String, ElasticsearchService.TermResult> overview;
		public Map<String, ElasticsearchService.TermResult> suggestions;

		public static AccessionOverview from(String filterCode, AccessionFilter filter, Map<String, ElasticsearchService.TermResult> overview, long accessionCount, Map<String, ElasticsearchService.TermResult> suggestions) {
			AccessionOverview res = new AccessionOverview();
			res.filterCode = filterCode;
			res.filter = filter;
			res.overview = overview;
			res.accessionCount = accessionCount;
			res.suggestions = suggestions;

			return res;
		}
	}

	public static class AccessionSuggestionPage<T, F extends SuperModelFilter<F, ?>> {
		@JsonUnwrapped
		public FilteredPage<T, F> page;
		public Map<String, ElasticsearchService.TermResult> suggestions;

		public AccessionSuggestionPage(FilteredPage<T, F> page, Map<String, ElasticsearchService.TermResult> suggestions) {
			this.page = page;
			this.suggestions = suggestions;
		}
	}

	public static class AccessionSuggestionSlice<T> {
		@JsonUnwrapped
		public FilteredSlice<T> slice;
		public Map<String, ElasticsearchService.TermResult> suggestions;

		public AccessionSuggestionSlice(FilteredSlice<T> slice, Map<String, ElasticsearchService.TermResult> suggestions) {
			this.slice = slice;
			this.suggestions = suggestions;
		}
	}

	public static class LabelValue<T> {
		private final T value;
		private final String label;

		public LabelValue(T value, String label) {
			this.value = value;
			this.label = label;
		}

		public T getValue() {
			return value;
		}

		public String getLabel() {
			return label;
		}
	}

	/**
	 * List taxonomic records for filtered accession.
	 *
	 * @param filter the filter
	 * @return the list
	 */
	List<Taxonomy2> 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 FaoInstitute}
	 * @param newInstitute new institute
	 * @return number of updated {@link DatasetAccessionRefe} records
	 */
	long changeInstitute(FaoInstitute currentInstitute, FaoInstitute 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;

	/**
	 * Utility method to find only {@code accession.uuids} for the specified filter
	 * 
	 * @param filters
	 * @return Set of accessions for {@code filters}
	 */
	List<UUID> getAccessionUuids(AccessionFilter filters);

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

}