SubsetController.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.api.v1;

import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.genesys.blocks.model.JsonViews;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.FilteredPage;
import org.genesys.server.api.Pagination;
import org.genesys.server.component.aspect.DownloadEndpoint;
import org.genesys.server.exception.DetailedConstraintViolationException;
import org.genesys.server.exception.InvalidApiUsageException;
import org.genesys.server.exception.NotFoundElement;
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.SubsetAccessionRef;
import org.genesys.server.service.AccessionService;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.ElasticsearchService.TermResult;
import org.genesys.server.service.ShortFilterService.FilterInfo;
import org.genesys.server.service.SubsetService;
import org.genesys.server.service.SubsetService.SubsetOverview;
import org.genesys.server.service.SubsetService.SubsetSuggestionPage;
import org.genesys.server.service.filter.AccessionFilter;
import org.genesys.server.service.filter.SubsetFilter;
import org.genesys.server.exception.SearchException;
import org.genesys.server.service.worker.ShortFilterProcessor;
import org.genesys.spring.CSVMessageConverter;
import org.genesys.taxonomy.gringlobal.component.CabReader;
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 com.fasterxml.jackson.annotation.JsonView;
import com.google.common.collect.Sets;
import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;

import io.swagger.annotations.Api;

/**
 * The Class SubsetController.
 *
 * @author Maxym Borodenko
 */
