InstituteController.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.genesys.server.api.v2;

import com.fasterxml.jackson.annotation.JsonView;
import com.google.common.collect.Sets;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.genesys.blocks.model.JsonViews;
import org.genesys.server.api.ApiBaseController;
import org.genesys.server.api.FilteredCRUDController;
import org.genesys.server.api.v1.InstituteController.InstituteDetails;
import org.genesys.server.api.v1.model.Article;
import org.genesys.server.api.v2.mapper.MapstructMapper;
import org.genesys.server.api.v2.model.impl.FaoInstituteInfo;
import org.genesys.server.component.aspect.DownloadEndpoint;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.server.service.CRMException;
import org.genesys.server.service.ContentService;
import org.genesys.server.service.DownloadService;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.GenesysService;
import org.genesys.server.service.InstituteService;
import org.genesys.server.service.ShortFilterService;
import org.genesys.server.service.SubsetService;
import org.genesys.server.service.filter.AccessionFilter;
import org.genesys.server.service.filter.InstituteFilter;
import org.genesys.server.service.filter.SubsetFilter;
import org.genesys.server.exception.SearchException;
import org.genesys.server.service.worker.AccessionAuditLogDownload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
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.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import static org.springframework.context.i18n.LocaleContextHolder.getLocale;

/**
 * Institute API v2
 */
@RestController("instituteApi2")
@PreAuthorize("isAuthenticated()")
@RequestMapping(InstituteController.API_URL)
@Tag(name = "Institute")
public class InstituteController extends FilteredCRUDController<FaoInstitute, InstituteService, InstituteFilter> {
	public static final String API_URL = ApiBaseController.APIv2_BASE + "/wiews";
	private static final Logger LOG = LoggerFactory.getLogger(InstituteController.class);

	@Autowired
	private InstituteService instituteService;

	@Autowired
	private SubsetService subsetService;


	@Autowired(required = false)
	private ElasticsearchService elasticsearchService;

	@Autowired
	private GenesysService genesysService;

	@Autowired
	private ContentService contentService;

	/** The short filter service. */
	@Autowired
	protected ShortFilterService shortFilterService;

	@Autowired
	private DownloadService downloadService;

	@Autowired
	private AccessionAuditLogDownload accessionAuditLogDownload;

	@Autowired
	private MapstructMapper mapper;

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

	@PostMapping(value = "/list-info", produces = MediaType.APPLICATION_JSON_VALUE)
	public List<FaoInstituteInfo> getInfo(@RequestBody List<String> instCodes) {
		return mapper.map(instituteService.getInstitutes(instCodes), mapper::mapInfo);
	}

	@JsonView({ JsonViews.Protected.class })
	@GetMapping(value = "/{code:[A-Z]+[0-9]+}", produces = MediaType.APPLICATION_JSON_VALUE)
	public FaoInstitute getByCode(@PathVariable(value = "code") String code) {
		return instituteService.findInstitute(code);
	}

