1
|
package eu.etaxonomy.cdm.app.pesi.merging;
|
2
|
|
3
|
import java.io.FileWriter;
|
4
|
import java.io.IOException;
|
5
|
import java.util.ArrayList;
|
6
|
import java.util.Iterator;
|
7
|
import java.util.List;
|
8
|
import java.util.Set;
|
9
|
|
10
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
11
|
import eu.etaxonomy.cdm.app.common.CdmDestinations;
|
12
|
import eu.etaxonomy.cdm.app.common.TestDatabase;
|
13
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
14
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
15
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
16
|
import eu.etaxonomy.cdm.io.pesi.merging.FaunaEuErmsMerging;
|
17
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
18
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
19
|
import eu.etaxonomy.cdm.model.name.IZoologicalName;
|
20
|
import eu.etaxonomy.cdm.model.name.Rank;
|
21
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
22
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
23
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
24
|
|
25
|
public class FaunaEuErmsFindIdenticalNamesActivator {
|
26
|
|
27
|
static final ICdmDataSource faunaEuropaeaSource = CdmDestinations.localH2();
|
28
|
//static final ICdmDataSource ermsSource = CdmDestinations.cdm_test_andreasM();
|
29
|
|
30
|
//TODO hole aus beiden DB alle TaxonNameBases
|
31
|
|
32
|
|
33
|
private CdmApplicationController initDb(ICdmDataSource db) {
|
34
|
|
35
|
// Init source DB
|
36
|
CdmApplicationController appCtrInit = null;
|
37
|
|
38
|
appCtrInit = TestDatabase.initDb(db, DbSchemaValidation.VALIDATE, false);
|
39
|
|
40
|
return appCtrInit;
|
41
|
}
|
42
|
|
43
|
|
44
|
/**
|
45
|
* @param args
|
46
|
*/
|
47
|
public static void main(String[] args) {
|
48
|
|
49
|
FaunaEuErmsFindIdenticalNamesActivator sc = new FaunaEuErmsFindIdenticalNamesActivator();
|
50
|
|
51
|
CdmApplicationController appCtrFaunaEu = sc.initDb(faunaEuropaeaSource);
|
52
|
String sFileName = "c:\\test";
|
53
|
//CdmApplicationController appCtrErms = sc.initDb(ermsSource);
|
54
|
List<String> propertyPaths = new ArrayList<String>();
|
55
|
propertyPaths.add("sources.*");
|
56
|
propertyPaths.add("sources.idInSource");
|
57
|
propertyPaths.add("sources.idNamespace");
|
58
|
propertyPaths.add("taxonBases.*");
|
59
|
propertyPaths.add("taxonBases.relationsFromThisTaxon");
|
60
|
propertyPaths.add("taxonBases.taxonNodes.*");
|
61
|
propertyPaths.add("taxonBases.taxonNodes.parent.*");
|
62
|
propertyPaths.add("taxonBases.taxonNodes.parent.taxon.name.*");
|
63
|
System.err.println("Start getIdenticalNames...");
|
64
|
List<TaxonName> namesOfIdenticalTaxa = appCtrFaunaEu.getTaxonService().findIdenticalTaxonNameIds(propertyPaths);
|
65
|
//List<UUID> namesOfIdenticalTaxa = appCtrFaunaEu.getTaxonService().findIdenticalTaxonNameIds(propertyPaths);
|
66
|
|
67
|
System.err.println("first name: " + namesOfIdenticalTaxa.get(0) + " " + namesOfIdenticalTaxa.size());
|
68
|
TaxonName zooName = namesOfIdenticalTaxa.get(0);
|
69
|
System.err.println(zooName + " nr of taxa " + namesOfIdenticalTaxa.size());
|
70
|
//TaxonNameComparator taxComp = new TaxonNameComparator();
|
71
|
|
72
|
//Collections.sort(namesOfIdenticalTaxa,taxComp);
|
73
|
System.err.println(namesOfIdenticalTaxa.get(0) + " - " + namesOfIdenticalTaxa.get(1) + " - " + namesOfIdenticalTaxa.get(2));
|
74
|
List<FaunaEuErmsMerging> mergingObjects = new ArrayList<FaunaEuErmsMerging>();
|
75
|
FaunaEuErmsMerging mergeObject;
|
76
|
TaxonName faunaEuTaxName;
|
77
|
TaxonName ermsTaxName;
|
78
|
|
79
|
mergingObjects= sc.createMergeObjects(namesOfIdenticalTaxa, appCtrFaunaEu);
|
80
|
|
81
|
sc.writeSameNamesdifferentAuthorToCsv(mergingObjects, sFileName + "_authors.csv");
|
82
|
sc.writeSameNamesdifferentStatusToCsv(mergingObjects, sFileName + "_status.csv");
|
83
|
sc.writeSameNamesToCsVFile(mergingObjects, sFileName + "_names.csv");
|
84
|
sc.writeSameNamesdifferentPhylumToCsv(mergingObjects, sFileName + "_phylum.csv");
|
85
|
|
86
|
|
87
|
System.out.println("End merging Fauna Europaea and Erms");
|
88
|
|
89
|
}
|
90
|
|
91
|
private boolean writeSameNamesToCsVFile(
|
92
|
List<FaunaEuErmsMerging> mergingObjects, String string) {
|
93
|
try{
|
94
|
FileWriter writer = new FileWriter(string);
|
95
|
|
96
|
//create Header
|
97
|
String firstLine = "same names";
|
98
|
createHeader(writer, firstLine);
|
99
|
for (FaunaEuErmsMerging merging : mergingObjects){
|
100
|
writeCsvLine(writer, merging) ;
|
101
|
}
|
102
|
writer.flush();
|
103
|
writer.close();
|
104
|
}
|
105
|
catch(IOException e)
|
106
|
{
|
107
|
return false;
|
108
|
}
|
109
|
return true;
|
110
|
}
|
111
|
|
112
|
|
113
|
private boolean writeSameNamesdifferentPhylumToCsv(List<FaunaEuErmsMerging> mergingObjects, String sfileName){
|
114
|
try
|
115
|
{
|
116
|
FileWriter writer = new FileWriter(sfileName);
|
117
|
|
118
|
//create Header
|
119
|
String firstLine = "same names but different phylum";
|
120
|
createHeader(writer, firstLine);
|
121
|
|
122
|
//write data
|
123
|
for (FaunaEuErmsMerging merging : mergingObjects){
|
124
|
//TODO
|
125
|
if ((merging.getPhylumInErms()== null )^ (merging.getPhylumInFaunaEu()== null)){
|
126
|
writeCsvLine(writer, merging) ;
|
127
|
}else if(!((merging.getPhylumInErms()==null) && (merging.getPhylumInFaunaEu()==null))){
|
128
|
if(!merging.getPhylumInErms().equals(merging.getPhylumInFaunaEu())){
|
129
|
writeCsvLine(writer, merging) ;
|
130
|
}
|
131
|
}
|
132
|
}
|
133
|
writer.flush();
|
134
|
writer.close();
|
135
|
}
|
136
|
catch(IOException e)
|
137
|
{
|
138
|
return false;
|
139
|
}
|
140
|
return true;
|
141
|
}
|
142
|
|
143
|
private boolean writeSameNamesdifferentRankToCsv(List<FaunaEuErmsMerging> mergingObjects, String sfileName){
|
144
|
try
|
145
|
{
|
146
|
FileWriter writer = new FileWriter(sfileName);
|
147
|
String firstLine = "same names but different rank";
|
148
|
//create Header
|
149
|
createHeader(writer, firstLine);
|
150
|
|
151
|
//write data
|
152
|
for (FaunaEuErmsMerging merging : mergingObjects){
|
153
|
|
154
|
if (!merging.getRankInErms().equals(merging.getRankInFaunaEu())){
|
155
|
writeCsvLine(writer, merging);
|
156
|
}
|
157
|
}
|
158
|
writer.flush();
|
159
|
writer.close();
|
160
|
}
|
161
|
catch(IOException e)
|
162
|
{
|
163
|
return false;
|
164
|
}
|
165
|
return true;
|
166
|
}
|
167
|
|
168
|
private void createHeader(FileWriter writer, String firstLine) throws IOException{
|
169
|
writer.append(firstLine);
|
170
|
writer.append('\n');
|
171
|
writer.append("uuid in Fauna Europaea");
|
172
|
writer.append(';');
|
173
|
writer.append("id in Fauna Europaea");
|
174
|
writer.append(';');
|
175
|
writer.append("name");
|
176
|
writer.append(';');
|
177
|
writer.append("author");
|
178
|
writer.append(';');
|
179
|
writer.append("rank");
|
180
|
writer.append(';');
|
181
|
writer.append("state");
|
182
|
writer.append(';');
|
183
|
writer.append("phylum");
|
184
|
writer.append(';');
|
185
|
writer.append("parent");
|
186
|
writer.append(';');
|
187
|
writer.append("parent rank");
|
188
|
writer.append(';');
|
189
|
|
190
|
writer.append("uuid in Erms");
|
191
|
writer.append(';');
|
192
|
writer.append("id in Erms");
|
193
|
writer.append(';');
|
194
|
writer.append("name");
|
195
|
writer.append(';');
|
196
|
writer.append("author");
|
197
|
writer.append(';');
|
198
|
writer.append("rank");
|
199
|
writer.append(';');
|
200
|
writer.append("state");
|
201
|
writer.append(';');
|
202
|
writer.append("phylum");
|
203
|
writer.append(';');
|
204
|
writer.append("parent");
|
205
|
writer.append(';');
|
206
|
writer.append("parent rank");
|
207
|
writer.append('\n');
|
208
|
}
|
209
|
|
210
|
private boolean writeSameNamesdifferentStatusToCsv(List<FaunaEuErmsMerging> mergingObjects, String sfileName){
|
211
|
try
|
212
|
{
|
213
|
FileWriter writer = new FileWriter(sfileName);
|
214
|
|
215
|
//create Header
|
216
|
String firstLine = "same names but different status";
|
217
|
createHeader(writer, firstLine);
|
218
|
|
219
|
//write data
|
220
|
for (FaunaEuErmsMerging merging : mergingObjects){
|
221
|
|
222
|
if (merging.isStatInErms()^merging.isStatInFaunaEu()){
|
223
|
writeCsvLine(writer, merging);
|
224
|
}
|
225
|
}
|
226
|
|
227
|
|
228
|
writer.flush();
|
229
|
writer.close();
|
230
|
}
|
231
|
catch(IOException e)
|
232
|
{
|
233
|
return false;
|
234
|
}
|
235
|
return true;
|
236
|
}
|
237
|
|
238
|
private boolean writeSameNamesdifferentAuthorToCsv(List<FaunaEuErmsMerging> mergingObjects, String sfileName){
|
239
|
try
|
240
|
{
|
241
|
FileWriter writer = new FileWriter(sfileName);
|
242
|
|
243
|
//create Header
|
244
|
String firstLine = "same names but different authors";
|
245
|
createHeader(writer, firstLine);
|
246
|
|
247
|
//write data
|
248
|
for (FaunaEuErmsMerging merging : mergingObjects){
|
249
|
|
250
|
if (!merging.getAuthorInErms().equals(merging.getAuthorInFaunaEu())){
|
251
|
writeCsvLine(writer, merging);
|
252
|
}
|
253
|
}
|
254
|
|
255
|
|
256
|
writer.flush();
|
257
|
writer.close();
|
258
|
}
|
259
|
catch(IOException e)
|
260
|
{
|
261
|
return false;
|
262
|
}
|
263
|
return true;
|
264
|
}
|
265
|
|
266
|
private void writeCsvLine(FileWriter writer, FaunaEuErmsMerging merging) throws IOException{
|
267
|
|
268
|
writer.append(merging.getUuidFaunaEu());
|
269
|
writer.append(';');
|
270
|
writer.append(merging.getIdInFaunaEu());
|
271
|
writer.append(';');
|
272
|
writer.append(merging.getNameCacheInFaunaEu());
|
273
|
writer.append(';');
|
274
|
writer.append(merging.getAuthorInFaunaEu());
|
275
|
writer.append(';');
|
276
|
writer.append(merging.getRankInFaunaEu());
|
277
|
writer.append(';');
|
278
|
if (merging.isStatInFaunaEu()){
|
279
|
writer.append("accepted");
|
280
|
}else{
|
281
|
writer.append("synonym");
|
282
|
}
|
283
|
writer.append(';');
|
284
|
writer.append(merging.getPhylumInFaunaEu());
|
285
|
writer.append(';');
|
286
|
writer.append(merging.getParentStringInFaunaEu());
|
287
|
writer.append(';');
|
288
|
writer.append(merging.getParentRankStringInFaunaEu());
|
289
|
writer.append(';');
|
290
|
|
291
|
writer.append(merging.getUuidErms());
|
292
|
writer.append(';');
|
293
|
writer.append(merging.getIdInErms());
|
294
|
writer.append(';');
|
295
|
writer.append(merging.getNameCacheInErms());
|
296
|
writer.append(';');
|
297
|
writer.append(merging.getAuthorInErms());
|
298
|
writer.append(';');
|
299
|
writer.append(merging.getRankInErms());
|
300
|
writer.append(';');
|
301
|
if (merging.isStatInErms()){
|
302
|
writer.append("accepted");
|
303
|
}else{
|
304
|
writer.append("synonym");
|
305
|
}
|
306
|
|
307
|
writer.append(';');
|
308
|
writer.append(merging.getPhylumInErms());
|
309
|
writer.append(';');
|
310
|
writer.append(merging.getParentStringInErms());
|
311
|
writer.append(';');
|
312
|
writer.append(merging.getParentRankStringInErms());
|
313
|
writer.append('\n');
|
314
|
}
|
315
|
|
316
|
|
317
|
private List<FaunaEuErmsMerging> createMergeObjects(List<TaxonName> names, CdmApplicationController appCtr){
|
318
|
|
319
|
List<FaunaEuErmsMerging> merge = new ArrayList<FaunaEuErmsMerging>();
|
320
|
TaxonName zooName, zooName2;
|
321
|
FaunaEuErmsMerging mergeObject;
|
322
|
String idInSource1;
|
323
|
for (int i = 0; i<names.size()-1; i=i+2){
|
324
|
zooName = names.get(i);
|
325
|
zooName2 = names.get(i+1);
|
326
|
mergeObject = new FaunaEuErmsMerging();
|
327
|
//TODO:überprüfen, ob die beiden Namen identisch sind und aus unterschiedlichen DB kommen
|
328
|
|
329
|
//getPhylum
|
330
|
String phylum1 = null;
|
331
|
if (!zooName.getRank().isHigher(Rank.PHYLUM())){
|
332
|
phylum1 =appCtr.getTaxonService().getPhylumName(zooName);
|
333
|
}
|
334
|
|
335
|
String phylum2 = null;
|
336
|
if (!zooName2.getRank().isHigher(Rank.PHYLUM())){
|
337
|
phylum2 = appCtr.getTaxonService().getPhylumName(zooName2);
|
338
|
}
|
339
|
mergeObject.setPhylumInErms(phylum1);
|
340
|
mergeObject.setPhylumInFaunaEu(phylum2);
|
341
|
|
342
|
//getUuids
|
343
|
mergeObject.setUuidErms(zooName.getUuid().toString());
|
344
|
mergeObject.setUuidFaunaEu(zooName.getUuid().toString());
|
345
|
|
346
|
Iterator sources = zooName.getSources().iterator();
|
347
|
if (sources.hasNext()){
|
348
|
IdentifiableSource source = (IdentifiableSource)sources.next();
|
349
|
idInSource1 = source.getIdInSource();
|
350
|
mergeObject.setIdInErms(idInSource1);
|
351
|
}
|
352
|
sources = zooName2.getSources().iterator();
|
353
|
if (sources.hasNext()){
|
354
|
IdentifiableSource source = (IdentifiableSource)sources.next();
|
355
|
idInSource1 = source.getIdInSource();
|
356
|
mergeObject.setIdInFaunaEu(idInSource1);
|
357
|
}
|
358
|
|
359
|
mergeObject.setNameCacheInErms(zooName.getNameCache());
|
360
|
mergeObject.setNameCacheInFaunaEu(zooName2.getNameCache());
|
361
|
|
362
|
mergeObject.setAuthorInErms(zooName.getAuthorshipCache());
|
363
|
mergeObject.setAuthorInFaunaEu(zooName2.getAuthorshipCache());
|
364
|
Set<Taxon> taxa = zooName.getTaxa();
|
365
|
if (!taxa.isEmpty()){
|
366
|
mergeObject.setStatInErms(true);
|
367
|
Iterator taxaIterator = taxa.iterator();
|
368
|
Taxon taxon = null;
|
369
|
while (taxaIterator.hasNext()){
|
370
|
taxon = (Taxon) taxaIterator.next();
|
371
|
if (!taxon.isMisapplication()){
|
372
|
break;
|
373
|
}
|
374
|
}
|
375
|
Set<TaxonNode> nodes = taxon.getTaxonNodes();
|
376
|
Iterator taxonNodeIterator = nodes.iterator();
|
377
|
TaxonNode node, parentNode = null;
|
378
|
while (taxonNodeIterator.hasNext()){
|
379
|
node = (TaxonNode)taxonNodeIterator.next();
|
380
|
if (!node.isTopmostNode()){
|
381
|
parentNode = node.getParent();
|
382
|
}
|
383
|
}
|
384
|
//TODO: ändern mit erweitertem Initializer..
|
385
|
if (parentNode != null){
|
386
|
TaxonName parentName = HibernateProxyHelper.deproxy(parentNode.getTaxon().getName());
|
387
|
String parentNameCache = parentName.getNameCache();
|
388
|
mergeObject.setParentStringInErms(parentNameCache);
|
389
|
mergeObject.setParentRankStringInErms(parentName.getRank().getLabel());
|
390
|
//System.err.println("parentName: " + parentNameCache);
|
391
|
}
|
392
|
}else{
|
393
|
mergeObject.setStatInErms(false);
|
394
|
}
|
395
|
taxa = zooName2.getTaxa();
|
396
|
if (!taxa.isEmpty()){
|
397
|
mergeObject.setStatInFaunaEu(true);
|
398
|
Iterator taxaIterator = taxa.iterator();
|
399
|
Taxon taxon = null;
|
400
|
while (taxaIterator.hasNext()){
|
401
|
taxon = (Taxon) taxaIterator.next();
|
402
|
if (!taxon.isMisapplication()){
|
403
|
break;
|
404
|
}
|
405
|
}
|
406
|
Set<TaxonNode> nodes = taxon.getTaxonNodes();
|
407
|
Iterator taxonNodeIterator = nodes.iterator();
|
408
|
TaxonNode node, parentNode = null;
|
409
|
while (taxonNodeIterator.hasNext()){
|
410
|
node = (TaxonNode)taxonNodeIterator.next();
|
411
|
if (!node.isTopmostNode()){
|
412
|
parentNode = node.getParent();
|
413
|
}
|
414
|
}
|
415
|
//TODO: ändern mit erweitertem Initializer..
|
416
|
if (parentNode != null){
|
417
|
if (parentNode.getTaxon().getName().isZoological()){
|
418
|
|
419
|
IZoologicalName parentName = CdmBase.deproxy(parentNode.getTaxon().getName());
|
420
|
String parentNameCache = parentName.getNameCache();
|
421
|
mergeObject.setParentStringInFaunaEu(parentNameCache);
|
422
|
mergeObject.setParentRankStringInFaunaEu(parentName.getRank().getLabel());
|
423
|
System.err.println("parentName: " + parentNameCache);
|
424
|
}else{
|
425
|
System.err.println("no zoologicalName: " + parentNode.getTaxon().getName().getTitleCache() +" . "+parentNode.getTaxon().getName().getUuid());
|
426
|
}
|
427
|
|
428
|
}
|
429
|
}else{
|
430
|
mergeObject.setStatInErms(false);
|
431
|
}
|
432
|
taxa = zooName2.getTaxa();
|
433
|
if (!taxa.isEmpty()){
|
434
|
mergeObject.setStatInFaunaEu(true);
|
435
|
}else{
|
436
|
mergeObject.setStatInFaunaEu(false);
|
437
|
|
438
|
}
|
439
|
|
440
|
mergeObject.setRankInErms(zooName.getRank().getLabel());
|
441
|
mergeObject.setRankInFaunaEu(zooName2.getRank().getLabel());
|
442
|
|
443
|
|
444
|
|
445
|
|
446
|
//set parent informations
|
447
|
|
448
|
|
449
|
/*
|
450
|
Set<HybridRelationship> parentRelations = zooName.getParentRelationships();
|
451
|
Iterator parentIterator = parentRelations.iterator();
|
452
|
HybridRelationship parentRel;
|
453
|
ZoologicalName parentName;
|
454
|
while (parentIterator.hasNext()){
|
455
|
parentRel = (HybridRelationship)parentIterator.next();
|
456
|
parentName = (ZoologicalName)parentRel.getParentName();
|
457
|
mergeObject.setParentRankStringInErms(parentName.getRank().getLabel());
|
458
|
mergeObject.setParentStringInErms(parentName.getNameCache());
|
459
|
}
|
460
|
|
461
|
parentRelations = zooName2.getParentRelationships();
|
462
|
parentIterator = parentRelations.iterator();
|
463
|
|
464
|
while (parentIterator.hasNext()){
|
465
|
parentRel = (HybridRelationship)parentIterator.next();
|
466
|
parentName = (ZoologicalName)parentRel.getParentName();
|
467
|
mergeObject.setParentRankStringInFaunaEu(parentName.getRank().getLabel());
|
468
|
mergeObject.setParentStringInFaunaEu(parentName.getNameCache());
|
469
|
}*/
|
470
|
merge.add(mergeObject);
|
471
|
}
|
472
|
|
473
|
return merge;
|
474
|
|
475
|
|
476
|
}
|
477
|
|
478
|
}
|