Project

General

Profile

Revision c938072f

IDc938072f8578b90efb67d2d336735f1c6c6dcc39
Parent fa9ef8c4
Child 2e91751e

Added by Andreas Müller 11 months ago

ref #1447 fix findIdenticalName function

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/taxon/TaxonDaoHibernateImpl.java
1379 1379
        return taxonNames;
1380 1380
    }
1381 1381

  
1382

  
1383

  
1384
    /**
1385
     * Returns a map of nameCaches and names available for sec1 and sec2
1386
     * this is used for the merging of two instances into one
1387
     *
1388
     * @param secRef1
1389
     * @param secRef2
1390
     * return map
1391
     *
1392
     */
1393 1382
    @Override
1394 1383
    public Map<String, Map<UUID,Set<TaxonName>>> findIdenticalNamesNew(List<UUID> sourceRefUuids, List<String> propertyPaths){
1384
        Set<String> nameCacheCandidates = new HashSet<>();
1385
        try {
1386
            for (int i = 0; i<sourceRefUuids.size()-1;i++){
1387
                UUID sourceUuid1 = sourceRefUuids.get(i);
1388
                for (int j = i+1; j<sourceRefUuids.size();j++){
1389
                    UUID sourceUuid2 = sourceRefUuids.get(j);
1390
                    if (sourceUuid1.equals(sourceUuid2)){
1391
                        continue;  //just in case we have duplicates in the list
1392
                    }
1395 1393

  
1396
        Query query=getSession().createQuery(
1397
                  " SELECT DISTINCT n1.nameCache "
1398
                + " FROM TaxonName n1 JOIN n1.sources s1 JOIN s1.citation ref1 "
1399
                + " WHERE ref1.uuid IN (:sourceUuids ) AND  EXISTS ("
1400
                + "         FROM TaxonName n2 JOIN n2.sources s2 JOIN s2.citation ref2 "
1401
                + "         WHERE ref2.uuid IN (:sourceUuids ) AND ref1.uuid <> ref2.uuid "
1402
                + " ORDER BY n1.nameCache ");
1403
        query.setParameterList("sourceUuids", sourceRefUuids);
1404

  
1405
        @SuppressWarnings("unchecked")
1406
        List<String> nameCacheCandidates = query.list();
1407

  
1408
        Map<UUID, List<TaxonName>> duplicates = new HashMap<>();
1409
        for (UUID sourceUuid : sourceRefUuids){
1410
            query=getSession().createQuery("SELECT n "
1411
                    + " FROM TaxonName n JOIN n.sources s JOIN s.citation ref "
1412
                    + " WHERE ref.uuid = :sourceUuid AND n.nameCache IN (:nameCacheCandidates) "
1413
                    + " ORDER BY n.nameCache");
1414
            query.setParameter("sourceUuid", sourceUuid);
1415
            query.setParameterList("nameCacheCandidates", nameCacheCandidates);
1416
            @SuppressWarnings("unchecked")
1417
            List<TaxonName> sourceDuplicates = query.list();
1418
            defaultBeanInitializer.initializeAll(sourceDuplicates, propertyPaths);
1394
                    Query query = getSession().createQuery(
1395
                            " SELECT DISTINCT n1.nameCache "
1396
                          + " FROM TaxonName n1 JOIN n1.sources s1 JOIN s1.citation ref1 "
1397
                          +         " , TaxonName n2 JOIN n2.sources s2 JOIN s2.citation ref2 "
1398
                          + " WHERE  ref1.uuid = (:sourceUuid1) "
1399
                          + "       AND n1.id <> n2.id "
1400
                          + "       AND ref2.uuid IN (:sourceUuid2)"
1401
                          + "       AND ref1.uuid <> ref2.uuid "
1402
                          + "       AND n1.nameCache = n2.nameCache) "
1403
                          + " ORDER BY n1.nameCache ");
1404
                    query.setParameter("sourceUuid1", sourceUuid1);
1405
                    query.setParameter("sourceUuid2", sourceUuid2);
1406

  
1407
                    @SuppressWarnings("unchecked")
1408
                    List<String> queryNameCacheCandidates = query.list();
1409
                    nameCacheCandidates.addAll(queryNameCacheCandidates);
1410
                }
1411
            }
1419 1412

  
1420
            duplicates.put(sourceUuid, sourceDuplicates);
1421
        }
1413
            Map<UUID, List<TaxonName>> duplicates = new HashMap<>();
1414
            for (UUID sourceUuid : sourceRefUuids){
1415
                Query query=getSession().createQuery("SELECT n "
1416
                        + " FROM TaxonName n JOIN n.sources s JOIN s.citation ref "
1417
                        + " WHERE ref.uuid = :sourceUuid AND n.nameCache IN (:nameCacheCandidates) "
1418
                        + " ORDER BY n.nameCache");
1419
                query.setParameter("sourceUuid", sourceUuid);
1420
                query.setParameterList("nameCacheCandidates", nameCacheCandidates);
1421
                @SuppressWarnings("unchecked")
1422
                List<TaxonName> sourceDuplicates = query.list();
1423
                defaultBeanInitializer.initializeAll(sourceDuplicates, propertyPaths);
1424

  
1425
                 duplicates.put(sourceUuid, sourceDuplicates);
1426
            }
1422 1427

  
1423
        Map<String, Map<UUID,Set<TaxonName>>> result = new HashMap<>();
1424
        for (String nameCache: nameCacheCandidates) {
1425
            Map<UUID,Set<TaxonName>> uuidNameMap = new HashMap<>();
1426
            result.put(nameCache, uuidNameMap);
1427
            for(UUID sourceUuid: duplicates.keySet()){
1428
                Set<TaxonName> names = duplicates.get(sourceUuid).stream().filter(name->
1429
                        name.getNameCache().equals(nameCache)).collect(Collectors.toSet());
1430
                uuidNameMap.put(sourceUuid, names);
1428
            List<String> nameCacheCandidateList = new ArrayList<>(nameCacheCandidates);
1429
            Map<String, Map<UUID,Set<TaxonName>>> result = new HashMap<>();
1430
            for (String nameCache: nameCacheCandidateList) {
1431
                Map<UUID,Set<TaxonName>> uuidNameMap = new HashMap<>();
1432
                result.put(nameCache, uuidNameMap);
1433
                for(UUID sourceUuid: duplicates.keySet()){
1434
                    Set<TaxonName> names = duplicates.get(sourceUuid).stream().filter(name->
1435
                            name.getNameCache().equals(nameCache)).collect(Collectors.toSet());
1436
                    uuidNameMap.put(sourceUuid, names);
1437
                }
1431 1438
            }
1432
        }
1433 1439

  
1434
        return result;
1440
            return result;
1441
        } catch (Exception e) {
1442
            e.printStackTrace();
1443
            throw e;
1444
        }
1435 1445
    }
1436 1446

  
1437 1447
    @Override

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)