Project

General

Profile

Revision f38408d7

IDf38408d7da020d7cf3c60adcb5fa22391dbb1a2d
Parent c0c161dc
Child 1d37e9b7

Added by Katja Luther about 7 years ago

fixed deletion of taxonrelations. #2437

View differences:

cdmlib-model/src/main/java/eu/etaxonomy/cdm/model/taxon/Taxon.java
567 567
            }
568 568
        }
569 569
        //delete Relationship from other related Taxon
570
        if (fromTaxon != null && fromTaxon != this){
570
        if (fromTaxon != this){
571 571
            rel.setToTaxon(null);  //remove this Taxon from relationship
572
            fromTaxon.removeTaxonRelation(rel);
572
            if (fromTaxon != null){
573
            	if (fromTaxon.getTaxonRelations().contains(rel)){
574
            		fromTaxon.removeTaxonRelation(rel);
575
            	}
576
            }
573 577
        }
574
        if (toTaxon != null && toTaxon != this){
578
        if (toTaxon != this ){
575 579
            rel.setFromTaxon(null); //remove this Taxon from relationship
576
            toTaxon.removeTaxonRelation(rel);
580
           if (toTaxon != null){
581
	           if (toTaxon.getTaxonRelations().contains(rel)) {
582
	        	   toTaxon.removeTaxonRelation(rel);
583
	           }
584
           }
577 585
        }
578 586
    }
579 587

  
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/TaxonServiceImpl.java
923 923
                    throw new ReferencedObjectUndeletableException(message);
924 924
                }
