Project

General

Profile

Download (16.4 KB) Statistics
| Branch: | Tag: | Revision:
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.TaxonNameBase;
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
 * @created 04.08.2008
50
 * @version 1.0
51
 */
52
public abstract class TcsXmlImportBase  extends CdmImportBase<TcsXmlImportConfigurator, TcsXmlImportState> {
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
//	/* (non-Javadoc)
69
//	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
70
//	 */
71
//	@Override
72
//	protected boolean doInvoke(IImportConfigurator config,
73
//			Map<String, MapWrapper<? extends CdmBase>> stores){
74
//		TcsXmlImportState state = ((TcsXmlImportConfigurator)config).getState();
75
//		state.setConfig((TcsXmlImportConfigurator)config);
76
//		return doInvoke(state);
77
//	}
78

    
79

    
80
	protected boolean makeStandardMapper(Element parentElement, CdmBase ref, Set<String> omitAttributes, CdmSingleAttributeXmlMapperBase[] classMappers){
81
		if (omitAttributes == null){
82
			omitAttributes = new HashSet<String>();
83
		}
84
		boolean result = true;
85
		for (CdmSingleAttributeXmlMapperBase mapper : classMappers){
86
			Object value = getValue(mapper, parentElement);
87
			//write to destination
88
			if (value != null){
89
				String destinationAttribute = mapper.getDestinationAttribute();
90
				if (! omitAttributes.contains(destinationAttribute)){
91
					result &= ImportHelper.addValue(value, ref, destinationAttribute, mapper.getTypeClass(), OVERWRITE, OBLIGATORY);
92
				}
93
			}
94
		}
95
		return true;
96
	}
97

    
98
	private Object getValue(CdmSingleAttributeXmlMapperBase mapper, Element parentElement){
99
		String sourceAttribute = mapper.getSourceAttribute();
100
		Namespace sourceNamespace = mapper.getSourceNamespace(parentElement);
101
		Element child = parentElement.getChild(sourceAttribute, sourceNamespace);
102
		if (child == null){
103
			return null;
104
		}
105
		if (child.getContentSize() > 1){
106
			logger.warn("Element is not String");
107
		}
108
		Object value = child.getTextTrim();
109
		return value;
110
	}
111

    
112
	protected boolean checkAdditionalContents(Element parentElement, CdmSingleAttributeXmlMapperBase[] classMappers, CdmSingleAttributeXmlMapperBase[] operationalMappers, CdmSingleAttributeXmlMapperBase[] unclearMappers){
113
		List<Content> additionalContentList = new ArrayList<Content>();
114
		List<Content> contentList = parentElement.getContent();
115
		List<CdmSingleAttributeXmlMapperBase> mapperList = new ArrayList<CdmSingleAttributeXmlMapperBase>();
116

    
117
		mapperList.addAll(Arrays.asList(classMappers));
118
		mapperList.addAll(Arrays.asList(operationalMappers));
119
		mapperList.addAll(Arrays.asList(unclearMappers));
120

    
121
		for(Content content: contentList){
122
			boolean contentExists = false;
123
			if (content instanceof Element){
124
				for (CdmSingleAttributeXmlMapperBase mapper : mapperList){
125
					if (mapper.mapsSource(content, parentElement)){
126
						contentExists = true;
127
						break;
128
					}
129
				}
130

    
131
			}else if (content instanceof Text){
132
				//empty Text
133
				if (((Text)content).getTextNormalize().equals("")){
134
					contentExists = true;
135
				}else{
136
					//
137
				}
138
			}
139

    
140
			if (contentExists == false){
141
				additionalContentList.add(content);
142
			}
143
		}
144
		for (Content additionalContent : additionalContentList){
145
			logger.warn("Additional content: " +  additionalContent);
146
		}
147
		return (additionalContentList.size() == 0);
148
	}
149

    
150
	protected Element getDataSetElement(TcsXmlImportConfigurator tcsConfig){
151
		Element root = tcsConfig.getSourceRoot();
152

    
153
		if (! "DataSet".equals(root.getName())){
154
			logger.error("Root element is not 'DataSet'");
155
			return null;
156
		}
157
		if (tcsConfig.getTcsXmlNamespace() == null){
158
			logger.error("No namespace defined for tcs");
159
			return null;
160
		}
161
		if (! tcsConfig.getTcsXmlNamespace().equals(root.getNamespace())){
162
			logger.error("Wrong namespace for element 'DataSet'");
163
			return null;
164
		}
165
		//TODO prevent multiple elements
166

    
167
		return root;
168
	}
169

    
170
//	static public boolean checkFirstTwoFunctionElements(List<Object> objList){
171
//		if (! (objList.get(0) instanceof TcsXmlImportConfigurator)){
172
//			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()));
173
//			return false;
174
//		}
175
//		if (! (objList.get(1) == null) && ! (objList.get(1) instanceof Element)){
176
//			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()));
177
//			return false;
178
//		}
179
//		return true;
180
//	}
181

    
182

    
183
	protected boolean testAdditionalElements(Element parentElement, List<String> excludeList){
184
		boolean result = true;
185
		List<Element> list = parentElement.getChildren();
186
		for (Element element : list){
187
			if (! excludeList.contains(element.getName())){
188
				logger.warn("Unknown element (" + element.getName() + ") in parent element (" + parentElement.getName() + ")");
189
				result = false;
190
			}
191
		}
192
		return result;
193
	}
194

    
195

    
196
	protected <T extends IdentifiableEntity> T makeReferenceType(Element element, Class<? extends T> clazz, MapWrapper<? extends T> objectMap, ResultWrapper<Boolean> success){
197
		T result = null;
198
		String linkType = element.getAttributeValue("linkType");
199
		String ref = element.getAttributeValue("ref");
200
		if (ref != null){
201
			if (ref.matches("urn:lsid:ipni.org:.*:*")){
202
				ref = ref.substring(0,ref.lastIndexOf(":"));
203
			}
204
		}
205
		if(ref == null && linkType == null){
206
			result = getInstance(clazz);
207
			if (result != null){
208
				String title = element.getTextNormalize();
209
				result.setTitleCache(title, true);
210
			}
211
		}else if (linkType == null || linkType.equals("local")){
212
			//TODO
213
			result = objectMap.get(ref);
214
			if (result == null){
215
				result = getInstance(clazz);
216
				if (result != null){
217
					String title = element.getTextNormalize();
218
					result.setTitleCache(title, true);
219
				}
220
			}
221
		}else if(linkType.equals("external")){
222
			logger.warn("External link types not yet implemented");
223
		}else if(linkType.equals("other")){
224
			logger.warn("Other link types not yet implemented");
225
		}else{
226
			logger.warn("Unknown link type or missing ref");
227
		}
228
		if (result == null){
229
			success.setValue(false);
230
		}
231
		return result;
232
	}
233

    
234

    
235
	protected Reference makeAccordingTo(Element elAccordingTo, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
236
		Reference result = null;
237
		if (elAccordingTo != null){
238
			String childName = "AccordingToDetailed";
239
			boolean obligatory = false;
240
			Element elAccordingToDetailed = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
241

    
242
			childName = "Simple";
243
			obligatory = true;
244
			Element elSimple = XmlHelp.getSingleChildElement(success, elAccordingTo, childName, elAccordingTo.getNamespace(), obligatory);
245

    
246
			if (elAccordingToDetailed != null){
247
				result = makeAccordingToDetailed(elAccordingToDetailed, referenceMap, success);
248
			}else{
249
				result = ReferenceFactory.newGeneric();
250
				String title = elSimple.getTextNormalize();
251
				result.setTitleCache(title, true);
252
			}
253
		}
254
		return result;
255
	}
256

    
257

    
258
	private Reference makeAccordingToDetailed(Element elAccordingToDetailed, MapWrapper<Reference> referenceMap, ResultWrapper<Boolean> success){
259
		Reference result = null;
260
		Namespace tcsNamespace = elAccordingToDetailed.getNamespace();
261
		if (elAccordingToDetailed != null){
262
			//AuthorTeam
263
			String childName = "AuthorTeam";
264
			boolean obligatory = false;
265
			Element elAuthorTeam = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
266
			makeAccordingToAuthorTeam(elAuthorTeam, success);
267

    
268
			//PublishedIn
269
			childName = "PublishedIn";
270
			obligatory = false;
271
			Element elPublishedIn = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
272
			result = makeReferenceType(elPublishedIn, Reference.class, referenceMap, success);
273

    
274
			//MicroReference
275
			childName = "MicroReference";
276
			obligatory = false;
277
			Element elMicroReference = XmlHelp.getSingleChildElement(success, elAccordingToDetailed, childName, tcsNamespace, obligatory);
278
			if (elMicroReference != null){
279
				String microReference = elMicroReference.getTextNormalize();
280
				if (CdmUtils.Nz(microReference).equals("")){
281
					//TODO
282
					logger.warn("MicroReference not yet implemented for AccordingToDetailed");
283
				}
284
			}
285
		}
286
		return result;
287
	}
288

    
289
	private Team makeAccordingToAuthorTeam(Element elAuthorTeam, ResultWrapper<Boolean> succes){
290
		Team result = null;
291
		if (elAuthorTeam != null){
292
			//TODO
293
			logger.warn("AuthorTeam not yet implemented for AccordingToDetailed");
294
		}
295
		return result;
296
	}
297

    
298

    
299

    
300

    
301

    
302
	protected void testNoMoreElements(){
303
		//TODO
304
		//logger.info("testNoMoreElements Not yet implemented");
305
	}
306

    
307

    
308

    
309

    
310

    
311
	@SuppressWarnings("unchecked")
312
	protected TeamOrPersonBase<?> makeNameCitation(Element elNameCitation, MapWrapper<Person> authorMap, ResultWrapper<Boolean> success){
313
		TeamOrPersonBase<?> result = null;
314
		String childName;
315
		boolean obligatory;
316
		if (elNameCitation != null){
317
			Namespace ns = elNameCitation.getNamespace();
318

    
319
			childName = "Authors";
320
			obligatory = false;
321
			Element elAuthors = XmlHelp.getSingleChildElement(success, elNameCitation, childName, ns, obligatory);
322
			testNoMoreElements();
323

    
324
			if (elAuthors != null){
325
				childName = "AgentName";
326
				List<Element> elAgentList = elAuthors.getChildren(childName, ns);
327
				Team team = Team.NewInstance();
328
				result = team;
329
				if (elAgentList.size() > 1){
330
					for(Element elAgent : elAgentList){
331
						Person teamMember = makeAgent(elAgent, ns, authorMap, success);
332
						team.addTeamMember(teamMember);
333
					}
334
				}else if(elAgentList.size() == 1){
335
					result = makeAgent(elAgentList.get(0), ns, authorMap, success);
336
				}
337
			}else{
338
				childName = "Simple";
339
				obligatory = true;
340
				Element elSimple = XmlHelp.getSingleChildElement(success, elNameCitation, childName, ns, obligatory);
341
				String simple = (elSimple == null)? "" : elSimple.getTextNormalize();
342
				result = Team.NewInstance();
343
				result.setNomenclaturalTitle(simple);
344
			}
345
		}
346
		return result;
347
	}
348

    
349
	private Person makeAgent(Element elAgentName, Namespace ns, MapWrapper<Person> agentMap, ResultWrapper<Boolean> success){
350
		Person result = null;
351
		if (elAgentName != null){
352
			String authorTitle = elAgentName.getTextNormalize();
353
			result = Person.NewTitledInstance(authorTitle);
354
			Class<? extends Person> clazz = Person.class;
355
			result = makeReferenceType(elAgentName, clazz, agentMap, success);
356
			return result;
357
		}else{
358
			return null;
359
		}
360
	}
361

    
362

    
363

    
364

    
365

    
366

    
367

    
368

    
369

    
370
	protected Integer getIntegerYear(String year){
371
		try {
372
			Integer result = Integer.valueOf(year);
373
			return result;
374
		} catch (NumberFormatException e) {
375
			logger.warn("Year string could not be parsed. Set = 9999 instead");
376
			return 9999;
377
		}
378
	}
379

    
380
	protected String removeVersionOfRef(String ref){
381
		if (ref != null && ref.matches("urn:lsid:ipni.org:.*:.*:.*")){
382
			return ref = ref.substring(0,ref.lastIndexOf(":"));
383
		} else {
384
			return ref;
385
		}
386

    
387
	}
388

    
389

    
390

    
391
	protected void makeTypification(TaxonNameBase name, Element elTypifiacation, ResultWrapper<Boolean> success){
392
		if (elTypifiacation != null){
393
			//logger.warn("makeTypification not yet implemented");
394
			//success.setValue(false);
395
		}
396
	}
397

    
398

    
399
	protected void makePublicationStatus(TaxonNameBase name, Element elPublicationStatus, ResultWrapper<Boolean> success){
400
		//Status
401

    
402
		if (elPublicationStatus != null){
403
			String pubStat = elPublicationStatus.getAttributeValue("Note");
404

    
405
		}
406
	}
407

    
408
	protected void makeProviderLink(TaxonNameBase name, Element elProviderLink, ResultWrapper<Boolean> success){
409
		if (elProviderLink != null){
410
			//logger.warn("makeProviderLink not yet implemented");
411
			//success.setValue(false);
412
		}
413
	}
414

    
415

    
416
	protected void makeProviderSpecificData(TaxonNameBase name, Element elProviderSpecificData, ResultWrapper<Boolean> success, TcsXmlImportState state){
417
		if (elProviderSpecificData != null){
418

    
419
			Namespace ns = elProviderSpecificData.getNamespace();
420

    
421
			String childName = "ipniData";
422
			boolean obligatory = true;
423
			List<Element> elIpniData = elProviderSpecificData.getChildren();
424
			Element el =  elIpniData.get(0);
425

    
426

    
427
			childName = "citationType";
428
			ns = el.getNamespace();
429

    
430
			Element elCitationType = XmlHelp.getSingleChildElement(success, el, childName, ns, obligatory);
431

    
432
			childName = "referenceRemarks";
433
			Element elReferenceRemarks = XmlHelp.getSingleChildElement(success, el, childName, ns, obligatory);
434

    
435
			childName = "suppressed";
436
			Element elSuppressed = XmlHelp.getSingleChildElement(success, el, childName, ns, obligatory);
437

    
438
			childName = "score";
439
			Element elScore = XmlHelp.getSingleChildElement(success, el, childName, ns, obligatory);
440

    
441
			childName = "nomenclaturalSynonym";
442
			Element elNomenclaturalSynonym = XmlHelp.getSingleChildElement(success, el, childName, ns, obligatory);
443
			 ns = elProviderSpecificData.getNamespace();
444
			childName = "RelatedName";
445
			Element elRelatedName = XmlHelp.getSingleChildElement(success, elNomenclaturalSynonym, childName, ns, obligatory);
446

    
447
			//create homotypic synonym
448
			if (elRelatedName != null){
449
    			String id =elRelatedName.getAttributeValue("ref");
450
    			System.out.println(removeVersionOfRef(id));
451
    			if (name.getTaxa().iterator().hasNext()){
452
    			    Taxon taxon = (Taxon) name.getTaxa().iterator().next();
453
    			    //if taxon already exist
454
    			    taxon.addHomotypicSynonym((Synonym)state.getStore(TAXON_STORE).get(removeVersionOfRef(id)), null, null);
455
    			    //otherwise add to a map for homotypic synonyms
456
    			}
457
			}
458

    
459
		}
460
	}
461

    
462

    
463
	/* (non-Javadoc)
464
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
465
	 */
466
	@Override
467
    protected boolean isIgnore(TcsXmlImportState state){
468
		return ! state.getConfig().isDoTaxonNames();
469
	}
470

    
471

    
472
	protected static final Reference<?> unknownSec(){
473
		Reference<?> result = ReferenceFactory.newGeneric();
474
		result.setTitleCache("UNKNOWN", true);
475
		return result;
476
	}
477

    
478

    
479

    
480
}
(2-2/11)