commit bc416076df7e31846efdeface25ccb1c37d84dd6
Author: Andreas Müller <a.mueller@bgbm.org>
Date:   Mon Jan 6 15:40:45 2020 +0100

    ref #1447 cleanup FaunaEuErmsFindIdenticalNamesActivator

diff --git a/cdm-pesi/src/main/java/eu/etaxonomy/cdm/app/pesi/merging/FaunaEuErmsFindIdenticalNamesActivator.java b/cdm-pesi/src/main/java/eu/etaxonomy/cdm/app/pesi/merging/FaunaEuErmsFindIdenticalNamesActivator.java
index 0dae4f73..1d6fc936 100644
--- a/cdm-pesi/src/main/java/eu/etaxonomy/cdm/app/pesi/merging/FaunaEuErmsFindIdenticalNamesActivator.java
+++ b/cdm-pesi/src/main/java/eu/etaxonomy/cdm/app/pesi/merging/FaunaEuErmsFindIdenticalNamesActivator.java
@@ -13,12 +13,11 @@ import org.apache.log4j.Logger;
 
 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
 import eu.etaxonomy.cdm.app.common.CdmDestinations;
-import eu.etaxonomy.cdm.app.pesi.ErmsImportActivator;
 import eu.etaxonomy.cdm.database.DbSchemaValidation;
 import eu.etaxonomy.cdm.database.ICdmDataSource;
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.io.api.application.CdmIoApplicationController;
-import eu.etaxonomy.cdm.io.pesi.merging.FaunaEuErmsMerging;
+import eu.etaxonomy.cdm.io.pesi.merging.PesiMergeObject;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
 import eu.etaxonomy.cdm.model.name.IZoologicalName;
