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.tcsxml.in;
|
11
|
|
12
|
import static eu.etaxonomy.cdm.io.common.ImportHelper.OBLIGATORY;
|
13
|
import static eu.etaxonomy.cdm.io.common.ImportHelper.OVERWRITE;
|
14
|
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.Arrays;
|
17
|
import java.util.HashSet;
|
18
|
import java.util.List;
|
19
|
import java.util.Set;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.jdom.Content;
|
23
|
import org.jdom.Element;
|
24
|
import org.jdom.Namespace;
|
25
|
import org.jdom.Text;
|
26
|
import org.springframework.beans.factory.annotation.Autowired;
|
27
|
|
28
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
29
|
import eu.etaxonomy.cdm.common.ResultWrapper;
|
30
|
import eu.etaxonomy.cdm.common.XmlHelp;
|
31
|
import eu.etaxonomy.cdm.ext.ipni.IpniService;
|
32
|
import eu.etaxonomy.cdm.io.common.CdmImportBase;
|
33
|
import eu.etaxonomy.cdm.io.common.ImportHelper;
|
34
|
import eu.etaxonomy.cdm.io.common.MapWrapper;
|
35
|
import eu.etaxonomy.cdm.io.tcsxml.CdmSingleAttributeXmlMapperBase;
|
36
|
import eu.etaxonomy.cdm.model.agent.Person;
|
37
|
import eu.etaxonomy.cdm.model.agent.Team;
|
38
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
39
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
40
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
41
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
42
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
43
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
44
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
45
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
46
|
|
47
|
/**
|
48
|
* @author a.mueller
|
49
|
* @since 04.08.2008
|
50
|
*/
|
51
|
public abstract class TcsXmlImportBase extends CdmImportBase<TcsXmlImportConfigurator, TcsXmlImportState> {
|
52
|
private static final long serialVersionUID = -2169244092211698392L;
|
53
|
|
54
|
private static final Logger logger = Logger.getLogger(TcsXmlImportBase.class);
|
55
|
|
56
|
protected static Namespace nsTcom = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/Common#");
|
57
|
protected static Namespace nsTn = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonName#");
|
58
|
protected static Namespace nsTgeo = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/GeographicRegion#");
|
59
|
protected static Namespace nsTc = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/TaxonConcept#");
|
60
|
protected static Namespace nsTpub = Namespace.getNamespace("http://rs.tdwg.org/ontology/voc/PublicationCitation#");
|
61
|
protected static Namespace nsTpalm = Namespace.getNamespace("http://wp5.e-taxonomy.eu/import/palmae/common");
|
62
|
|
63
|
@Autowired
|
64
|
IpniService service;
|
65
|
|
66
|
@Override
|
67
|
protected abstract void doInvoke(TcsXmlImportState state);
|
68
|
|
69
|
// /* (non-Javadoc)
|
70
|
// * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
|
71
|
// */
|
72
|
// @Override
|
73
|
// protected boolean doInvoke(IImportConfigurator config,
|
74
|
// Map<String, MapWrapper<? extends CdmBase>> stores){
|
75
|
// TcsXmlImportState state = ((TcsXmlImportConfigurator)config).getState();
|
76
|
// state.setConfig((TcsXmlImportConfigurator)config);
|
77
|
// return doInvoke(state);
|
78
|
// }
|
79
|
|
80
|
|
81
|
protected boolean makeStandardMapper(Element parentElement, CdmBase ref, Set<String> omitAttributes, CdmSingleAttributeXmlMapperBase[] classMappers){
|
82
|
if (omitAttributes == null){
|
83
|
omitAttributes = new HashSet<String>();
|
84
|
}
|
85
|
boolean result = true;
|
86
|
for (CdmSingleAttributeXmlMapperBase mapper : classMappers){
|
87
|
Object value = getValue(mapper, parentElement);
|
88
|
//write to destination
|
89
|
if (value != null){
|
90
|
String destinationAttribute = mapper.getDestinationAttribute();
|
91
|
if (! omitAttributes.contains(destinationAttribute)){
|
92
|
result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
|
93
|
}
|
94
|
}
|
95
|
}
|
96
|
return true;
|
97
|
}
|
98
|
|
99
|
private Object getValue(CdmSingleAttributeXmlMapperBase mapper, Element parentElement){
|
100
|
String sourceAttribute = mapper.getSourceAttribute();
|
101
|
Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
|
102
|
Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
|
103
|
if (child == null){
|
104
|
return null;
|
105
|
}
|
106
|
if (child.getContentSize() > 1){
|
107
|
logger.warn("Element is not String");
|
108
|
}
|
109
|
Object value = child.getTextTrim();
|
110
|
return value;
|
111
|
}
|
112
|
|
113
|
protected boolean checkAdditionalContents(Element parentElement, CdmSingleAttributeXmlMapperBase[] classMappers, CdmSingleAttributeXmlMapperBase[] operationalMappers, CdmSingleAttributeXmlMapperBase[] unclearMappers){
|
114
|
List<Content> additionalContentList = new ArrayList<Content>();
|
115
|
List<Content> contentList = parentElement.getContent();
|
116
|
List<CdmSingleAttributeXmlMapperBase> mapperList = new ArrayList<CdmSingleAttributeXmlMapperBase>();
|
117
|
|
118
|
mapperList.addAll(Arrays.asList(classMappers));
|
119
|
mapperList.addAll(Arrays.asList(operationalMappers));
|
120
|
mapperList.addAll(Arrays.asList(unclearMappers));
|
121
|
|
122
|
for(Content content: contentList){
|
123
|
boolean contentExists = false;
|
124
|
if (content instanceof Element){
|
125
|
for (CdmSingleAttributeXmlMapperBase mapper : mapperList){
|
126
|
if (mapper.mapsSource(content, parentElement)){
|
127
|
contentExists = true;
|
128
|
break;
|
129
|
}
|
130
|
}
|
131
|
|
132
|
}else if (content instanceof Text){
|
133
|
//empty Text
|
134
|
if (((Text)content).getTextNormalize().equals("")){
|
135
|
contentExists = true;
|
136
|
}else{
|
137
|
//
|
138
|
}
|
139
|
}
|
140
|
|
141
|
if (contentExists == false){
|
142
|
additionalContentList.add(content);
|
143
|
}
|
144
|
}
|
145
|
for (Content additionalContent : additionalContentList){
|
146
|
logger.warn("Additional content: " + additionalContent);
|
147
|
}
|
148
|
return (additionalContentList.size() == 0);
|
149
|
}
|
150
|
|
151
|
protected Element getDataSetElement(TcsXmlImportConfigurator tcsConfig){
|
152
|
Element root = tcsConfig.getSourceRoot();
|
153
|
|
154
|
if (! "DataSet".equals(root.getName())){
|
155
|
logger.error("Root element is not 'DataSet'");
|
156
|
return null;
|
157
|
}
|
158
|
if (tcsConfig.getTcsXmlNamespace() == null){
|
159
|
logger.error("No namespace defined for tcs");
|
160
|
return null;
|
161
|
}
|
162
|
if (! tcsConfig.getTcsXmlNamespace().equals(root.getNamespace())){
|
163
|
logger.error("Wrong namespace for element 'DataSet'");
|
164
|
return null;
|
165
|
}
|
166
|
//TODO prevent multiple elements
|
167
|
|
168
|
return root;
|
169
|
}
|
170
|
|
171
|
// static public boolean checkFirstTwoFunctionElements(List<Object> objList){
|
172
|
// if (! (objList.get(0) instanceof TcsXmlImportConfigurator)){
|
173
|
// logger.error("first method object has wrong type. Must be " + TcsXmlImportConfigurator.class.getSimpleName() + " but is " + (objList.get(0) == null ? "null": objList.get(0).getClass().getSimpleName()));
|
174
|
// return false;
|
175
|
// }
|
176
|
// if (! (objList.get(1) == null) && ! (objList.get(1) instanceof Element)){
|
177
|
// logger.error("first method object has wrong type. Must be " + Element.class.getSimpleName() + " but is " + (objList.get(1) == null ? "null": objList.get(1).getClass().getSimpleName()));
|
178
|
// return false;
|
179
|
// }
|
180
|
// return true;
|
181
|
// }
|
182
|
|
183
|
|
184
|
protected boolean testAdditionalElements(Element parentElement, List<String> excludeList){
|
185
|
boolean result = true;
|
186
|
List<Element> list = parentElement.getChildren();
|
187
|
for (Element element : list){
|
188
|
if (! excludeList.contains(element.getName())){
|
189
|
logger.warn("Unknown element (" + element.getName() + ") in parent element (" + parentElement.getName() + ")");
|
190
|
result = false;
|
191
|
}
|
192
|
}
|
193
|
return result;
|
194
|
}
|
195
|
|
196
|
|
197
|
protected <T extends IdentifiableEntity> T makeReferenceType(Element element, Class<? extends T> clazz, MapWrapper<? extends T> objectMap, ResultWrapper<Boolean> success){
|
198
|
T result = null;
|
199
|
String linkType = element.getAttributeValue("linkType");
|
200
|
String ref = element.getAttributeValue("ref");
|
201
|
if (ref != null){
|
202
|
if (ref.matches("urn:lsid:ipni.org:.*:*")){
|
203
|
ref = ref.substring(0,ref.lastIndexOf(":"));
|
204
|
}
|
205
|
}
|
206
|
if(ref == null && linkType == null){
|
207
|
result = getInstance(clazz);
|
208
|
if (result != null){
|
209
|
String title = element.getTextNormalize();
|
210
|
result.setTitleCache(title, true);
|
211
|
}
|
212
|
}else if (linkType == null || linkType.equals("local")){
|
213
|
//TODO
|
214
|
result = objectMap.get(ref);
|
215
|
if (result == null){
|
216
|
result = getInstance(clazz);
|
217
|
if (result != null){
|
218
|
String title = element.getTextNormalize();
|
219
|
result.setTitleCache(title, true);
|
220
|
}
|
221
|
}
|
222
|
}else if(linkType.equals("external")){
|
223
|
logger.warn("External link types not yet implemented");
|
224
|
}else if(linkType.equals("other")){
|
225
|
logger.warn("Other link types not yet implemented");
|
226
|
}else{
|
227
|
logger.warn("Unknown link type or missing ref");
|
228
|
}
|
229
|
if (result == null){
|
230
|
success.setValue(false);
|
231
|
}
|
232
|
return result;
|
233
|
}
|
234
|
|
235
|
|
236
|
protected Reference makeAccordingTo(Element elAccordingTo, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
|
237
|
Reference result = null;
|
238
|
if (elAccordingTo != null){
|
239
|
String childName = "AccordingToDetailed";
|
240
|
boolean obligatory = false;
|
241
|
Element elAccordingToDetailed = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
|
242
|
|
243
|
childName = "Simple";
|
244
|
obligatory = true;
|
245
|
Element elSimple = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
|
246
|
|
247
|
if (elAccordingToDetailed != null){
|
248
|
result = makeAccordingToDetailed(elAccordingToDetailed, referenceMap, success);
|
249
|
}else{
|
250
|
result = ReferenceFactory.newGeneric();
|
251
|
String title = elSimple.getTextNormalize();
|
252
|
result.setTitleCache(title, true);
|
253
|
}
|
254
|
}
|
255
|
return result;
|
256
|
}
|
257
|
|
258
|
|
259
|
private Reference makeAccordingToDetailed(Element elAccordingToDetailed, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
|
260
|
Reference result = null;
|
261
|
Namespace tcsNamespace = elAccordingToDetailed.getNamespace();
|
262
|
if (elAccordingToDetailed != null){
|
263
|
//AuthorTeam
|
264
|
String childName = "AuthorTeam";
|
265
|
boolean obligatory = false;
|
266
|
Element elAuthorTeam = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
267
|
makeAccordingToAuthorTeam(elAuthorTeam, success);
|
268
|
|
269
|
//PublishedIn
|
270
|
childName = "PublishedIn";
|
271
|
obligatory = false;
|
272
|
Element elPublishedIn = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
273
|
result = makeReferenceType(elPublishedIn, Reference.class, referenceMap, success);
|
274
|
|
275
|
//MicroReference
|
276
|
childName = "MicroReference";
|
277
|
obligatory = false;
|
278
|
Element elMicroReference = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
|
279
|
if (elMicroReference != null){
|
280
|
String microReference = elMicroReference.getTextNormalize();
|
281
|
if (CdmUtils.Nz(microReference).equals("")){
|
282
|
//TODO
|
283
|
logger.warn("MicroReference not yet implemented for AccordingToDetailed");
|
284
|
}
|
285
|
}
|
286
|
}
|
287
|
return result;
|
288
|
}
|
289
|
|
290
|
private Team makeAccordingToAuthorTeam(Element elAuthorTeam, ResultWrapper<Boolean> succes){
|
291
|
Team result = null;
|
292
|
if (elAuthorTeam != null){
|
293
|
//TODO
|
294
|
logger.warn("AuthorTeam not yet implemented for AccordingToDetailed");
|
295
|
}
|
296
|
return result;
|
297
|
}
|
298
|
|
299
|
|
300
|
|
301
|
|
302
|
|
303
|
protected void testNoMoreElements(){
|
304
|
//TODO
|
305
|
//logger.info("testNoMoreElements Not yet implemented");
|
306
|
}
|
307
|
|
308
|
|
309
|
|
310
|
|
311
|
|
312
|
@SuppressWarnings("unchecked")
|
313
|
protected TeamOrPersonBase<?> makeNameCitation(Element elNameCitation, MapWrapper<Person> authorMap, ResultWrapper<Boolean> success){
|
314
|
TeamOrPersonBase<?> result = null;
|
315
|
String childName;
|
316
|
boolean obligatory;
|
317
|
if (elNameCitation != null){
|
318
|
Namespace ns = elNameCitation.getNamespace();
|
319
|
|
320
|
childName = "Authors";
|
321
|
obligatory = false;
|
322
|
Element elAuthors = XmlHelp.getSingleChildElement(success, elNameCitation, childName, ns, obligatory);
|
323
|
testNoMoreElements();
|
324
|
|
325
|
if (elAuthors != null){
|
326
|
childName = "AgentName";
|
327
|
List<Element> elAgentList = elAuthors.getChildren(childName, ns);
|
328
|
Team team = Team.NewInstance();
|
329
|
result = team;
|
330
|
if (elAgentList.size() > 1){
|
331
|
for(Element elAgent : elAgentList){
|
332
|
Person teamMember = makeAgent(elAgent, ns, authorMap, success);
|
333
|
team.addTeamMember(teamMember);
|
334
|
}
|
335
|
}else if(elAgentList.size() == 1){
|
336
|
result = makeAgent(elAgentList.get(0), ns, authorMap, success);
|
337
|
}
|
338
|
}else{
|
339
|
childName = "Simple";
|
340
|
obligatory = true;
|
341
|
Element elSimple = XmlHelp.getSingleChildElement(success, elNameCitation, childName, ns, obligatory);
|
342
|
String simple = (elSimple == null)? "" : elSimple.getTextNormalize();
|
343
|
result = Team.NewInstance();
|
344
|
result.setNomenclaturalTitle(simple);
|
345
|
}
|
346
|
}
|
347
|
return result;
|
348
|
}
|
349
|
|
350
|
private Person makeAgent(Element elAgentName, Namespace ns, MapWrapper<Person> agentMap, ResultWrapper<Boolean> success){
|
351
|
Person result = null;
|
352
|
if (elAgentName != null){
|
353
|
String authorTitle = elAgentName.getTextNormalize();
|
354
|
result = Person.NewTitledInstance(authorTitle);
|
355
|
Class<? extends Person> clazz = Person.class;
|
356
|
result = makeReferenceType(elAgentName, clazz, agentMap, success);
|
357
|
return result;
|
358
|
}else{
|
359
|
return null;
|
360
|
}
|
361
|
}
|
362
|
|
363
|
|
364
|
|
365
|
|
366
|
|
367
|
|
368
|
|
369
|
|
370
|
|
371
|
protected Integer getIntegerYear(String year){
|
372
|
try {
|
373
|
Integer result = Integer.valueOf(year);
|
374
|
return result;
|
375
|
} catch (NumberFormatException e) {
|
376
|
logger.warn("Year string could not be parsed. Set = 9999 instead");
|
377
|
return 9999;
|
378
|
}
|
379
|
}
|
380
|
|
381
|
protected String removeVersionOfRef(String ref){
|
382
|
if (ref != null && ref.matches("urn:lsid:ipni.org:.*:.*:.*")){
|
383
|
return ref = ref.substring(0,ref.lastIndexOf(":"));
|
384
|
} else {
|
385
|
return ref;
|
386
|
}
|
387
|
|
388
|
}
|
389
|
|
390
|
|
391
|
|
392
|
protected void makeTypification(TaxonName name, Element elTypifiacation, ResultWrapper<Boolean> success){
|
393
|
if (elTypifiacation != null){
|
394
|
//logger.warn("makeTypification not yet implemented");
|
395
|
//success.setValue(false);
|
396
|
}
|
397
|
}
|
398
|
|
399
|
|
400
|
protected void makePublicationStatus(TaxonName name, Element elPublicationStatus, ResultWrapper<Boolean> success){
|
401
|
//Status
|
402
|
|
403
|
if (elPublicationStatus != null){
|
404
|
String pubStat = elPublicationStatus.getAttributeValue("Note");
|
405
|
|
406
|
}
|
407
|
}
|
408
|
|
409
|
protected void makeProviderLink(TaxonName name, Element elProviderLink, ResultWrapper<Boolean> success){
|
410
|
if (elProviderLink != null){
|
411
|
//logger.warn("makeProviderLink not yet implemented");
|
412
|
//success.setValue(false);
|
413
|
}
|
414
|
}
|
415
|
|
416
|
|
417
|
protected void makeProviderSpecificData(TaxonName name, Element elProviderSpecificData, ResultWrapper<Boolean> success, TcsXmlImportState state){
|
418
|
if (elProviderSpecificData != null){
|
419
|
|
420
|
Namespace ns = elProviderSpecificData.getNamespace();
|
421
|
|
422
|
String childName = "ipniData";
|
423
|
boolean obligatory = true;
|
424
|
List<Element> elIpniData = elProviderSpecificData.getChildren();
|
425
|
Element el = elIpniData.get(0);
|
426
|
|
427
|
|
428
|
childName = "citationType";
|
429
|
ns = el.getNamespace();
|
430
|
|
431
|
Element elCitationType = XmlHelp.getSingleChildElement(success, el, childName, ns, obligatory);
|
432
|
|
433
|
childName = "referenceRemarks";
|
434
|
Element elReferenceRemarks = XmlHelp.getSingleChildElement(success, el, childName, ns, obligatory);
|
435
|
|
436
|
childName = "suppressed";
|
437
|
Element elSuppressed = XmlHelp.getSingleChildElement(success, el, childName, ns, obligatory);
|
438
|
|
439
|
childName = "score";
|
440
|
Element elScore = XmlHelp.getSingleChildElement(success, el, childName, ns, obligatory);
|
441
|
|
442
|
childName = "nomenclaturalSynonym";
|
443
|
Element elNomenclaturalSynonym = XmlHelp.getSingleChildElement(success, el, childName, ns, obligatory);
|
444
|
ns = elProviderSpecificData.getNamespace();
|
445
|
childName = "RelatedName";
|
446
|
Element elRelatedName = XmlHelp.getSingleChildElement(success, elNomenclaturalSynonym, childName, ns, obligatory);
|
447
|
|
448
|
//create homotypic synonym
|
449
|
if (elRelatedName != null){
|
450
|
String id =elRelatedName.getAttributeValue("ref");
|
451
|
System.out.println(removeVersionOfRef(id));
|
452
|
if (name.getTaxa().iterator().hasNext()){
|
453
|
Taxon taxon = (Taxon) name.getTaxa().iterator().next();
|
454
|
//if taxon already exist
|
455
|
taxon.addHomotypicSynonym((Synonym)state.getStore(TAXON_STORE).get(removeVersionOfRef(id)));
|
456
|
//otherwise add to a map for homotypic synonyms
|
457
|
}
|
458
|
}
|
459
|
|
460
|
}
|
461
|
}
|
462
|
|
463
|
@Override
|
464
|
protected boolean isIgnore(TcsXmlImportState state){
|
465
|
return ! state.getConfig().isDoTaxonNames();
|
466
|
}
|
467
|
|
468
|
|
469
|
protected static final Reference unknownSec(){
|
470
|
Reference result = ReferenceFactory.newGeneric();
|
471
|
result.setTitleCache("UNKNOWN", true);
|
472
|
return result;
|
473
|
}
|
474
|
|
475
|
|
476
|
|
477
|
}
|