925 925
            }else{
926
            	Set<TaxonNode> nodes = taxon.getTaxonNodes();
927
            	Iterator<TaxonNode> iterator = nodes.iterator();
928
            	TaxonNode node = null;
929
            	boolean deleteChildren;
930
        		if (config.getTaxonNodeConfig().getChildHandling().equals(ChildHandling.DELETE)){
931
        			deleteChildren = true;
932
        		}else {
933
        			deleteChildren = false;
934
            }
935
        		boolean success = true;
936
            	if (!config.isDeleteInAllClassifications() && !(classification == null)){
937
            		while (iterator.hasNext()){
938
	            		node = iterator.next();
939
	            		if (node.getClassification().equals(classification)){
940
	            			break;
926
            	if (taxon.getTaxonNodes().size() != 0){
927
	            	Set<TaxonNode> nodes = taxon.getTaxonNodes();
928
	            	Iterator<TaxonNode> iterator = nodes.iterator();
929
	            	TaxonNode node = null;
930
	            	boolean deleteChildren;
931
	        		if (config.getTaxonNodeConfig().getChildHandling().equals(ChildHandling.DELETE)){
932
	        			deleteChildren = true;
933
	        		}else {
934
	        			deleteChildren = false;
935
	        		}
936
	        		boolean success = true;
937
	            	if (!config.isDeleteInAllClassifications() && !(classification == null)){
938
	            		while (iterator.hasNext()){
939
		            		node = iterator.next();
940
		            		if (node.getClassification().equals(classification)){
941
		            			break;
942
		            		}
943
		            		node = null;
944
		            	}
945
	            		if (node != null){
946
	            			success =taxon.removeTaxonNode(node, deleteChildren);
947
	            		} else {
948
	            			String message = "Taxon is not used in defined classification";
949
	            			throw new DataChangeNoRollbackException(message);
941 950
	            		}
942
	            		node = null;
951
	            	} else if (config.isDeleteInAllClassifications()){
952
		            	success = taxon.removeTaxonNodes(deleteChildren);
953
	            	}
954
	            	if (!success){
955
	            		String message = "The taxon node could not be deleted.";
956
	            		throw new DataChangeNoRollbackException(message);
943 957
	            	}
944
            		if (node != null){
945
            			success =taxon.removeTaxonNode(node, deleteChildren);
946
            		} else {
947
            			String message = "Taxon is not used in defined classification";
948
            			throw new DataChangeNoRollbackException(message);
949
            		}
950
            	} else if (config.isDeleteInAllClassifications()){
951
	            	success = taxon.removeTaxonNodes(deleteChildren);
952
            	}
953
            	if (!success){
954
            		String message = "The taxon node could not be deleted.";
955
            		throw new DataChangeNoRollbackException(message);
956 958
            	}
957 959
            }
958 960

  
......
981 983
                    String message = "Taxon can't be deleted as it is related to another taxon. Remove taxon from all relations to other taxa prior to deletion.";
982 984
                    throw new ReferencedObjectUndeletableException(message);
983 985
                }
986
            } else{
987
            	for (TaxonRelationship taxRel: taxon.getTaxonRelations()){
988
            		Taxon taxFrom = taxRel.getFromTaxon();
989
            		Taxon taxTo = taxRel.getToTaxon();
990
            		TaxonDeletionConfigurator taxConf = new TaxonDeletionConfigurator();
991
        			taxConf.setDeleteSynonymRelations(false);
992
        			taxConf.setDeleteSynonymsIfPossible(false);
993
        			taxConf.setDeleteTaxonNodes(false);
994
        			taxConf.setDeleteTaxonRelationships(false);
995
        			taxon.removeTaxonRelation(taxRel);
996
        			
997
            		if (taxFrom.equals(taxon)){
998
            			try{
999
            				this.deleteTaxon(taxTo, taxConf, classification);
1000
            			} catch(DataChangeNoRollbackException e){
1001
            				logger.debug("A related taxon will not be deleted." + e.getMessage());
1002
            			}
1003
                	} else {
1004
                		try{
1005
                			this.deleteTaxon(taxFrom, taxConf, classification);
1006
            			} catch(DataChangeNoRollbackException e){
1007
            				logger.debug("A related taxon will not be deleted." + e.getMessage());
1008
            			}
1009
            			
1010
            		}
1011
            	}
984 1012
            }
985 1013

  
986 1014

  
......
989 1017

  
990 1018

  
991 1019
                //check references with only reverse mapping
992
            Set<CdmBase> referencingObjects = genericDao.getReferencingObjects(taxon);
993
            for (CdmBase referencingObject : referencingObjects){
994
                //IIdentificationKeys (Media, Polytomous, MultiAccess)
995
                if (HibernateProxyHelper.isInstanceOf(referencingObject, IIdentificationKey.class)){
996
                    String message = "Taxon can't be deleted as it is used in an identification key. Remove from identification key prior to deleting this name";
997
                    message = String.format(message, CdmBase.deproxy(referencingObject, DerivedUnitBase.class).getTitleCache());
998
                    throw new ReferencedObjectUndeletableException(message);
999
                }
1000

  
1001

  
1002
                //PolytomousKeyNode
1003
                if (referencingObject.isInstanceOf(PolytomousKeyNode.class)){
1004
                    String message = "Taxon can't be deleted as it is used in polytomous key node";
1005
                    throw new ReferencedObjectUndeletableException(message);
1006
                }
1007

  
1008
                //TaxonInteraction
1009
                if (referencingObject.isInstanceOf(TaxonInteraction.class)){
1010
                    String message = "Taxon can't be deleted as it is used in taxonInteraction#taxon2";
1011
                    throw new ReferencedObjectUndeletableException(message);
1012
                }
1013
            }
1020
            String message = checkForReferences(taxon);
1021
			if (message != null){
1022
				throw new ReferencedObjectUndeletableException(message.toString());
1023
			}
1014 1024

  
1015 1025

  
1016 1026
            //TaxonNameBase
......
1020 1030
                	name.removeTaxonBase(taxon);
1021 1031
                	nameService.save(name);
1022 1032
                	
1023
                    nameService.delete(taxon.getName(), config.getNameDeletionConfig());
1033
                    nameService.delete(name, config.getNameDeletionConfig());
1024 1034
                } catch (ReferencedObjectUndeletableException e) {
1025 1035
                    //do nothing
1026 1036
                    if (logger.isDebugEnabled()){logger.debug("Name could not be deleted");}
......
1048 1058
            if (taxon.getTaxonNodes() == null || taxon.getTaxonNodes().size()== 0){
1049 1059
            	dao.delete(taxon);
1050 1060
            } else{
1051
            	String message = "Taxon can't be deleted as it is used in another Taxonnode";
1061
            	message = "Taxon can't be deleted as it is used in another Taxonnode";
1052 1062
                    throw new ReferencedObjectUndeletableException(message);
1053 1063
            }
1054 1064
            
1055 1065

  
1056 1066
    }
1067
    