	@GetMapping(value = "/{code:[A-Z]+[0-9]+}/details", produces = MediaType.APPLICATION_JSON_VALUE)
	public InstituteDetails details(@PathVariable(value = "code") String code) {

		FaoInstitute faoInstitute = instituteService.getInstitute(code);

		if (faoInstitute == null) {
			throw new NotFoundElement();
		}

		AccessionFilter byInstituteFilter = new AccessionFilter();
		byInstituteFilter.holder().code(Sets.newHashSet(faoInstitute.getCode()));

		InstituteDetails details = new InstituteDetails();
		details.details = faoInstitute;
		details.blurb = contentService.getArticle(faoInstitute, ContentService.ENTITY_BLURB_SLUG, getLocale());
		details.pdciStats = faoInstitute.getStatisticsPDCI();
		details.lastUpdates = genesysService.getLastUpdatedStatistics(faoInstitute);
		details.overview = getOverviewData(byInstituteFilter);

		PageRequest pageRequest = PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "lastModifiedDate"));
		SubsetFilter subsetFilter = new SubsetFilter();
		subsetFilter.institutes().add(faoInstitute.getCode());
		try {
			details.recentSubsets = subsetService.list(subsetFilter, pageRequest).getContent();
		} catch (SearchException e) {
			LOG.error("Error occurred during search", e);
		}

		return details;
	}

	/**
	 * Gets the passport data coverage for the institute.
	 *
	 * @param code the institute WIEWS code
	 * @return the coverage
	 */
	@JsonView({ JsonViews.Protected.class })
	@GetMapping(value = "/{code:[A-Z]+[0-9]+}/coverage", produces = MediaType.APPLICATION_JSON_VALUE)
	@PreAuthorize("hasRole('USER')")
	public Map<String, Long> getCoverage(@PathVariable(value = "code") String code) {
		FaoInstitute faoInstitute = instituteService.getInstitute(code);

		if (faoInstitute == null) {
			throw new NotFoundElement();
		}

		AccessionFilter filter = new AccessionFilter();
		filter.holder().id(Set.of(faoInstitute.getId()));
		try {
			return elasticsearchService.countMissingValues(Accession.class, filter);
		} catch (SearchException e) {
			LOG.error("Error occurred during search", e);
			return Collections.emptyMap();
		}
	}

	@DownloadEndpoint
	@RequestMapping(value = "/{wiewsCode}/download", method = RequestMethod.POST, params = { "dwca" })
	public void downloadDwca(@PathVariable(value = "wiewsCode", required = true) String wiewsCode, HttpServletResponse response) throws Exception {
		final FaoInstitute faoInstitute = instituteService.getInstitute(wiewsCode);
		if (faoInstitute == null) {
			throw new NotFoundElement();
		}
		LOG.warn("Downloading DwC-A accessions of: {}", faoInstitute);

		// Create JSON filter
		final AccessionFilter filter = new AccessionFilter();
		filter.holder().code(Sets.newHashSet(faoInstitute.getCode()));

		// Write Darwin Core Archive to the stream.
		response.setContentType("application/zip");
		response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-accessions-%1$s.zip\"", faoInstitute.getCode()));

		final OutputStream outputStream = response.getOutputStream();
		genesysService.writeAccessions(filter, outputStream, null, "/wiews/" + faoInstitute.getCode());
		response.flushBuffer();
	}

	@DownloadEndpoint
	@RequestMapping(value = "/{wiewsCode}/download", method = RequestMethod.POST, params = { "pdci" })
	public void downloadPdci(@PathVariable(value = "wiewsCode", required = true) String wiewsCode, HttpServletResponse response) throws IOException {
		final FaoInstitute faoInstitute = instituteService.getInstitute(wiewsCode);
		if (faoInstitute == null) {
			throw new NotFoundElement();
		}
		LOG.warn("Downloading PDCI for: {}", faoInstitute);

		// Create JSON filter
		final AccessionFilter filter = new AccessionFilter();
		filter.holder().code(Sets.newHashSet(faoInstitute.getCode()));

		// Write MCPD to the stream.
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-pdci-%1$s.xlsx\"", faoInstitute.getCode()));
		// response.flushBuffer();

		final OutputStream outputStream = response.getOutputStream();
		try {
			downloadService.writeXlsxPDCI(filter, outputStream, null, "/wiews/" + faoInstitute.getCode());
			response.flushBuffer();
		} catch (EOFException e) {
			LOG.warn("Download was aborted", e);
			throw e;
		}
	}

	@DownloadEndpoint
	@RequestMapping(value = "/{wiewsCode}/download", method = RequestMethod.POST, params = { "mcpd" })
	public void downloadMcpd(@PathVariable(value = "wiewsCode", required = true) String wiewsCode, HttpServletResponse response) throws IOException {
		final FaoInstitute faoInstitute = instituteService.getInstitute(wiewsCode);
		if (faoInstitute == null) {
			throw new NotFoundElement();
		}
		LOG.warn("Downloading MCPD accessions of: {}", faoInstitute);

		// Create JSON filter
		final AccessionFilter filter = new AccessionFilter();
		filter.holder().code(Sets.newHashSet(faoInstitute.getCode()));

		// Write MCPD to the stream.
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-accessions-%1$s.xlsx\"", faoInstitute.getCode()));
		// response.flushBuffer();

		final OutputStream outputStream = response.getOutputStream();
		try {
			downloadService.writeXlsxMCPD(filter, outputStream, null, "/wiews/" + faoInstitute.getCode());
			response.flushBuffer();
		} catch (EOFException e) {
			LOG.warn("Download was aborted", e);
			throw e;
		}
	}


	@DownloadEndpoint
	@RequestMapping(value = "/{wiewsCode}/download", method = RequestMethod.POST, params = { "auditlog" })
	public void downloadAuditLog(@PathVariable(value = "wiewsCode", required = true) String wiewsCode, HttpServletResponse response) throws IOException {
		final FaoInstitute faoInstitute = instituteService.getInstitute(wiewsCode);
		if (faoInstitute == null) {
			throw new NotFoundElement();
		}
		LOG.warn("Downloading Audit Log of {} accessions", faoInstitute);

		// Write MCPD to the stream.
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.addHeader("Content-Disposition", String.format("attachment; filename=\"genesys-auditlog-%1$s.xlsx\"", faoInstitute.getCode()));
		// response.flushBuffer();

		final OutputStream outputStream = response.getOutputStream();
		try {
			accessionAuditLogDownload.writePassportAuditLog(faoInstitute, null, null, outputStream);
			response.flushBuffer();
		} catch (EOFException e) {
			LOG.warn("Download was aborted", e);
			throw e;
		}
	}


	@JsonView({ JsonViews.Protected.class })
	@PostMapping(value = "/{code:[A-Z]+[0-9]+}/update")
	public FaoInstitute update(@PathVariable(value = "code") String code, @RequestBody FaoInstitute institute) {
		return instituteService.update(code, institute);
	}

	/**
	 * Update the article.
	 *
	 * @param code the code
	 * @param article the article
	 * @return the updated article
	 */
	@RequestMapping(value = "/{code:[A-Z]+[0-9]+}/update-article", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
	public Article updateArticle(@PathVariable(value = "code") String code, @RequestBody final Article article) throws NotFoundElement, ClassNotFoundException, CRMException {
		final FaoInstitute faoInstitute = instituteService.getInstitute(code);
		if (faoInstitute == null) {
			throw new NotFoundElement();
		}
		return contentService.updateArticle(faoInstitute, ContentService.ENTITY_BLURB_SLUG, article.getTitle(), article.getSummary(), article.getBody(), new Locale(article.getLang()));
	}

	private Map<String, ElasticsearchService.TermResult> getOverviewData(AccessionFilter byInstituteFilter) {
		String[] terms = new String[] { "crop.shortName", "cropName", "taxonomy.genus", "taxonomy.genusSpecies", "sampStat", "countryOfOrigin.code3",
			"storage", "curationType", "donorCode", "breederCode", "duplSite", "sgsv", "mlsStatus", "available"};

		try {
			return elasticsearchService.termStatisticsAuto(Accession.class, byInstituteFilter, 10, terms);
		} catch (Exception e) {
			LOG.error("Error occurred during search", e);
			return null;
		}
	}

}