use of eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO in project cdmlib by cybertaxonomy.
the class OccurrenceServiceImpl method listRootUnitDTOsByAssociatedTaxon.
@Override
@Transactional
public List<SpecimenOrObservationBaseDTO> listRootUnitDTOsByAssociatedTaxon(Set<TaxonRelationshipEdge> includedRelationships, UUID associatedTaxonUuid, List<String> propertyPaths) {
Set<Taxon> taxa = new HashSet<>();
Set<SpecimenOrObservationBaseDTO> rootUnitDTOs = new HashSet<>();
boolean includeUnpublished = INCLUDE_UNPUBLISHED;
Taxon associatedTaxon = (Taxon) taxonService.load(associatedTaxonUuid);
if (includedRelationships != null) {
taxa = taxonService.listRelatedTaxa(associatedTaxon, includedRelationships, null, includeUnpublished, null, null, null);
}
taxa.add(associatedTaxon);
HashMap<UUID, SpecimenOrObservationBaseDTO> alreadyCollectedUnits = new HashMap<>();
for (Taxon taxon : taxa) {
// TODO there might be a good potential to speed up the whole processing by collecting all entities first
// and to create the DTOs in a second step
Set<SpecimenOrObservationBase> perTaxonOccurrences = dao.listByAssociatedTaxon(null, taxon, null, null, null, propertyPaths).stream().map(u -> HibernateProxyHelper.deproxy(u, SpecimenOrObservationBase.class)).collect(Collectors.toSet());
for (SpecimenOrObservationBase<?> unit : perTaxonOccurrences) {
unit = HibernateProxyHelper.deproxy(unit);
if (unit instanceof DerivedUnit) {
DerivedUnitDTO derivativeDTO;
if (!alreadyCollectedUnits.containsKey(unit.getUuid())) {
DerivedUnit derivedUnit = (DerivedUnit) unit;
derivativeDTO = (DerivedUnitDTO) SpecimenOrObservationDTOFactory.fromEntity(derivedUnit, null);
if (unit instanceof DnaSample) {
derivativeDTO = DNASampleDTO.fromEntity((DnaSample) unit);
} else {
derivativeDTO = DerivedUnitDTO.fromEntity(derivedUnit, null, null);
}
alreadyCollectedUnits.put(derivativeDTO.getUuid(), derivativeDTO);
derivativeDTO.addAllDerivatives(getDerivedUnitDTOsFor(derivativeDTO, derivedUnit, alreadyCollectedUnits));
}
derivativeDTO = (DerivedUnitDTO) alreadyCollectedUnits.get(unit.getUuid());
rootUnitDTOs.addAll(findRootUnitDTOs(derivativeDTO, alreadyCollectedUnits));
} else {
// only other option is FieldUnit
rootUnitDTOs.add(FieldUnitDTO.fromEntity((FieldUnit) unit, 0, null));
}
}
}
List<SpecimenOrObservationBaseDTO> orderdDTOs = new ArrayList<>(rootUnitDTOs);
// TODO order dtos by date can only be done by string comparison
// the FieldUnitDTO.date needs to be a Partial object for sensible ordering
Collections.sort(orderdDTOs, new Comparator<SpecimenOrObservationBaseDTO>() {
@Override
public int compare(SpecimenOrObservationBaseDTO o1, SpecimenOrObservationBaseDTO o2) {
if (o1 instanceof FieldUnitDTO && o2 instanceof FieldUnitDTO) {
FieldUnitDTO fu1 = (FieldUnitDTO) o1;
FieldUnitDTO fu2 = (FieldUnitDTO) o2;
// PartialComparator.INSTANCE_NULL_SMALLEST() here
return PartialComparator.INSTANCE().compare(fu1.getDate(), fu2.getDate());
}
if (o1 instanceof DerivedUnitDTO && o2 instanceof DerivedUnitDTO) {
SpecimenOrObservationBaseDTO du1 = o1;
SpecimenOrObservationBaseDTO du2 = o2;
return StringUtils.compare(du1.getLabel(), du2.getLabel());
}
if (o1 instanceof FieldUnitDTO && o2 instanceof DerivedUnitDTO) {
return -1;
} else {
return 1;
}
}
});
return orderdDTOs;
}
use of eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO in project cdmlib by cybertaxonomy.
the class OccurrenceServiceImpl method findRootUnitDTOs.
@Override
public Collection<SpecimenOrObservationBaseDTO> findRootUnitDTOs(UUID unitUUID) {
SpecimenOrObservationBase<?> entity = load(unitUUID);
SpecimenOrObservationBaseDTO derivedUnitDTO = SpecimenOrObservationDTOFactory.fromEntity(entity);
Collection<SpecimenOrObservationBaseDTO> rootUnitDTOs = new ArrayList<>();
if (derivedUnitDTO != null) {
if (derivedUnitDTO instanceof FieldUnitDTO) {
rootUnitDTOs.add(derivedUnitDTO);
} else {
Map<UUID, SpecimenOrObservationBaseDTO> alreadyCollectedSpecimen = new HashMap<>();
rootUnitDTOs = findRootUnitDTOs((DerivedUnitDTO) derivedUnitDTO, alreadyCollectedSpecimen);
}
}
return rootUnitDTOs;
}
use of eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO in project cdmlib by cybertaxonomy.
the class OccurrenceListController method doGetByGeneticAccessionNumber.
@RequestMapping(method = RequestMethod.GET, value = "byGeneticAccessionNumber")
public SpecimenOrObservationBaseDTO doGetByGeneticAccessionNumber(@RequestParam(value = "accessionNumber", required = true) String accessionNumber, HttpServletRequest request, HttpServletResponse response) throws IOException {
logger.info("doGetByGeneticAccessionNumber() - " + requestPathAndQuery(request));
SpecimenOrObservationBaseDTO sobDto = service.findByGeneticAccessionNumber(accessionNumber, null);
if (sobDto == null) {
response.setHeader("Failure", "No DNA available for accession number ");
HttpStatusMessage.create("No DNA available for accession number " + accessionNumber, 400).send(response);
return null;
}
return sobDto;
}
use of eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO in project cdmlib by cybertaxonomy.
the class OccurrenceServiceImpl method originalDTOs.
/**
* @param originalDTO
* @return
*/
private Set<SpecimenOrObservationBaseDTO> originalDTOs(UUID derivativeUuid) {
Set<SpecimenOrObservationBaseDTO> dtos = new HashSet<>();
List<SpecimenOrObservationBase> specimens = dao.findOriginalsForDerivedUnit(derivativeUuid, null);
for (SpecimenOrObservationBase sob : specimens) {
if (sob instanceof FieldUnit) {
dtos.add(FieldUnitDTO.fromEntity((FieldUnit) sob));
} else {
dtos.add(DerivedUnitDTO.fromEntity((DerivedUnit) sob));
}
}
return dtos;
}
use of eu.etaxonomy.cdm.api.service.dto.SpecimenOrObservationBaseDTO in project cdmlib by cybertaxonomy.
the class OccurrenceServiceImpl method loadFieldUnitDTO.
@Override
@Transactional(readOnly = true)
public FieldUnitDTO loadFieldUnitDTO(UUID derivedUnitUuid) {
FieldUnitDTO fieldUnitDTO = null;
DerivedUnitDTO derivedUnitDTO = null;
Map<UUID, SpecimenOrObservationBaseDTO> cycleDetectionMap = new HashMap<>();
SpecimenOrObservationBase<?> derivative = dao.load(derivedUnitUuid);
if (derivative != null) {
if (derivative instanceof FieldUnit) {
fieldUnitDTO = FieldUnitDTO.fromEntity((FieldUnit) derivative);
return fieldUnitDTO;
} else {
// must be a DerivedUnit otherwise
derivedUnitDTO = DerivedUnitDTO.fromEntity((DerivedUnit) derivative);
while (true) {
Set<SpecimenOrObservationBaseDTO> originals = originalDTOs(derivedUnitDTO.getUuid());
if (originals.isEmpty()) {
break;
}
if (originals.size() > 1) {
logger.debug("The derived unit with uuid " + derivedUnitUuid + "has more than one orginal, ignoring all but the first one.");
}
SpecimenOrObservationBaseDTO originalDTO = originals.iterator().next();
// cycle detection and handling
if (cycleDetectionMap.containsKey(originalDTO.getUuid())) {
// cycle detected!!!
try {
throw new Exception();
} catch (Exception e) {
logger.error("Cycle in derivate graph detected at DerivedUnit with uuid=" + originalDTO.getUuid(), e);
}
// but let the derivate to be added to the original which is closer to the FieldUnit (below at originalDTO.addDerivate(derivedUnitDTO);)
for (SpecimenOrObservationBaseDTO seenOriginal : cycleDetectionMap.values()) {
for (SpecimenOrObservationBaseDTO derivateDTO : seenOriginal.getDerivatives()) {
if (derivateDTO.equals(originalDTO)) {
seenOriginal.getDerivatives().remove(originalDTO);
}
}
}
} else {
cycleDetectionMap.put(originalDTO.getUuid(), originalDTO);
}
if (originalDTO instanceof FieldUnitDTO) {
fieldUnitDTO = (FieldUnitDTO) originalDTO;
break;
} else {
// So this must be a DerivedUnitDTO
if (derivedUnitDTO == null) {
derivedUnitDTO = (DerivedUnitDTO) originalDTO;
} else {
derivedUnitDTO = (DerivedUnitDTO) originalDTO;
}
}
}
}
}
return fieldUnitDTO;
}
Aggregations