AccessionApiServiceImpl.java

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

import org.gringlobal.api.model.AccessionDTO;
import org.gringlobal.api.model.AccessionDetail;
import org.gringlobal.api.model.AccessionInvGroupDTO;
import org.gringlobal.api.model.AcquisitionDTO;
import org.gringlobal.api.v2.facade.AccessionApiService;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Accession;
import org.gringlobal.model.Inventory;
import org.gringlobal.model.community.AccessionMCPD;
import org.gringlobal.persistence.AccessionInvAnnotationRepository;
import org.gringlobal.persistence.AccessionInvAttachRepository;
import org.gringlobal.persistence.AccessionInvNameRepository;
import org.gringlobal.service.AccessionInvGroupService;
import org.gringlobal.service.AccessionService;
import org.gringlobal.service.AccessionSourceMapService;
import org.gringlobal.service.ShortFilterService.FilterInfo;
import org.gringlobal.service.filter.AccessionFilter;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AccessionApiServiceImpl extends APIFilteredServiceFacadeImpl<AccessionService, AccessionDTO, Accession, AccessionFilter> implements AccessionApiService {
	
	@Autowired
	private AccessionInvGroupService accessionInvGroupService;

	@Autowired(required = false)
	private AccessionDuplicateFinder accessionDuplicateFinder;

	@Autowired
	private AccessionInvNameRepository accessionInvNameRepository;

	@Autowired
	private AccessionInvAttachRepository accessionInvAttachRepository;

	@Autowired
	private AccessionInvAnnotationRepository annotationRepository;

	@Autowired
	private AccessionSourceMapService accessionSourceMapService;

	@Override
	protected AccessionDTO convert(Accession source) {
		return mapper.map(source);
	}

	@Override
	protected Accession convert(AccessionDTO source) {
		return mapper.map(source);
	}

	@Transactional(readOnly = true)
	@Override
	public AccessionDetail getAccessionDetails(Long id) {
		var accessionDetails = new AccessionDetail();
//		map(accessionService.get(id));
		var accession = service.get(id);

		accessionDetails.setAccession(mapper.map(accession));
		accessionDetails.setSources(mapper.map(accession.getAccessionSources(), mapper::map));

		accessionDetails.setActions(mapper.map(accession.getAccessionActions(), mapper::map));
		accessionDetails.setIpr(mapper.map(accession.getAccessionIprs(), mapper::map));
		accessionDetails.setPedigree(mapper.map(accession.getAccessionPedigree()));
		accessionDetails.setQuarantine(mapper.map(accession.getAccessionQuarantines(), mapper::map));
		accessionDetails.setCitations(mapper.map(accession.getCitations(), mapper::map));
		accessionDetails.setNames(mapper.map(accessionInvNameRepository.findAccessionNames(accession, Inventory.SYSTEM_INVENTORY_FTC), mapper::map));
		accessionDetails.setGroups(mapper.map(accessionInvGroupService.listAccessionGroups(accession), mapper::map));
		accessionDetails.setAttachments(mapper.map(accessionInvAttachRepository.findAccessionAttachments(accession, Inventory.SYSTEM_INVENTORY_FTC), mapper::map));
		accessionDetails.setAnnotations(mapper.map(annotationRepository.findAccessionAnnotations(accession, Inventory.SYSTEM_INVENTORY_FTC), mapper::map));
		accessionDetails.setSourceCooperators(mapper.map(accessionSourceMapService.listAccessionSourceMaps(accession), mapper::map));

		return accessionDetails;
	}

	@Override
	@Transactional
	public AccessionInvGroupDTO acquire(AcquisitionDTO acquisitionBatch) {
		return mapper.map(service.acquire(mapper.mapInfo(acquisitionBatch)));
	}

	@Override
	@Transactional(readOnly = true)
	public Map<Object, Number> accessionOverview(String groupBy, AccessionFilter filter) {
		return service.accessionOverview(groupBy, filter);
	}

	@Transactional(readOnly = true)
	@Override
	public Page<AccessionMCPD> listMCPD(AccessionFilter filter, Pageable pageable) throws SearchException {
		return service.listMCPD(filter, pageable);
	}

	@Override
	@Transactional(readOnly = true)
	public List<DuplicateFinder.Hit<AccessionDTO>> findSimilar(AccessionDTO source) {
		var result = accessionDuplicateFinder.findSimilar(mapper.map(source));
		return result.stream().map(hit -> {
			var dtoHit = new DuplicateFinder.Hit<>(mapper.map(hit.result), hit.score);
			dtoHit.hitRating = hit.hitRating;
			dtoHit.matches = hit.matches;
			return dtoHit;
		}).collect(Collectors.toList());
	}

	@Override
	@Transactional(readOnly = true)
	public AccessionService.MapInfo<AccessionFilter> mapInfo(FilterInfo<AccessionFilter> filterInfo) {
		return service.mapInfo(filterInfo);
	}
}