InstituteApiServiceImpl.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.v1.facade.impl;

import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.genesys.server.api.v1.facade.InstituteApiService;
import org.genesys.server.api.v1.mapper.APIv1Mapper;
import org.genesys.server.api.v1.model.FaoInstitute;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.service.ContentService;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.GenesysService;
import org.genesys.server.service.InstituteService;
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.service.worker.AccessionAuditLogDownload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

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

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

@Service("instituteV2APIService")
@Validated
@Transactional(readOnly = true)
@Slf4j
public class InstituteApiServiceImpl implements InstituteApiService {

	@Autowired
	private InstituteService service;

	@Autowired
	private ContentService contentService;

	@Autowired
	private SubsetService subsetService;

	@Autowired
	private GenesysService genesysService;

	@Autowired(required = false)
	private ElasticsearchService elasticsearchService;

	@Autowired
	private AccessionAuditLogDownload accessionAuditLogDownload;

	@Autowired
	private APIv1Mapper mapper;

	protected FaoInstitute convert(org.genesys.server.model.impl.FaoInstitute source) {
		try {
			return mapper.map(source);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	protected org.genesys.server.model.impl.FaoInstitute convert(FaoInstitute source) {
		try {
			return mapper.map(source);
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public Page<FaoInstitute> list(InstituteFilter filter, Pageable page) throws SearchException {
		return mapper.map(service.list(filter, page), this::convert);
	}

	@Override
	public FaoInstitute findInstitute(String code) {
		return convert(service.findInstitute(code));
	}

	@Override
	public InstituteDetails details(String code) {
		var faoInstitute = service.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 = convert(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;
	}

	@Override
	public Map<String, Long> getCoverage(String code) {
		var faoInstitute = service.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();
		}
	}

	@Override
	public FaoInstitute getInstitute(String wiewsCode) {
		return convert(service.getInstitute(wiewsCode));
	}

	@Override
	public void downloadAuditLog(String wiewsCode, HttpServletResponse response) throws IOException {
		var faoInstitute = service.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;
		}
	}

	@Override
	@Transactional
	public FaoInstitute update(String code, FaoInstitute institute) {
		return convert(service.update(code, convert(institute)));
	}

	private Map<String, ElasticsearchService.TermResult> getOverviewData(AccessionFilter byInstituteFilter) {
		String[] terms = new String[] { "crop.shortName", "cropName", "taxonomy.genus", "taxonomy.genusSpecies" };

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