BrAPIServiceImpl.java
/*
* Copyright 2020 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.service.impl;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Accession;
import org.gringlobal.model.Crop;
import org.gringlobal.model.brapi.Germplasm;
import org.gringlobal.service.AccessionService;
import org.gringlobal.service.BrAPIService;
import org.gringlobal.service.CropService;
import org.gringlobal.service.filter.AccessionFilter;
import org.gringlobal.worker.AccessionMCPDConverter;
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;
/**
* BrAPI v1 implementation
*/
@Service
@Transactional(readOnly = true)
public class BrAPIServiceImpl implements BrAPIService {
@Autowired
private AccessionService accessionService;
@Autowired
private CropService cropService;
@Autowired
private AccessionMCPDConverter accessionMCPDConverter;
@Override
public Page<String> getCrops(Pageable pageable) {
Page<Crop> crops = cropService.listCrops(pageable);
return new PageImpl<>(crops.getContent().stream().map(Crop::getName).collect(Collectors.toList()), pageable, crops.getTotalElements());
}
@Override
public Germplasm getGermplasmById(long germplasmId) {
return toGermplasm(accessionService.get(germplasmId));
}
@Override
public Page<Germplasm> searchGermplasm(String germplasmName, String germplasmPUI, Long germplasmDbId, Pageable page) throws SearchException {
AccessionFilter accessionFilter = new AccessionFilter();
if (StringUtils.isNotBlank(germplasmPUI)) {
accessionFilter.doi().add(germplasmPUI);
}
if (germplasmDbId != null) {
accessionFilter.id().add(germplasmDbId);
}
if (StringUtils.isNotBlank(germplasmName)) {
accessionFilter.accessionNumbers().add(germplasmName);
}
Page<Accession> accessions = accessionService.list(accessionFilter, page);
return new PageImpl<>(accessions.getContent().stream().map(this::toGermplasm).collect(Collectors.toList()), page, accessions.getTotalElements());
}
@Override
public Page<Germplasm> searchGermplasm(AccessionFilter filter, Pageable page) throws SearchException {
Page<Accession> accessions = accessionService.list(filter, page);
return new PageImpl<>(accessions.getContent().stream().map(this::toGermplasm).collect(Collectors.toList()), page, accessions.getTotalElements());
}
/**
* Convert Accession to Germplasm
*/
// TODO Add details
private Germplasm toGermplasm(Accession accession) {
var mcpd = accessionMCPDConverter.convert(accession);
Germplasm g = new Germplasm();
g.setId(Long.toString(accession.getId()));
g.setGermplasmPUI(mcpd.puid);
g.setAcceNumb(mcpd.acceNumb);
g.setAcceName(mcpd.acceName);
g.setDefaultDisplayName(StringUtils.defaultIfBlank(mcpd.acceName, mcpd.acceNumb));
if (StringUtils.isNotBlank(mcpd.otherNumb)) {
g.setSynonyms(List.of(mcpd.otherNumb.split(";")));
}
g.setInstCode(mcpd.instCode);
g.setInstName(accession.getSite().getSiteLongName());
g.setGenus(mcpd.genus);
g.setSpecies(mcpd.species);
g.setSpAuthor(mcpd.spAuthor);
g.setSubtaxa(mcpd.subtaxa);
g.setSubtAuthor(mcpd.subtAuthor);
g.setAcqDate(mcpd.acqDate);
g.setPedigree(mcpd.ancest);
g.setCommonCropName(mcpd.cropName);
g.setSampStat(mcpd.sampStat);
g.setOrigCty(mcpd.origCty);
// g.setStorage(mcpd.storage);
return g;
}
}