AccessionApiServiceImpl.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 org.genesys.server.api.Slice;
import org.genesys.server.api.v2.facade.AccessionApiService;
import org.genesys.server.api.v2.mapper.MapstructMapper;
import org.genesys.server.api.v2.model.impl.AccessionDTO;
import org.genesys.server.api.v2.model.impl.FaoInstituteDTO;
import org.genesys.server.api.v2.model.impl.Taxonomy2Info;
import org.genesys.server.exception.SearchException;
import org.genesys.server.model.impl.AccessionIdentifier3;
import org.genesys.server.service.AccessionService;
import org.genesys.server.service.ElasticsearchService;
import org.genesys.server.service.filter.AccessionFilter;
import org.genesys.server.service.worker.dupe.AccessionDuplicateFinder;
import org.genesys.server.service.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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Collectors;

@Service
@Validated
@Transactional(readOnly = true)
public class AccessionApiServiceImpl implements AccessionApiService {

	@Autowired
	private MapstructMapper mapper;

	@Autowired
	private AccessionService service;

	@Autowired
	private AccessionDuplicateFinder duplicateFinder;

	@Override
	public long countAccessions(AccessionFilter filter) throws SearchException {
		return service.countAccessions(filter);
	}

	@Override
	public UUID uuidFromId(long id) {
		return service.uuidFromId(id);
	}

	@Override
	public List<UUID> uuidsFromIds(List<Long> ids) {
		return service.uuidsFromIds(ids);
	}

	@Override
	public UUID uuidFromAcceNumber(String instCode, String acceNumber) {
		return service.uuidFromAcceNumber(instCode, acceNumber);
	}

	@Override
	public List<UUID> uuidsFromAcceNumbers(String instCode, List<String> acceNumbers) {
		return service.uuidsFromAcceNumbers(instCode, acceNumbers);
	}

	@Override
	public Page<AccessionDTO> list(AccessionFilter filter, Pageable page) throws SearchException {
		return mapper.map(service.listFast(filter, page), mapper::map);
	}

	@Override
	public Page<AccessionDetails> withImages(AccessionFilter filter, Pageable page) throws SearchException {
		return mapper.map(service.withImages(filter, page), mapper::map);
	}

	@Override
	public Slice<AccessionDetails> withImagesSlice(AccessionFilter filter, Pageable page) throws SearchException {
		var details = service.withImagesSlice(filter, page);
		return new Slice<>(mapper.map(details.content, mapper::map), details.offset, details.totalElements, details.sort);
	}

	@Override
	public int countAccessionsImages(AccessionFilter filter) throws Exception {
		return service.countAccessionsImages(filter);
	}

	@Override
	public List<AccessionService.LabelValue<String>> autocomplete(AccessionFilter filter, String field, String text) throws IOException {
		return service.autocomplete(filter, field, text);
	}

	@Override
	public Map<String, ElasticsearchService.TermResult> getSuggestions(AccessionFilter filter) throws SearchException, IOException {
		return service.getSuggestions(filter);
	}

	@Override
	public Number[][] getGeoBounds(AccessionFilter filter) {
		return service.getGeoBounds(filter);
	}

	@Override
	public AccessionDTO getByUuid(UUID uuid) {
		return mapper.map(service.getByUuid(uuid));
	}

	@Override
	public AccessionDTO getByDoi(String doi) {
		return mapper.map(service.getByDoi(doi));
	}

	@Override
	public AccessionDetails getAccessionDetails(AccessionDTO accession) {
		return mapper.map(service.getAccessionDetails(mapper.map(accession)));
	}

	@Override
	public AccessionService.AccessionAuditLog getAuditLog(AccessionDTO accession) {
		return service.getAuditLog(mapper.map(accession));
	}

	@Override
	public Map<UUID, AccessionIdentifier3> toUUID(List<? extends AccessionIdentifier3> identifiers) {
		return service.toUUID(identifiers);
	}

	@Override
	public List<AccessionDTO> forUuids(Set<UUID> uuids) {
		return mapper.map(service.forUuids(uuids), mapper::map);
	}

	@Override
	public List<Taxonomy2Info> listSpecies(AccessionFilter filter) {
		return mapper.map(service.listSpecies(filter), mapper::mapInfo);
	}

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

	@Override
	public Set<Integer> listTileIndex3min(AccessionFilter filter) throws SearchException, Exception {
		return service.listTileIndex3min(filter);
	}

	@Override
	public Map<String, Set<Integer>> listTileIndex3minByCrop(AccessionFilter filter) throws SearchException, Exception {
		return service.listTileIndex3minByCrop(filter);
	}

	@Override
	public Page<Map<String, ?>> query(AccessionFilter filter, List<String> select, Pageable page, boolean useMcpdStyle) throws Exception {
		return service.query(filter, select, page, useMcpdStyle);
	}

	@Override
	public void query(AccessionFilter filter, List<String> select, Pageable page, boolean useMcpdStyle, Consumer<Map<String, ?>> consumer) throws Exception {
		service.query(filter, select, page, useMcpdStyle, consumer);
	}

	@Override
	public AccessionDetails getAccessionDetailsByDoi(String doi) {
		return mapper.map(service.getAccessionDetails(service.getByDoi(doi)));
	}

	@Override
	public AccessionDetails getAccessionDetailsByUuid(UUID uuid) {
		return mapper.map(service.getAccessionDetails(service.getByUuid(uuid)));
	}

	@Override
	public List<DuplicateFinder.Hit<AccessionDTO>> getSimilarAccessionsForUUID(UUID uuid) {
		return getSimilarAccessionsForUUID(uuid, null);
	}
	
	@Override
	public List<DuplicateFinder.Hit<AccessionDTO>> getSimilarAccessionsForUUID(UUID uuid, AccessionFilter filter) {
		return duplicateFinder.findSimilar(service.getByUuid(uuid), filter).stream().map(hit -> hit.map(mapper::map)).collect(Collectors.toList());
	}

	@Override
	public AccessionService.AccessionAuditLog getAccessionAuditLogByDoi(String doi) {
		return service.getAuditLog(service.getByDoi(doi));
	}

	@Override
	public AccessionService.AccessionAuditLog getAccessionAuditLogByUUID(UUID uuid) {
		return service.getAuditLog(service.getByUuid(uuid));
	}
}