DiversityTreeController.java

/*
 * Copyright 2025 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.annotations.Api;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.FilteredPage;
import org.genesys.server.api.Pagination;
import org.genesys.server.api.v2.facade.AccessionApiService;
import org.genesys.server.api.v2.facade.DiversityTreeApiService;
import org.genesys.server.api.v2.facade.impl.DiversityTreeApiServiceImpl;
import org.genesys.server.api.v2.model.impl.AccessionDTO;
import org.genesys.server.api.v2.model.impl.DiversityTreeAccessionRefDTO;
import org.genesys.server.api.v2.model.impl.DiversityTreeDTO;
import org.genesys.server.exception.InvalidApiUsageException;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.exception.SearchException;
import org.genesys.server.service.DiversityTreeService;
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.service.worker.ShortFilterProcessor;
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.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 java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

@RestController("diversityTreeApi2")
@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.APIv2_BASE + "/div-tree";

	@Autowired
	private DiversityTreeApiService treeApiService;

	@Autowired
	private DiversityTreeService treeService;

	@Autowired
	protected ShortFilterProcessor shortFilterProcessor;

	@Autowired
	private AccessionApiService accessionApiService;

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

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

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

	/**
	 * Remove record.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return removed record
	 */
	@DeleteMapping(value = "/{uuid},{version}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public DiversityTreeDTO remove(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		return treeApiService.remove(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<DiversityTreeDTO, 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, treeApiService.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 DiversityTreeApiServiceImpl.DiversityTreeSuggestionPageDTO 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<DiversityTreeDTO, DiversityTreeFilter> pageRes = new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, treeApiService.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 DiversityTreeApiServiceImpl.DiversityTreeSuggestionPageDTO.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<DiversityTreeDTO, 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, treeApiService.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
	 */
	@GetMapping(value = "/accessions/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE, CSVMessageConverter.TEXT_CSV_VALUE })
	public Page<DiversityTreeAccessionRefDTO> listAccessions(@PathVariable("uuid") final UUID uuid, @ParameterObject final Pagination page) throws NotFoundElement {
		return treeApiService.listAccessions(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
	 */
	@GetMapping(value = "/accessions/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE, CSVMessageConverter.TEXT_CSV_VALUE }, params = { "full" })
	public Page<AccessionDTO> listFullAccessions(@PathVariable("uuid") final UUID uuid, @ParameterObject final Pagination page) throws NotFoundElement, SearchException {
		AccessionFilter filter = new AccessionFilter()
			.diversityTrees(Sets.newHashSet(uuid));
		return accessionApiService.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 DiversityTreeDTO addAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<DiversityTreeAccessionRefDTO> accessionRefs) {
		return treeApiService.addAccessions(uuid, version, accessionRefs);
	}

	/**
	 * 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 DiversityTreeDTO setAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version, @RequestBody final Set<DiversityTreeAccessionRefDTO> accessionRefs) {
		return treeApiService.setAccessions(uuid, version, accessionRefs);
	}

	/**
	 * 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 DiversityTreeDTO 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.");
		}
		return treeApiService.uploadAccessions(uuid, version, separator, quotechar, file);
	}

	/**
	 * Rematch accessions.
	 *
	 * @param uuid the uuid
	 * @param version the version
	 * @return updated record
	 */
	@PostMapping(value = "/rematch-accessions/{uuid},{version}")
	public DiversityTreeDTO rematchAccessions(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		return treeApiService.rematchAccessions(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 DiversityTreeDTO approve(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		return treeApiService.approve(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 DiversityTreeDTO reject(@PathVariable("uuid") final UUID uuid, @PathVariable("version") final int version) {
		return treeApiService.reject(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 DiversityTreeDTO createNewVersion(@PathVariable("uuid") final UUID uuid) {
		return treeApiService.createNewVersion(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
	 * @return updated tree
	 */
	@PostMapping(value = "/upload/{uuid}", produces = { MediaType.APPLICATION_JSON_VALUE })
	public DiversityTreeDTO uploadFile(@PathVariable(name = "uuid") final UUID uuid, @RequestPart(name = "file") final MultipartFile file)
		throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {

		return treeApiService.uploadFile(uuid, file);
	}

}