@RestController("subsetApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(SubsetController.CONTROLLER_URL)
@Api(tags = { "subset" })
public class SubsetController extends ApiBaseController {

	/** The Constant CONTROLLER_URL. */
	public static final String CONTROLLER_URL = ApiBaseController.APIv1_BASE + "/subset";

	private final Set<String> terms = Set.of("owner.uuid", "crops");

	@Autowired(required = false)
	private ElasticsearchService elasticsearchService;

	@Autowired
	private SubsetService subsetService;

	/** The short filter service. */
	@Autowired
	protected ShortFilterProcessor shortFilterProcessor;

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

	@Autowired
	private Validator validator;

	/**
	 * Register the subset.
	 *
	 * @param subset the subset
	 * @return the subset
	 */
	@PostMapping(value = "/create", produces = { MediaType.APPLICATION_JSON_VALUE })
	public Subset create(@RequestBody final Subset subset) {
		return subsetService.create(subset);
	}

	/**
	 * Updater subset.
	 *
	 * @param subset the subset
	 * @return the user
	 */
	@PostMapping(value = "/update", produces = { MediaType.APPLICATION_JSON_VALUE })
	public Subset update(@RequestBody final Subset subset) {
		return subsetService.update(subset);
	}

	/**
	 * Gets the subset
	 *
	 * @param uuid the uuid
	 * @return the subset
	 */
	@GetMapping(value = "/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public Subset get(@PathVariable("uuid") final UUID uuid) {
		return subsetService.loadSubset(uuid);
	}

	/**
	 * Remove subset.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return the subset
	 */
	@DeleteMapping(value = "/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public Subset remove(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		return subsetService.delete(subsetService.getSubset(uuid, version));
	}

	/**
	 * List of subsets.
	 *
	 * @param page the page
	 * @param filter the filter
	 * @return the page
	 * @throws IOException 
	 */
	@PostMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_VALUE })
	public FilteredPage<Subset, SubsetFilter> list(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
			@RequestBody(required = false) SubsetFilter filter) throws IOException, SearchException {
		
		FilterInfo<SubsetFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, SubsetFilter.class);
		return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, subsetService.list(filterInfo.filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "id")));
	}

	/**
	 * 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
	 */
	@PostMapping(value = "/filter", produces = { MediaType.APPLICATION_JSON_VALUE })
	public SubsetSuggestionPage listSuggestions(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
			@RequestBody(required = false) SubsetFilter filter) throws IOException, SearchException {

		FilterInfo<SubsetFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, SubsetFilter.class);

		var pageRes = new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, subsetService.list(filterInfo.filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "id")));
		Map<String, ElasticsearchService.TermResult> suggestionRes = subsetService.getSuggestions(filterInfo.filter);

		return SubsetSuggestionPage.from(pageRes, suggestionRes);
	}

	/**
	 * My subsets.
	 *
	 * @param page the page
	 * @param filter the descriptor filter
	 * @return the page
	 * @throws IOException
	 */
	@PostMapping(value = "/list-mine")
	public FilteredPage<Subset, SubsetFilter> mySubsets(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
			@RequestBody(required = false) SubsetFilter filter) throws IOException {

		FilterInfo<SubsetFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, SubsetFilter.class);
		return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, subsetService.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 })
	public 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 {

		FilterInfo<SubsetFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, SubsetFilter.class);
		filterInfo.filter.state(Set.of(PublishState.PUBLISHED));

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

		return 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 })
	public 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.");
		}

		FilterInfo<SubsetFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, SubsetFilter.class);
		filterInfo.filter
			.state(Set.of(PublishState.PUBLISHED))
			.current(true);

		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
	 */
	@JsonView({ JsonViews.Public.class })
	@GetMapping(value = "/accessions/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE, CSVMessageConverter.TEXT_CSV_VALUE })
	public Page<SubsetAccessionRef> listAccessions(@PathVariable("uuid") final UUID uuid, @ParameterObject final Pagination page) throws NotFoundElement {
		return subsetService.listAccessions(subsetService.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
	 */
	@JsonView({ JsonViews.Public.class })
	@GetMapping(value = "/accessions/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE, CSVMessageConverter.TEXT_CSV_VALUE }, params = { "full" })
	public Page<Accession> listFullAccessions(@PathVariable("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
	 * @param accessionIds the accessions UUIDs to be added
	 * @return the subset
	 */
	@PostMapping(value = "/add-accessions/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Validated
	public Subset addAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<SubsetAccessionRef> accessionRefs) {
		Subset subset = subsetService.getSubset(uuid);
		LOG.info("Want to add {} accessionRefs to subset {}", accessionRefs.size(), subset.getUuid());

		return subsetService.addAccessionRefs(subset, accessionRefs);
	}

	/**
	 * Set accessions to subset.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return the subset
	 */
	@PostMapping(value = "/set-accessions/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public Subset setAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<SubsetAccessionRef> accessionRefs) {
		Subset subset = subsetService.getSubset(uuid, version);
		return subsetService.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 = "/upload-accessions/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public Subset uploadAccessions(@PathVariable("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 {

		// Permit only a CSV file
		if (!file.getContentType().equalsIgnoreCase("text/csv")) {
			throw new InvalidApiUsageException("Invalid file type: " + file.getContentType() + " is not permitted.");
		}

		Subset subset = subsetService.getSubset(uuid, version);
		List<SubsetAccessionRef> accessionRefs = new ArrayList<>();

		// Build CSV parser
		CSVParser csvParser = new CSVParserBuilder().withSeparator(separator).withQuoteChar(quotechar).withEscapeChar((char) 0)
				.withStrictQuotes(false).withIgnoreLeadingWhiteSpace(false).withIgnoreQuotations(true).build();
		// Read file bytes as CSV
		try (CSVReader reader = new CSVReaderBuilder(CabReader.bomSafeReader(file.getInputStream())).withSkipLines(0).withCSVParser(csvParser).build()) {
			Iterator<SubsetAccessionRef> beanReader = CabReader.beanReader(SubsetAccessionRef.class, reader).iterator();
			SubsetAccessionRef acceRef = null;
			while (beanReader.hasNext() && (acceRef = beanReader.next()) != null) {
				Set<ConstraintViolation<SubsetAccessionRef>> violations = validator.validate(acceRef);
				if (violations == null || violations.isEmpty()) {
					accessionRefs.add(acceRef);
				} else {
					throw new DetailedConstraintViolationException("Failed to read CSV file in line " + reader.getLinesRead(), violations);
				}
			}
		}

		return subsetService.setAccessionRefs(subset, accessionRefs);
	}

	/**
	 * Rematch accessions.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return the subset
	 */
	@PostMapping(value = "/rematch-accessions/{uuid},{version}")
	public Subset rematchAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		final Subset subset = subsetService.getSubset(uuid, version);
		return subsetService.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 })
	public Subset approveSubset(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
		final Subset subset = subsetService.getSubset(uuid, version);
		return subsetService.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 })
	public Subset reviewSubset(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
		final Subset subset = subsetService.getSubset(uuid, version);
		return subsetService.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 })
	public Subset rejectSubset(@RequestParam(value = "uuid", required = true) final UUID uuid, @RequestParam(value = "version", required = true) final int version) {
		final Subset subset = subsetService.getSubset(uuid, version);
		return subsetService.rejectSubset(subset);
	}

	@DownloadEndpoint
	@RequestMapping(value = "{uuid}/download", method = RequestMethod.POST, params = { "mcpd" })
	public void downloadMcpd(@PathVariable("uuid") final UUID subsetUuid, HttpServletResponse response) throws IOException, NotFoundElement {
		final Subset subset = subsetService.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 {
			subsetService.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")
	public Subset createNewVersion(@RequestParam(value = "uuid", required = true) final UUID uuid) {
		final Subset subset = subsetService.loadSubset(uuid);
		return subsetService.createNewVersion(subset);
	}
}