@@ -33,98 +32,81 @@ import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
 
 public class FaunaEuErmsFindIdenticalNamesActivator {
-	private static final Logger logger = Logger.getLogger(FaunaEuErmsFindIdenticalNamesActivator.class);
-	//static final ICdmDataSource faunaEuropaeaSource = CdmDestinations.localH2();
-	static final ICdmDataSource faunaEuropaeaSource = CdmDestinations.cdm_test_local_faunaEu_mysql();
-	static Reference faunaSec;
-	static Reference ermsSec;
 
-	//TODO hole aus beiden DB alle TaxonNameBases
-
-	private CdmApplicationController initDb(ICdmDataSource db) {
-
-		// Init source DB
-		//CdmApplicationController appCtrInit = null;
-		CdmApplicationController appCtrInit = CdmIoApplicationController.NewInstance(db, DbSchemaValidation.VALIDATE, false);
-
-		
-		//appCtrInit = TestDatabase.initDb(db, DbSchemaValidation.VALIDATE, false);
-
-		return appCtrInit;
+    private static final Logger logger = Logger.getLogger(FaunaEuErmsFindIdenticalNamesActivator.class);
+
+    //static final ICdmDataSource faunaEuropaeaSource = CdmDestinations.localH2();
+	static final ICdmDataSource pesiSource = CdmDestinations.cdm_test_local_faunaEu_mysql();
+
+	static final String path = "C:\\Users\\k.luther\\test";
+
+	private static UUID faunaEuSecUuid = UUID.fromString("6786d863-75d4-4796-b916-c1c3dff4cb70");
+	private static UUID ermsSecUuid = UUID.fromString("7744bc26-f914-42c4-b54a-dd2a030a8bb7");
+	private static UUID ifSecUuid;
+	private static UUID emSecUuid;
+
+	private void invoke(ICdmDataSource source){
+
+        CdmApplicationController app = CdmIoApplicationController.NewInstance(source, DbSchemaValidation.VALIDATE, false);
+
+        List<String> propertyPaths = new ArrayList<>();
+        propertyPaths.add("sources.*");
+        propertyPaths.add("sources.idInSource");
+        propertyPaths.add("sources.idNamespace");
+        propertyPaths.add("taxonBases.*");
+        propertyPaths.add("taxonBases.relationsFromThisTaxon");
+        propertyPaths.add("taxonBases.taxonNodes.*");
+        propertyPaths.add("taxonBases.taxonNodes.parent.*");
+        propertyPaths.add("taxonBases.taxonNodes.childNodes.*");
+        propertyPaths.add("taxonBases.taxonNodes.childNodes.classification.rootNode.childNodes.*");
+        propertyPaths.add("taxonBases.taxonNodes.parent.taxon.name.*");
+        propertyPaths.add("taxonBases.acceptedTaxon.taxonNodes.*");
+        propertyPaths.add("taxonBases.acceptedTaxon.taxonNodes.childNodes.*");
+        propertyPaths.add("taxonBases.acceptedTaxon.taxonNodes.childNodes.classification.rootNode.childNodes.*");
+        System.out.println("Start getIdenticalNames...");
+
+        Reference faunaEuSec = app.getReferenceService().load(faunaEuSecUuid);
+        Reference ermsSec = app.getReferenceService().load(ermsSecUuid);
+        Map<String, List<TaxonName>> namesOfIdenticalTaxa = app.getTaxonService().findIdenticalTaxonNameIds(ermsSec, faunaEuSec, propertyPaths);
+
+        System.out.println("Start creating merging objects");
+        List<PesiMergeObject> mergingObjects= createMergeObjects(namesOfIdenticalTaxa, app);
+        boolean resultOK = true;
+        System.out.println("Start creating csv files");
+        resultOK &= writeSameNamesDifferentAuthorToCsv(mergingObjects, path + "_authors.csv");
+        resultOK &= writeSameNamesDifferentStatusToCsv(mergingObjects, path + "_status.csv");
+        resultOK &= writeSameNamesToCsvFile(mergingObjects, path + "_names.csv");
+        resultOK &= writeSameNamesDifferentPhylumToCsv(mergingObjects, path + "_phylum.csv");
+        resultOK &= writeSameNamesDifferentParentToCsv(mergingObjects, path + "parent.csv");
+
+        System.out.println("End merging Fauna Europaea and Erms: " + resultOK);
 	}
 
+	private boolean writeSameNamesToCsvFile(
+			List<PesiMergeObject> mergingObjects, String string) {
 
-	/**
-	 * @param args
-	 */
-	public static void main(String[] args) {
-
-		FaunaEuErmsFindIdenticalNamesActivator sc = new FaunaEuErmsFindIdenticalNamesActivator();
-
-		CdmApplicationController appCtrFaunaEu = sc.initDb(faunaEuropaeaSource);
-		String sFileName = "C:\\Users\\k.luther\\test";
-		//CdmApplicationController appCtrErms = sc.initDb(ermsSource);
-		List<String> propertyPaths = new ArrayList<>();
-		propertyPaths.add("sources.*");
-		propertyPaths.add("sources.idInSource");
-		propertyPaths.add("sources.idNamespace");
-		propertyPaths.add("taxonBases.*");
-		propertyPaths.add("taxonBases.relationsFromThisTaxon");
-		propertyPaths.add("taxonBases.taxonNodes.*");
-		propertyPaths.add("taxonBases.taxonNodes.parent.*");
-		propertyPaths.add("taxonBases.taxonNodes.childNodes.*");
-		propertyPaths.add("taxonBases.taxonNodes.childNodes.classification.rootNode.childNodes.*");
-		propertyPaths.add("taxonBases.taxonNodes.parent.taxon.name.*");
-		propertyPaths.add("taxonBases.acceptedTaxon.taxonNodes.*");
-		propertyPaths.add("taxonBases.acceptedTaxon.taxonNodes.childNodes.*");
-		propertyPaths.add("taxonBases.acceptedTaxon.taxonNodes.childNodes.classification.rootNode.childNodes.*");
-		System.err.println("Start getIdenticalNames...");
-		
-		faunaSec = appCtrFaunaEu.getReferenceService().load(UUID.fromString("6786d863-75d4-4796-b916-c1c3dff4cb70"));
-		ermsSec = appCtrFaunaEu.getReferenceService().load(UUID.fromString("7744bc26-f914-42c4-b54a-dd2a030a8bb7"));
-		Map<String, List<TaxonName>> namesOfIdenticalTaxa = appCtrFaunaEu.getTaxonService().findIdenticalTaxonNameIds(ermsSec, faunaSec, propertyPaths);
-			
-		List<FaunaEuErmsMerging> mergingObjects = new ArrayList<>();
-		FaunaEuErmsMerging mergeObject;
-		TaxonName faunaEuTaxName;
-		TaxonName ermsTaxName;
-		System.err.println("Start creating merging objects");
-		mergingObjects= sc.createMergeObjects(namesOfIdenticalTaxa, appCtrFaunaEu);
-		boolean resultOK = true;
-		System.err.println("Start creating csv files");
-		resultOK = resultOK && sc.writeSameNamesdifferentAuthorToCsv(mergingObjects, sFileName + "_authors.csv");
-		resultOK = resultOK &&sc.writeSameNamesdifferentStatusToCsv(mergingObjects, sFileName + "_status.csv");
-		resultOK = resultOK &&sc.writeSameNamesToCsVFile(mergingObjects, sFileName + "_names.csv");
-		resultOK = resultOK &&sc.writeSameNamesdifferentPhylumToCsv(mergingObjects, sFileName + "_phylum.csv");
-		resultOK = resultOK &&sc.writeSameNamesDifferentParentToCsv(mergingObjects, sFileName + "parent.csv");
-
-		System.err.println("End merging Fauna Europaea and Erms" + resultOK);
-		System.exit(0);
-
-	}
-
-	private boolean writeSameNamesToCsVFile(
-			List<FaunaEuErmsMerging> mergingObjects, String string) {
 	    try{
     		FileWriter writer = new FileWriter(string);
 
     	    //create Header
     	    String firstLine = "same names";
     	    createHeader(writer, firstLine);
-    		for (FaunaEuErmsMerging merging : mergingObjects){
+    		for (PesiMergeObject merging : mergingObjects){
     	    	writeCsvLine(writer, merging) ;
     		}
     		writer.flush();
     		writer.close();
+    		return true;
     	}catch(IOException e){
+    	    logger.error(e.getMessage());
     	    return false;
     	}
-    	return true;
 	}
 
-	private boolean writeSameNamesdifferentPhylumToCsv(List<FaunaEuErmsMerging> mergingObjects, String sfileName){
-		try
-		{
+	private boolean writeSameNamesDifferentPhylumToCsv(
+	        List<PesiMergeObject> mergingObjects, String sfileName){
+
+	    try{
 		    FileWriter writer = new FileWriter(sfileName);
 
 		    //create Header
@@ -132,7 +114,7 @@ public class FaunaEuErmsFindIdenticalNamesActivator {
 		   createHeader(writer, firstLine);
 
 			//write data
-			for (FaunaEuErmsMerging merging : mergingObjects){
+			for (PesiMergeObject merging : mergingObjects){
 		    	//TODO
 				if ((merging.getPhylumInErms()== null )^ (merging.getPhylumInFaunaEu()== null)){
 					writeCsvLine(writer, merging) ;
@@ -144,17 +126,17 @@ public class FaunaEuErmsFindIdenticalNamesActivator {
 			}
 			writer.flush();
 			writer.close();
+			return true;
+		}catch(IOException e){
+		    logger.error(e.getMessage());
+            return false;
 		}
-		catch(IOException e)
-		{
-		 return false;
-		}
-		return true;
 	}
-	
-	private boolean writeSameNamesDifferentParentToCsv(List<FaunaEuErmsMerging> mergingObjects, String sfileName){
-		try
-		{
+
+	private boolean writeSameNamesDifferentParentToCsv(
+	        List<PesiMergeObject> mergingObjects, String sfileName){
+
+	    try{
 		    FileWriter writer = new FileWriter(sfileName);
 
 		    //create Header
@@ -162,7 +144,7 @@ public class FaunaEuErmsFindIdenticalNamesActivator {
 		   createHeader(writer, firstLine);
 
 			//write data
-			for (FaunaEuErmsMerging merging : mergingObjects){
+			for (PesiMergeObject merging : mergingObjects){
 		    	//TODO
 				if ((merging.getParentStringInErms()== null )^ (merging.getParentStringInFaunaEu()== null)){
 					writeCsvLine(writer, merging) ;
@@ -174,24 +156,23 @@ public class FaunaEuErmsFindIdenticalNamesActivator {
 			}
 			writer.flush();
 			writer.close();
+			return true;
+		}catch(IOException e){
+		    return false;
 		}
-		catch(IOException e)
-		{
-		 return false;
-		}
-		return true;
 	}
 
-	private boolean writeSameNamesdifferentRankToCsv(List<FaunaEuErmsMerging> mergingObjects, String sfileName){
-		try
-		{
+	private boolean writeSameNamesdifferentRankToCsv(
+	        List<PesiMergeObject> mergingObjects, String sfileName){
+
+	    try{
 		    FileWriter writer = new FileWriter(sfileName);
 		    String firstLine = "same names but different rank";
 		    //create Header
 		    createHeader(writer, firstLine);
 
 			//write data
-			for (FaunaEuErmsMerging merging : mergingObjects){
+			for (PesiMergeObject merging : mergingObjects){
 
 				if (!merging.getRankInErms().equals(merging.getRankInFaunaEu())){
 					writeCsvLine(writer, merging);
@@ -199,12 +180,10 @@ public class FaunaEuErmsFindIdenticalNamesActivator {
 			}
 			writer.flush();
 			writer.close();
+			return true;
+		}catch(IOException e){
+		    return false;
 		}
-		catch(IOException e)
-		{
-		 return false;
-		}
-		return true;
 	}
 
 	private void createHeader(FileWriter writer, String firstLine) throws IOException{
@@ -249,9 +228,10 @@ public class FaunaEuErmsFindIdenticalNamesActivator {
 			writer.append('\n');
 	}
 
-	private boolean writeSameNamesdifferentStatusToCsv(List<FaunaEuErmsMerging> mergingObjects, String sfileName){
-		try
-		{
+	private boolean writeSameNamesDifferentStatusToCsv(
+	        List<PesiMergeObject> mergingObjects, String sfileName){
+
+	    try{
 		    FileWriter writer = new FileWriter(sfileName);
 
 		    //create Header
@@ -259,8 +239,7 @@ public class FaunaEuErmsFindIdenticalNamesActivator {
 		    createHeader(writer, firstLine);
 
 			//write data
-			for (FaunaEuErmsMerging merging : mergingObjects){
-
+			for (PesiMergeObject merging : mergingObjects){
 				if (merging.isStatInErms()^merging.isStatInFaunaEu()){
 					 writeCsvLine(writer, merging);
 				}
@@ -268,25 +247,24 @@ public class FaunaEuErmsFindIdenticalNamesActivator {
 
 			writer.flush();
 			writer.close();
+			return true;
+		}catch(IOException e){
+		    return false;
 		}
-		catch(IOException e)
-		{
-		 return false;
-		}
-		return true;
 	}
 
-	private boolean writeSameNamesdifferentAuthorToCsv(List<FaunaEuErmsMerging> mergingObjects, String sfileName){
-		try
-		{
+	private boolean writeSameNamesDifferentAuthorToCsv(
+	        List<PesiMergeObject> mergingObjects, String sfileName){
+
+	    try{
 		    FileWriter writer = new FileWriter(sfileName);
 
 		    //create Header
-		   String firstLine = "same names but different authors";
-		   createHeader(writer, firstLine);
+		    String firstLine = "same names but different authors";
+		    createHeader(writer, firstLine);
 
 			//write data
-			for (FaunaEuErmsMerging merging : mergingObjects){
+			for (PesiMergeObject merging : mergingObjects){
 
 				if (merging.getAuthorInErms() != null && merging.getAuthorInFaunaEu() != null && !merging.getAuthorInErms().equals(merging.getAuthorInFaunaEu())){
 					 writeCsvLine(writer, merging);
@@ -295,18 +273,15 @@ public class FaunaEuErmsFindIdenticalNamesActivator {
 				}
 			}
 
-
 			writer.flush();
 			writer.close();
+			return true;
+		}catch(IOException e){
+		    return false;
 		}
-		catch(IOException e)
-		{
-		 return false;
-		}
-		return true;
 	}
 
-	private void writeCsvLine(FileWriter writer, FaunaEuErmsMerging merging) throws IOException{
+	private void writeCsvLine(FileWriter writer, PesiMergeObject merging) throws IOException{
 
 		writer.append(merging.getUuidFaunaEu());
 		writer.append(';');
@@ -356,257 +331,243 @@ public class FaunaEuErmsFindIdenticalNamesActivator {
 		writer.append('\n');
 	}
 
+	private List<PesiMergeObject> createMergeObjects(Map<String,List<TaxonName>> names,
+	        CdmApplicationController appCtr){
 
-	private List<FaunaEuErmsMerging> createMergeObjects(Map<String,List<TaxonName>> names, CdmApplicationController appCtr){
-		Classification faunaEuClassification = appCtr.getClassificationService().load(UUID.fromString("44d8605e-a7ce-41e1-bee9-99edfec01e7c"));
-		Classification ermsClassification = appCtr.getClassificationService().load(UUID.fromString("6fa988a9-10b7-48b0-a370-2586fbc066eb"));
-		List<FaunaEuErmsMerging> merge = new ArrayList<>();
-		
-		FaunaEuErmsMerging mergeObject;
-		String idInSource1;
-		List<TaxonName> identicalNames;
-		
-		for (String nameCache: names.keySet()){
-			identicalNames = names.get(nameCache);
-			
-			mergeObject = new FaunaEuErmsMerging();
-			
-			
-			//getPhylum
-			TaxonNodeDto phylum1 = null;
-			TaxonName faunaEuName = null;
-			TaxonName ermsName = null;
-			TaxonBase tempName = null;
-			if (identicalNames.size() == 2) {
-				Set<TaxonBase> taxonBases = identicalNames.get(0).getTaxonBases();
-				if (taxonBases.size()==1) {
-					Iterator<TaxonBase> it = taxonBases.iterator();
-					tempName = it.next();
-					if (tempName.getSec().equals(faunaSec)) {
-						faunaEuName = identicalNames.get(0);
-						ermsName = identicalNames.get(1);
-					}else {
-						faunaEuName = identicalNames.get(1);
-						ermsName = identicalNames.get(0);
-					}
-				}else {
-					//TODO: find the two correct names
-				}
-			}else {
-				logger.debug(nameCache + " has more than two identical namecaches");
-				continue;
-			}
-			phylum1 = null;
-			if (faunaEuName.getRank().equals(Rank.PHYLUM())) {
-				Taxon taxon = null;
-				taxon = getAcceptedTaxon(faunaEuName);
-				if (taxon != null) {
-					phylum1 = new TaxonNodeDto(taxon.getTaxonNode(faunaEuClassification));
-				}
-				
-			}
-			if (phylum1 == null && faunaEuName != null && !faunaEuName.getRank().isHigher(Rank.PHYLUM())){
-					phylum1 =appCtr.getTaxonNodeService().taxonNodeDtoParentRank(faunaEuClassification, Rank.PHYLUM(), faunaEuName);
-			}
-
-			TaxonNodeDto phylum2 = null;
-			if (ermsName.getRank().equals(Rank.PHYLUM())) {
-				Taxon taxon = null;
-				taxon = getAcceptedTaxon(ermsName);
-				if (taxon != null) {
-					phylum2 = new TaxonNodeDto(taxon.getTaxonNode(ermsClassification));
-				}
-				
-			}
-			if (phylum2 == null && ermsName != null && !ermsName.getRank().isHigher(Rank.PHYLUM())){
-				phylum2 = appCtr.getTaxonNodeService().taxonNodeDtoParentRank(ermsClassification, Rank.PHYLUM(), ermsName);
-			}
-			mergeObject.setPhylumInErms(phylum1);
-			mergeObject.setPhylumInFaunaEu(phylum2);
-
-			//getUuids
-			mergeObject.setUuidErms(ermsName.getUuid().toString());
-			mergeObject.setUuidFaunaEu(faunaEuName.getUuid().toString());
-
-			Iterator<IdentifiableSource> sources = ermsName.getSources().iterator();
-			if (sources.hasNext()){
-				IdentifiableSource source = sources.next();
-				idInSource1 = source.getIdInSource();
-				mergeObject.setIdInErms(idInSource1);
-			}
-			sources = faunaEuName.getSources().iterator();
-			if (sources.hasNext()){
-				IdentifiableSource source = sources.next();
-				idInSource1 = source.getIdInSource();
-				mergeObject.setIdInFaunaEu(idInSource1);
-			}
-
-			mergeObject.setNameCacheInErms(ermsName.getNameCache());
-			mergeObject.setNameCacheInFaunaEu(faunaEuName.getNameCache());
-
-			mergeObject.setAuthorInErms(ermsName.getAuthorshipCache());
-			mergeObject.setAuthorInFaunaEu(faunaEuName.getAuthorshipCache());
-			Set<Taxon> taxa = ermsName.getTaxa();
-			if (!taxa.isEmpty()){
-				mergeObject.setStatInErms(true);
-				Iterator<Taxon> taxaIterator = taxa.iterator();
-				Taxon taxon = null;
-				while (taxaIterator.hasNext()){
-					taxon = taxaIterator.next();
-					if (!taxon.isMisapplication()){
-						break;
-					}
-				}
-				Set<TaxonNode> nodes = taxon.getTaxonNodes();
-				Iterator<TaxonNode> taxonNodeIterator = nodes.iterator();
-				TaxonNode node, parentNode = null;
-				while (taxonNodeIterator.hasNext()){
-					node = taxonNodeIterator.next();
-					if (!node.isTopmostNode()){
-						parentNode = node.getParent();
-					}
-				}
-				//TODO: ändern mit erweitertem Initializer..
-				if (parentNode != null){
-				    TaxonName parentName = HibernateProxyHelper.deproxy(parentNode.getTaxon().getName());
-					String parentNameCache = parentName.getNameCache();
-					mergeObject.setParentStringInErms(parentNameCache);
-					mergeObject.setParentRankStringInErms(parentName.getRank().getLabel());
-					
-				}
-			}else{
-				mergeObject.setStatInErms(false);
-				TaxonNode parentNode = getAcceptedNode(ermsName);
-				//TODO: ändern mit erweitertem Initializer..
-				if (parentNode != null){
-				    TaxonName parentName = HibernateProxyHelper.deproxy(parentNode.getTaxon().getName());
-					String parentNameCache = parentName.getNameCache();
-					mergeObject.setParentStringInErms(parentNameCache);
-					mergeObject.setParentRankStringInErms(parentName.getRank().getLabel());
-					
-				}
-			}
-			taxa = faunaEuName.getTaxa();
-			if (!taxa.isEmpty()){
-				mergeObject.setStatInFaunaEu(true);
-				Iterator<Taxon> taxaIterator = taxa.iterator();
-				Taxon taxon = null;
-				while (taxaIterator.hasNext()){
-					taxon = taxaIterator.next();
-					if (!taxon.isMisapplication()){
-						break;
-					}
-				}
-				Set<TaxonNode> nodes = taxon.getTaxonNodes();
-				Iterator<TaxonNode> taxonNodeIterator = nodes.iterator();
-				TaxonNode node, parentNode = null;
-				while (taxonNodeIterator.hasNext()){
-					node = taxonNodeIterator.next();
-					if (!node.isTopmostNode()){
-						parentNode = node.getParent();
-					}
-				}
-				//TODO: ändern mit erweitertem Initializer..
-				if (parentNode != null){
-					if (parentNode.getTaxon().getName().isZoological()){
-
-    					IZoologicalName parentName = CdmBase.deproxy(parentNode.getTaxon().getName());
-    					String parentNameCache = parentName.getNameCache();
-    					mergeObject.setParentStringInFaunaEu(parentNameCache);
-    					mergeObject.setParentRankStringInFaunaEu(parentName.getRank().getLabel());
-    					
-					}else{
-						logger.debug("no zoologicalName: " + parentNode.getTaxon().getName().getTitleCache() +" . "+parentNode.getTaxon().getName().getUuid());
-					}
+	    UUID uuidClassification1 = UUID.fromString("44d8605e-a7ce-41e1-bee9-99edfec01e7c");
+	    UUID uuidClassification2 = UUID.fromString("6fa988a9-10b7-48b0-a370-2586fbc066eb");
+	    Classification classification1 = appCtr.getClassificationService().load(uuidClassification1);
+		Classification classification2 = appCtr.getClassificationService().load(uuidClassification2);
 
-				}
-			}else{
-				mergeObject.setStatInFaunaEu(false);
-				TaxonNode parentNode = getAcceptedNode(faunaEuName);
-				//TODO: ändern mit erweitertem Initializer..
-				if (parentNode != null){
-				    TaxonName parentName = HibernateProxyHelper.deproxy(parentNode.getTaxon().getName());
-					String parentNameCache = parentName.getNameCache();
-					mergeObject.setParentStringInFaunaEu(parentNameCache);
-					mergeObject.setParentRankStringInFaunaEu(parentName.getRank().getLabel());
-					
-				}
-			}
-			
-
-			mergeObject.setRankInErms(ermsName.getRank().getLabel());
-			mergeObject.setRankInFaunaEu(faunaEuName.getRank().getLabel());
-
-			//set parent informations
-
-
-			/*
-			Set<HybridRelationship> parentRelations = zooName.getParentRelationships();
-			Iterator parentIterator = parentRelations.iterator();
-			HybridRelationship parentRel;
-			ZoologicalName parentName;
-			while (parentIterator.hasNext()){
-				parentRel = (HybridRelationship)parentIterator.next();
-				parentName = (ZoologicalName)parentRel.getParentName();
-				mergeObject.setParentRankStringInErms(parentName.getRank().getLabel());
-				mergeObject.setParentStringInErms(parentName.getNameCache());
-			}
-
-			parentRelations = zooName2.getParentRelationships();
-			parentIterator = parentRelations.iterator();
+		List<PesiMergeObject> merge = new ArrayList<>();
 
-			while (parentIterator.hasNext()){
-				parentRel = (HybridRelationship)parentIterator.next();
-				parentName = (ZoologicalName)parentRel.getParentName();
-				mergeObject.setParentRankStringInFaunaEu(parentName.getRank().getLabel());
-				mergeObject.setParentStringInFaunaEu(parentName.getNameCache());
-			}*/
-			merge.add(mergeObject);
+		for (String nameCache: names.keySet()){
+		    createSingleMergeObject(appCtr, merge, nameCache, names.get(nameCache),classification1, classification2);
 		}
-//		}
 
 		return merge;
-
 	}
 
 
-	/**
-	 * @param ermsName
-	 * @return
-	 */
+    private void createSingleMergeObject(CdmApplicationController appCtr, List<PesiMergeObject> merge, String nameCache,
+            List<TaxonName> identicalNames,
+            Classification classification1, Classification classification2) {
+
+        PesiMergeObject mergeObject = new PesiMergeObject();
+
+        if(identicalNames.size()!= 2) {
+            logger.warn(nameCache + " has more than 2 names with identical name cache from different sources. This is not yet handled.");
+            return;
+        }
+        //getPhylum
+        TaxonNodeDto phylum1 = null;
+        TaxonName faunaEuName = null;
+        TaxonName ermsName = null;
+
+        Set<TaxonBase> taxonBases = identicalNames.get(0).getTaxonBases();
+        if (taxonBases.size()==1) {
+            Iterator<TaxonBase> it = taxonBases.iterator();
+            TaxonBase<?> tempName = it.next();
+            if (tempName.getSec().getUuid().equals(faunaEuSecUuid)) {
+                faunaEuName = identicalNames.get(0);
+                ermsName = identicalNames.get(1);
+            }else {
+                faunaEuName = identicalNames.get(1);
+                ermsName = identicalNames.get(0);
+            }
+        }else {
+            //TODO: find the two correct names
+            logger.warn("Name has not exact 1 but " + taxonBases.size() + " taxon base attached. This is not yet handled");
+            return;
+        }
+        if (faunaEuName.getRank().equals(Rank.PHYLUM())) {
+            Taxon taxon = null;
+            taxon = getAcceptedTaxon(faunaEuName);
+            if (taxon != null) {
+                phylum1 = new TaxonNodeDto(taxon.getTaxonNode(classification1));
+            }
+
+        }
+        if (phylum1 == null && !faunaEuName.getRank().isHigher(Rank.PHYLUM())){
+                phylum1 = appCtr.getTaxonNodeService().taxonNodeDtoParentRank(classification1, Rank.PHYLUM(), faunaEuName);
+        }
+
+        TaxonNodeDto phylum2 = null;
+        if (ermsName.getRank().equals(Rank.PHYLUM())) {
+            Taxon taxon = null;
+            taxon = getAcceptedTaxon(ermsName);
+            if (taxon != null) {
+                phylum2 = new TaxonNodeDto(taxon.getTaxonNode(classification2));
+            }
+
+        }
+        if (phylum2 == null && !ermsName.getRank().isHigher(Rank.PHYLUM())){
+            phylum2 = appCtr.getTaxonNodeService().taxonNodeDtoParentRank(classification2, Rank.PHYLUM(), ermsName);
+        }
+        mergeObject.setPhylumInErms(phylum1);
+        mergeObject.setPhylumInFaunaEu(phylum2);
+
+        //getUuids
+        mergeObject.setUuidErms(ermsName.getUuid().toString());
+        mergeObject.setUuidFaunaEu(faunaEuName.getUuid().toString());
+
+        Iterator<IdentifiableSource> sources = ermsName.getSources().iterator();
+        if (sources.hasNext()){
+            IdentifiableSource source = sources.next();
+            String idInSource1 = source.getIdInSource();
+            mergeObject.setIdInErms(idInSource1);
+        }
+        sources = faunaEuName.getSources().iterator();
+        if (sources.hasNext()){
+            IdentifiableSource source = sources.next();
+            String idInSource1 = source.getIdInSource();
+            mergeObject.setIdInFaunaEu(idInSource1);
+        }
+
+        mergeObject.setNameCacheInErms(ermsName.getNameCache());
+        mergeObject.setNameCacheInFaunaEu(faunaEuName.getNameCache());
+
+        mergeObject.setAuthorInErms(ermsName.getAuthorshipCache());
+        mergeObject.setAuthorInFaunaEu(faunaEuName.getAuthorshipCache());
+        Set<Taxon> taxa = ermsName.getTaxa();
+        if (!taxa.isEmpty()){
+            mergeObject.setStatInErms(true);
+            Iterator<Taxon> taxaIterator = taxa.iterator();
+            Taxon taxon = null;
+            while (taxaIterator.hasNext()){
+                taxon = taxaIterator.next();
+                if (!taxon.isMisapplication()){
+                    break;
+                }
+            }
+            Set<TaxonNode> nodes = taxon.getTaxonNodes();
+            Iterator<TaxonNode> taxonNodeIterator = nodes.iterator();
+            TaxonNode node, parentNode = null;
+            while (taxonNodeIterator.hasNext()){
+                node = taxonNodeIterator.next();
+                if (!node.isTopmostNode()){
+                    parentNode = node.getParent();
+                }
+            }
+            //TODO: ändern mit erweitertem Initializer..
+            if (parentNode != null){
+                TaxonName parentName = CdmBase.deproxy(parentNode.getTaxon().getName());
+                String parentNameCache = parentName.getNameCache();
+                mergeObject.setParentStringInErms(parentNameCache);
+                mergeObject.setParentRankStringInErms(parentName.getRank().getLabel());
+            }
+        }else{
+            mergeObject.setStatInErms(false);
+            TaxonNode parentNode = getAcceptedNode(ermsName);
+            //TODO: ändern mit erweitertem Initializer..
+            if (parentNode != null){
+                TaxonName parentName = HibernateProxyHelper.deproxy(parentNode.getTaxon().getName());
+                String parentNameCache = parentName.getNameCache();
+                mergeObject.setParentStringInErms(parentNameCache);
+                mergeObject.setParentRankStringInErms(parentName.getRank().getLabel());
+            }
+        }
+        taxa = faunaEuName.getTaxa();
+        if (!taxa.isEmpty()){
+            mergeObject.setStatInFaunaEu(true);
+            Iterator<Taxon> taxaIterator = taxa.iterator();
+            Taxon taxon = null;
+            while (taxaIterator.hasNext()){
+                taxon = taxaIterator.next();
+                if (!taxon.isMisapplication()){
+                    break;
+                }
+            }
+            Set<TaxonNode> nodes = taxon.getTaxonNodes();
+            Iterator<TaxonNode> taxonNodeIterator = nodes.iterator();
+            TaxonNode node, parentNode = null;
+            while (taxonNodeIterator.hasNext()){
+                node = taxonNodeIterator.next();
+                if (!node.isTopmostNode()){
+                    parentNode = node.getParent();
+                }
+            }
+            //TODO: ändern mit erweitertem Initializer..
+            if (parentNode != null){
+                if (parentNode.getTaxon().getName().isZoological()){
+
+                    IZoologicalName parentName = CdmBase.deproxy(parentNode.getTaxon().getName());
+                    String parentNameCache = parentName.getNameCache();
+                    mergeObject.setParentStringInFaunaEu(parentNameCache);
+                    mergeObject.setParentRankStringInFaunaEu(parentName.getRank().getLabel());
+
+                }else{
+                    logger.debug("no zoologicalName: " + parentNode.getTaxon().getName().getTitleCache() +" . "+parentNode.getTaxon().getName().getUuid());
+                }
+            }
+        }else{
+            mergeObject.setStatInFaunaEu(false);
+            TaxonNode parentNode = getAcceptedNode(faunaEuName);
+            //TODO: ändern mit erweitertem Initializer..
+            if (parentNode != null){
+                TaxonName parentName = HibernateProxyHelper.deproxy(parentNode.getTaxon().getName());
+                String parentNameCache = parentName.getNameCache();
+                mergeObject.setParentStringInFaunaEu(parentNameCache);
+                mergeObject.setParentRankStringInFaunaEu(parentName.getRank().getLabel());
+
+            }
+        }
+
+        mergeObject.setRankInErms(ermsName.getRank().getLabel());
+        mergeObject.setRankInFaunaEu(faunaEuName.getRank().getLabel());
+
+        //set parent informations
+
+        /*
+        Set<HybridRelationship> parentRelations = zooName.getParentRelationships();
+        Iterator parentIterator = parentRelations.iterator();
+        HybridRelationship parentRel;
+        ZoologicalName parentName;
+        while (parentIterator.hasNext()){
+            parentRel = (HybridRelationship)parentIterator.next();
+            parentName = (ZoologicalName)parentRel.getParentName();
+            mergeObject.setParentRankStringInErms(parentName.getRank().getLabel());
+            mergeObject.setParentStringInErms(parentName.getNameCache());
+        }
+
+        parentRelations = zooName2.getParentRelationships();
+        parentIterator = parentRelations.iterator();
+
+        while (parentIterator.hasNext()){
+            parentRel = (HybridRelationship)parentIterator.next();
+            parentName = (ZoologicalName)parentRel.getParentName();
+            mergeObject.setParentRankStringInFaunaEu(parentName.getRank().getLabel());
+            mergeObject.setParentStringInFaunaEu(parentName.getNameCache());
+        }*/
+        merge.add(mergeObject);
+    }
+
 	private TaxonNode getAcceptedNode(TaxonName ermsName) {
 		Set<TaxonBase> taxonBases = ermsName.getTaxonBases();
 		Taxon taxon = null;
 		if (taxonBases != null && !taxonBases.isEmpty()) {
-			TaxonBase taxonBase = taxonBases.iterator().next();
+			TaxonBase<?> taxonBase = taxonBases.iterator().next();
 			if (taxonBase instanceof Synonym) {
 				taxon = ((Synonym)taxonBase).getAcceptedTaxon();
 			}
 		}
-		
-		
+
 		Set<TaxonNode> nodes = taxon.getTaxonNodes();
-		
-		TaxonNode node, parentNode = null;
+
+		TaxonNode parentNode = null;
 		if (nodes != null && !nodes.isEmpty()) {
 			parentNode = nodes.iterator().next();
 		}
 		return parentNode;
 	}
 
-
-	/**
-	 * @param ermsName
-	 * @param taxon
-	 * @return
-	 */
 	private Taxon getAcceptedTaxon(TaxonName ermsName) {
 		Taxon taxon = null;
 		if (ermsName.getTaxa() != null && !ermsName.getTaxa().isEmpty()){
 			taxon = ermsName.getTaxa().iterator().next();
-			
+
 		}else if (ermsName.getTaxonBases() != null && !ermsName.getTaxonBases().isEmpty()){
-			TaxonBase taxonBase = ermsName.getTaxonBases().iterator().next();
+			TaxonBase<?> taxonBase = ermsName.getTaxonBases().iterator().next();
 			if (taxonBase instanceof Synonym) {
 				Synonym syn = (Synonym)taxonBase;
 				taxon = syn.getAcceptedTaxon();
@@ -614,4 +575,10 @@ public class FaunaEuErmsFindIdenticalNamesActivator {
 		}
 		return taxon;
 	}
+
+    public static void main(String[] args) {
+        FaunaEuErmsFindIdenticalNamesActivator activator = new FaunaEuErmsFindIdenticalNamesActivator();
+        activator.invoke(pesiSource);
+        System.exit(0);
+    }
 }
diff --git a/cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/merging/FaunaEuErmsMerging.java b/cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/merging/FaunaEuErmsMerging.java
deleted file mode 100644
index 5b317ad9..00000000
--- a/cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/merging/FaunaEuErmsMerging.java
+++ /dev/null
@@ -1,171 +0,0 @@
-package eu.etaxonomy.cdm.io.pesi.merging;
-
-import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
-
-public class FaunaEuErmsMerging {
-	
-	private String uuidFaunaEu;
-	private String uuidErms;
-	
-	private String idInFaunaEu;
-	private String idInErms;
-	
-	private String nameCacheInFaunaEu;
-	private String nameCacheInErms;
-	
-	private boolean statInFaunaEu;
-	private boolean statInErms;
-	
-	private String authorInFaunaEu;
-	private String authorInErms;
-	
-	private String rankInFaunaEu;
-	private String rankInErms;
-	
-	private TaxonNodeDto phylumInFaunaEu;
-	private TaxonNodeDto phylumInErms;
-	
-	private String parentStringInFaunaEu;
-	private String parentStringInErms;
-	
-	private String parentRankStringInFaunaEu;
-	private String parentRankStringInErms;
-	
-	
-	public String getUuidFaunaEu() {
-		return uuidFaunaEu;
-	}
-
-	public void setUuidFaunaEu(String uuidFaunaEu) {
-		this.uuidFaunaEu = uuidFaunaEu;
-	}
-	public String getUuidErms() {
-		return uuidErms;
-	}
-
-	public void setUuidErms(String uuidErms) {
-		this.uuidErms = uuidErms;
-	}
-	
-	public String getParentRankStringInFaunaEu() {
-		return parentRankStringInFaunaEu;
-	}
-
-	public void setParentRankStringInFaunaEu(String parentRankStringInFaunaEu) {
-		this.parentRankStringInFaunaEu = parentRankStringInFaunaEu;
-	}
-
-	public String getParentRankStringInErms() {
-		return parentRankStringInErms;
-	}
-
-	public void setParentRankStringInErms(String parentRankStringInErms) {
-		this.parentRankStringInErms = parentRankStringInErms;
-	}
-
-	public String getParentStringInFaunaEu() {
-		return parentStringInFaunaEu;
-	}
-
-	public void setParentStringInFaunaEu(String parentStringInFaunaEu) {
-		this.parentStringInFaunaEu = parentStringInFaunaEu;
-	}
-
-	public String getParentStringInErms() {
-		return parentStringInErms;
-	}
-
-	public void setParentStringInErms(String parentStringInErms) {
-		this.parentStringInErms = parentStringInErms;
-	}
-
-	public String getRankInFaunaEu() {
-		return rankInFaunaEu;
-	}
-
-	public void setRankInFaunaEu(String rankInFaunaEu) {
-		this.rankInFaunaEu = rankInFaunaEu;
-	}
-
-	public TaxonNodeDto getPhylumInFaunaEu() {
-		return phylumInFaunaEu;
-	}
-
-	public void setPhylumInFaunaEu(TaxonNodeDto phylumInFaunaEu) {
-		this.phylumInFaunaEu = phylumInFaunaEu;
-	}
-
-	public TaxonNodeDto getPhylumInErms() {
-		return phylumInErms;
-	}
-
-	public void setPhylumInErms(TaxonNodeDto phylumInErms) {
-		this.phylumInErms = phylumInErms;
-	}
-
-	public String getRankInErms() {
-		return rankInErms;
-	}
-
-	public void setRankInErms(String rankInErms) {
-		this.rankInErms = rankInErms;
-	}
-	
-	
-	public static FaunaEuErmsMerging newInstance(){
-		return new FaunaEuErmsMerging();
-		
-	}
-	
-	public boolean isStatInFaunaEu() {
-		return statInFaunaEu;
-	}
-	public void setStatInFaunaEu(boolean statInFaunaEu) {
-		this.statInFaunaEu = statInFaunaEu;
-	}
-	public boolean isStatInErms() {
-		return statInErms;
-	}
-	public void setStatInErms(boolean statInErms) {
-		this.statInErms = statInErms;
-	}
-	public String getAuthorInFaunaEu() {
-		return authorInFaunaEu;
-	}
-	public void setAuthorInFaunaEu(String authorInFaunaEu) {
-		this.authorInFaunaEu = authorInFaunaEu;
-	}
-	public String getAuthorInErms() {
-		return authorInErms;
-	}
-	public void setAuthorInErms(String authorInErms) {
-		this.authorInErms = authorInErms;
-	}
-	public String getIdInFaunaEu() {
-		return idInFaunaEu;
-	}
-	public void setIdInFaunaEu(String idInFaunaEu) {
-		this.idInFaunaEu = idInFaunaEu;
-	}
-	public String getIdInErms() {
-		return idInErms;
-	}
-	public void setIdInErms(String idInErms) {
-		this.idInErms = idInErms;
-	}
-	public String getNameCacheInFaunaEu() {
-		return nameCacheInFaunaEu;
-	}
-	public void setNameCacheInFaunaEu(String nameCacheInFaunaEu) {
-		this.nameCacheInFaunaEu = nameCacheInFaunaEu;
-	}
-	public String getNameCacheInErms() {
-		return nameCacheInErms;
-	}
-	public void setNameCacheInErms(String nameCacheInErms) {
-		this.nameCacheInErms = nameCacheInErms;
-	}
-
-	
-	
-}
diff --git a/cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/merging/PesiMergeObject.java b/cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/merging/PesiMergeObject.java
new file mode 100644
index 00000000..8df0eb2d
--- /dev/null
+++ b/cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/merging/PesiMergeObject.java
@@ -0,0 +1,171 @@
+package eu.etaxonomy.cdm.io.pesi.merging;
+
+import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
+
+public class PesiMergeObject {
+
+	private String uuidFaunaEu;
+	private String uuidErms;
+
+	private String idInFaunaEu;
+	private String idInErms;
+
+	private String nameCacheInFaunaEu;
+	private String nameCacheInErms;
+
+	private boolean statInFaunaEu;
+	private boolean statInErms;
+
+	private String authorInFaunaEu;
+	private String authorInErms;
+
+	private String rankInFaunaEu;
+	private String rankInErms;
+
+	private TaxonNodeDto phylumInFaunaEu;
+	private TaxonNodeDto phylumInErms;
+
+	private String parentStringInFaunaEu;
+	private String parentStringInErms;
+
+	private String parentRankStringInFaunaEu;
+	private String parentRankStringInErms;
+
+
+	public String getUuidFaunaEu() {
+		return uuidFaunaEu;
+	}
+	public void setUuidFaunaEu(String uuidFaunaEu) {
+		this.uuidFaunaEu = uuidFaunaEu;
+	}
+
+	public String getUuidErms() {
+		return uuidErms;
+	}
+
+	public void setUuidErms(String uuidErms) {
+		this.uuidErms = uuidErms;
+	}
+
+	public String getParentRankStringInFaunaEu() {
+		return parentRankStringInFaunaEu;
+	}
+
+	public void setParentRankStringInFaunaEu(String parentRankStringInFaunaEu) {
+		this.parentRankStringInFaunaEu = parentRankStringInFaunaEu;
+	}
+
+	public String getParentRankStringInErms() {
+		return parentRankStringInErms;
+	}
+
+	public void setParentRankStringInErms(String parentRankStringInErms) {
+		this.parentRankStringInErms = parentRankStringInErms;
+	}
+
+	public String getParentStringInFaunaEu() {
+		return parentStringInFaunaEu;
+	}
+
+	public void setParentStringInFaunaEu(String parentStringInFaunaEu) {
+		this.parentStringInFaunaEu = parentStringInFaunaEu;
+	}
+
+	public String getParentStringInErms() {
+		return parentStringInErms;
+	}
+
+	public void setParentStringInErms(String parentStringInErms) {
+		this.parentStringInErms = parentStringInErms;
+	}
+
+	public String getRankInFaunaEu() {
+		return rankInFaunaEu;
+	}
+
+	public void setRankInFaunaEu(String rankInFaunaEu) {
+		this.rankInFaunaEu = rankInFaunaEu;
+	}
+
+	public TaxonNodeDto getPhylumInFaunaEu() {
+		return phylumInFaunaEu;
+	}
+
+	public void setPhylumInFaunaEu(TaxonNodeDto phylumInFaunaEu) {
+		this.phylumInFaunaEu = phylumInFaunaEu;
+	}
+
+	public TaxonNodeDto getPhylumInErms() {
+		return phylumInErms;
+	}
+
+	public void setPhylumInErms(TaxonNodeDto phylumInErms) {
+		this.phylumInErms = phylumInErms;
+	}
+
+	public String getRankInErms() {
+		return rankInErms;
+	}
+
+	public void setRankInErms(String rankInErms) {
+		this.rankInErms = rankInErms;
+	}
+
+
+	public static PesiMergeObject newInstance(){
+		return new PesiMergeObject();
+
+	}
+
+	public boolean isStatInFaunaEu() {
+		return statInFaunaEu;
+	}
+	public void setStatInFaunaEu(boolean statInFaunaEu) {
+		this.statInFaunaEu = statInFaunaEu;
+	}
+	public boolean isStatInErms() {
+		return statInErms;
+	}
+	public void setStatInErms(boolean statInErms) {
+		this.statInErms = statInErms;
+	}
+	public String getAuthorInFaunaEu() {
+		return authorInFaunaEu;
+	}
+	public void setAuthorInFaunaEu(String authorInFaunaEu) {
+		this.authorInFaunaEu = authorInFaunaEu;
+	}
+	public String getAuthorInErms() {
+		return authorInErms;
+	}
+	public void setAuthorInErms(String authorInErms) {
+		this.authorInErms = authorInErms;
+	}
+	public String getIdInFaunaEu() {
+		return idInFaunaEu;
+	}
+	public void setIdInFaunaEu(String idInFaunaEu) {
+		this.idInFaunaEu = idInFaunaEu;
+	}
+	public String getIdInErms() {
+		return idInErms;
+	}
+	public void setIdInErms(String idInErms) {
+		this.idInErms = idInErms;
+	}
+	public String getNameCacheInFaunaEu() {
+		return nameCacheInFaunaEu;
+	}
+	public void setNameCacheInFaunaEu(String nameCacheInFaunaEu) {
+		this.nameCacheInFaunaEu = nameCacheInFaunaEu;
+	}
+	public String getNameCacheInErms() {
+		return nameCacheInErms;
+	}
+	public void setNameCacheInErms(String nameCacheInErms) {
+		this.nameCacheInErms = nameCacheInErms;
+	}
+
+
+
+}
