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