Project

General

Profile

« Previous | Next » 

Revision 9aa22b7e

Added by Andreas Müller almost 8 years ago

#5958 fix findByName in pesi

View differences:

cdm-pesi/src/main/java/eu/etaxonomy/cdm/app/pesi/merging/FaunaEuErmsMergeActivator.java
1
package eu.etaxonomy.cdm.app.pesi.merging;
2

  
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileNotFoundException;
6
import java.io.FileReader;
7
import java.io.IOException;
8
import java.util.ArrayList;
9
import java.util.HashSet;
10
import java.util.Iterator;
11
import java.util.List;
12
import java.util.Set;
13
import java.util.StringTokenizer;
14
import java.util.UUID;
15

  
16
import org.apache.log4j.Logger;
17

  
18
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
19
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
20
import eu.etaxonomy.cdm.api.service.pager.Pager;
21
import eu.etaxonomy.cdm.app.common.CdmDestinations;
22
import eu.etaxonomy.cdm.app.common.TestDatabase;
23
import eu.etaxonomy.cdm.database.DbSchemaValidation;
24
import eu.etaxonomy.cdm.database.ICdmDataSource;
25
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26
import eu.etaxonomy.cdm.model.common.Annotation;
27
import eu.etaxonomy.cdm.model.common.Credit;
28
import eu.etaxonomy.cdm.model.common.Extension;
29
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
30
import eu.etaxonomy.cdm.model.common.Marker;
31
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
32
import eu.etaxonomy.cdm.model.description.Distribution;
33
import eu.etaxonomy.cdm.model.description.Feature;
34
import eu.etaxonomy.cdm.model.description.TaxonDescription;
35
import eu.etaxonomy.cdm.model.name.NameRelationship;
36
import eu.etaxonomy.cdm.model.name.Rank;
37
import eu.etaxonomy.cdm.model.name.ZoologicalName;
38
import eu.etaxonomy.cdm.model.reference.Reference;
39
import eu.etaxonomy.cdm.model.taxon.Synonym;
40
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
41
import eu.etaxonomy.cdm.model.taxon.Taxon;
42
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
44

  
45
public class FaunaEuErmsMergeActivator {
46

  
47
//	static final ICdmDataSource faunaEuropaeaSource = CdmDestinations.cdm_test_patricia();
48
	static final ICdmDataSource faunaEuropaeaSource = CdmDestinations.localH2();
49

  
50
	static final int faunaEuUuid = 0;
51
	static final int ermsUuid = 9;
52
	static final int rankFaunaEu = 4;
53
	static final int rankErms = 13;
54

  
55
	CdmApplicationController appCtrInit;
56

  
57
	private static final Logger logger = Logger.getLogger(FaunaEuErmsMergeActivator.class);
58

  
59
	//csv files starting with...
60
	static String sFileName = "c:\\test";
61

  
62
	private void initDb(ICdmDataSource db) {
63

  
64
		// Init source DB
65
		appCtrInit = TestDatabase.initDb(db, DbSchemaValidation.VALIDATE, false);
66

  
67

  
68
	}
69

  
70
	public static void main(String[] args) {
71

  
72
		FaunaEuErmsMergeActivator sc = new FaunaEuErmsMergeActivator();
73

  
74
		sc.initDb(faunaEuropaeaSource);
75

  
76
		sc.mergeAuthors();
77

  
78
		//set the ranks of Agnatha and Gnathostomata to 50 instead of 45
79
		List<TaxonBase> taxaToChangeRank = new ArrayList<TaxonBase>();
80
		Pager<TaxonBase> agnatha = sc.appCtrInit.getTaxonService().findTaxaByName(TaxonBase.class, "Agnatha", null, null, null, Rank.INFRAPHYLUM(), 10, 0);
81
		List<TaxonBase> agnathaList = agnatha.getRecords();
82
		taxaToChangeRank.addAll(agnathaList);
83
		Pager<TaxonBase> gnathostomata = sc.appCtrInit.getTaxonService().findTaxaByName(TaxonBase.class, "Gnathostomata", null, null, null, Rank.INFRAPHYLUM(), 10, 0);
84
		List<TaxonBase> gnathostomataList = gnathostomata.getRecords();
85
		taxaToChangeRank.addAll(gnathostomataList);
86

  
87
		sc.setSpecificRank(taxaToChangeRank,Rank.SUPERCLASS());
88

  
89
		//ermsTaxon is accepted, fauna eu taxon is synonym
90
		//ermsTaxon is synonym, faunaEu is accepted
91

  
92
		sc.mergeDiffStatus();
93

  
94
		//erms is synonym, faunaEu as well
95

  
96
		// erms is accepted, faunaEu as well
97

  
98

  
99

  
100

  
101

  
102

  
103

  
104
	}
105

  
106
	private static List readCsvFile(String fileName){
107

  
108
		List<List<String>> result = new ArrayList<List<String>>();
109
		File file = new File(fileName);
110
		BufferedReader bufRdr;
111
		try {
112
			bufRdr = new BufferedReader(new FileReader(file));
113
			String line = null;
114
			//read each line of text file
115
			while((line = bufRdr.readLine()) != null){
116
				StringTokenizer st = new StringTokenizer(line,",");
117
				List<String> rowList = new ArrayList<String>();
118
				while (st.hasMoreTokens()){
119
					//get next token and store it in the array
120
					rowList.add(st.nextToken());
121
				}
122
			result.add(rowList);
123
			}
124
			//close the file
125
			bufRdr.close();
126
		} catch (FileNotFoundException e) {
127
			// TODO Auto-generated catch block
128
			e.printStackTrace();
129
		} catch (IOException e) {
130
			// TODO Auto-generated catch block
131
			e.printStackTrace();
132
		}
133
		return result;
134
	}
135

  
136

  
137
	private void mergeAuthors(){
138
		List<List<String>> authors = readCsvFile(sFileName + "_authors.csv");
139
		//authors: get firstAuthor if isFauEu = 1 otherwise get secondAuthor
140

  
141
		Iterator<List<String>> authorIterator = authors.iterator();
142
		List<String> row;
143
		TaxonBase taxonFaunaEu;
144
		TaxonBase taxonErms;
145
		List<TaxonBase> taxaToSave = new ArrayList<TaxonBase>();
146
		while (authorIterator.hasNext()){
147
			row = authorIterator.next();
148
			UUID uuidFaunaEu = UUID.fromString(row.get(faunaEuUuid));
149
			UUID uuidErms = UUID.fromString(row.get(ermsUuid));
150
			taxonFaunaEu = appCtrInit.getTaxonService().find(uuidFaunaEu);
151
			taxonErms = appCtrInit.getTaxonService().find(uuidFaunaEu);
152

  
153
			if (Integer.parseInt(row.get(18)) == 1){
154
				//isFaunaEu = 1 -> copy the author of Fauna Europaea to Erms
155
				if (((ZoologicalName)taxonFaunaEu.getName()).getBasionymAuthorship()!= null){
156
					((ZoologicalName)taxonErms.getName()).setBasionymAuthorship(((ZoologicalName)taxonFaunaEu.getName()).getBasionymAuthorship());
157
				}
158
				if (((ZoologicalName)taxonFaunaEu.getName()).getCombinationAuthorship()!= null){
159
					((ZoologicalName)taxonErms.getName()).setCombinationAuthorship(((ZoologicalName)taxonFaunaEu.getName()).getCombinationAuthorship());
160
				}
161
				((ZoologicalName)taxonErms.getName()).generateAuthorship();
162
				taxaToSave.add(taxonErms);
163
			}else{
164
				if (((ZoologicalName)taxonErms.getName()).getBasionymAuthorship()!= null){
165
					((ZoologicalName)taxonFaunaEu.getName()).setBasionymAuthorship(((ZoologicalName)taxonErms.getName()).getBasionymAuthorship());
166
				}
167
				if (((ZoologicalName)taxonErms.getName()).getCombinationAuthorship()!= null){
168
					((ZoologicalName)taxonFaunaEu.getName()).setCombinationAuthorship(((ZoologicalName)taxonErms.getName()).getCombinationAuthorship());
169
				}
170
				((ZoologicalName)taxonFaunaEu.getName()).generateAuthorship();
171
				taxaToSave.add(taxonFaunaEu);
172
			}
173

  
174

  
175
		}
176
	}
177

  
178
	public void setSpecificRank(List<TaxonBase> taxa, Rank rank){
179

  
180
		for (TaxonBase taxon: taxa){
181
			taxon.getName().setRank(rank);
182
		}
183
	}
184

  
185
	private void mergeDiffStatus(){
186
		List<List<String>> diffStatus = readCsvFile(sFileName + "_status.csv");
187

  
188
		//find all taxa accepted in erms, but synonyms in FauEu  and the same rank
189
		List<List<String>> accErmsSynFaunaEu = new ArrayList<List<String>>();
190
		for (List<String> rowList: diffStatus){
191
			if ((rowList.get(5).equals("synonym")) && (rowList.get(rankFaunaEu).equals(rowList.get(rankErms)))){
192
				//both conditions are true
193
				accErmsSynFaunaEu.add(rowList);
194
			}
195
		}
196
		mergeErmsAccFaunaEuSyn(accErmsSynFaunaEu);
197

  
198
		//find all taxa accepted in faunaEu, but synonyms in Erms and the same rank
199
		List<List<String>> synErmsAccFaunaEu = new ArrayList<List<String>>();
200
		for (List<String> rowList: diffStatus){
201
			if ((rowList.get(5).equals("accepted")) && (rowList.get(rankFaunaEu).equals(rowList.get(rankErms)))){
202
				//both conditions are true
203
				synErmsAccFaunaEu.add(rowList);
204
			}
205
		}
206
		mergeErmsSynFaunaEuAcc(synErmsAccFaunaEu);
207

  
208

  
209
	}
210

  
211
	private void mergeSameStatus(){
212
		List<List<String>> sameStatus = readCsvFile(sFileName + "_names.csv");
213

  
214
		TaxonBase taxonFaunaEu;
215
		TaxonBase taxonErms;
216

  
217
		for (List<String> row: sameStatus){
218
			taxonFaunaEu = appCtrInit.getTaxonService().find(UUID.fromString(row.get(faunaEuUuid)));
219
			taxonErms = appCtrInit.getTaxonService().find(UUID.fromString(row.get(ermsUuid)));
220
			moveAllInformationsFromFaunaEuToErms(taxonFaunaEu, taxonErms);
221
			if (taxonErms instanceof Taxon){
222
				moveFaunaEuSynonymsToErmsTaxon((Taxon)taxonFaunaEu, (Taxon)taxonErms);
223
			}
224
		}
225
	}
226

  
227

  
228

  
229
	private void mergeErmsAccFaunaEuSyn(List<List<String>> ermsAccFaEuSyn){
230

  
231
		// update nameRelationships -> if the nameRelationship does not exist, then create a new one with ermsAcc as relatedTo TaxonName
232
		updateNameRelationships(ermsAccFaEuSyn);
233

  
234
		//delete all synonymRelationships of FaunaEu Syn
235
		for (List<String> rowList: ermsAccFaEuSyn){
236
			UUID faunaUUID = UUID.fromString(rowList.get(faunaEuUuid));
237
			//UUID ermsUUID = UUID.fromString(rowList.get(ermsUuid));
238
			Synonym syn = (Synonym)appCtrInit.getTaxonService().find(faunaUUID);
239
			appCtrInit.getTaxonService().deleteSynonymRelationships(syn);
240
		}
241

  
242
		//merge the infos of
243

  
244

  
245
	}
246

  
247
	private  void mergeErmsSynFaunaEuAcc (List<List<String>> ermsAccFaEuSyn){
248
		//occurence: verkn�pfe statt dem Fauna Europaea Taxon das akzeptierte Taxon, des Synonyms mit der Occurence (CDM -> distribution)
249
		//suche distribution (�ber das Taxon der TaxonDescription), dessen Taxon, das entsprechende Fauna Eu Taxon ist und verkn�pfe es mit dem akzeptieren Taxon des Erms Syn
250
		Taxon taxonFaunaEu = null;
251
		Taxon taxonErms = null;
252
		Synonym synErms = null;
253
		for (List<String> row: ermsAccFaEuSyn){
254
			taxonFaunaEu = (Taxon)appCtrInit.getTaxonService().find(UUID.fromString(row.get(faunaEuUuid)));
255
			synErms = (Synonym)appCtrInit.getTaxonService().find(UUID.fromString(row.get(ermsUuid)));
256
			synErms = HibernateProxyHelper.deproxy(synErms, Synonym.class);
257
			Set<SynonymRelationship> synRel=synErms.getSynonymRelations();
258

  
259
			if (synRel.size()>1){
260
				//TODO: which Relationship??
261
				Iterator<SynonymRelationship> iterator = synRel.iterator();
262
				taxonErms = iterator.next().getAcceptedTaxon();
263
			}else if (synRel.size() == 1){
264
				Iterator<SynonymRelationship> iterator = synRel.iterator();
265
				taxonErms = iterator.next().getAcceptedTaxon();
266
			} else {
267
				taxonErms = null;
268
				logger.debug("There is no SynonymRelationship for the synonym" + synErms.getTitleCache());
269
			}
270

  
271
			Set<Feature> features = new HashSet<Feature>();
272
			features.add(Feature.DISTRIBUTION());
273
			List<String> propertyPaths = new ArrayList<String>();
274
			propertyPaths.add("inDescription.Taxon.*");
275
			List<Distribution> distributions = appCtrInit.getDescriptionService().getDescriptionElementsForTaxon(taxonFaunaEu, features, Distribution.class, 10, 0, null);
276

  
277

  
278
			for(Distribution distribution: distributions){
279
				TaxonDescription description = (TaxonDescription)distribution.getInDescription();
280
				TaxonDescription newDescription = TaxonDescription.NewInstance(taxonErms);
281
				newDescription.addElement(distribution);
282
				try{
283
					appCtrInit.getDescriptionService().delete(description);
284
				}catch (Exception e){
285
					logger.debug("The description of" + description.getTaxon().getTitleCache() + description.getTitleCache() + "can't be deleted because it is referenced.");
286
				}
287
			}
288

  
289

  
290
			//Child-Parent Relationship aktualisieren -> dem Child des Fauna Europaea Taxons als parent das akzeptierte Taxon von synErms
291
			Set<TaxonNode> nodesErms = taxonErms.getTaxonNodes();
292
			Set<TaxonNode> nodesFaunaEu =taxonFaunaEu.getTaxonNodes();
293
			if (nodesFaunaEu.size()>1 || nodesFaunaEu.isEmpty()){
294

  
295
			}else{
296
				Iterator<TaxonNode> iteratorNodesErms = nodesErms.iterator();
297

  
298
				Iterator<TaxonNode> iteratorNodesFaunaEu = nodesFaunaEu.iterator();
299
				TaxonNode node = iteratorNodesFaunaEu.next();
300
				List<TaxonNode> children = node.getChildNodes();
301
				Iterator<TaxonNode> childrenIterator = children.iterator();
302
				TaxonNode childNode;
303
				if (iteratorNodesErms.hasNext()){
304
					TaxonNode ermsNode = iteratorNodesErms.next();
305
					while (childrenIterator.hasNext()){
306
						childNode = childrenIterator.next();
307
						ermsNode.addChildNode(childNode, childNode.getReference(), childNode.getMicroReference());
308
					}
309
				}
310

  
311
			}
312
			moveFaunaEuSynonymsToErmsTaxon(taxonFaunaEu, taxonErms);
313
			moveAllInformationsFromFaunaEuToErms(taxonFaunaEu, taxonErms);
314
			moveOriginalDbToErmsTaxon(taxonFaunaEu, taxonErms);
315
			//neue sec Referenz an das ErmsTaxon oder an das Synonym und Taxon oder nur Synonym??
316
			try{
317
				deleteFaunaEuTaxon(taxonFaunaEu);
318
			}catch(ReferencedObjectUndeletableException e){
319
				logger.debug("The taxon " + taxonFaunaEu.getTitleCache() + " can't be deleted because it is referenced.");
320
			}
321
		}
322

  
323

  
324

  
325

  
326
	}
327

  
328

  
329

  
330
	private void updateNameRelationships(List<List<String>> ermsAccFaEuSyn){
331
		//suche alle NameRelationships aus FaunaEu und Erms, wo (faunaEu)relatedFrom.name.titleCache = (erms)relatedFrom.name.titleCache und ersetze in der faunaEu Relationship den relatedTo.name durch den relatedTo.name der erms-relationship
332
		//wenn es diese relationship noch nicht gibt und der typ der gleiche ist!!
333
		//wenn der relatedTo Name zu einem Erms Taxon und einem FaunaEu Synonym geh�rt
334

  
335
		Synonym synFaunaEu;
336
		Taxon taxonErms;
337
		for (List<String> row: ermsAccFaEuSyn){
338
			synFaunaEu = (Synonym)appCtrInit.getTaxonService().find(UUID.fromString(row.get(faunaEuUuid)));
339
			taxonErms = (Taxon)appCtrInit.getTaxonService().find(UUID.fromString(row.get(ermsUuid)));
340
			List<NameRelationship> relSynFaunaEu = appCtrInit.getNameService().listToNameRelationships(synFaunaEu.getName(), null, 100, 0, null, null);
341
			List<NameRelationship> relTaxonErms = appCtrInit.getNameService().listToNameRelationships(taxonErms.getName(), null, 100, 0, null, null);
342

  
343
			List<NameRelationship> deleteRel = new ArrayList<NameRelationship>();
344
			for (NameRelationship relFauEu: relSynFaunaEu){
345
				boolean createNewRelationship = true;
346
				for (NameRelationship relErms: relTaxonErms){
347
					if ((relErms.getFromName().getTitleCache().equals(relFauEu.getFromName().getTitleCache())) && (relErms.getToName().getTitleCache().equals(relFauEu.getFromName().getTitleCache()))){
348
						//delete the faunaEu relationship because there exist an analogous relationship in erms
349
						deleteRel.add(relFauEu);
350
						createNewRelationship = false;
351
						break;
352
					}
353
				}
354
				if (createNewRelationship){
355
					//if relationship does not exist, create a new one with erms synonym
356
					taxonErms.getName().addRelationshipFromName(relFauEu.getFromName(), relFauEu.getType(), relFauEu.getRuleConsidered());
357
				}
358
			}
359

  
360
		}
361
	}
362

  
363
	private void updateSynonymRelationships(List<List<String>> ermsSynFaEuAcc){
364
//		-- Update queries for RelTaxon (synonym relationships - move relationships to ERMS accepted taxon if not already existent or delete if already existent)
365
//		UPDATE RelTaxon_1 SET RelTaxon_1.TaxonFk2 = RT.TaxonFk2
366
//		FROM         Taxon AS ERMSSyn INNER JOIN
367
//		                      Taxon AS FaEuAcc ON ERMSSyn.RankFk = FaEuAcc.RankFk AND ERMSSyn.FullName = FaEuAcc.FullName AND
368
//		                      ERMSSyn.TaxonStatusFk <> ISNULL(FaEuAcc.TaxonStatusFk, 0) INNER JOIN
369
//		                      RelTaxon AS RT ON ERMSSyn.TaxonId = RT.TaxonFk1 INNER JOIN
370
//		                      RelTaxon AS RelTaxon_1 ON FaEuAcc.TaxonId = RelTaxon_1.TaxonFk2 INNER JOIN
371
//		                      Taxon AS FaEuSyn ON RelTaxon_1.TaxonFk1 = FaEuSyn.TaxonId LEFT OUTER JOIN
372
//		                      Taxon AS ERMSAllSyn ON RT.TaxonFk1 = ERMSAllSyn.TaxonId AND FaEuSyn.FullName <> ERMSAllSyn.FullName --(!!)
373
//		WHERE     (ERMSSyn.OriginalDB = N'ERMS') AND (RT.RelTaxonQualifierFk > 100) AND (ERMSSyn.TaxonStatusFk <> 1) AND (ERMSSyn.KingdomFk = 2) AND
374
//		                      (FaEuAcc.OriginalDB = N'FaEu') AND (RelTaxon_1.RelTaxonQualifierFk > 100)
375
		Taxon taxonFaunaEu;
376
		Synonym synErms;
377
		Taxon taxonErms;
378
		Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
379
		for (List<String> row: ermsSynFaEuAcc){
380
			taxonFaunaEu = (Taxon)appCtrInit.getTaxonService().find(UUID.fromString(row.get(faunaEuUuid)));
381
			synErms = (Synonym)appCtrInit.getTaxonService().find(UUID.fromString(row.get(ermsUuid)));
382
			acceptedTaxa.clear();
383
			acceptedTaxa.addAll( synErms.getAcceptedTaxa());
384
			if (!acceptedTaxa.isEmpty()){
385
				taxonErms = acceptedTaxa.iterator().next();
386
				if (acceptedTaxa.size() > 1){
387
					logger.debug("There are more than one accepted taxon for synonym " + synErms.getTitleCache());
388
				}
389
			}else{
390
				taxonErms = null;
391
				logger.debug("There is no accepted taxon for synonym "  + synErms.getTitleCache());
392
			}
393

  
394
			if (taxonErms != null){
395
				List<SynonymRelationship> relTaxonFaunaEu = appCtrInit.getTaxonService().listSynonymRelationships(taxonFaunaEu, null, 100, 0, null, null, Direction.relatedTo);
396
				List<SynonymRelationship> relTaxonErms = appCtrInit.getTaxonService().listSynonymRelationships(taxonErms, null, 100, 0, null, null, Direction.relatedTo);
397

  
398
				List<SynonymRelationship> deleteRel = new ArrayList<SynonymRelationship>();
399
				for (SynonymRelationship relFauEu: relTaxonFaunaEu){
400
					//TODO: wenn es noch keine SynonymRelationship gibt zu einem Synonym mit gleichem Namen, dann erzeuge die SynonymRelationship vom FaunaEuSyn (des FaunaEu Taxons, dass identischen Namen hat) zum akzeptierten Taxon des Erms Syn
401
					boolean createNewRelationship = true;
402
					for (SynonymRelationship relErms: relTaxonErms){
403
						if (relErms.getSynonym().getTitleCache().equals(relFauEu.getSynonym().getTitleCache())){
404
							//es gibt schon eine Relationship zu einem Synonym mit dem gleichen Namen wie das FaunaEu Synonym, also Relationship l�schen.
405
							createNewRelationship = false;
406
							break;
407
						}
408
					}
409
					if (createNewRelationship){
410
						taxonErms.addSynonym(relFauEu.getSynonym(), relFauEu.getType());
411
					}
412

  
413
					deleteRel.add(relFauEu);
414
				}
415
			}
416

  
417
		}
418
	}
419

  
420

  
421
	private void deleteFaunaEuTaxon(Taxon taxonFaunaEu) throws ReferencedObjectUndeletableException{
422
		appCtrInit.getTaxonService().delete(taxonFaunaEu);
423

  
424
	}
425

  
426
	//wenn Name und Rang identisch sind und auch der Status gleich, dann alle Informationen vom Fauna Europaea Taxon/Synonym zum Erms Taxon/Synonym
427

  
428
	private void moveAllInformationsFromFaunaEuToErms(TaxonBase faunaEu, TaxonBase erms){
429
		Set<Annotation> annotations = faunaEu.getAnnotations();
430
		Set<Extension> extensions = faunaEu.getExtensions();
431
		Set<Marker> markers = faunaEu.getMarkers();
432
		List<Credit> credits = faunaEu.getCredits();
433
		if (faunaEu instanceof Taxon){
434
			Set<TaxonDescription> descriptions = ((Taxon)faunaEu).getDescriptions();
435
			Set<Taxon> misappliedNames = ((Taxon)faunaEu).getMisappliedNames();
436

  
437
			if (erms instanceof Taxon){
438
				Iterator<TaxonDescription> descriptionsIterator = descriptions.iterator();
439
				TaxonDescription description;
440
				while (descriptionsIterator.hasNext()){
441
					description = descriptionsIterator.next();
442
					((Taxon) erms).addDescription(description);
443
				}
444

  
445
				Iterator<Taxon> misappliedNamesIterator = misappliedNames.iterator();
446
				Taxon misappliedName;
447
				while (misappliedNamesIterator.hasNext()){
448
					misappliedName = misappliedNamesIterator.next();
449
					((Taxon) erms).addMisappliedName(misappliedName, null, null);
450
				}
451
			}
452
		}
453

  
454
		//move all these informations to the erms taxon
455
		Iterator<Annotation> annotationsIterator = annotations.iterator();
456
		Annotation annotation;
457
		while (annotationsIterator.hasNext()){
458
			annotation = annotationsIterator.next();
459
			erms.addAnnotation(annotation);
460
		}
461

  
462
		Iterator<Extension> extensionIterator = extensions.iterator();
463
		Extension extension;
464
		while (extensionIterator.hasNext()){
465
			extension = extensionIterator.next();
466
			erms.addExtension(extension);
467
		}
468

  
469
		Iterator<Marker> markerIterator = markers.iterator();
470
		Marker marker;
471
		while (markerIterator.hasNext()){
472
			marker = markerIterator.next();
473
			erms.addMarker(marker);
474
		}
475

  
476
		for (Credit credit: credits){
477
			erms.addCredit(credit);
478
		}
479

  
480

  
481
	}
482

  
483
	//if name, rank, and status (accepted) are the same, then move the synonyms of faunaEu taxon to the erms taxon
484

  
485
	private void moveFaunaEuSynonymsToErmsTaxon(Taxon faunaEu, Taxon erms){
486
		Set<SynonymRelationship> synRel =faunaEu.getSynonymRelations();
487
		Iterator<SynonymRelationship> synRelIterator = synRel.iterator();
488
		SynonymRelationship rel;
489
		while (synRelIterator.hasNext()){
490
			rel = synRelIterator.next();
491
			faunaEu.removeSynonym(rel.getSynonym());
492
			erms.addSynonym(rel.getSynonym(), rel.getType());
493
		}
494
	}
495

  
496
	//after merging faunaEu taxon and erms taxon, the originalSource of the faunaEu taxon has to be moved to the erms taxon
497
	private void moveOriginalDbToErmsTaxon(TaxonBase faunaEu, TaxonBase erms){
498
		Set<IdentifiableSource> sourcesFaunaEu = faunaEu.getSources();
499
		IdentifiableSource sourceFaunaEu = sourcesFaunaEu.iterator().next();
500
		erms.addSource(sourceFaunaEu);
501
	}
502

  
503
	//merged taxon should have a new sec reference
504
	private void addNewSecForMergedTaxon(Taxon taxon, Reference sec){
505
		taxon.setSec(sec);
506
		taxon.setUuid(UUID.randomUUID());
507
	}
508

  
509
	// ----------- methods for merging Erms synonyms and Fauna Europaea Taxon
510

  
511

  
512

  
513

  
514
}
1
package eu.etaxonomy.cdm.app.pesi.merging;
2

  
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileNotFoundException;
6
import java.io.FileReader;
7
import java.io.IOException;
8
import java.util.ArrayList;
9
import java.util.HashSet;
10
import java.util.Iterator;
11
import java.util.List;
12
import java.util.Set;
13
import java.util.StringTokenizer;
14
import java.util.UUID;
15

  
16
import org.apache.log4j.Logger;
17

  
18
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
19
import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
20
import eu.etaxonomy.cdm.api.service.pager.Pager;
21
import eu.etaxonomy.cdm.app.common.CdmDestinations;
22
import eu.etaxonomy.cdm.app.common.TestDatabase;
23
import eu.etaxonomy.cdm.database.DbSchemaValidation;
24
import eu.etaxonomy.cdm.database.ICdmDataSource;
25
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26
import eu.etaxonomy.cdm.model.common.Annotation;
27
import eu.etaxonomy.cdm.model.common.Credit;
28
import eu.etaxonomy.cdm.model.common.Extension;
29
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
30
import eu.etaxonomy.cdm.model.common.Marker;
31
import eu.etaxonomy.cdm.model.common.RelationshipBase.Direction;
32
import eu.etaxonomy.cdm.model.description.Distribution;
33
import eu.etaxonomy.cdm.model.description.Feature;
34
import eu.etaxonomy.cdm.model.description.TaxonDescription;
35
import eu.etaxonomy.cdm.model.name.NameRelationship;
36
import eu.etaxonomy.cdm.model.name.Rank;
37
import eu.etaxonomy.cdm.model.name.ZoologicalName;
38
import eu.etaxonomy.cdm.model.reference.Reference;
39
import eu.etaxonomy.cdm.model.taxon.Synonym;
40
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
41
import eu.etaxonomy.cdm.model.taxon.Taxon;
42
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
44

  
45
public class FaunaEuErmsMergeActivator {
46

  
47
//	static final ICdmDataSource faunaEuropaeaSource = CdmDestinations.cdm_test_patricia();
48
	static final ICdmDataSource faunaEuropaeaSource = CdmDestinations.localH2();
49

  
50
	static final int faunaEuUuid = 0;
51
	static final int ermsUuid = 9;
52
	static final int rankFaunaEu = 4;
53
	static final int rankErms = 13;
54

  
55
	CdmApplicationController appCtrInit;
56

  
57
	private static final Logger logger = Logger.getLogger(FaunaEuErmsMergeActivator.class);
58

  
59
	//csv files starting with...
60
	static String sFileName = "c:\\test";
61

  
62
	private void initDb(ICdmDataSource db) {
63

  
64
		// Init source DB
65
		appCtrInit = TestDatabase.initDb(db, DbSchemaValidation.VALIDATE, false);
66

  
67

  
68
	}
69

  
70
	public static void main(String[] args) {
71

  
72
		FaunaEuErmsMergeActivator sc = new FaunaEuErmsMergeActivator();
73

  
74
		sc.initDb(faunaEuropaeaSource);
75

  
76
		sc.mergeAuthors();
77

  
78
		//set the ranks of Agnatha and Gnathostomata to 50 instead of 45
79
		List<TaxonBase> taxaToChangeRank = new ArrayList<TaxonBase>();
80
		Pager<TaxonBase> agnatha = sc.appCtrInit.getTaxonService().findTaxaByName(TaxonBase.class, "Agnatha", null, null, null, "*", Rank.INFRAPHYLUM(), 10, 0);
81
		List<TaxonBase> agnathaList = agnatha.getRecords();
82
		taxaToChangeRank.addAll(agnathaList);
83
		Pager<TaxonBase> gnathostomata = sc.appCtrInit.getTaxonService().findTaxaByName(TaxonBase.class, "Gnathostomata", null, null, null, "*", Rank.INFRAPHYLUM(), 10, 0);
84
		List<TaxonBase> gnathostomataList = gnathostomata.getRecords();
85
		taxaToChangeRank.addAll(gnathostomataList);
86

  
87
		sc.setSpecificRank(taxaToChangeRank,Rank.SUPERCLASS());
88

  
89
		//ermsTaxon is accepted, fauna eu taxon is synonym
90
		//ermsTaxon is synonym, faunaEu is accepted
91

  
92
		sc.mergeDiffStatus();
93

  
94
		//erms is synonym, faunaEu as well
95

  
96
		// erms is accepted, faunaEu as well
97

  
98
	}
99

  
100
	private static List readCsvFile(String fileName){
101

  
102
		List<List<String>> result = new ArrayList<List<String>>();
103
		File file = new File(fileName);
104
		BufferedReader bufRdr;
105
		try {
106
			bufRdr = new BufferedReader(new FileReader(file));
107
			String line = null;
108
			//read each line of text file
109
			while((line = bufRdr.readLine()) != null){
110
				StringTokenizer st = new StringTokenizer(line,",");
111
				List<String> rowList = new ArrayList<String>();
112
				while (st.hasMoreTokens()){
113
					//get next token and store it in the array
114
					rowList.add(st.nextToken());
115
				}
116
			result.add(rowList);
117
			}
118
			//close the file
119
			bufRdr.close();
120
		} catch (FileNotFoundException e) {
121
			// TODO Auto-generated catch block
122
			e.printStackTrace();
123
		} catch (IOException e) {
124
			// TODO Auto-generated catch block
125
			e.printStackTrace();
126
		}
127
		return result;
128
	}
129

  
130

  
131
	private void mergeAuthors(){
132
		List<List<String>> authors = readCsvFile(sFileName + "_authors.csv");
133
		//authors: get firstAuthor if isFauEu = 1 otherwise get secondAuthor
134

  
135
		Iterator<List<String>> authorIterator = authors.iterator();
136
		List<String> row;
137
		TaxonBase taxonFaunaEu;
138
		TaxonBase taxonErms;
139
		List<TaxonBase> taxaToSave = new ArrayList<TaxonBase>();
140
		while (authorIterator.hasNext()){
141
			row = authorIterator.next();
142
			UUID uuidFaunaEu = UUID.fromString(row.get(faunaEuUuid));
143
			UUID uuidErms = UUID.fromString(row.get(ermsUuid));
144
			taxonFaunaEu = appCtrInit.getTaxonService().find(uuidFaunaEu);
145
			taxonErms = appCtrInit.getTaxonService().find(uuidFaunaEu);
146

  
147
			if (Integer.parseInt(row.get(18)) == 1){
148
				//isFaunaEu = 1 -> copy the author of Fauna Europaea to Erms
149
				if (((ZoologicalName)taxonFaunaEu.getName()).getBasionymAuthorship()!= null){
150
					((ZoologicalName)taxonErms.getName()).setBasionymAuthorship(((ZoologicalName)taxonFaunaEu.getName()).getBasionymAuthorship());
151
				}
152
				if (((ZoologicalName)taxonFaunaEu.getName()).getCombinationAuthorship()!= null){
153
					((ZoologicalName)taxonErms.getName()).setCombinationAuthorship(((ZoologicalName)taxonFaunaEu.getName()).getCombinationAuthorship());
154
				}
155
				((ZoologicalName)taxonErms.getName()).generateAuthorship();
156
				taxaToSave.add(taxonErms);
157
			}else{
158
				if (((ZoologicalName)taxonErms.getName()).getBasionymAuthorship()!= null){
159
					((ZoologicalName)taxonFaunaEu.getName()).setBasionymAuthorship(((ZoologicalName)taxonErms.getName()).getBasionymAuthorship());
160
				}
161
				if (((ZoologicalName)taxonErms.getName()).getCombinationAuthorship()!= null){
162
					((ZoologicalName)taxonFaunaEu.getName()).setCombinationAuthorship(((ZoologicalName)taxonErms.getName()).getCombinationAuthorship());
163
				}
164
				((ZoologicalName)taxonFaunaEu.getName()).generateAuthorship();
165
				taxaToSave.add(taxonFaunaEu);
166
			}
167

  
168

  
169
		}
170
	}
171

  
172
	public void setSpecificRank(List<TaxonBase> taxa, Rank rank){
173

  
174
		for (TaxonBase taxon: taxa){
175
			taxon.getName().setRank(rank);
176
		}
177
	}
178

  
179
	private void mergeDiffStatus(){
180
		List<List<String>> diffStatus = readCsvFile(sFileName + "_status.csv");
181

  
182
		//find all taxa accepted in erms, but synonyms in FauEu  and the same rank
183
		List<List<String>> accErmsSynFaunaEu = new ArrayList<List<String>>();
184
		for (List<String> rowList: diffStatus){
185
			if ((rowList.get(5).equals("synonym")) && (rowList.get(rankFaunaEu).equals(rowList.get(rankErms)))){
186
				//both conditions are true
187
				accErmsSynFaunaEu.add(rowList);
188
			}
189
		}
190
		mergeErmsAccFaunaEuSyn(accErmsSynFaunaEu);
191

  
192
		//find all taxa accepted in faunaEu, but synonyms in Erms and the same rank
193
		List<List<String>> synErmsAccFaunaEu = new ArrayList<List<String>>();
194
		for (List<String> rowList: diffStatus){
195
			if ((rowList.get(5).equals("accepted")) && (rowList.get(rankFaunaEu).equals(rowList.get(rankErms)))){
196
				//both conditions are true
197
				synErmsAccFaunaEu.add(rowList);
198
			}
199
		}
200
		mergeErmsSynFaunaEuAcc(synErmsAccFaunaEu);
201

  
202

  
203
	}
204

  
205
	private void mergeSameStatus(){
206
		List<List<String>> sameStatus = readCsvFile(sFileName + "_names.csv");
207

  
208
		TaxonBase taxonFaunaEu;
209
		TaxonBase taxonErms;
210

  
211
		for (List<String> row: sameStatus){
212
			taxonFaunaEu = appCtrInit.getTaxonService().find(UUID.fromString(row.get(faunaEuUuid)));
213
			taxonErms = appCtrInit.getTaxonService().find(UUID.fromString(row.get(ermsUuid)));
214
			moveAllInformationsFromFaunaEuToErms(taxonFaunaEu, taxonErms);
215
			if (taxonErms instanceof Taxon){
216
				moveFaunaEuSynonymsToErmsTaxon((Taxon)taxonFaunaEu, (Taxon)taxonErms);
217
			}
218
		}
219
	}
220

  
221

  
222

  
223
	private void mergeErmsAccFaunaEuSyn(List<List<String>> ermsAccFaEuSyn){
224

  
225
		// update nameRelationships -> if the nameRelationship does not exist, then create a new one with ermsAcc as relatedTo TaxonName
226
		updateNameRelationships(ermsAccFaEuSyn);
227

  
228
		//delete all synonymRelationships of FaunaEu Syn
229
		for (List<String> rowList: ermsAccFaEuSyn){
230
			UUID faunaUUID = UUID.fromString(rowList.get(faunaEuUuid));
231
			//UUID ermsUUID = UUID.fromString(rowList.get(ermsUuid));
232
			Synonym syn = (Synonym)appCtrInit.getTaxonService().find(faunaUUID);
233
			appCtrInit.getTaxonService().deleteSynonymRelationships(syn);
234
		}
235

  
236
		//merge the infos of
237

  
238

  
239
	}
240

  
241
	private  void mergeErmsSynFaunaEuAcc (List<List<String>> ermsAccFaEuSyn){
242
		//occurence: verkn�pfe statt dem Fauna Europaea Taxon das akzeptierte Taxon, des Synonyms mit der Occurence (CDM -> distribution)
243
		//suche distribution (�ber das Taxon der TaxonDescription), dessen Taxon, das entsprechende Fauna Eu Taxon ist und verkn�pfe es mit dem akzeptieren Taxon des Erms Syn
244
		Taxon taxonFaunaEu = null;
245
		Taxon taxonErms = null;
246
		Synonym synErms = null;
247
		for (List<String> row: ermsAccFaEuSyn){
248
			taxonFaunaEu = (Taxon)appCtrInit.getTaxonService().find(UUID.fromString(row.get(faunaEuUuid)));
249
			synErms = (Synonym)appCtrInit.getTaxonService().find(UUID.fromString(row.get(ermsUuid)));
250
			synErms = HibernateProxyHelper.deproxy(synErms, Synonym.class);
251
			Set<SynonymRelationship> synRel=synErms.getSynonymRelations();
252

  
253
			if (synRel.size()>1){
254
				//TODO: which Relationship??
255
				Iterator<SynonymRelationship> iterator = synRel.iterator();
256
				taxonErms = iterator.next().getAcceptedTaxon();
257
			}else if (synRel.size() == 1){
258
				Iterator<SynonymRelationship> iterator = synRel.iterator();
259
				taxonErms = iterator.next().getAcceptedTaxon();
260
			} else {
261
				taxonErms = null;
262
				logger.debug("There is no SynonymRelationship for the synonym" + synErms.getTitleCache());
263
			}
264

  
265
			Set<Feature> features = new HashSet<Feature>();
266
			features.add(Feature.DISTRIBUTION());
267
			List<String> propertyPaths = new ArrayList<String>();
268
			propertyPaths.add("inDescription.Taxon.*");
269
			List<Distribution> distributions = appCtrInit.getDescriptionService().getDescriptionElementsForTaxon(taxonFaunaEu, features, Distribution.class, 10, 0, null);
270

  
271

  
272
			for(Distribution distribution: distributions){
273
				TaxonDescription description = (TaxonDescription)distribution.getInDescription();
274
				TaxonDescription newDescription = TaxonDescription.NewInstance(taxonErms);
275
				newDescription.addElement(distribution);
276
				try{
277
					appCtrInit.getDescriptionService().delete(description);
278
				}catch (Exception e){
279
					logger.debug("The description of" + description.getTaxon().getTitleCache() + description.getTitleCache() + "can't be deleted because it is referenced.");
280
				}
281
			}
282

  
283

  
284
			//Child-Parent Relationship aktualisieren -> dem Child des Fauna Europaea Taxons als parent das akzeptierte Taxon von synErms
285
			Set<TaxonNode> nodesErms = taxonErms.getTaxonNodes();
286
			Set<TaxonNode> nodesFaunaEu =taxonFaunaEu.getTaxonNodes();
287
			if (nodesFaunaEu.size()>1 || nodesFaunaEu.isEmpty()){
288

  
289
			}else{
290
				Iterator<TaxonNode> iteratorNodesErms = nodesErms.iterator();
291

  
292
				Iterator<TaxonNode> iteratorNodesFaunaEu = nodesFaunaEu.iterator();
293
				TaxonNode node = iteratorNodesFaunaEu.next();
294
				List<TaxonNode> children = node.getChildNodes();
295
				Iterator<TaxonNode> childrenIterator = children.iterator();
296
				TaxonNode childNode;
297
				if (iteratorNodesErms.hasNext()){
298
					TaxonNode ermsNode = iteratorNodesErms.next();
299
					while (childrenIterator.hasNext()){
300
						childNode = childrenIterator.next();
301
						ermsNode.addChildNode(childNode, childNode.getReference(), childNode.getMicroReference());
302
					}
303
				}
304

  
305
			}
306
			moveFaunaEuSynonymsToErmsTaxon(taxonFaunaEu, taxonErms);
307
			moveAllInformationsFromFaunaEuToErms(taxonFaunaEu, taxonErms);
308
			moveOriginalDbToErmsTaxon(taxonFaunaEu, taxonErms);
309
			//neue sec Referenz an das ErmsTaxon oder an das Synonym und Taxon oder nur Synonym??
310
			try{
311
				deleteFaunaEuTaxon(taxonFaunaEu);
312
			}catch(ReferencedObjectUndeletableException e){
313
				logger.debug("The taxon " + taxonFaunaEu.getTitleCache() + " can't be deleted because it is referenced.");
314
			}
315
		}
316

  
317

  
318

  
319

  
320
	}
321

  
322

  
323

  
324
	private void updateNameRelationships(List<List<String>> ermsAccFaEuSyn){
325
		//suche alle NameRelationships aus FaunaEu und Erms, wo (faunaEu)relatedFrom.name.titleCache = (erms)relatedFrom.name.titleCache und ersetze in der faunaEu Relationship den relatedTo.name durch den relatedTo.name der erms-relationship
326
		//wenn es diese relationship noch nicht gibt und der typ der gleiche ist!!
327
		//wenn der relatedTo Name zu einem Erms Taxon und einem FaunaEu Synonym geh�rt
328

  
329
		Synonym synFaunaEu;
330
		Taxon taxonErms;
331
		for (List<String> row: ermsAccFaEuSyn){
332
			synFaunaEu = (Synonym)appCtrInit.getTaxonService().find(UUID.fromString(row.get(faunaEuUuid)));
333
			taxonErms = (Taxon)appCtrInit.getTaxonService().find(UUID.fromString(row.get(ermsUuid)));
334
			List<NameRelationship> relSynFaunaEu = appCtrInit.getNameService().listToNameRelationships(synFaunaEu.getName(), null, 100, 0, null, null);
335
			List<NameRelationship> relTaxonErms = appCtrInit.getNameService().listToNameRelationships(taxonErms.getName(), null, 100, 0, null, null);
336

  
337
			List<NameRelationship> deleteRel = new ArrayList<NameRelationship>();
338
			for (NameRelationship relFauEu: relSynFaunaEu){
339
				boolean createNewRelationship = true;
340
				for (NameRelationship relErms: relTaxonErms){
341
					if ((relErms.getFromName().getTitleCache().equals(relFauEu.getFromName().getTitleCache())) && (relErms.getToName().getTitleCache().equals(relFauEu.getFromName().getTitleCache()))){
342
						//delete the faunaEu relationship because there exist an analogous relationship in erms
343
						deleteRel.add(relFauEu);
344
						createNewRelationship = false;
345
						break;
346
					}
347
				}
348
				if (createNewRelationship){
349
					//if relationship does not exist, create a new one with erms synonym
350
					taxonErms.getName().addRelationshipFromName(relFauEu.getFromName(), relFauEu.getType(), relFauEu.getRuleConsidered());
351
				}
352
			}
353

  
354
		}
355
	}
356

  
357
	private void updateSynonymRelationships(List<List<String>> ermsSynFaEuAcc){
358
//		-- Update queries for RelTaxon (synonym relationships - move relationships to ERMS accepted taxon if not already existent or delete if already existent)
359
//		UPDATE RelTaxon_1 SET RelTaxon_1.TaxonFk2 = RT.TaxonFk2
360
//		FROM         Taxon AS ERMSSyn INNER JOIN
361
//		                      Taxon AS FaEuAcc ON ERMSSyn.RankFk = FaEuAcc.RankFk AND ERMSSyn.FullName = FaEuAcc.FullName AND
362
//		                      ERMSSyn.TaxonStatusFk <> ISNULL(FaEuAcc.TaxonStatusFk, 0) INNER JOIN
363
//		                      RelTaxon AS RT ON ERMSSyn.TaxonId = RT.TaxonFk1 INNER JOIN
364
//		                      RelTaxon AS RelTaxon_1 ON FaEuAcc.TaxonId = RelTaxon_1.TaxonFk2 INNER JOIN
365
//		                      Taxon AS FaEuSyn ON RelTaxon_1.TaxonFk1 = FaEuSyn.TaxonId LEFT OUTER JOIN
366
//		                      Taxon AS ERMSAllSyn ON RT.TaxonFk1 = ERMSAllSyn.TaxonId AND FaEuSyn.FullName <> ERMSAllSyn.FullName --(!!)
367
//		WHERE     (ERMSSyn.OriginalDB = N'ERMS') AND (RT.RelTaxonQualifierFk > 100) AND (ERMSSyn.TaxonStatusFk <> 1) AND (ERMSSyn.KingdomFk = 2) AND
368
//		                      (FaEuAcc.OriginalDB = N'FaEu') AND (RelTaxon_1.RelTaxonQualifierFk > 100)
369
		Taxon taxonFaunaEu;
370
		Synonym synErms;
371
		Taxon taxonErms;
372
		Set<Taxon> acceptedTaxa = new HashSet<Taxon>();
373
		for (List<String> row: ermsSynFaEuAcc){
374
			taxonFaunaEu = (Taxon)appCtrInit.getTaxonService().find(UUID.fromString(row.get(faunaEuUuid)));
375
			synErms = (Synonym)appCtrInit.getTaxonService().find(UUID.fromString(row.get(ermsUuid)));
376
			acceptedTaxa.clear();
377
			acceptedTaxa.addAll( synErms.getAcceptedTaxa());
378
			if (!acceptedTaxa.isEmpty()){
379
				taxonErms = acceptedTaxa.iterator().next();
380
				if (acceptedTaxa.size() > 1){
381
					logger.debug("There are more than one accepted taxon for synonym " + synErms.getTitleCache());
382
				}
383
			}else{
384
				taxonErms = null;
385
				logger.debug("There is no accepted taxon for synonym "  + synErms.getTitleCache());
386
			}
387

  
388
			if (taxonErms != null){
389
				List<SynonymRelationship> relTaxonFaunaEu = appCtrInit.getTaxonService().listSynonymRelationships(taxonFaunaEu, null, 100, 0, null, null, Direction.relatedTo);
390
				List<SynonymRelationship> relTaxonErms = appCtrInit.getTaxonService().listSynonymRelationships(taxonErms, null, 100, 0, null, null, Direction.relatedTo);
391

  
392
				List<SynonymRelationship> deleteRel = new ArrayList<SynonymRelationship>();
393
				for (SynonymRelationship relFauEu: relTaxonFaunaEu){
394
					//TODO: wenn es noch keine SynonymRelationship gibt zu einem Synonym mit gleichem Namen, dann erzeuge die SynonymRelationship vom FaunaEuSyn (des FaunaEu Taxons, dass identischen Namen hat) zum akzeptierten Taxon des Erms Syn
395
					boolean createNewRelationship = true;
396
					for (SynonymRelationship relErms: relTaxonErms){
397
						if (relErms.getSynonym().getTitleCache().equals(relFauEu.getSynonym().getTitleCache())){
398
							//es gibt schon eine Relationship zu einem Synonym mit dem gleichen Namen wie das FaunaEu Synonym, also Relationship l�schen.
399
							createNewRelationship = false;
400
							break;
401
						}
402
					}
403
					if (createNewRelationship){
404
						taxonErms.addSynonym(relFauEu.getSynonym(), relFauEu.getType());
405
					}
406

  
407
					deleteRel.add(relFauEu);
408
				}
409
			}
410

  
411
		}
412
	}
413

  
414

  
415
	private void deleteFaunaEuTaxon(Taxon taxonFaunaEu) throws ReferencedObjectUndeletableException{
416
		appCtrInit.getTaxonService().delete(taxonFaunaEu);
417

  
418
	}
419

  
420
	//wenn Name und Rang identisch sind und auch der Status gleich, dann alle Informationen vom Fauna Europaea Taxon/Synonym zum Erms Taxon/Synonym
421

  
422
	private void moveAllInformationsFromFaunaEuToErms(TaxonBase faunaEu, TaxonBase erms){
423
		Set<Annotation> annotations = faunaEu.getAnnotations();
424
		Set<Extension> extensions = faunaEu.getExtensions();
425
		Set<Marker> markers = faunaEu.getMarkers();
426
		List<Credit> credits = faunaEu.getCredits();
427
		if (faunaEu instanceof Taxon){
428
			Set<TaxonDescription> descriptions = ((Taxon)faunaEu).getDescriptions();
429
			Set<Taxon> misappliedNames = ((Taxon)faunaEu).getMisappliedNames();
430

  
431
			if (erms instanceof Taxon){
432
				Iterator<TaxonDescription> descriptionsIterator = descriptions.iterator();
433
				TaxonDescription description;
434
				while (descriptionsIterator.hasNext()){
435
					description = descriptionsIterator.next();
436
					((Taxon) erms).addDescription(description);
437
				}
438

  
439
				Iterator<Taxon> misappliedNamesIterator = misappliedNames.iterator();
440
				Taxon misappliedName;
441
				while (misappliedNamesIterator.hasNext()){
442
					misappliedName = misappliedNamesIterator.next();
443
					((Taxon) erms).addMisappliedName(misappliedName, null, null);
444
				}
445
			}
446
		}
447

  
448
		//move all these informations to the erms taxon
449
		Iterator<Annotation> annotationsIterator = annotations.iterator();
450
		Annotation annotation;
451
		while (annotationsIterator.hasNext()){
452
			annotation = annotationsIterator.next();
453
			erms.addAnnotation(annotation);
454
		}
455

  
456
		Iterator<Extension> extensionIterator = extensions.iterator();
457
		Extension extension;
458
		while (extensionIterator.hasNext()){
459
			extension = extensionIterator.next();
460
			erms.addExtension(extension);
461
		}
462

  
463
		Iterator<Marker> markerIterator = markers.iterator();
464
		Marker marker;
465
		while (markerIterator.hasNext()){
466
			marker = markerIterator.next();
467
			erms.addMarker(marker);
468
		}
469

  
470
		for (Credit credit: credits){
471
			erms.addCredit(credit);
472
		}
473

  
474

  
475
	}
476

  
477
	//if name, rank, and status (accepted) are the same, then move the synonyms of faunaEu taxon to the erms taxon
478

  
479
	private void moveFaunaEuSynonymsToErmsTaxon(Taxon faunaEu, Taxon erms){
480
		Set<SynonymRelationship> synRel =faunaEu.getSynonymRelations();
481
		Iterator<SynonymRelationship> synRelIterator = synRel.iterator();
482
		SynonymRelationship rel;
483
		while (synRelIterator.hasNext()){
484
			rel = synRelIterator.next();
485
			faunaEu.removeSynonym(rel.getSynonym());
486
			erms.addSynonym(rel.getSynonym(), rel.getType());
487
		}
488
	}
489

  
490
	//after merging faunaEu taxon and erms taxon, the originalSource of the faunaEu taxon has to be moved to the erms taxon
491
	private void moveOriginalDbToErmsTaxon(TaxonBase faunaEu, TaxonBase erms){
492
		Set<IdentifiableSource> sourcesFaunaEu = faunaEu.getSources();
493
		IdentifiableSource sourceFaunaEu = sourcesFaunaEu.iterator().next();
494
		erms.addSource(sourceFaunaEu);
495
	}
496

  
497
	//merged taxon should have a new sec reference
498
	private void addNewSecForMergedTaxon(Taxon taxon, Reference sec){
499
		taxon.setSec(sec);
500
		taxon.setUuid(UUID.randomUUID());
501
	}
502

  
503
	// ----------- methods for merging Erms synonyms and Fauna Europaea Taxon
504

  
505

  
506

  
507

  
508
}
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/faunaEuropaea/FaunaEuropaeaAdditionalTaxonDataImport.java
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

  
10
package eu.etaxonomy.cdm.io.pesi.faunaEuropaea;
11

  
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

  
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21
import org.springframework.transaction.TransactionStatus;
22

  
23
import eu.etaxonomy.cdm.database.ICdmDataSource;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.name.NonViralName;
26
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
27
import eu.etaxonomy.cdm.model.taxon.Taxon;
28
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
29

  
30

  
31
/**
32
 * @author a.babadshanjan
33
 * @created 21.08.2010
34
 * @version 1.0
35
 */
