SubsetController.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.impl;

import com.google.common.collect.Sets;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.FilteredPage;
import org.genesys.server.api.Pagination;
import org.genesys.server.api.v2.EntityUuidAndVersion;
import org.genesys.server.api.v2.FilteredCRUDController;
import org.genesys.server.api.v2.TranslatedCRUDController;
import org.genesys.server.api.v2.facade.SubsetApiService;
import org.genesys.server.api.v2.facade.SubsetLangApiService;
import org.genesys.server.api.v2.model.impl.SubsetAccessionRefDTO;
import org.genesys.server.api.v2.model.impl.SubsetDTO;
import org.genesys.server.api.v2.model.impl.SubsetLangDTO;
import org.genesys.server.api.v2.model.impl.TranslatedSubsetDTO;
import org.genesys.server.component.aspect.DownloadEndpoint;
import org.genesys.server.exception.InvalidApiUsageException;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.PublishState;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.impl.QSubset;
import org.genesys.server.model.impl.Subset;
import org.genesys.server.model.impl.SubsetLang;
import org.genesys.server.service.AccessionService;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.SubsetService;
import org.genesys.server.service.TranslatorService.TranslatorException;
import org.genesys.server.service.filter.AccessionFilter;
import org.genesys.server.service.filter.SubsetFilter;
import org.genesys.server.service.filter.SubsetLangFilter;
import org.genesys.spring.CSVMessageConverter;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

