Project

General

Profile

Revision 7dab8082

ID7dab80823df41f0e8f650e800be7c02ca83a634d
Parent 1cfc76cb
Child 08bbdc46

Added by Patrick Plitzner over 1 year ago

ref #6484 Adjust count method to find method to return the same number

  • performance can still be improved by integrating AssignmentStatus and/or indirectlyAssociatedSpecimen into the HQL query

View differences:

cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/OccurrenceServiceImpl.java
1457 1457
            if(occurrenceConfig.getAssociatedTaxonNameUuid()!=null){
1458 1458
                taxonName = nameService.load(occurrenceConfig.getAssociatedTaxonNameUuid());
1459 1459
            }
1460
            /*TODO: #6484 Neither isRetrieveIndirectlyAssociatedSpecimens() nor the AssignmentStatus
1461
             * is currently reflected in the HQL query. So using these in the count method will
1462
             * significantly slow down this method as we have to retreive the entities instead of
1463
             * the just the amount.
1464
             */
1465
            if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens() || !occurrenceConfig.getAssignmentStatus().equals(AssignmentStatus.ALL_SPECIMENS)){
1466
                List<SpecimenOrObservationBase> occurrences = new ArrayList<>();
1467
                occurrences.addAll(dao.findOccurrences(occurrenceConfig.getClazz(),
1468
                        occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
1469
                        occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
1470
                        occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths()));
1471
                occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
1472
                return occurrences.size();
1473
            }
1474

  
1460 1475
            return dao.countOccurrences(occurrenceConfig.getClazz(),
1461 1476
                    occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
1462 1477
                    occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
......
1465 1480
        else{
1466 1481
            return dao.countByTitle(config.getTitleSearchString());
1467 1482
        }
1468

  
1469 1483
    }
1470 1484

  
1471 1485
    @Override
......
1489 1503
                    occurrenceConfig.getTitleSearchString(), occurrenceConfig.getSignificantIdentifier(),
1490 1504
                    occurrenceConfig.getSpecimenType(), taxon, taxonName, occurrenceConfig.getMatchMode(), null, null,
1491 1505
                    occurrenceConfig.getOrderHints(), occurrenceConfig.getPropertyPaths()));
1492
            //filter out (un-)assigned specimens
