DiversityTreeController.java

/*
 * Copyright 2020 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.IOException;
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 javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.genesys.blocks.model.JsonViews;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.FilteredPage;
import org.genesys.server.api.Pagination;
import org.genesys.server.exception.DetailedConstraintViolationException;
import org.genesys.server.exception.InvalidApiUsageException;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.impl.DiversityTree;
import org.genesys.server.model.impl.DiversityTreeAccessionRef;
import org.genesys.server.service.AccessionService;
import org.genesys.server.service.DiversityTreeService;
import org.genesys.server.service.DiversityTreeService.DiversityTreeSuggestionPage;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.ShortFilterService;
import org.genesys.server.service.filter.AccessionFilter;
import org.genesys.server.service.filter.DiversityTreeFilter;
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.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
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.Lists;
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 DiversityTreeController.
 *
 * @author Maxym Borodenko
 */
@RestController("diversityTreeApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(DiversityTreeController.CONTROLLER_URL)
@Api(tags = { "diversityTree" })
public class DiversityTreeController extends ApiBaseController {

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

	@Autowired
	private DiversityTreeService treeService;

	@Autowired
	protected ShortFilterProcessor shortFilterProcessor;

	@Autowired
	private AccessionService accessionService;

	@Autowired
	private Validator validator;

	/**
	 * Load the record by UUID
	 *
	 * @param uuid the uuid
	 * @return loaded record
	 */
	@GetMapping(value = "/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public DiversityTree get(@PathVariable("uuid") final UUID uuid) {
		return treeService.load(uuid);
	}

	/**
	 * Create a new record.
	 *
	 * @param source the source
	 * @return saved record
	 */
	@PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
	public DiversityTree create(@RequestBody final DiversityTree source) {
		return treeService.create(source);
	}

	/**
	 * Update existing record.
	 *
	 * @param source the source
	 * @return updated record
	 */
	@PutMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
	public DiversityTree update(@RequestBody final DiversityTree source) {
		return treeService.update(source);
	}

	/**
	 * Remove record.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return removed record
	 */
	@DeleteMapping(value = "/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public DiversityTree remove(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		return treeService.remove(treeService.get(uuid, version));
	}

	/**
	 * List of records.
	 *
	 * @param page the page
	 * @param filter the filter
	 * @return the page
	 * @throws IOException
	 */
	@PostMapping(value = "/list", produces = { MediaType.APPLICATION_JSON_VALUE })
	public FilteredPage<DiversityTree, DiversityTreeFilter> list(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
			@RequestBody(required = false) DiversityTreeFilter filter) throws IOException, SearchException {

		ShortFilterService.FilterInfo<DiversityTreeFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, DiversityTreeFilter.class);
		return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, treeService.list(filterInfo.filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.ASC, "id")));
	}

	/**
	 * List of records 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 DiversityTreeSuggestionPage listSuggestions(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
			@RequestBody(required = false) DiversityTreeFilter filter) throws IOException, SearchException {

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

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

		return DiversityTreeSuggestionPage.from(pageRes, suggestionRes);
	}

	/**
	 * My trees.
	 *
	 * @param page the page
	 * @param filter the descriptor filter
	 * @return the page
	 * @throws IOException
	 */
	@PostMapping(value = "/list-mine", produces = { MediaType.APPLICATION_JSON_VALUE })
	public FilteredPage<DiversityTree, DiversityTreeFilter> myTrees(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
			@RequestBody(required = false) DiversityTreeFilter filter) throws IOException {

		ShortFilterService.FilterInfo<DiversityTreeFilter> filterInfo = shortFilterProcessor.processFilter(filterCode, filter, DiversityTreeFilter.class);
		return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, treeService.listForCurrentUser(filterInfo.filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, Sort.Direction.DESC, "lastModifiedDate")));
	}

	/**
	 * Load AccessionRef list by tree
	 *
	 * @param uuid the uuid of tree
	 * @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<DiversityTreeAccessionRef> listAccessions(@PathVariable("uuid") final UUID uuid, @ParameterObject final Pagination page) throws NotFoundElement {
		return treeService.listAccessionRefs(treeService.get(uuid), page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	/**
	 * Load full accessions list by DiversityTree
	 *
	 * @param uuid uuid of DiversityTree
	 * @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()
			.diversityTrees(Sets.newHashSet(uuid));
		return accessionService.list(filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	/**
	 * Add accessions to diversity tree.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @param accessionRefs the accessionRefs to be added
	 * @return updated record
	 */
	@PostMapping(value = "/add-accessions/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Validated
	public DiversityTree addAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<DiversityTreeAccessionRef> accessionRefs) {
		DiversityTree tree = treeService.get(uuid, version);
		LOG.info("Want to add {} accessionRefs to tree {}", accessionRefs.size(), tree.getUuid());

		final Authentication contextAuth = SecurityContextHolder.getContext().getAuthentication();
		Lists.partition(new ArrayList<>(accessionRefs), 2000).parallelStream().forEach(batch -> {
			final Authentication prevAuth = SecurityContextHolder.getContext().getAuthentication();
			try {
				SecurityContextHolder.getContext().setAuthentication(contextAuth);
				treeService.addAccessionRefs(tree, batch);
			} finally {
				SecurityContextHolder.getContext().setAuthentication(prevAuth);
			}
		});

		treeService.rematchAccessions(tree);
		return treeService.load(uuid);
	}

	/**
	 * Set accessions to DiversityTree.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @param accessionRefs the accessionRefs to be added
	 * @return updated record
	 */
	@PostMapping(value = "/set-accessions/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public DiversityTree setAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<DiversityTreeAccessionRef> accessionRefs) {
		DiversityTree tree = treeService.get(uuid, version);
		tree = treeService.setAccessionRefs(tree, accessionRefs);
		tree = treeService.rematchAccessions(tree);
		return tree;
	}

	/**
	 * Set accessions to DiversityTree 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 DiversityTree 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.");
		}

		DiversityTree tree = treeService.get(uuid, version);
		List<DiversityTreeAccessionRef> 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<DiversityTreeAccessionRef> beanReader = CabReader.beanReader(DiversityTreeAccessionRef.class, reader).iterator();
			DiversityTreeAccessionRef dtAcceRef = null;
			while (beanReader.hasNext() && (dtAcceRef = beanReader.next()) != null) {
				Set<ConstraintViolation<DiversityTreeAccessionRef>> violations = validator.validate(dtAcceRef);
				if (violations == null || violations.isEmpty()) {
					accessionRefs.add(dtAcceRef);
				} else {
					throw new DetailedConstraintViolationException("Failed to read CSV file in line " + reader.getLinesRead(), violations);
				}
			}
		}

		tree = treeService.setAccessionRefs(tree, accessionRefs);
		tree = treeService.rematchAccessions(tree);

		return tree;
	}

	/**
	 * Rematch accessions.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return updated record
	 */
	@PostMapping(value = "/rematch-accessions/{uuid},{version}")
	public DiversityTree rematchAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		return treeService.rematchAccessions(treeService.get(uuid, version));
	}

	/**
	 * Publish diversity tree.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return published record (admin-only)
	 */
	@PostMapping(value = "/approve/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public DiversityTree approve(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		return treeService.approve(treeService.get(uuid, version));
	}

	/**
	 * Unpublish diversity tree.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return unpublished record
	 */
	@PostMapping(value = "/reject/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public DiversityTree reject(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		return treeService.reject(treeService.get(uuid, version));
	}

	/**
	 * Create a new version of DiversityTree based on an existing published DiversityTree.
	 *
	 * @param uuid the uuid
	 * @return the new version of DiversityTree
	 */
	@PostMapping(value = "/new-version/{uuid}")
	public DiversityTree createNewVersion(@PathVariable("uuid") final UUID uuid) {
		return treeService.createNewVersion(treeService.load(uuid));
	}

	/**
	 * Upload JSON file to the repository and add it to the DiversityTree
	 *
	 * @param uuid the UUID of divTree
	 * @param file the file to be uploaded
	 * @param metadata the file metadata
	 * @return updated tree
	 */
	@PostMapping(value = "/upload/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public DiversityTree uploadFile(@PathVariable(name = "uuid") final UUID uuid, @RequestPart(name = "file") final MultipartFile file,
			@RequestPart(name = "metadata", required = false) final RepositoryFile metadata)
			throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {

		return treeService.uploadFile(treeService.get(uuid), file, metadata);
	}

}