Project

General

Profile

« Previous | Next » 

Revision 48014773

Added by Andreas Müller almost 9 years ago

Rename NonViralName authorteams to authorship #4968

View differences:

cdm-pesi/src/main/java/eu/etaxonomy/cdm/app/pesi/merging/FaunaEuErmsMergeActivator.java
43 43
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
44 44

  
45 45
public class FaunaEuErmsMergeActivator {
46
	
46

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

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

  
55 55
	CdmApplicationController appCtrInit;
56
	
56

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

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

  
62 62
	private void initDb(ICdmDataSource db) {
63 63

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

  
67
		
67

  
68 68
	}
69
	
69

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

  
72 72
		FaunaEuErmsMergeActivator sc = new FaunaEuErmsMergeActivator();
73
		
73

  
74 74
		sc.initDb(faunaEuropaeaSource);
75
			
75

  
76 76
		sc.mergeAuthors();
77
		
77

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

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

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

  
92 92
		sc.mergeDiffStatus();
93
		
93

  
94 94
		//erms is synonym, faunaEu as well
95
		
95

  
96 96
		// erms is accepted, faunaEu as well
97
		
98
		
99
		
100
		
101
		
102
		
103
		
97

  
98

  
99

  
100

  
101

  
102

  
103

  
104 104
	}
105
	
105

  
106 106
	private static List readCsvFile(String fileName){
107
		
107

  
108 108
		List<List<String>> result = new ArrayList<List<String>>();
109 109
		File file = new File(fileName);
110 110
		BufferedReader bufRdr;
......
132 132
		}
133 133
		return result;
134 134
	}
135
	
136
	
135

  
136

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

  
141 141
		Iterator<List<String>> authorIterator = authors.iterator();
142 142
		List<String> row;
143 143
		TaxonBase taxonFaunaEu;
......
149 149
			UUID uuidErms = UUID.fromString(row.get(ermsUuid));
150 150
			taxonFaunaEu = appCtrInit.getTaxonService().find(uuidFaunaEu);
151 151
			taxonErms = appCtrInit.getTaxonService().find(uuidFaunaEu);
152
			
152

  
153 153
			if (Integer.parseInt(row.get(18)) == 1){
154 154
				//isFaunaEu = 1 -> copy the author of Fauna Europaea to Erms
155
				if (((ZoologicalName)taxonFaunaEu.getName()).getBasionymAuthorTeam()!= null){
156
					((ZoologicalName)taxonErms.getName()).setBasionymAuthorTeam(((ZoologicalName)taxonFaunaEu.getName()).getBasionymAuthorTeam());
157
				} 
158
				if (((ZoologicalName)taxonFaunaEu.getName()).getCombinationAuthorTeam()!= null){
159
					((ZoologicalName)taxonErms.getName()).setCombinationAuthorTeam(((ZoologicalName)taxonFaunaEu.getName()).getCombinationAuthorTeam());
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 160
				}
161 161
				((ZoologicalName)taxonErms.getName()).generateAuthorship();
162 162
				taxaToSave.add(taxonErms);
163 163
			}else{
164
				if (((ZoologicalName)taxonErms.getName()).getBasionymAuthorTeam()!= null){
165
					((ZoologicalName)taxonFaunaEu.getName()).setBasionymAuthorTeam(((ZoologicalName)taxonErms.getName()).getBasionymAuthorTeam());
166
				} 
167
				if (((ZoologicalName)taxonErms.getName()).getCombinationAuthorTeam()!= null){
168
					((ZoologicalName)taxonFaunaEu.getName()).setCombinationAuthorTeam(((ZoologicalName)taxonErms.getName()).getCombinationAuthorTeam());
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 169
				}
170 170
				((ZoologicalName)taxonFaunaEu.getName()).generateAuthorship();
171 171
				taxaToSave.add(taxonFaunaEu);
172 172
			}
173
			
174
			
173

  
174

  
175 175
		}
176 176
	}
177
	
177

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

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

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

  
188 188
		//find all taxa accepted in erms, but synonyms in FauEu  and the same rank
189 189
		List<List<String>> accErmsSynFaunaEu = new ArrayList<List<String>>();
190 190
		for (List<String> rowList: diffStatus){
......
194 194
			}
195 195
		}
196 196
		mergeErmsAccFaunaEuSyn(accErmsSynFaunaEu);
197
		
197

  
198 198
		//find all taxa accepted in faunaEu, but synonyms in Erms and the same rank
199 199
		List<List<String>> synErmsAccFaunaEu = new ArrayList<List<String>>();
200 200
		for (List<String> rowList: diffStatus){
......
204 204
			}
205 205
		}
206 206
		mergeErmsSynFaunaEuAcc(synErmsAccFaunaEu);
207
		
208
	
207

  
208

  
209 209
	}
210
	
210

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

  
214 214
		TaxonBase taxonFaunaEu;
215 215
		TaxonBase taxonErms;
216
		
