use of eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO 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.FieldUnitDTO in project cdmlib by cybertaxonomy.
the class OccurrenceServiceImpl method assembleFieldUnitDTO.
@Override
public FieldUnitDTO assembleFieldUnitDTO(FieldUnit fieldUnit) {
if (!getSession().contains(fieldUnit)) {
fieldUnit = (FieldUnit) load(fieldUnit.getUuid());
}
// FIXME the filter for SpecimenOrObservationType.PreservedSpecimen has been preserved from the former implementation (see commit 07e3f63c7d and older)
// it is questionable if this filter makes sense for all use cases or if it is only a sensible default for the
// compressed specimen table in the cdm-dataportal (see #6816, #6870)
EnumSet<SpecimenOrObservationType> typeIncludeFilter = EnumSet.of(SpecimenOrObservationType.PreservedSpecimen);
FieldUnitDTO fieldUnitDTO = FieldUnitDTO.fromEntity(fieldUnit, null, typeIncludeFilter);
return fieldUnitDTO;
}
use of eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO 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.FieldUnitDTO 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;
}
use of eu.etaxonomy.cdm.api.service.dto.FieldUnitDTO in project cdmlib by cybertaxonomy.
the class OccurrenceServiceImpl method _findRootUnitDTOs.
/**
* Method for recursive calls, must only be used by {@link #findRootUnitDTOs(DerivedUnitDTO, HashMap)}
* <p>
* It will search recursively over all {@link DerivationEvent}s and get the "originals" ({@link SpecimenOrObservationBase})
* from which this DerivedUnit was derived until all FieldUnits are found.
*/
private void _findRootUnitDTOs(DerivedUnitDTO derivedUnitDTO, Map<UUID, SpecimenOrObservationBaseDTO> rootUnitDTOs, Map<UUID, SpecimenOrObservationBaseDTO> alreadyCollectedSpecimen) {
List<String> propertyPaths = new ArrayList<>();
// add the supplied DTO the the alreadyCollectedSpecimen if not yet there
if (!alreadyCollectedSpecimen.containsKey(derivedUnitDTO.getUuid())) {
alreadyCollectedSpecimen.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
}
List<SpecimenOrObservationBase> originals = dao.findOriginalsForDerivedUnit(derivedUnitDTO.getUuid(), propertyPaths);
if (originals.size() > 0) {
if (originals.size() > 1) {
logger.warn("The derived unit with uuid " + derivedUnitDTO.getUuid() + "has more than one orginal");
}
// FIXME allow handling multiple originals
SpecimenOrObservationBase<?> original = originals.get(0);
original = HibernateProxyHelper.deproxy(original);
if (alreadyCollectedSpecimen.containsKey(original.getUuid())) {
alreadyCollectedSpecimen.get(original.getUuid()).addDerivative(derivedUnitDTO);
// if ( alreadyCollectedSpecimen.get(specimen.getUuid()) instanceof FieldUnitDTO){
// ((FieldUnitDTO)alreadyCollectedSpecimen.get(specimen.getUuid())).getTaxonRelatedDerivedUnits().add(derivedUnitDTO.getUuid());
// }
} else {
if (!rootUnitDTOs.containsKey(original.getUuid())) {
// the direct derivatives of the field unit are added in the factory method, so it is guaranteed that
// the derivedUnitDTO is already contained.
// ----
// Don't assemble derivatives for originals since we have them collected already
// when ascending to the originals, we only want to collect those derivatives which are on the path up to the root
final Integer maxDepth = 0;
SpecimenOrObservationBaseDTO originalDTO = SpecimenOrObservationDTOFactory.fromEntity(original, maxDepth);
originalDTO.addDerivative(derivedUnitDTO);
alreadyCollectedSpecimen.put(originalDTO.getUuid(), originalDTO);
if (original instanceof FieldUnit) {
rootUnitDTOs.put(originalDTO.getUuid(), originalDTO);
} else {
_findRootUnitDTOs((DerivedUnitDTO) originalDTO, rootUnitDTOs, alreadyCollectedSpecimen);
}
} else {
SpecimenOrObservationBaseDTO previouslyFoundRootUnit = rootUnitDTOs.get(original.getUuid());
if (!previouslyFoundRootUnit.getDerivatives().stream().anyMatch(uDTO -> uDTO.getUuid().equals(derivedUnitDTO.getUuid()))) {
previouslyFoundRootUnit.addDerivative(derivedUnitDTO);
}
}
}
} else {
rootUnitDTOs.put(derivedUnitDTO.getUuid(), derivedUnitDTO);
}
}
Aggregations