From 1127a9536a4af69a6581403c7414c757bdae7105 Mon Sep 17 00:00:00 2001 From: Katja Date: Fri, 19 Jan 2024 09:43:25 +0100 Subject: [PATCH] ref #10456: order of occurrences --- .../api/service/OccurrenceServiceImpl.java | 43 ++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/OccurrenceServiceImpl.java b/cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/OccurrenceServiceImpl.java index 534042dc15..0c61b8928d 100644 --- a/cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/OccurrenceServiceImpl.java +++ b/cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/OccurrenceServiceImpl.java @@ -343,7 +343,7 @@ public class OccurrenceServiceImpl // gather the IDs of all relevant root units Set rootUnitUuids = new HashSet<>(); List records = listByAssociatedTaxon(null, includeRelationships, - associatedTaxon, includeUnpublished, maxDepth, null, null, orderHints, propertyPaths); + associatedTaxon, includeUnpublished, maxDepth, null, null, null, propertyPaths); for (SpecimenOrObservationBase specimen : records) { for (SpecimenOrObservationBase rootUnit : findRootUnits(specimen.getUuid(), null)) { if(type == null || type.isAssignableFrom(rootUnit.getClass())) { @@ -353,12 +353,43 @@ public class OccurrenceServiceImpl } long totalCount = rootUnitUuids.size(); //dao.list() does the paging of the field units. Passing the field units directly to the Pager would not work - List rootUnits = dao.list(rootUnitUuids, pageSize, pageNumber, orderHints, propertyPaths); - List castedUnits = new ArrayList<>(rootUnits.size()); - for(SpecimenOrObservationBase sob : rootUnits) { - // this cast should be save since the uuids have been filtered by type above - castedUnits.add((T)sob); + List castedUnits = new ArrayList<>(rootUnitUuids.size()); + try { + List rootUnits = dao.list(rootUnitUuids, pageSize, pageNumber, orderHints, propertyPaths); + castedUnits = new ArrayList<>(rootUnits.size()); + for(SpecimenOrObservationBase sob : rootUnits) { + // this cast should be save since the uuids have been filtered by type above + castedUnits.add((T)sob); + } + }catch(Exception e) { + e.printStackTrace(); } + + Collections.sort(castedUnits, new Comparator() { + + @Override + public int compare(SpecimenOrObservationBase o1, SpecimenOrObservationBase o2) { + if(o1 instanceof FieldUnit && o2 instanceof FieldUnit) { + FieldUnit fu1 = (FieldUnit)o1; + FieldUnit fu2 = (FieldUnit)o2; + + return PartialComparator.INSTANCE().compare(fu1.getGatheringEvent().getGatheringDate(), fu2.getGatheringEvent().getGatheringDate()); + } + if(o1 instanceof DerivedUnit && o2 instanceof DerivedUnit) { + SpecimenOrObservationBase du1 = o1; + SpecimenOrObservationBase du2 = o2; + return StringUtils.compare(du1.getTitleCache(), du2.getTitleCache()); + } + if(o1 instanceof FieldUnit && o2 instanceof DerivedUnit) { + return -1; + } else { + return 1; + } + } + + }); + + return new DefaultPagerImpl<>(pageNumber, totalCount, pageSize, castedUnits); } -- 2.34.1