36
@Component
37
public class FaunaEuropaeaAdditionalTaxonDataImport extends FaunaEuropaeaImportBase  {
38
	
39
	private static final Logger logger = Logger.getLogger(FaunaEuropaeaAdditionalTaxonDataImport.class);
40
//	private static final String parentPluralString = "Synonyms";
41
	private static final String pluralString = "InfraGenericEpithets";
42
	//private static final String acceptedTaxonUUID = "A9C24E42-69F5-4681-9399-041E652CF338"; // any accepted taxon uuid, taken from original fauna europaea database
43

  
44
	/* (non-Javadoc)
45
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
46
	 */
47
	@Override
48
	protected boolean doCheck(FaunaEuropaeaImportState state) {
49
		boolean result = true;
50
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
51
		logger.warn("Checking for Taxa not yet fully implemented");
52
//		result &= checkTaxonStatus(fauEuConfig);
53
		
54
		return result;
55
	}
56

  
57
	
58
	/** 
59
	 * Import taxa from FauEU DB
60
	 */
61
	protected void doInvoke(FaunaEuropaeaImportState state) {				
62
		
63
		if(logger.isInfoEnabled()) {
64
			logger.info("Started creating " + pluralString + "...");
65
		}
66
		
67
		processAdditionalInfraGenericEpithets(state);
68
		
69
		logger.info("The End is Nigh... " + pluralString + "...");
70
		return;
71
	}
72

  
73
	/**
74
	 * 
75
	 * @param state
76
	 * @return
77
	 */
78
	private void processAdditionalInfraGenericEpithets(FaunaEuropaeaImportState state) {
79
		int count = 0;
80
		int pageSize = 1000;
81
		Set<UUID> uuidSet = new HashSet<UUID>();
82
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
83
		ICdmDataSource destination = fauEuConfig.getDestination();
84
		TransactionStatus txStatus = null;
85
		List<TaxonNameBase> taxonNames = null;
86
		txStatus = startTransaction(false);
87

  
88
		String selectQuery = "SELECT t.uuid from TaxonNameBase t INNER JOIN " +
89
				"TaxonNameBase t2 ON t.GenusOrUninomial = t2.GenusOrUninomial AND t.SpecificEpithet = t2.SpecificEpithet " +
90
				"WHERE t.InfraGenericEpithet IS NULL AND t.rank_id = 764 AND t2.rank_id = 766 AND t2.InfraGenericEpithet IS NOT NULL";
91
		
92
		logger.info("Retrieving TaxonNames...");
93
		
94
		ResultSet resultSet;
95
		try {
96
			resultSet = destination.executeQuery(selectQuery);
97
		
98
			// Collect UUIDs
99
			while (resultSet.next()) {
100
				uuidSet.add(UUID.fromString(resultSet.getString("UUID")));
101
			}
102
		} catch (SQLException e) {
103
			logger.error("An error occured: ", e);
104
		}
105

  
106
		// Fetch TaxonName objects for UUIDs
107
		if (!uuidSet.isEmpty()){
108
			taxonNames = getNameService().find(uuidSet);
109
			
110
			for (TaxonNameBase taxonName : taxonNames) {
111
				
112
				// Check whether its taxonName has an infraGenericEpithet
113
				if (taxonName != null && (taxonName.isInstanceOf(NonViralName.class))) {
114
					NonViralName targetNonViralName = CdmBase.deproxy(taxonName, NonViralName.class);
115
					String infraGenericEpithet = targetNonViralName.getInfraGenericEpithet();
116
					if (infraGenericEpithet == null) {
117
						String genusOrUninomial = targetNonViralName.getGenusOrUninomial();
118
						String specificEpithet = targetNonViralName.getSpecificEpithet();
119
						List<TaxonBase> foundTaxa = getTaxonService().listTaxaByName(Taxon.class, genusOrUninomial, "*", specificEpithet, 
120
								"*", null, pageSize, 1);
121
						if (foundTaxa.size() == 1) {
122
							// one matching Taxon found
123
							TaxonBase taxon = foundTaxa.iterator().next();
124
							if (taxon != null) {
125
								TaxonNameBase name = taxon.getName();
126
								if (name != null && name.isInstanceOf(NonViralName.class)) {
127
									NonViralName nonViralName = CdmBase.deproxy(name, NonViralName.class);
128
									infraGenericEpithet = nonViralName.getInfraGenericEpithet();
129
									
130
									// set infraGenericEpithet
131
	//									targetNonViralName.setInfraGenericEpithet(infraGenericEpithet);
132
									logger.debug("Added an InfraGenericEpithet to this TaxonName: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
133
									count++;
134
								}
135
							}
136
						} else if (foundTaxa.size() > 1) {
137
							logger.warn("Multiple taxa match search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
138
							for (TaxonBase foundTaxon : foundTaxa) {
139
								logger.warn(foundTaxon.getUuid() + ", " + foundTaxon.getTitleCache());
140
							}
141
						} else if (foundTaxa.size() == 0) {
142
	//							logger.error("No matches for search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
143
						}
144
					}
145
					
146
				}
147
			}
148
		}else {
149
			logger.debug("There are no additional infrageneric epithets!");
150
		}
151

  
152
		// Commit transaction
153
		commitTransaction(txStatus);
154
		logger.info("Committed transaction.");
155
		
156
		return;
157
	}
158
	
159
	
160
	/* (non-Javadoc)
161
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
162
	 */
163
	protected boolean isIgnore(FaunaEuropaeaImportState state) {
164
		return ! state.getConfig().isDoTaxa();
165
	}
166

  
167
}
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

  
10
package eu.etaxonomy.cdm.io.pesi.faunaEuropaea;
11

  
12
import java.sql.ResultSet;
13
import java.sql.SQLException;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

  
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21
import org.springframework.transaction.TransactionStatus;
22

  
23
import eu.etaxonomy.cdm.database.ICdmDataSource;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.name.NonViralName;
26
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
27
import eu.etaxonomy.cdm.model.taxon.Taxon;
28
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
29

  
30

  
31
/**
32
 * @author a.babadshanjan
33
 * @created 21.08.2010
34
 * @version 1.0
35
 */
36
@Component
37
public class FaunaEuropaeaAdditionalTaxonDataImport extends FaunaEuropaeaImportBase  {
38

  
39
	private static final Logger logger = Logger.getLogger(FaunaEuropaeaAdditionalTaxonDataImport.class);
40
//	private static final String parentPluralString = "Synonyms";
41
	private static final String pluralString = "InfraGenericEpithets";
42
	//private static final String acceptedTaxonUUID = "A9C24E42-69F5-4681-9399-041E652CF338"; // any accepted taxon uuid, taken from original fauna europaea database
43

  
44
	/* (non-Javadoc)
45
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
46
	 */
47
	@Override
48
	protected boolean doCheck(FaunaEuropaeaImportState state) {
49
		boolean result = true;
50
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
51
		logger.warn("Checking for Taxa not yet fully implemented");
52
//		result &= checkTaxonStatus(fauEuConfig);
53

  
54
		return result;
55
	}
56

  
57

  
58
	/**
59
	 * Import taxa from FauEU DB
60
	 */
61
	@Override
62
    protected void doInvoke(FaunaEuropaeaImportState state) {
63

  
64
		if(logger.isInfoEnabled()) {
65
			logger.info("Started creating " + pluralString + "...");
66
		}
67

  
68
		processAdditionalInfraGenericEpithets(state);
69

  
70
		logger.info("The End is Nigh... " + pluralString + "...");
71
		return;
72
	}
73

  
74
	/**
75
	 *
76
	 * @param state
77
	 * @return
78
	 */
79
	private void processAdditionalInfraGenericEpithets(FaunaEuropaeaImportState state) {
80
		int count = 0;
81
		int pageSize = 1000;
82
		Set<UUID> uuidSet = new HashSet<UUID>();
83
		FaunaEuropaeaImportConfigurator fauEuConfig = state.getConfig();
84
		ICdmDataSource destination = fauEuConfig.getDestination();
85
		TransactionStatus txStatus = null;
86
		List<TaxonNameBase> taxonNames = null;
87
		txStatus = startTransaction(false);
88

  
89
		String selectQuery = "SELECT t.uuid from TaxonNameBase t INNER JOIN " +
90
				"TaxonNameBase t2 ON t.GenusOrUninomial = t2.GenusOrUninomial AND t.SpecificEpithet = t2.SpecificEpithet " +
91
				"WHERE t.InfraGenericEpithet IS NULL AND t.rank_id = 764 AND t2.rank_id = 766 AND t2.InfraGenericEpithet IS NOT NULL";
92

  
93
		logger.info("Retrieving TaxonNames...");
94

  
95
		ResultSet resultSet;
96
		try {
97
			resultSet = destination.executeQuery(selectQuery);
98

  
99
			// Collect UUIDs
100
			while (resultSet.next()) {
101
				uuidSet.add(UUID.fromString(resultSet.getString("UUID")));
102
			}
103
		} catch (SQLException e) {
104
			logger.error("An error occured: ", e);
105
		}
106

  
107
		// Fetch TaxonName objects for UUIDs
108
		if (!uuidSet.isEmpty()){
109
			taxonNames = getNameService().find(uuidSet);
110

  
111
			for (TaxonNameBase taxonName : taxonNames) {
112

  
113
				// Check whether its taxonName has an infraGenericEpithet
114
				if (taxonName != null && (taxonName.isInstanceOf(NonViralName.class))) {
115
					NonViralName targetNonViralName = CdmBase.deproxy(taxonName, NonViralName.class);
116
					String infraGenericEpithet = targetNonViralName.getInfraGenericEpithet();
117
					if (infraGenericEpithet == null) {
118
						String genusOrUninomial = targetNonViralName.getGenusOrUninomial();
119
						String specificEpithet = targetNonViralName.getSpecificEpithet();
120
						List<TaxonBase> foundTaxa = getTaxonService().listTaxaByName(Taxon.class, genusOrUninomial, "*", specificEpithet,
121
								"*", "*", null, pageSize, 1);
122
						if (foundTaxa.size() == 1) {
123
							// one matching Taxon found
124
							TaxonBase taxon = foundTaxa.iterator().next();
125
							if (taxon != null) {
126
								TaxonNameBase name = taxon.getName();
127
								if (name != null && name.isInstanceOf(NonViralName.class)) {
128
									NonViralName nonViralName = CdmBase.deproxy(name, NonViralName.class);
129
									infraGenericEpithet = nonViralName.getInfraGenericEpithet();
130

  
131
									// set infraGenericEpithet
132
	//									targetNonViralName.setInfraGenericEpithet(infraGenericEpithet);
133
									logger.debug("Added an InfraGenericEpithet to this TaxonName: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
134
									count++;
135
								}
136
							}
137
						} else if (foundTaxa.size() > 1) {
138
							logger.warn("Multiple taxa match search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
139
							for (TaxonBase foundTaxon : foundTaxa) {
140
								logger.warn(foundTaxon.getUuid() + ", " + foundTaxon.getTitleCache());
141
							}
142
						} else if (foundTaxa.size() == 0) {
143
	//							logger.error("No matches for search criteria: " + taxonName.getUuid() + " (" + taxonName.getTitleCache() + ")");
144
						}
145
					}
146

  
147
				}
148
			}
149
		}else {
150
			logger.debug("There are no additional infrageneric epithets!");
151
		}
152

  
153
		// Commit transaction
154
		commitTransaction(txStatus);
155
		logger.info("Committed transaction.");
156

  
157
		return;
158
	}
159

  
160

  
161
	/* (non-Javadoc)
162
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
163
	 */
164
	@Override
165
    protected boolean isIgnore(FaunaEuropaeaImportState state) {
166
		return ! state.getConfig().isDoTaxa();
167
	}
168

  
169
}
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiTaxonExport.java
792 792

  
793 793

  
794 794

  
795
		while ((taxonList  = getTaxonService().listTaxaByName(Taxon.class, "*", "*", "*", "*", Rank.SPECIES(), pageSize, pageNumber)).size() > 0) {
795
		while ((taxonList  = getTaxonService().listTaxaByName(Taxon.class, "*", "*", "*", "*", "*", Rank.SPECIES(), pageSize, pageNumber)).size() > 0) {
796 796
			HashMap<Integer, TaxonNameBase<?,?>> inferredSynonymsDataToBeSaved = new HashMap<Integer, TaxonNameBase<?,?>>();
797 797

  
798 798
			logger.info("Fetched " + taxonList.size() + " " + parentPluralString + ". Exporting...");
......
819 819
			pageNumber++;
820 820
		}
821 821
		taxonList = null;
822
		while ((taxonList  = getTaxonService().listTaxaByName(Taxon.class, "*", "*", "*", "*", Rank.SUBSPECIES(), pageSize, pageNumber)).size() > 0) {
822
		while ((taxonList  = getTaxonService().listTaxaByName(Taxon.class, "*", "*", "*", "*", "*", Rank.SUBSPECIES(), pageSize, pageNumber)).size() > 0) {
823 823
			HashMap<Integer, TaxonNameBase<?,?>> inferredSynonymsDataToBeSaved = new HashMap<Integer, TaxonNameBase<?,?>>();
824 824

  
825 825
			logger.info("Fetched " + taxonList.size() + " " + parentPluralString + ". Exporting...");
......
1730 1730
		if (nvn != null){
1731 1731
			Reference nomRef = (Reference)nvn.getNomenclaturalReference();
1732 1732
			if (nomRef != null){
1733
			    logger.warn("Semantics of getAbbrevTitleCache has changed. Please check if output is still correct. See #5388");
1733 1734
				return nomRef.getAbbrevTitleCache();
1734 1735
			}
1735

  
1736 1736
		}
1737 1737
		return null;
1738 1738
	}

Also available in: Unified diff