@RestController("subsetApi2")
@PreAuthorize("isAuthenticated()")
@RequestMapping(SubsetController.CONTROLLER_URL)
@Tag(name = "Subset")
public class SubsetController extends TranslatedCRUDController<SubsetDTO, TranslatedSubsetDTO,
	SubsetLangDTO, Subset, SubsetLang, SubsetApiService, SubsetFilter> {

	/** The Constant CONTROLLER_URL. */
	public static final String CONTROLLER_URL = ApiBaseController.APIv2_BASE + "/subset";
	
	private final Set<String> terms = Set.of("owner.uuid", "crops");

	/** The accession service. */
	@Autowired
	private AccessionService accessionService;

	@Autowired(required = false)
	private ElasticsearchService elasticsearchService;

	/**
	 * Gets the subset
	 *
	 * @param uuid the uuid
	 * @return the subset
	 */
	@GetMapping(value = "/{uuid:\\w{8}\\-\\w{4}.+}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "getByUuid", summary = "Get by UUID", description = "Load subset by uuid")
	public TranslatedSubsetDTO get(@PathVariable("uuid") @Parameter(description = "Subset UUID") final UUID uuid) {
		return translatedApiService.loadSubset(uuid);
	}

	/**
	 * Remove subset.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return the subset
	 */
	@DeleteMapping(value = "", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "deleteSubset", summary = "Remove subset", description = "Remove unpublished subset by uuid and version")
	public SubsetDTO remove(@RequestBody @Valid EntityUuidAndVersion entity) {
		return translatedApiService.remove(translatedApiService.getSubset(entity.getUuid(), entity.getVersion()));
	}

	/**
	 * List subsets by filterCode or filter with suggestions
	 *
	 * @param page the page
	 * @param filterCode short filter code
	 * @param filter the filter
	 * @return the page with suggestions
	 * @throws IOException
	 */
	@Override
	@PostMapping(value = "/filter", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "filter", summary = "List", description = "List subsets by filter")
	public SubsetApiService.SubsetsWithSuggestions filter(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject Pagination page,
	@RequestBody(required = false) SubsetFilter filter) throws IOException, SearchException {

		var filterInfo = processFilter(filterCode, filter, SubsetFilter.class);

		var data = translatedApiService.listFiltered(filterInfo.filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "id"));
		Map<String, ElasticsearchService.TermResult> suggestionRes = translatedApiService.getSuggestions(filterInfo.filter);

		return SubsetApiService.SubsetsWithSuggestions.from(filterInfo.filterCode, filterInfo.filter, data, suggestionRes);
	}

	/**
	 * My subsets.
	 *
	 * @param page the page
	 * @param filter the descriptor filter
	 * @return the page
	 * @throws IOException
	 */
	@PostMapping(value = "/list-mine")
	@Operation(operationId = "filterMine", summary = "List my subsets", description = "List only subsets the user has access by filter")
	public FilteredPage<SubsetDTO, SubsetFilter> mySubsets(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
		@RequestBody(required = false) SubsetFilter filter) throws IOException {

		var filterInfo = processFilter(filterCode, filter, SubsetFilter.class);
		return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, translatedApiService.listSubsetsForCurrentUser(filterInfo.filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.DESC, "lastModifiedDate")));
	}

	/**
	 * Get term overview for filters
	 *
	 * @param filterCode short filter code
	 * @param filter the filter
	 * @return the overview
	 * @throws SearchException
	 * @throws IOException
	 */
	@PostMapping(value = "/overview", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "overview", summary = "Overview", description = "Generate subset statistics by filter")
	public SubsetService.SubsetOverview overview(@RequestParam(name = "f", required = false) String filterCode, @RequestBody(required = false) SubsetFilter filter,
		@RequestParam(name = "limit", defaultValue = "10", required = false) final int limit) throws IOException, SearchException {

		var filterInfo = processFilter(filterCode, filter, SubsetFilter.class);
		filterInfo.filter.state(Set.of(PublishState.PUBLISHED));

		Map<String, ElasticsearchService.TermResult> overview = elasticsearchService.termStatisticsAuto(Subset.class, filterInfo.filter, Math.min(50, limit), terms.toArray(new String[] {}));
		long subsetCount = translatedApiService.countSubsets(filterInfo.filter);
		Map<String, ElasticsearchService.TermResult> suggestionRes = translatedApiService.getSuggestions(filterInfo.filter);

		return SubsetService.SubsetOverview.from(filterInfo.filterCode, filterInfo.filter, overview, subsetCount, suggestionRes);
	}

	/**
	 * Load more data for the specified term
	 *
	 * @param filterCode short filter code
	 * @param filter the filter
	 * @param term the term
	 * @return the term result
	 * @throws SearchException the search exception
	 * @throws IOException signals that an I/O exception has occurred
	 */
	@PostMapping(value = "/overview/{term}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "overviewProperty", summary = "Overview for property", description = "Generate subset statistics for selected term by filter")
	public ElasticsearchService.TermResult loadMoreTerms(@PathVariable(name = "term") final String term, @RequestBody(required = false) final SubsetFilter filter,
		@RequestParam(name = "f", required = false) final String filterCode, @RequestParam(name = "limit", defaultValue = "20", required = false) final int limit)
		throws IOException, SearchException, ExecutionException, InterruptedException {

		if (! terms.contains(term)) {
			throw new InvalidApiUsageException("No such term. Will not search.");
		}

		var filterInfo = processFilter(filterCode, filter, SubsetFilter.class);
		filterInfo.filter
			.state(Set.of(PublishState.PUBLISHED))
			.current(true);

		ElasticsearchService.TermResult termResult = elasticsearchService.termStatisticsAuto(Subset.class, filterInfo.filter, Math.min(200, limit), term);
		if (term.equals("crops")) {
			termResult = elasticsearchService.recountResult(Subset.class, QSubset.subset.crops, filterInfo.filter, termResult, "crops");
		}
		return termResult;
	}

	/**
	 * Load AccessionRef list by Subset
	 *
	 * @param uuid uuid of Subset
	 * @param page Pageable
	 * @return the page
	 * @throws NotFoundElement
	 */
	@GetMapping(value = "/{uuid}/accessions", produces = { MediaType.APPLICATION_JSON_VALUE, CSVMessageConverter.TEXT_CSV_VALUE })
	@Operation(operationId = "listAccessions", summary = "List accessions", description = "List accessions of a subset")
	public Page<SubsetAccessionRefDTO> listAccessions(@PathVariable("uuid") @Parameter(description = "Subset UUID") final UUID uuid, @ParameterObject final Pagination page) throws NotFoundElement {
		return translatedApiService.listAccessions(translatedApiService.getSubset(uuid), page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	/**
	 * Load full accessions list by Subset
	 *
	 * @param uuid uuid of Subset
	 * @param page Pageable
	 * @return the page
	 * @throws NotFoundElement
	 */
	@GetMapping(value = "/{uuid}/accessions", produces = { MediaType.APPLICATION_JSON_VALUE, CSVMessageConverter.TEXT_CSV_VALUE }, params = { "full" })
	@Operation(operationId = "listAccessionsFull", summary = "List full accessions", description = "List detailed accessions of a subset")
	public Page<Accession> listFullAccessions(@PathVariable("uuid") @Parameter(description = "Subset UUID") final UUID uuid, @ParameterObject final Pagination page) throws NotFoundElement, SearchException {
		AccessionFilter filter = new AccessionFilter()
			.subsets(Sets.newHashSet(uuid));
		return accessionService.list(filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	/**
	 * Add accessions to subset.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return the subset
	 */
	@Validated
	@PostMapping(value = "/{uuid},{version}/add-accessions", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "addAccessions", summary = "Add accessions", description = "Add accessions to a subset")
	public SubsetDTO addAccessions(@PathVariable("uuid") @Parameter(description = "Subset UUID") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<SubsetAccessionRefDTO> accessionRefs) {
		SubsetDTO subset = translatedApiService.getSubset(uuid);
		LOG.info("Want to add {} accessionRefs to subset {}", accessionRefs.size(), subset.getUuid());

		return translatedApiService.get(translatedApiService.addAccessionRefs(subset, accessionRefs));
	}

	/**
	 * Set accessions to subset.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return the subset
	 */
	@PostMapping(value = "/{uuid},{version}/set-accessions", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "setAccessions", summary = "Replace accessions", description = "Replace existing accessions in a subset")
	public SubsetDTO setAccessions(@PathVariable("uuid") @Parameter(description = "Subset UUID") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<SubsetAccessionRefDTO> accessionRefs) {
		SubsetDTO subset = translatedApiService.getSubset(uuid, version);
		return translatedApiService.get(translatedApiService.setAccessionRefs(subset, accessionRefs));
	}

	/**
	 * Set accessions to Subset from uploaded CSV file.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @param separator the delimiter to use for separating entries in the CSV file
	 * @param quotechar the character to use for quoted elements in the CSV file
	 * @param file the CSV file with accessionRefs to be added
	 * @return updated record
	 */
	@PostMapping(value = "/{uuid},{version}/upload-accessions", produces = { MediaType.APPLICATION_JSON_VALUE })
	public SubsetDTO uploadAccessions(@PathVariable("uuid") @Parameter(description = "Subset UUID") final UUID uuid, @PathVariable("version") final int version,
		// CSV settings
		@RequestParam(required = false, defaultValue = "\t") char separator, @RequestParam(required = false, defaultValue = "\"") char quotechar,
		// The file
		@RequestPart(name = "file") final MultipartFile file) throws IOException {

		return translatedApiService.uploadAccessions(uuid, version, separator, quotechar, file);
	}

	/**
	 * Rematch accessions.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return the subset
	 */
	@PostMapping(value = "/{uuid},{version}/rematch-accessions")
	public TranslatedSubsetDTO rematchAccessions(@PathVariable("uuid") @Parameter(description = "Subset UUID") final UUID uuid, @PathVariable("version") final int version) {
		final SubsetDTO subset = translatedApiService.getSubset(uuid, version);
		return translatedApiService.loadTranslated(translatedApiService.rematchSubsetAccessions(subset));
	}

	/**
	 * Loads subset by uuid and version and tries to publish it.
	 *
	 * @param uuid subset UUID
	 * @param version record version
	 * @return published Subset (admin-only)
	 */
	@PostMapping(value = "/approve", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "approveSubset", summary = "Approve and publish subset", description = "Make an unpublished subset publicly accessible")
	public TranslatedSubsetDTO approveSubset(@RequestBody @Valid EntityUuidAndVersion entity) {
		final SubsetDTO subset = translatedApiService.getSubset(entity.getUuid(), entity.getVersion());
		return translatedApiService.loadTranslated(translatedApiService.approveSubset(subset));
	}

	/**
	 * Loads subset by uuid and version and send to review.
	 *
	 * @param uuid subset UUID
	 * @param version record version
	 * @return subset in review state
	 */
	@PostMapping(value = "/for-review", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "reviewSubset", summary = "Submit for review", description = "Submit an unpublished subset for review")
	public TranslatedSubsetDTO reviewSubset(@RequestBody @Valid EntityUuidAndVersion entity) {
		final SubsetDTO subset = translatedApiService.getSubset(entity.getUuid(), entity.getVersion());
		return translatedApiService.loadTranslated(translatedApiService.reviewSubset(subset));
	}

	/**
	 * Loads subset by uuid and version and unpublish it.
	 *
	 * @param uuid subset UUID
	 * @param version record version
	 * @return unpublished subset
	 */
	@PostMapping(value = "/reject", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "rejectSubset", summary = "Reject subset", description = "Cancel publication of the subset and reset its state to draft")
	public TranslatedSubsetDTO rejectSubset(@RequestBody @Valid EntityUuidAndVersion entity) {
		final SubsetDTO subset = translatedApiService.getSubset(entity.getUuid(), entity.getVersion());
		return translatedApiService.loadTranslated(translatedApiService.rejectSubset(subset));
	}

	@DownloadEndpoint
	@RequestMapping(value = "{uuid}/download", method = RequestMethod.POST, params = { "mcpd" })
	@Operation(operationId = "downloadMcpd", summary = "Download MCPD", description = "Download subset accession data in MCPD format")
	public void downloadMcpd(@PathVariable("uuid") @Parameter(description = "Subset UUID") final UUID subsetUuid, HttpServletResponse response) throws IOException, NotFoundElement {
		final SubsetDTO subset = translatedApiService.getSubset(subsetUuid);
		if (subset == null) {
			throw new NotFoundElement();
		}

		// Write MCPD to the stream.
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.addHeader("Content-Disposition", String.format("attachment; filename=\"MCPD - %1s.xlsx\"", subset.getUuid()));
		// response.flushBuffer();

		final OutputStream outputStream = response.getOutputStream();
		try {
			translatedApiService.writeXlsxMCPD(subset, outputStream);
			response.flushBuffer();
		} catch (EOFException e) {
			LOG.warn("Download was aborted: {}", e.getMessage());
			throw e;
		}
	}

	/**
	 * Create a new version of Subset based on an existing published Subset.
	 *
	 * @param uuid Subset UUID
	 * @return the new version of Subset
	 */
	@PostMapping(value = "/create-new-version")
	@Operation(operationId = "newVersion", summary = "Replicate subset", description = "Copy an existing subset and create a new version")
	public SubsetDTO createNewVersion(@RequestParam(value = "uuid", required = true) @Parameter(description = "Subset UUID") final UUID uuid) {
		final SubsetDTO subset = translatedApiService.getSubset(uuid);
		return translatedApiService.get(translatedApiService.createNewVersion(subset));
	}

	/**
	 * Gets machine translation of the Subset
	 *
	 * @param uuid the Subset uuid
	 * @param targetLang target language tag
	 * @return 
	 * @throws TranslatorException 
	 */
	@GetMapping(value = "/{uuid:\\w{8}\\-\\w{4}.+}/translate/{targetLang}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "machineTranslate", summary = "Machine translate", description = "Generate machine translation of the subset by uuid")
	public SubsetLangDTO machineTranslate(
		@PathVariable @Parameter(description = "Subset UUID") final UUID uuid,
		@PathVariable @Parameter(description = "Target language tag") final String targetLang
	) throws TranslatorException {
		return translatedApiService.machineTranslate(uuid, targetLang);
	}

	@Override
	@Operation(hidden = true)
	public SubsetLangDTO machineTranslate(long entityId, String languageTag) throws TranslatorException {
		return super.machineTranslate(entityId, languageTag);
	}

	@RestController("subsetLangApi2")
	@RequestMapping(SubsetController.SubsetLangController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Subset")
	public static class SubsetLangController extends FilteredCRUDController<SubsetLangDTO, SubsetLang, SubsetLangApiService, SubsetLangFilter> {
		/** The Constant API_URL. */
		public static final String API_URL = SubsetController.CONTROLLER_URL + "/lang";
	}

}