AccessionController.java

/*
 * Copyright 2023 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.gringlobal.api.v2.impl;

import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.auditlog.model.filters.AuditLogFilter;
import org.genesys.blocks.auditlog.service.AuditTrailService;
import org.gringlobal.api.model.AccessionActionDTO;
import org.gringlobal.api.model.AccessionActionRequestDTO;
import org.gringlobal.api.model.AccessionDTO;
import org.gringlobal.api.model.AccessionDetail;
import org.gringlobal.api.model.AccessionInvGroupDTO;
import org.gringlobal.api.model.AcquisitionDTO;
import org.gringlobal.api.model.AuditLogDTO;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.api.v2.ActionController;
import org.gringlobal.api.v2.FilteredCRUDController;
import org.gringlobal.api.v2.facade.AccessionActionApiService;
import org.gringlobal.api.v2.facade.AccessionApiService;
import org.gringlobal.api.v2.mapper.MapstructMapper;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Accession;
import org.gringlobal.model.AccessionAction;
import org.gringlobal.model.QAccession;
import org.gringlobal.model.QAccessionAction;
import org.gringlobal.model.community.AccessionMCPD;
import org.gringlobal.service.AccessionActionService;
import org.gringlobal.service.AccessionService.MapInfo;
import org.gringlobal.service.DownloadService;
import org.gringlobal.service.filter.AccessionActionFilter;
import org.gringlobal.service.filter.AccessionFilter;
import org.gringlobal.service.glis.impl.GlisDOIRegistrationManager;
import org.gringlobal.spring.CSVMessageConverter;
import org.gringlobal.worker.dupe.DuplicateFinder;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
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.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.querydsl.core.types.OrderSpecifier;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController("accessionApi2")
@RequestMapping(AccessionController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Accession")
@Slf4j
public class AccessionController extends FilteredCRUDController<AccessionDTO, Accession, AccessionApiService, AccessionFilter> {

	/** The Constant API_URL. */
	public static final String API_URL = ApiBaseController.APIv2_BASE + "/a";

	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private AuditTrailService auditService;

	@Autowired
	private DownloadService downloadService;

	@Autowired
	private GlisDOIRegistrationManager glisDOIRegistrationManager;
	
	@Autowired
	private MapstructMapper mapper;

	@Override
	protected OrderSpecifier<?>[] defaultSort() {
		return new OrderSpecifier[] { QAccession.accession.id.asc() };
	}

	@RestController("accessionActionApi2")
	@RequestMapping(AccessionActionController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Accession")
	public static class AccessionActionController extends ActionController<AccessionActionDTO, AccessionAction, AccessionActionFilter, AccessionActionRequestDTO, AccessionActionService.AccessionActionScheduleFilter, AccessionActionApiService> {
		public static final String API_URL = AccessionController.API_URL;

		@Override
		protected OrderSpecifier<?>[] defaultSort() {
			return new OrderSpecifier[] { QAccessionAction.accessionAction.createdDate.desc() };
		}
	}

	@Override
	public AccessionDTO create(@RequestBody AccessionDTO entity) {
		return super.create(entity);
	}

	@Override
	public AccessionDTO update(@RequestBody AccessionDTO entity) {
		return super.update(entity);
	}

	@Override
	@PostMapping(value = FilteredCRUDController.ENDPOINT_LIST, produces = { MediaType.APPLICATION_JSON_VALUE, CSVMessageConverter.TEXT_CSV_VALUE })
	public FilteredPage<AccessionDTO, AccessionFilter> list(@ParameterObject final Pagination page, @RequestBody AccessionFilter filter) throws SearchException, IOException {
		return super.list(page, filter);
	}

	@Override
	public FilteredPage<AccessionDTO, AccessionFilter> filter(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
			@RequestBody(required = false) AccessionFilter filter) throws IOException, SearchException {
		return super.filter(filterCode, page, filter);
	}

	@PostMapping(value = "/mcpd", produces = { MediaType.APPLICATION_JSON_VALUE, CSVMessageConverter.TEXT_CSV_VALUE })
	public FilteredPage<AccessionMCPD, AccessionFilter> listMcpd(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
			@RequestBody AccessionFilter filter) throws SearchException, IOException {

		final var filterInfo = shortFilterService.processFilter(filterCode, filter, AccessionFilter.class);
		Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
		return new FilteredPage<>(filterInfo.filterCode, filterInfo.filter, serviceFacade.listMCPD(filterInfo.filter, pageable));
	}

	/**
	 * Retrieve accession details by id
	 *
	 * @param id the id
	 * @return the accession details
	 */
	@GetMapping(value = "/details/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "getDetails", description = "Retrieve accession details by ID", summary = "Details")
	public AccessionDetail details(@PathVariable("id") final long id) {
		return serviceFacade.getAccessionDetails(id);
	}

	/**
	 * Retrieve a list of audit logs for the specified accession
	 *
	 * @param accessionId the accessionId
	 * @param page the page request
	 * @return the list of all log entries
	 */
	@GetMapping("/auditlog/{id}")
	public Page<AuditLogDTO> accessionAuditLogs(@PathVariable(value = "id") final Long accessionId, @ParameterObject final Pagination page) {
		var filter = new AuditLogFilter();
		filter.entityId().eq().add(serviceFacade.get(accessionId).getId());
		filter.classname(Accession.class.getName());

		// TODO create auditServiceFacade
		return mapper.map(auditService.listAuditLogs(filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE)), mapper::map);
	}

	@PostMapping(value = "/acquire", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "addMaterial", description = "Register new accessions and inventories", summary = "Add new material")
	public AccessionInvGroupDTO acquire(@RequestBody(required = true) AcquisitionDTO acquisitionBatch) {
		return serviceFacade.acquire(acquisitionBatch);
	}

	@PostMapping(value = "/overview/{groupBy:.+}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "accessionOverview", description = "Get accession statistics", summary = "Overview")
	public Map<?, ?> accessionOverview(@PathVariable(name = "groupBy", required = true) String groupBy, @RequestBody AccessionFilter filter) {
		return serviceFacade.accessionOverview(groupBy, filter);
	}

	@RequestMapping(value = "/download-mcpd", method = RequestMethod.POST, params = { "filter" })
	public void downloadMcpd(@RequestParam(name = "filter", required = true) String filter, HttpServletResponse response) throws IOException, SearchException {

		var accessionFilters = objectMapper.readValue(filter, AccessionFilter.class);
		final var filterInfo = shortFilterService.processFilter(null, accessionFilters, AccessionFilter.class);

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

		final OutputStream outputStream = response.getOutputStream();
		try {
			downloadService.writeXlsxMCPD(filterInfo.filter, outputStream, filterInfo.filterCode, "/a/" + filterInfo.filterCode);
			response.flushBuffer();
		} catch (EOFException e) {
			log.warn("Download was aborted: {}", e.getMessage());
			throw e;
		}
	}

	/**
	 * Update GLIS DOI Registration Service for one accession
	 *
	 * @param id Accession ID
	 * @return DOI Registration status
	 * @throws Throwable the problem error
	 */
	@PostMapping(value = ENDPOINT_ID + "/assign-doi")
	public GlisDOIRegistrationManager.GlisDoiResponse assignDoiToAccessions(@PathVariable(name = "id") final long id) throws Throwable {
		var accession = serviceFacade.get(id);
		return glisDOIRegistrationManager.updateDoiRegistration(null, (AccessionFilter) new AccessionFilter().id(Set.of(accession.getId()))).get(0);
	}

	/**
	 * Update GLIS DOI Registration Service for accession by filter with propress data
	 *
	 * @param filterCode Filter code
	 * @param filter AccessionFilter
	 * @return DOI Registration progress data
	 * @throws Exception the problem error
	 */
	@PostMapping(value = "/assign-doi")
	public GlisDOIRegistrationManager.AssignDoiState assignDoiToAccessions(
		@RequestParam(name = "f", required = false) String filterCode,
		@RequestBody(required = false) final org.gringlobal.service.filter.AccessionFilter filter
	) throws Exception {
		return glisDOIRegistrationManager.updateDoiRegistrationWithProgress(filterCode, filter);
	}

	@PostMapping("/assign-doi/cancel")
	public ResponseEntity<HttpStatus> cancelDoiAssignment(@RequestParam(name = "f", required = false) String filterCode) {
		return glisDOIRegistrationManager.cancelDoiAssignment(filterCode);
	}

	/**
	 * Find similar accessions by source
	 *
	 * @param source source AccessionDTO
	 * @return the list of similar Accessions
	 */
	@PostMapping(value = "/similar")
	public List<DuplicateFinder.Hit<AccessionDTO>> findSimilarForUnsaved(@RequestBody final AccessionDTO source) {
		return serviceFacade.findSimilar(source);
	}


	@PostMapping(value = "/map-info")
	public MapInfo<AccessionFilter> mapInfo(
			@RequestParam(name = "f", required = false) String filterCode,
			@RequestBody AccessionFilter filter
	) throws SearchException, IOException {

		final var filterInfo = shortFilterService.processFilter(filterCode, filter, AccessionFilter.class);
		return serviceFacade.mapInfo(filterInfo);
	}

}