SubsetApiServiceImpl.java

/*
 * Copyright 2024 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.facade.impl;

import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import org.genesys.server.api.v2.facade.SubsetApiService;
import org.genesys.server.api.v2.model.impl.SubsetAccessionRefDTO;
import org.genesys.server.api.v2.model.impl.SubsetCreatorDTO;
import org.genesys.server.api.v2.model.impl.SubsetDTO;
import org.genesys.server.api.v2.model.impl.SubsetLangDTO;
import org.genesys.server.api.v2.model.impl.TranslatedSubsetDTO;
import org.genesys.server.exception.DetailedConstraintViolationException;
import org.genesys.server.exception.InvalidApiUsageException;
import org.genesys.server.exception.NotFoundElement;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.impl.Subset;
import org.genesys.server.model.impl.SubsetAccessionRef;
import org.genesys.server.model.impl.SubsetLang;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.SubsetService;
import org.genesys.server.service.SubsetTranslationService;
import org.genesys.server.service.TranslatorService.TranslatorException;
import org.genesys.server.service.filter.SubsetFilter;
import org.genesys.taxonomy.gringlobal.component.CabReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

@Service
@Validated
public class SubsetApiServiceImpl extends APIFilteredTranslatedServiceFacadeImpl<SubsetService, SubsetDTO,
	TranslatedSubsetDTO, SubsetLangDTO, Subset, SubsetLang,
	SubsetTranslationService.TranslatedSubset, SubsetFilter> implements SubsetApiService {

	@Autowired
	private Validator validator;

	@Override
	protected TranslatedSubsetDTO convertTranslation(SubsetTranslationService.TranslatedSubset source) {
		return mapper.map(source);
	}

	@Override
	protected SubsetLang convertLang(SubsetLangDTO source) {
		return mapper.map(source);
	}

	@Override
	protected SubsetLangDTO convertLang(SubsetLang source) {
		return mapper.map(source);
	}

	@Override
	protected Subset convert(SubsetDTO source) {
		return mapper.map(source);
	}

	@Override
	protected SubsetDTO convert(Subset source) {
		return mapper.map(source);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<SubsetAccessionRefDTO> listAccessions(SubsetDTO subset, Pageable page) {
		return mapper.map(service.listAccessions(convert(subset), page), mapper::map);
	}

	@Override
	@Transactional(readOnly = true)
	public long countSubsets(SubsetFilter filter) throws SearchException {
		return service.countSubsets(filter);
	}

	@Override
	@Transactional(readOnly = true)
	public Page<SubsetDTO> listSubsetsForCurrentUser(SubsetFilter filter, Pageable page) {
		return mapper.map(service.listSubsetsForCurrentUser(filter, page), this::convert);
	}

	@Override
	@Transactional(readOnly = true)
	public TranslatedSubsetDTO loadSubset(UUID uuid) {
		return convertTranslation(translationService.loadTranslated(uuid));
	}

	@Override
	@Transactional(readOnly = true)
	public SubsetDTO getSubset(UUID uuid) {
		return convert(service.getSubset(uuid));
	}

	@Override
	@Transactional(readOnly = true)
	public SubsetDTO getSubset(UUID uuid, int version) {
		return convert(service.getSubset((uuid), version));
	}

	@Override
	@Transactional(readOnly = true)
	public Map<String, ElasticsearchService.TermResult> getSuggestions(SubsetFilter filter) throws SearchException, IOException {
		return service.getSuggestions(filter);
	}

	@Override
	@Transactional
	public Subset addAccessionRefs(SubsetDTO input, Collection<SubsetAccessionRefDTO> accessionRefs) {
		return service.addAccessionRefs(convert(input), mapper.map(accessionRefs, mapper::map));
	}

	@Override
	@Transactional
	public Subset reviewSubset(SubsetDTO subset) {
		return service.reviewSubset(convert(subset));
	}

	@Override
	@Transactional
	public Subset rejectSubset(SubsetDTO subset) {
		return service.rejectSubset(convert(subset));
	}

	@Override
	@Transactional
	public Subset approveSubset(SubsetDTO subset) {
		return service.approveSubset(convert(subset));
	}

	// @Override
	// @Transactional(readOnly = true)
	// public List<SubsetDTO> listByAccession(AccessionDTO accession) {
	// 	return mapper.map(service.listByAccession(mapper.map(accession)), this::convert);
	// }

	@Override
	@Transactional
	public SubsetCreatorDTO createSubsetCreator(SubsetDTO subset, SubsetCreatorDTO input) throws NotFoundElement {
		return mapper.map(service.createSubsetCreator(convert(subset), mapper.map(input)));
	}

	@Override
	@Transactional
	public SubsetCreatorDTO removeSubsetCreator(SubsetDTO subset, SubsetCreatorDTO input) throws NotFoundElement {
		return mapper.map(service.removeSubsetCreator(convert(subset), mapper.map(input)));
	}

	@Override
	@Transactional
	public SubsetCreatorDTO removeSubsetCreator(SubsetDTO subset, UUID subsetCreatorUuid) throws NotFoundElement {
		return mapper.map(service.removeSubsetCreator(convert(subset), subsetCreatorUuid));
	}

	@Override
	@Transactional(readOnly = true)
	public SubsetCreatorDTO loadSubsetCreator(SubsetCreatorDTO subsetCreator) throws NotFoundElement {
		return mapper.map(service.loadSubsetCreator(mapper.map(subsetCreator)));
	}

	@Override
	@Transactional(readOnly = true)
	public SubsetCreatorDTO loadSubsetCreator(UUID subsetCreatorUuid) throws NotFoundElement {
		return mapper.map(service.loadSubsetCreator(subsetCreatorUuid));
	}

	@Override
	@Transactional(readOnly = true)
	public List<SubsetCreatorDTO> listSubsetCreators(SubsetDTO subset) throws NotFoundElement {
		return mapper.map(service.listSubsetCreators(mapper.map(subset)), mapper::map);
	}

	@Override
	@Transactional
	public SubsetCreatorDTO updateSubsetCreator(SubsetDTO subset, SubsetCreatorDTO subsetCreator) throws NotFoundElement {
		return mapper.map(service.updateSubsetCreator(convert(subset), mapper.map(subsetCreator)));
	}

	@Override
	@Transactional
	public List<SubsetCreatorDTO> autocompleteCreators(String text) {
		return mapper.map(service.autocompleteCreators(text), mapper::map);
	}

	@Override
	@Transactional
	public void rematchSubsetAccessions() {
		service.rematchSubsetAccessions();
	}

	@Override
	@Transactional
	public Subset rematchSubsetAccessions(SubsetDTO subset) {
		return service.rematchSubsetAccessions(convert(subset));
	}

	@Override
	@Transactional
	public void batchRematchAccessionRefs(List<SubsetAccessionRefDTO> accessionRefs) {
		service.batchRematchAccessionRefs(mapper.map(accessionRefs, mapper::map));
	}

	// @Override
	// @Transactional
	// public int clearAccessionRefs(Collection<AccessionDTO> accessions) {
	// 	return service.clearAccessionRefs(mapper.map(accessions, mapper::map));
	// }

	@Override
	@Transactional
	public Subset setAccessionRefs(SubsetDTO input, Collection<SubsetAccessionRefDTO> accessionRefs) {
		return service.setAccessionRefs(convert(input), mapper.map(accessionRefs, mapper::map));
	}

	@Override
	@Transactional(readOnly = true)
	public void writeXlsxMCPD(SubsetDTO subset, OutputStream outputStream) throws IOException {
		service.writeXlsxMCPD(convert(subset), outputStream);
	}

	@Override
	@Transactional
	public Subset createNewVersion(SubsetDTO source) {
		return service.createNewVersion(convert(source));
	}

	@Override
	public SubsetLangDTO machineTranslate(UUID uuid, String targetLang) throws TranslatorException {
		return super.machineTranslate(service.getSubset(uuid), targetLang);
	}

	@Override
	@Transactional
	public SubsetDTO uploadAccessions(UUID uuid, int version, char separator, char quotechar, 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.");
		}

		SubsetDTO subset = getSubset(uuid, version);
		List<SubsetAccessionRefDTO> accessionRefs = new ArrayList<>();

		// Build CSV parser
		CSVParser csvParser = new CSVParserBuilder().withSeparator(separator).withQuoteChar(quotechar).withEscapeChar((char) 0)
			.withStrictQuotes(false).withIgnoreLeadingWhiteSpace(false).withIgnoreQuotations(true).build();
		// Read file bytes as CSV
		try (CSVReader reader = new CSVReaderBuilder(CabReader.bomSafeReader(file.getInputStream())).withSkipLines(0).withCSVParser(csvParser).build()) {
			Iterator<SubsetAccessionRef> beanReader = CabReader.beanReader(SubsetAccessionRef.class, reader).iterator();
			SubsetAccessionRef acceRef = null;
			while (beanReader.hasNext() && (acceRef = beanReader.next()) != null) {
				Set<ConstraintViolation<SubsetAccessionRef>> violations = validator.validate(acceRef);
				if (violations == null || violations.isEmpty()) {
					accessionRefs.add(mapper.map(acceRef));
				} else {
					throw new DetailedConstraintViolationException("Failed to read CSV file in line " + reader.getLinesRead(), violations);
				}
			}
		}

		return get(setAccessionRefs(subset, accessionRefs));
	}

	// @Override
	// @Transactional
	// public long changeInstitute(FaoInstituteDTO currentInstitute, FaoInstituteDTO newInstitute) {
	// 	return service.changeInstitute(mapper.map(currentInstitute), mapper.map(newInstitute));
	// }
}