216

  
217 217
		for (List<String> row: sameStatus){
218 218
			taxonFaunaEu = appCtrInit.getTaxonService().find(UUID.fromString(row.get(faunaEuUuid)));
219 219
			taxonErms = appCtrInit.getTaxonService().find(UUID.fromString(row.get(ermsUuid)));
......
223 223
			}
224 224
		}
225 225
	}
226
	
227
	
226

  
227

  
228 228

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

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

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

  
242
		//merge the infos of
243

  
244

  
245 245
	}
246
	
246

  
247 247
	private  void mergeErmsSynFaunaEuAcc (List<List<String>> ermsAccFaEuSyn){
248 248
		//occurence: verkn�pfe statt dem Fauna Europaea Taxon das akzeptierte Taxon, des Synonyms mit der Occurence (CDM -> distribution)
249 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
......
255 255
			synErms = (Synonym)appCtrInit.getTaxonService().find(UUID.fromString(row.get(ermsUuid)));
256 256
			synErms = HibernateProxyHelper.deproxy(synErms, Synonym.class);
257 257
			Set<SynonymRelationship> synRel=synErms.getSynonymRelations();
258
			
258

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

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

  
277

  
278 278
			for(Distribution distribution: distributions){
279 279
				TaxonDescription description = (TaxonDescription)distribution.getInDescription();
280 280
				TaxonDescription newDescription = TaxonDescription.NewInstance(taxonErms);
......
285 285
					logger.debug("The description of" + description.getTaxon().getTitleCache() + description.getTitleCache() + "can't be deleted because it is referenced.");
286 286
				}
287 287
			}
288
			
289
			
288

  
289

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

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

  
298 298
				Iterator<TaxonNode> iteratorNodesFaunaEu = nodesFaunaEu.iterator();
299 299
				TaxonNode node = iteratorNodesFaunaEu.next();
300 300
				List<TaxonNode> children = node.getChildNodes();
......
307 307
						ermsNode.addChildNode(childNode, childNode.getReference(), childNode.getMicroReference());
308 308
					}
309 309
				}
310
				
310

  
311 311
			}
312 312
			moveFaunaEuSynonymsToErmsTaxon(taxonFaunaEu, taxonErms);
313 313
			moveAllInformationsFromFaunaEuToErms(taxonFaunaEu, taxonErms);
......
319 319
				logger.debug("The taxon " + taxonFaunaEu.getTitleCache() + " can't be deleted because it is referenced.");
320 320
			}
321 321
		}
322
		
323
		
324
		
325
	
322

  
323

  
324

  
325

  
326 326
	}
327
	
328
	
329
	
327

  
328

  
329

  
330 330
	private void updateNameRelationships(List<List<String>> ermsAccFaEuSyn){
331 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 332
		//wenn es diese relationship noch nicht gibt und der typ der gleiche ist!!
333 333
		//wenn der relatedTo Name zu einem Erms Taxon und einem FaunaEu Synonym geh�rt
334
		
334

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

  
343 343
			List<NameRelationship> deleteRel = new ArrayList<NameRelationship>();
344 344
			for (NameRelationship relFauEu: relSynFaunaEu){
345 345
				boolean createNewRelationship = true;
......
356 356
					taxonErms.getName().addRelationshipFromName(relFauEu.getFromName(), relFauEu.getType(), relFauEu.getRuleConsidered());
357 357
				}
358 358
			}
359
			
359

  
360 360
		}
361 361
	}
362
	
362

  
363 363
	private void updateSynonymRelationships(List<List<String>> ermsSynFaEuAcc){
364 364
//		-- Update queries for RelTaxon (synonym relationships - move relationships to ERMS accepted taxon if not already existent or delete if already existent)
365 365
//		UPDATE RelTaxon_1 SET RelTaxon_1.TaxonFk2 = RT.TaxonFk2
......
390 390
				taxonErms = null;
391 391
				logger.debug("There is no accepted taxon for synonym "  + synErms.getTitleCache());
392 392
			}
393
			
393

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

  
398 398
				List<SynonymRelationship> deleteRel = new ArrayList<SynonymRelationship>();
399 399
				for (SynonymRelationship relFauEu: relTaxonFaunaEu){
400 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
......
409 409
					if (createNewRelationship){
410 410
						taxonErms.addSynonym(relFauEu.getSynonym(), relFauEu.getType());
411 411
					}
412
					
412

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

  
417 417
		}
418 418
	}
419
	
420
	
419

  
420

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

  
424 424
	}
425 425

  
426 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
	
427

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

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

  
445 445
				Iterator<Taxon> misappliedNamesIterator = misappliedNames.iterator();
446 446
				Taxon misappliedName;
447 447
				while (misappliedNamesIterator.hasNext()){
......
450 450
				}
451 451
			}
452 452
		}
453
		
453

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

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

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

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

  
480

  
481 481
	}
482
	
482

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

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

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

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

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

  
511

  
512

  
513

  
514 514
}

Also available in: Unified diff