1493
            if(taxon==null && taxonName==null){
1494
                AssignmentStatus assignmentStatus = occurrenceConfig.getAssignmentStatus();
1495
                List<SpecimenOrObservationBase<?>> specimenWithAssociations = new ArrayList<>();
1496
                if(!assignmentStatus.equals(AssignmentStatus.ALL_SPECIMENS)){
1497
                    for (SpecimenOrObservationBase specimenOrObservationBase : occurrences) {
1498
                        Collection<TaxonBase<?>> associatedTaxa = listAssociatedTaxa(specimenOrObservationBase, null, null, null, null);
1499
                        if(!associatedTaxa.isEmpty()){
1500
                            specimenWithAssociations.add(specimenOrObservationBase);
1501
                        }
1506
            occurrences = filterOccurencesByAssignmentAndHierarchy(occurrenceConfig, occurrences, taxon, taxonName);
1507

  
1508
            return new DefaultPagerImpl<SpecimenOrObservationBase>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
1509
        }
1510
        return super.findByTitle(config);
1511
    }
1512

  
1513
    private List<SpecimenOrObservationBase> filterOccurencesByAssignmentAndHierarchy(
1514
            FindOccurrencesConfigurator occurrenceConfig, List<SpecimenOrObservationBase> occurrences, Taxon taxon,
1515
            TaxonName taxonName) {
1516
        //filter out (un-)assigned specimens
1517
        if(taxon==null && taxonName==null){
1518
            AssignmentStatus assignmentStatus = occurrenceConfig.getAssignmentStatus();
1519
            List<SpecimenOrObservationBase<?>> specimenWithAssociations = new ArrayList<>();
1520
            if(!assignmentStatus.equals(AssignmentStatus.ALL_SPECIMENS)){
1521
                for (SpecimenOrObservationBase specimenOrObservationBase : occurrences) {
1522
                    Collection<TaxonBase<?>> associatedTaxa = listAssociatedTaxa(specimenOrObservationBase, null, null, null, null);
1523
                    if(!associatedTaxa.isEmpty()){
1524
                        specimenWithAssociations.add(specimenOrObservationBase);
1502 1525
                    }
1503 1526
                }
1504
                if(assignmentStatus.equals(AssignmentStatus.UNASSIGNED_SPECIMENS)){
1505
                    occurrences.removeAll(specimenWithAssociations);
1506
                }
1507
                if(assignmentStatus.equals(AssignmentStatus.ASSIGNED_SPECIMENS)){
1508
                    occurrences = new ArrayList<>(specimenWithAssociations);
1509
                }
1510 1527
            }
1511
            // indirectly associated specimens
1512
            if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens()){
1513
                List<SpecimenOrObservationBase> indirectlyAssociatedOccurrences = new ArrayList<>(occurrences);
1514
                for (SpecimenOrObservationBase specimen : occurrences) {
1515
                    List<SpecimenOrObservationBase<?>> allHierarchyDerivates = getAllHierarchyDerivatives(specimen);
1516
                    for (SpecimenOrObservationBase<?> specimenOrObservationBase : allHierarchyDerivates) {
1517
                        if(!occurrences.contains(specimenOrObservationBase)){
1518
                            indirectlyAssociatedOccurrences.add(specimenOrObservationBase);
1519
                        }
1528
            if(assignmentStatus.equals(AssignmentStatus.UNASSIGNED_SPECIMENS)){
1529
                occurrences.removeAll(specimenWithAssociations);
1530
            }
1531
            if(assignmentStatus.equals(AssignmentStatus.ASSIGNED_SPECIMENS)){
1532
                occurrences = new ArrayList<>(specimenWithAssociations);
1533
            }
1534
        }
1535
        // indirectly associated specimens
1536
        if(occurrenceConfig.isRetrieveIndirectlyAssociatedSpecimens()){
1537
            List<SpecimenOrObservationBase> indirectlyAssociatedOccurrences = new ArrayList<>(occurrences);
1538
            for (SpecimenOrObservationBase specimen : occurrences) {
1539
                List<SpecimenOrObservationBase<?>> allHierarchyDerivates = getAllHierarchyDerivatives(specimen);
1540
                for (SpecimenOrObservationBase<?> specimenOrObservationBase : allHierarchyDerivates) {
1541
                    if(!occurrences.contains(specimenOrObservationBase)){
1542
                        indirectlyAssociatedOccurrences.add(specimenOrObservationBase);
1520 1543
                    }
1521 1544
                }
1522
                occurrences = indirectlyAssociatedOccurrences;
1523 1545
            }
1524

  
1525
            return new DefaultPagerImpl<SpecimenOrObservationBase>(config.getPageNumber(), occurrences.size(), config.getPageSize(), occurrences);
1546
            occurrences = indirectlyAssociatedOccurrences;
1526 1547
        }
1527
        return super.findByTitle(config);
1548
        return occurrences;
1528 1549
    }
1529 1550

  
1530 1551
    @Override
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/OccurrenceServiceTest.java
479 479
        // "TaxonName_TypeDesignationBase",
480 480
        // "HomotypicalGroup"}, "testListAssociatedAndTypedTaxa");
481 481
        // } catch (FileNotFoundException e) {
482
        // // TODO Auto-generated catch block
483 482
        // e.printStackTrace();
484 483
        // }
485 484
        //
......
953 952
//                  "TeamOrPersonBase",
954 953
//                  "HomotypicalGroup"}, "testListAssociatedTaxaAndListByAssociatedTaxon");
955 954
//      } catch (FileNotFoundException e) {
956
//          // TODO Auto-generated catch block
957 955
//          e.printStackTrace();
958 956
//      }
959 957
        //check initial state
......
1177 1175
        config.setClazz(DerivedUnit.class);
1178 1176
        config.setAssociatedTaxonUuid(taxon.getUuid());
1179 1177
        config.setRetrieveIndirectlyAssociatedSpecimens(true);
1180
        /* TODO issue #6484: the parameters FindOccurrencesConfigurator.getAssignmentStatus()
1181
        * and FindOccurrencesConfigurator.isRetrieveIndirectlyAssociatedSpecimens() are not evaluated
1182
        * in the count method
1183
        *
1184
        */
1185
//        assertEquals(3, occurrenceService.countOccurrences(config));
1178
        assertEquals(3, occurrenceService.countOccurrences(config));
1186 1179
        List<SpecimenOrObservationBase> indirectlyAssociatedSpecimens = occurrenceService.findByTitle(config)
1187 1180
                .getRecords();
1188 1181
        assertEquals(3, indirectlyAssociatedSpecimens.size());
......
1204 1197
        //all specimen
1205 1198
        config = new FindOccurrencesConfigurator();
1206 1199
        config.setAssignmentStatus(AssignmentStatus.ALL_SPECIMENS);
1207
        /* TODO issue #6484: the parameters FindOccurrencesConfigurator.getAssignmentStatus()
1208
        * and FindOccurrencesConfigurator.isRetrieveIndirectlyAssociatedSpecimens() are not evaluated
1209
        * in the count method
1210
        */
1211
//        assertEquals(4, occurrenceService.countOccurrences(config));
1200
        assertEquals(4, occurrenceService.countOccurrences(config));
1212 1201
        List<SpecimenOrObservationBase> allSpecimens = occurrenceService.findByTitle(config).getRecords();
1213 1202
        assertEquals(4, allSpecimens.size());
1214 1203
        assertTrue(allSpecimens.contains(derivedUnit1));
......
1219 1208
        //assigned specimen
1220 1209
        config = new FindOccurrencesConfigurator();
1221 1210
        config.setAssignmentStatus(AssignmentStatus.ASSIGNED_SPECIMENS);
1222
        /* TODO issue #6484: the parameters FindOccurrencesConfigurator.getAssignmentStatus()
1223
        * and FindOccurrencesConfigurator.isRetrieveIndirectlyAssociatedSpecimens() are not evaluated
1224
        * in the count method
1225
        */
1226
//        assertEquals(2, occurrenceService.countOccurrences(config));
1211
        assertEquals(2, occurrenceService.countOccurrences(config));
1227 1212
        List<SpecimenOrObservationBase> assignedSpecimens = occurrenceService.findByTitle(config).getRecords();
1228 1213
        assertEquals(2, assignedSpecimens.size());
1229 1214
        assertTrue(assignedSpecimens.contains(derivedUnit1));
......
1232 1217
        //unassigned specimen
1233 1218
        config = new FindOccurrencesConfigurator();
1234 1219
        config.setAssignmentStatus(AssignmentStatus.UNASSIGNED_SPECIMENS);
1235
        /* TODO issue #6484: the parameters FindOccurrencesConfigurator.getAssignmentStatus()
1236
        * and FindOccurrencesConfigurator.isRetrieveIndirectlyAssociatedSpecimens() are not evaluated
1237
        * in the count method
1238
        */
1239
//        assertEquals(2, occurrenceService.countOccurrences(config));
1220
        assertEquals(2, occurrenceService.countOccurrences(config));
1240 1221
        List<SpecimenOrObservationBase> unAssignedSpecimens = occurrenceService.findByTitle(config).getRecords();
1241 1222
        assertEquals(2, unAssignedSpecimens.size());
1242 1223
        assertTrue(unAssignedSpecimens.contains(derivedUnit2));

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)