DescriptorListService.java

/*
 * Copyright 2018 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.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonUnwrapped;

import org.genesys.server.api.FilteredPage;
import org.genesys.server.model.filters.DescriptorListFilter;
import org.genesys.server.model.traits.Descriptor;
import org.genesys.server.model.traits.DescriptorList;
import org.genesys.server.model.traits.DescriptorListLang;
import org.genesys.server.service.ElasticsearchService.TermResult;
import org.genesys.server.exception.SearchException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
 * DescriptorList service interface.
 *
 * @author Andrey Lugovskoy
 */
public interface DescriptorListService extends FilteredTranslatedCRUDService<
	DescriptorList, DescriptorListLang, DescriptorListTranslationService.TranslatedDescriptorList, DescriptorListFilter> {

	DescriptorList get(UUID uuid);

	/**
	 * Count descriptor lists.
	 *
	 * @param filter the filter
	 * @return the long
	 */
	long countDescriptorLists(DescriptorListFilter filter) throws SearchException;

	/**
	 * Remove descriptor of descriptorList.
	 *
	 * @param descriptorList descriptorList
	 * @param descriptors the descriptors
	 * @return updated descriptorList
	 */
	DescriptorList removeDescriptors(DescriptorList descriptorList, Descriptor... descriptors);

	/**
	 * Add new descriptor to descriptorList.
	 *
	 * @param descriptorList descriptorList
	 * @param descriptors the descriptors
	 * @return updated descriptorList
	 */
	DescriptorList addDescriptors(DescriptorList descriptorList, Descriptor... descriptors);

	/**
	 * Find descriptorList by UUID.
	 *
	 * @param uuid UUID of descriptorList
	 * @return found descriptorList
	 */
	DescriptorList loadDescriptorList(UUID uuid);

	/**
	 * Find translated descriptorList by UUID.
	 *
	 * @param uuid UUID of descriptorList
	 * @return found descriptorList
	 */
	DescriptorListTranslationService.TranslatedDescriptorList loadTranslatedDescriptorList(UUID uuid);

	/**
	 * Find descriptors of a descriptor List by uuid
	 *
	 * @param descriptorList UUID of descriptorList
	 * @return list of descriptors
	 */
	List<Descriptor> loadDescriptors(DescriptorList descriptorList);

	/**
	 * Find descriptorList by UUID and version.
	 *
	 * @param uuid UUID of descriptorList
	 * @param version record version
	 * @return found descriptorList
	 */
	DescriptorList loadDescriptorList(UUID uuid, int version);

	/**
	 * Reload descriptor list from database by identifiers and version.
	 *
	 * @param input query by example
	 * @return loaded descriptor list
	 */
	DescriptorList loadDescriptorList(DescriptorList input);

	/**
	 * Get terms by filter.
	 * @param filter the descriptor list filter
	 *
	 * @return the term result
	 * @throws SearchException the search exception
	 * @throws IOException the IO exception
	 */
	Map<String, ElasticsearchService.TermResult> getSuggestions(DescriptorListFilter filter) throws SearchException, IOException;

	/**
	 * List Descriptor Lists for current user.
	 *
	 * @param descriptorListFilter the descriptor list filter
	 * @param page page
	 * @return page with DescriptorList
	 */
	Page<DescriptorList> listDescriptorListsForCurrentUser(DescriptorListFilter descriptorListFilter, Pageable page) throws SearchException;

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

	/**
	 * Unpublishes published descriptorList. Admin and the user with MANAGE
	 * permission - only.
	 *
	 * @param descriptorList the descriptorList
	 * @return unpublished descriptorList
	 */
	DescriptorList rejectDescriptorList(DescriptorList descriptorList);

	/**
	 * Validates and publishes an unpublished descriptorList. Admin - only
	 *
	 * @param descriptorList the descriptorList
	 * @return published descriptorList
	 */
	DescriptorList approveDescriptorList(DescriptorList descriptorList);

	/**
	 * Completely update the descriptor list (including order).
	 *
	 * @param descriptorList the descriptor list
	 * @param descriptors the descriptors
	 * @return the descriptor list
	 */
	DescriptorList setDescriptors(DescriptorList descriptorList, Descriptor[] descriptors);

	/**
	 * Autocomplete descriptor lists.
	 *
	 * @param text the text
	 * @return list of matched descriptor lists
	 */
	List<DescriptorList> autocompleteDescriptorLists(String text);

	/**
	 * Write descriptorList as Excel to output stream.
	 *
	 * @param descriptorList the descriptorList
	 * @param outputStream the output stream
	 * @throws IOException
	 */
	void exportDescriptorList(DescriptorList descriptorList, OutputStream outputStream) throws IOException;

	public static class DescriptorListOverview {
		public String filterCode;
		public DescriptorListFilter filter;
		public long descriptorListCount;
		public Map<String, TermResult> overview;
		public Map<String, TermResult> suggestions;

		public static DescriptorListOverview from(String filterCode, DescriptorListFilter filter, Map<String, TermResult> overview, long descriptorListCount, Map<String, TermResult> suggestions) {
			DescriptorListOverview res = new DescriptorListOverview();
			res.filterCode = filterCode;
			res.filter = filter;
			res.overview = overview;
			res.descriptorListCount = descriptorListCount;
			res.suggestions = suggestions;

			return res;
		}
	}

	public static class DescriptorListSuggestionPage {
		@JsonUnwrapped
		public FilteredPage<DescriptorList, DescriptorListFilter> page;
		public Map<String, TermResult> suggestions;

		public static DescriptorListSuggestionPage from(FilteredPage<DescriptorList, DescriptorListFilter> page, Map<String, TermResult> suggestions) {
			DescriptorListSuggestionPage res = new DescriptorListSuggestionPage();
			res.page = page;
			res.suggestions = suggestions;

			return res;
		}
	}

	/**
	 * Get all localized descriptors for list by UUID
	 * @param descriptorList Descriptor list
	 */
  List<DescriptorTranslationService.TranslatedDescriptor> loadTranslatedDescriptors(DescriptorList descriptorList);

}