BrAPIServiceImpl.java

package org.genesys.brapi.service.impl;

import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.genesys.brapi.model.Germplasm;
import org.genesys.brapi.service.BrAPIService;
import org.genesys.server.model.genesys.Accession;
import org.genesys.server.model.genesys.Taxonomy2;
import org.genesys.server.model.impl.Crop;
import org.genesys.server.service.AccessionService;
import org.genesys.server.service.CropService;
import org.genesys.server.service.filter.AccessionFilter;
import org.genesys.server.exception.NoSuchAccessionException;
import org.genesys.server.exception.SearchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Genesys implementation of BrAPI v1
 * 
 * @author Matija Obreza
 */
@Service
@Transactional(readOnly = true)
public class BrAPIServiceImpl implements BrAPIService {

	@Autowired
	private AccessionService accessionService;
	@Autowired
	private CropService cropService;

	@Override
	public Germplasm getGermplasmById(UUID germplasmId) throws NoSuchAccessionException {
		return toGermplasm(accessionService.getByUuid(germplasmId));
	}

	/**
	 * Convert Accession to Germplasm
	 */
	private Germplasm toGermplasm(Accession accession) {
		Germplasm g = new Germplasm();

		g.setUuid(accession.getUuid());
		g.setGermplasmPUI(accession.getDoi());
		g.setAcceNumb(accession.getAccessionNumber());
		g.setDefaultDisplayName(accession.getAccessionNumber());
		g.setAcceName(accession.getAccessionNumber());

		if (accession.getAccessionId().getAliases() != null) {
			g.setSynonyms(accession.getAccessionId().getAliases().stream().map(alias -> alias.getName()).collect(Collectors.toList()));
		}

		g.setPedigree(accession.getAncest());
		if (accession.getCrop() != null)
			g.setCommonCropName(accession.getCrop().getShortName());
		else
			g.setCommonCropName(accession.getCropName());

		g.setInstCode(accession.getInstitute().getCode());
		g.setInstName(accession.getInstitute().getFullName());

		g.setSampStat(accession.getSampStat());
		if (accession.getCountryOfOrigin() != null) {
			g.setOrigCty(accession.getCountryOfOrigin().getCode3());
		}
		g.setStorage(accession.getAccessionId().getStorage());

		Taxonomy2 taxonomy = accession.getTaxonomy();
		g.setGenus(taxonomy.getGenus());
		g.setSpecies(taxonomy.getSpecies());
		g.setSpAuthor(taxonomy.getSpAuthor());
		g.setSubtaxa(taxonomy.getSubtaxa());
		g.setSubtAuthor(taxonomy.getSubtAuthor());

		g.setAcqDate(accession.getAcquisitionDate());
		return g;
	}

	@Override
	public Page<String> getCrops(Pageable pageable) {
		Page<Crop> crops = cropService.list(pageable);
		return new PageImpl<>(crops.getContent().stream().map(c -> c.getShortName()).collect(Collectors.toList()), pageable, crops.getTotalElements());
	}

	@Override
	public Page<Germplasm> searchGermplasm(String germplasmName, String germplasmPUI, UUID germplasmDbId, Pageable page) throws SearchException {
		AccessionFilter accessionFilter = new AccessionFilter();

		if (StringUtils.isNotBlank(germplasmPUI)) {
			accessionFilter.doi().add(germplasmPUI);
		}
		
		if (germplasmDbId != null) {
			accessionFilter.uuid(Set.of(germplasmDbId));
		}

		if (StringUtils.isNotBlank(germplasmName)) {
			accessionFilter.accessionNumbers().add(germplasmName);
		}
		

		Page<Accession> accns = accessionService.list(accessionFilter, page);
		return new PageImpl<>(accns.getContent().stream().map(a -> toGermplasm(a)).collect(Collectors.toList()), page, accns.getTotalElements());
	}

	@Override
	public Page<Germplasm> searchGermplasm(AccessionFilter filter, Pageable page) throws SearchException {
		Page<Accession> accns = accessionService.list(filter, page);
		return new PageImpl<>(accns.getContent().stream().map(a -> toGermplasm(a)).collect(Collectors.toList()), page, accns.getTotalElements());
	}
}