1068
    private String checkForReferences(Taxon taxon){
1069
    	Set<CdmBase> referencingObjects = genericDao.getReferencingObjects(taxon);
1070
        for (CdmBase referencingObject : referencingObjects){
1071
            //IIdentificationKeys (Media, Polytomous, MultiAccess)
1072
            if (HibernateProxyHelper.isInstanceOf(referencingObject, IIdentificationKey.class)){
1073
                String message = "Taxon" + taxon.getTitleCache() + "can't be deleted as it is used in an identification key. Remove from identification key prior to deleting this name";
1074
              
1075
                return message;
1076
            }
1077

  
1078

  
1079
            //PolytomousKeyNode
1080
            if (referencingObject.isInstanceOf(PolytomousKeyNode.class)){
1081
                String message = "Taxon" + taxon.getTitleCache() + " can't be deleted as it is used in polytomous key node";
1082
                return message;
1083
            }
1084

  
1085
            //TaxonInteraction
1086
            if (referencingObject.isInstanceOf(TaxonInteraction.class)){
1087
                String message = "Taxon can't be deleted as it is used in taxonInteraction#taxon2";
1088
                return message;
1089
            }
1090
        }
1091
        return null;
1092
    }
1093
    
1057 1094

  
1058 1095
    /* (non-Javadoc)
1059 1096
     * @see eu.etaxonomy.cdm.api.service.ITaxonService#deleteSynonym(eu.etaxonomy.cdm.model.taxon.Synonym, eu.etaxonomy.cdm.model.taxon.Taxon, boolean, boolean)
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/service/TaxonServiceImplTest.java
1291 1291
				
1292 1292
		assertEquals(0, size);
1293 1293
	}
1294
	
1295
	
1296
	@Test
1297
	@DataSet(value="BlankDataSet.xml")
1298
	public final void testTaxonDeletionConfiguratorTaxonWithMisappliedName(){
1299
		
1300
		Taxon testTaxon = TaxonGenerator.getTestTaxon();
1301
		UUID uuid = service.save(testTaxon);
1302
			
1303
		Taxon misappliedName = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
1304
				
1305
		Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1306
		TaxonNode node =nodes.next();
1307
		testTaxon.addMisappliedName(misappliedName, null, null);
1308
		UUID misappliedNameUUID = service.save(misappliedName);
1309
		
1310
		TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1311
		
1312
				
1313
		try {
1314
			service.deleteTaxon(testTaxon, config, null);
1315
		} catch (DataChangeNoRollbackException e) {
1316
			Assert.fail();
1317
		}
1318
				
1319
		commitAndStartNewTransaction(null);
1320
		Taxon tax = (Taxon)service.find(uuid);
1321
		assertNull(tax);
1322
		tax = (Taxon)service.find(misappliedNameUUID);
1323
		
1324
		assertNull(tax);
1325
		
1326
	}
1327
	
1328
	@Test
1329
	@DataSet(value="BlankDataSet.xml")
1330
	public final void testTaxonDeletionConfiguratorTaxonMisappliedName(){
1331
		
1332
		Taxon testTaxon = TaxonGenerator.getTestTaxon();
1333
		UUID uuid = service.save(testTaxon);
1334
			
1335
		Taxon misappliedNameTaxon = Taxon.NewInstance(BotanicalName.NewInstance(Rank.GENUS()), null);
1336
				
1337
		Iterator<TaxonNode> nodes = testTaxon.getTaxonNodes().iterator();
1338
		TaxonNode node =nodes.next();
1339
		testTaxon.addMisappliedName(misappliedNameTaxon, null, null);
1340
		UUID misappliedNameUUID = service.save(misappliedNameTaxon);
1341
		misappliedNameTaxon = (Taxon)service.find(misappliedNameUUID);
1342
		UUID misNameUUID = misappliedNameTaxon.getName().getUuid();
1343
		
1344
		TaxonDeletionConfigurator config = new TaxonDeletionConfigurator() ;
1345
		
1346
				
1347
		try {
1348
			service.deleteTaxon(misappliedNameTaxon, config, null);
1349
		} catch (DataChangeNoRollbackException e) {
1350
			e.printStackTrace();
1351
			
1352
		}
1353
				
1354
		commitAndStartNewTransaction(null);
1355
		Taxon tax = (Taxon)service.find(uuid);
1356
		assertNotNull(tax);
1357
		tax = (Taxon)service.find(misappliedNameUUID);
1358
		BotanicalName name = (BotanicalName) nameService.find(misNameUUID);
1359
		
1360
		assertNull(tax);
1361
		assertNull(name);
1362
		
1363
	}
1364
	
1365
	
1294 1366
}
1295 1367

  
1296 1368

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)