AccessionController.java

/*
 * Copyright 2019 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.v1.impl;

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

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.model.filters.AuditLogFilter;
import org.genesys.blocks.auditlog.service.AuditTrailService;
import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.filters.NumberFilter;
import org.gringlobal.api.v1.ActionController;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.FilteredCRUDController;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.custom.json.IgnoreEntityRefDeserializer;
import org.gringlobal.model.Accession;
import org.gringlobal.model.AccessionAction;
import org.gringlobal.model.AccessionInvGroup;
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.AccessionActionService.AccessionActionRequest;
import org.gringlobal.service.AccessionActionService.AccessionActionScheduleFilter;
import org.gringlobal.service.AccessionService;
import org.gringlobal.service.AccessionService.AcquisitionData;
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.AccessionDuplicateFinder;
import org.gringlobal.worker.dupe.DuplicateFinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
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.annotation.JsonView;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.querydsl.core.types.OrderSpecifier;

import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import io.swagger.v3.oas.annotations.tags.Tag;

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

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

	@Autowired
	private ObjectMapper objectMapper;

	@Autowired
	private AuditTrailService auditService;

	@Autowired
	private DownloadService downloadService;

	@Autowired
	private GlisDOIRegistrationManager glisDOIRegistrationManager;

	@Autowired(required = false)
	private AccessionDuplicateFinder accessionDuplicateFinder;

	@Override
	protected Class<AccessionFilter> filterType() {
		return AccessionFilter.class;
	}

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

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

		@Override
		protected Class<AccessionActionFilter> filterType() {
			return AccessionActionFilter.class;
		}

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

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

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

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

	@Override
	public FilteredPage<Accession, 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, crudService.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 AccessionService.AccessionDetails details(@PathVariable("id") final long id) {
		return crudService.getAccessionDetails(crudService.get(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<AuditLog> accessionAuditLogs(@PathVariable(value = "id") final Long accessionId, @ParameterObject final Pagination page) {
		var filter = new AuditLogFilter();
		filter
			.entityId(new NumberFilter<Long>().eq(Set.of(crudService.get(accessionId).getId())))
			.classname(Accession.class.getName());

		return auditService.listAuditLogs(filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	@PostMapping(value = "/acquire", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "addMaterial", description = "Register new accessions and inventories", summary = "Add new material")
	public AccessionInvGroup acquire(@RequestBody(required = true) @JsonView(JsonViews.Update.class) AcquisitionData acquisitionBatch) {
		return crudService.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 crudService.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 {

		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(required = true, name = "id") final long id) throws Throwable {
		var accession = crudService.get(id);
		return glisDOIRegistrationManager.updateDoiRegistration(null, (AccessionFilter) new AccessionFilter().id(Set.of(accession.getId()))).get(0);
	}

	/**
	 * Bulk update GLIS DOI Registration Service
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@PostMapping(value = "/assign-doi")
	public List<GlisDOIRegistrationManager.GlisDoiResponse> assignDoiToAccessions(@RequestBody final List<Long> ids) throws Exception {
		AccessionFilter filter = new AccessionFilter();
		filter.id(new HashSet<>(ids));

		return glisDOIRegistrationManager.updateDoiRegistration(null, filter);
	}

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

	@PostMapping(value = "/attach/{attachId}/share", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "shareAttachment", description = "Share attachment to accession", summary = "Share attachment file")
	public void shareAttachment(@PathVariable(name = "attachId") final Long attachId, @RequestBody @NotEmpty List<Long> accessionIds) {
		crudService.shareAttachment(attachId, accessionIds);
	}
}