merge-update from trunk
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / tcsrdf / TcsRdfTaxonNameImport.java
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.tcsrdf;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.apache.log4j.Logger;
18 import org.jdom.Attribute;
19 import org.jdom.Element;
20 import org.jdom.Namespace;
21 import org.springframework.stereotype.Component;
22
23 import com.hp.hpl.jena.rdf.model.Model;
24 import com.hp.hpl.jena.rdf.model.ModelFactory;
25 import com.hp.hpl.jena.rdf.model.RDFNode;
26 import com.hp.hpl.jena.rdf.model.ResIterator;
27 import com.hp.hpl.jena.rdf.model.Resource;
28 import com.hp.hpl.jena.rdf.model.Selector;
29 import com.hp.hpl.jena.rdf.model.SimpleSelector;
30 import com.hp.hpl.jena.rdf.model.Statement;
31 import com.hp.hpl.jena.rdf.model.StmtIterator;
32 import com.hp.hpl.jena.vocabulary.*;
33 import com.hp.hpl.jena.rdf.model.Property;
34
35 import org.apache.commons.io.IOUtils;
36 import org.apache.jena.iri.IRIFactory;
37
38 import eu.etaxonomy.cdm.common.XmlHelp;
39 import eu.etaxonomy.cdm.io.common.ICdmIO;
40 import eu.etaxonomy.cdm.io.common.ImportHelper;
41 import eu.etaxonomy.cdm.io.common.MapWrapper;
42 import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
43 import eu.etaxonomy.cdm.model.agent.Team;
44 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
45 import eu.etaxonomy.cdm.model.common.Marker;
46 import eu.etaxonomy.cdm.model.common.MarkerType;
47 import eu.etaxonomy.cdm.model.common.TimePeriod;
48 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
49 import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
50 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
51 import eu.etaxonomy.cdm.model.name.NonViralName;
52 import eu.etaxonomy.cdm.model.name.Rank;
53 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
54 import eu.etaxonomy.cdm.model.name.ViralName;
55 import eu.etaxonomy.cdm.model.reference.IGeneric;
56 import eu.etaxonomy.cdm.model.reference.Reference;
57 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
58 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
59
60 /**
61 * @author a.mueller
62 * @created 29.05.2008
63 * @version 1.0
64 */
65 @Component
66 public class TcsRdfTaxonNameImport extends TcsRdfImportBase implements ICdmIO<TcsRdfImportState> {
67 private static final Logger logger = Logger.getLogger(TcsRdfTaxonNameImport.class);
68
69 private static int modCount = 5000;
70
71 public TcsRdfTaxonNameImport(){
72 super();
73 }
74
75 @Override
76 public boolean doCheck(TcsRdfImportState config){
77 boolean result = true;
78 logger.warn("BasionymRelations not yet implemented");
79 logger.warn("Checking for TaxonNames not yet implemented");
80 //result &= checkArticlesWithoutJournal(tcsConfig);
81 //result &= checkPartOfJournal(tcsConfig);
82
83 return result;
84 }
85
86 protected static CdmSingleAttributeRDFMapperBase[] standardMappers = new CdmSingleAttributeRDFMapperBase[]{
87 new CdmTextElementMapper("genusPart", "genusOrUninomial")
88 , new CdmTextElementMapper("uninomial", "genusOrUninomial") //TODO make it a more specific Mapper for both attributes
89 , new CdmTextElementMapper("specificEpithet", "specificEpithet")
90 , new CdmTextElementMapper("infraspecificEpithet", "infraSpecificEpithet")
91 , new CdmTextElementMapper("infragenericEpithet", "infraGenericEpithet")
92 , new CdmTextElementMapper("microReference", nsTcom, "nomenclaturalMicroReference")
93
94 };
95
96 protected static CdmSingleAttributeRDFMapperBase[] operationalMappers = new CdmSingleAttributeRDFMapperBase[]{
97 new CdmUnclearMapper("basionymAuthorship")
98 , new CdmUnclearMapper("combinationAuthorship")
99 , new CdmUnclearMapper("hasAnnotation")
100 , new CdmUnclearMapper("rank")
101 , new CdmUnclearMapper("nomenclaturalCode")
102 , new CdmUnclearMapper("publishedIn", nsTcom)
103 , new CdmUnclearMapper("year")
104 };
105
106 protected static CdmSingleAttributeRDFMapperBase[] unclearMappers = new CdmSingleAttributeRDFMapperBase[]{
107 new CdmUnclearMapper("authorship")
108 , new CdmUnclearMapper("rankString")
109 , new CdmUnclearMapper("nameComplete")
110 , new CdmUnclearMapper("hasBasionym")
111 , new CdmUnclearMapper("dateOfEntry", nsTpalm)
112 };
113
114 @Override
115 protected void doInvoke(TcsRdfImportState state){
116
117 MapWrapper<TaxonNameBase> taxonNameMap = (MapWrapper<TaxonNameBase>)state.getStore(ICdmIO.TAXONNAME_STORE);
118 MapWrapper<Reference> referenceMap = (MapWrapper<Reference>)state.getStore(ICdmIO.REFERENCE_STORE);
119 MapWrapper<TeamOrPersonBase> authorMap = (MapWrapper<TeamOrPersonBase>)state.getStore(ICdmIO.TEAM_STORE);
120
121 String tcsElementName;
122 Namespace tcsNamespace;
123 String value;
124
125 logger.info("start makeTaxonNames ...");
126 TcsRdfImportConfigurator config = state.getConfig();
127 Model root = config.getSourceRoot();
128
129 String rdfNamespace = config.getRdfNamespaceURIString();
130 String taxonNameNamespace = config.getTnNamespaceURIString();
131
132 String idNamespace = "TaxonName";
133
134 Resource elTaxonName = root.getResource(taxonNameNamespace);
135
136 int i = 0;
137
138 TaxonNameBase name;
139 Property property = root.getProperty(taxonNameNamespace+"authorship");
140
141 ResIterator iterator = root.listSubjectsWithProperty(property, (RDFNode) null);
142 String id ;
143 while (iterator.hasNext()){
144
145 Resource resource = iterator.next();
146
147 name = handleNameResource(resource, config);
148 id = resource.getNameSpace();
149 taxonNameMap.put(id, name);
150 }
151
152 logger.info(i + " names handled");
153 getNameService().save(taxonNameMap.objects());
154 // makeNameSpecificData(nameMap);
155 logger.info("end makeTaxonNames ...");
156 return;
157
158 }
159 /* (non-Javadoc)
160 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
161 */
162 @Override
163 protected boolean isIgnore(TcsRdfImportState state){
164 return ! state.getConfig().isDoTaxonNames();
165 }
166
167 protected TaxonNameBase handleNameModel(Model model, TcsRdfImportConfigurator config, MapWrapper<TaxonNameBase> taxonNameMap, String uri){
168 Resource nameAbout = model.getResource(uri);
169 TaxonNameBase result = handleNameResource(nameAbout, config);
170 taxonNameMap.put(uri, result);
171 return result;
172
173 }
174
175 private TaxonNameBase handleNameResource(Resource nameAbout, TcsRdfImportConfigurator config){
176 String idNamespace = "TaxonName";
177
178 StmtIterator stmts = nameAbout.listProperties();
179
180 Property prop = nameAbout.getModel().getProperty(config.getTnNamespaceURIString()+"nomenclaturalCode");
181 Statement stateNomenclaturalCode = nameAbout.getProperty(prop);
182 String strNomenclaturalCode = stateNomenclaturalCode.getObject().toString();
183 //Rank
184 prop = nameAbout.getModel().getProperty(config.getTnNamespaceURIString()+"rankString");
185 String strRank = nameAbout.getProperty(prop).getString();
186
187
188
189 try {
190
191 Rank rank = TcsRdfTransformer.rankString2Rank(strRank);
192 NomenclaturalCode nomCode;
193 if (strNomenclaturalCode != null){
194 nomCode = TcsRdfTransformer.nomCodeString2NomCode(strNomenclaturalCode);
195 }else{
196 nomCode = NomenclaturalCode.ICNAFP;
197 }
198
199 TaxonNameBase<?,?> nameBase = nomCode.getNewTaxonNameInstance(rank);
200
201 Set<String> omitAttributes = null;
202 //makeStandardMapper(nameAbout, nameBase, omitAttributes, standardMappers);
203
204 prop = nameAbout.getModel().getProperty(config.getTnNamespaceURIString()+"nameComplete");
205 String strNameComplete = nameAbout.getProperty(prop).getString();
206 nameBase.setTitleCache(strNameComplete, true);
207
208 prop = nameAbout.getModel().getProperty(config.getCommonNamespaceURIString()+"publishedIn");
209 String strPublishedIn = nameAbout.getProperty(prop).getString();
210 if (strPublishedIn != null && strPublishedIn != ""){
211 IGeneric nomRef = ReferenceFactory.newGeneric(); //TODO
212 nomRef.setTitleCache(strPublishedIn, true);
213 nameBase.setNomenclaturalReference(nomRef);
214 try{
215 prop = nameAbout.getModel().getProperty(config.getTnNamespaceURIString()+"year");
216 String strYear = nameAbout.getProperty(prop).getString();
217 Integer year = null;
218 if (strYear != null){
219 try {
220 year = Integer.valueOf(strYear);
221 TimePeriod timeP = TimePeriod.NewInstance(year);
222 nomRef.setDatePublished(timeP);
223 } catch (RuntimeException e) {
224 logger.warn("year could not be parsed");
225 }
226 }
227 }catch(NullPointerException e){
228 }
229 if (config.isPublishReferences()){
230 ((Reference<?>)nomRef).addMarker(Marker.NewInstance(MarkerType.PUBLISH(), false));
231 }
232 }
233
234 if (nameBase instanceof NonViralName){
235 NonViralName<?> nonViralName = (NonViralName<?>)nameBase;
236 prop = nameAbout.getModel().getProperty(config.getTnNamespaceURIString()+"genusPart");
237 String strGenusPart;
238 try{
239 strGenusPart = nameAbout.getProperty(prop).getString();
240 }catch(NullPointerException e){
241 prop = nameAbout.getModel().getProperty(config.getTnNamespaceURIString()+"uninomial");
242 strGenusPart = nameAbout.getProperty(prop).getString();
243 }
244
245 nonViralName.setGenusOrUninomial(strGenusPart);
246
247 prop = nameAbout.getModel().getProperty(config.getTnNamespaceURIString()+"infragenericEpithet");
248 try{
249 String strInfragenericEpithet = nameAbout.getProperty(prop).getString();
250 nonViralName.setInfraGenericEpithet(strInfragenericEpithet);
251 }catch(NullPointerException e){
252
253 }
254 try {
255 prop = nameAbout.getModel().getProperty(config.getTnNamespaceURIString()+"specificEpithet");
256 String strSpecificEpithet = nameAbout.getProperty(prop).getString();
257 nonViralName.setSpecificEpithet(strSpecificEpithet);
258 }catch(NullPointerException e){
259
260 }
261 try{
262 prop = nameAbout.getModel().getProperty(config.getTnNamespaceURIString()+"infraspecificEpithet");
263 String strInfraspecificEpithet = nameAbout.getProperty(prop).getString();
264 nonViralName.setInfraSpecificEpithet(strInfraspecificEpithet);
265 }catch(NullPointerException e){
266
267 }
268 //AuthorTeams
269 //TODO
270 prop = nameAbout.getModel().getProperty(config.getTnNamespaceURIString()+"authorship");
271 Statement stateAuthorship = nameAbout.getProperty(prop);
272 prop = nameAbout.getModel().getProperty(config.getTeamNamespaceURIString()+"name");
273 Team authorTeam = new Team();
274 authorTeam.setTitleCache(stateAuthorship.getObject().toString(), true);
275 prop = nameAbout.getModel().getProperty(config.getTeamNamespaceURIString()+"hasMember");
276 //String strTeamMember = nameAbout.getProperty(prop).getString();
277 nonViralName.setCombinationAuthorTeam(authorTeam);
278
279 //Annotations:
280 /*
281 * <tn:hasAnnotation>
282 <tn:NomenclaturalNote>
283 <tn:noteType rdf:resource="http://rs.tdwg.org/ontology/voc/TaxonName#replacementNameFor"/>
284 <tn:objectTaxonName rdf:resource="urn:lsid:ipni.org:names:151538-1"/>
285 </tn:NomenclaturalNote>
286 </tn:hasAnnotation>
287 */
288 /*
289 String strInfraspecificEpithet = nameAbout.getProperty(prop).getString();
290 tcsElementName = "basionymAuthorship";
291 String basionymAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
292 if (basionymAuthorValue != null){
293 TeamOrPersonBase<?> basionymAuthor = Team.NewInstance();
294 basionymAuthor.setNomenclaturalTitle(basionymAuthorValue);
295 nonViralName.setBasionymAuthorTeam(basionymAuthor);
296 }
297
298 //TODO
299 tcsElementName = "combinationAuthorship";
300 String combinationAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
301 if (combinationAuthorValue != null){
302 TeamOrPersonBase<?> combinationAuthor = Team.NewInstance();
303 combinationAuthor.setNomenclaturalTitle(combinationAuthorValue);
304 nonViralName.setCombinationAuthorTeam(combinationAuthor);
305 }
306
307 //set the authorshipCache
308 tcsElementName = "authorship";
309 String authorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
310 String cache = nonViralName.getAuthorshipCache();
311 if ( authorValue != null){
312 //compare existing authorship cache with new one and check if it is necessary to
313 //make cache protected //TODO refinement
314 if (cache == null){
315 nonViralName.setAuthorshipCache(authorValue);
316 }else{
317 cache = basionymAuthorValue == null ? cache : cache.replace(basionymAuthorValue, "");
318 cache = combinationAuthorValue == null ? cache : cache.replace(combinationAuthorValue, "");
319 cache = cache.replace("\\(|\\)", "");
320 cache = cache.trim();
321 if (! cache.equals("")){
322 nonViralName.setAuthorshipCache(authorValue);
323 }
324 }
325 }*/
326 }
327 //ImportHelper.setOriginalSource(nameBase, config.getSourceReference(), nameAbout, idNamespace);
328
329 //checkAdditionalContents(elTaxonName, standardMappers, operationalMappers, unclearMappers);
330
331 return nameBase;
332
333 }catch(Exception e){
334 e.printStackTrace();
335 return null;
336 }
337 /*
338 //name
339 String strNameComplete = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "nameComplete", rdfNamespace);
340 nameBase.setTitleCache(strNameComplete, true);
341
342 //Reference
343 //TODO
344 String tcsElementName = "publishedIn";
345 Namespace tcsNamespace = config.getCommonNamespaceURIString();
346 String value = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, tcsNamespace);
347 if (value != null && value != ""){
348 IGeneric nomRef = ReferenceFactory.newGeneric(); //TODO
349 nomRef.setTitleCache(value, true);
350 nameBase.setNomenclaturalReference(nomRef);
351
352 //TODO
353 tcsElementName = "year";
354 tcsNamespace = taxonNameNamespace;
355 Integer year = null;
356 value = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, tcsNamespace);
357 if (value != null){
358 try {
359 year = Integer.valueOf(value);
360 TimePeriod timeP = TimePeriod.NewInstance(year);
361 nomRef.setDatePublished(timeP);
362 } catch (RuntimeException e) {
363 logger.warn("year could not be parsed");
364 }
365 }
366 if (config.isPublishReferences()){
367 ((Reference<?>)nomRef).addMarker(Marker.NewInstance(MarkerType.PUBLISH(), false));
368 }
369 }
370
371 //Status
372 tcsNamespace = taxonNameNamespace;
373 Element elAnnotation = elTaxonName.getChild("hasAnnotation", tcsNamespace);
374 if (elAnnotation != null){
375 Element elNomenclaturalNote = elAnnotation.getChild("NomenclaturalNote", tcsNamespace);
376 if (elNomenclaturalNote != null){
377 String statusValue = (String)ImportHelper.getXmlInputValue(elNomenclaturalNote, "note", tcsNamespace);
378 String type = XmlHelp.getChildAttributeValue(elNomenclaturalNote, "type", tcsNamespace, "resource", rdfNamespace);
379 String tdwgType = "http://rs.tdwg.org/ontology/voc/TaxonName#PublicationStatus";
380 if (tdwgType.equalsIgnoreCase(type)){
381 try {
382 NomenclaturalStatusType statusType = TcsRdfTransformer.nomStatusString2NomStatus(statusValue);
383 //NomenclaturalStatusType statusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusValue);
384 if (statusType != null){
385 nameBase.addStatus(NomenclaturalStatus.NewInstance(statusType));
386 }
387 } catch (UnknownCdmTypeException e) {
388 if (! statusValue.equals("valid")){
389 logger.warn("Unknown NomenclaturalStatusType: " + statusValue);
390 }
391 }
392 }
393 }
394 }
395
396 if (nameBase instanceof NonViralName){
397 NonViralName<?> nonViralName = (NonViralName<?>)nameBase;
398 String strGenusPart = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "genusPart", rdfNamespace);
399
400 //for names of rank genus the uninomial property should be used
401 if (strGenusPart == null){
402 strGenusPart = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "uninomial", rdfNamespace);
403 }
404 nonViralName.setGenusOrUninomial(strGenusPart);
405
406 String strInfragenericEpithet = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "infragenericEpithet", rdfNamespace);
407 nonViralName.setGenusOrUninomial(strInfragenericEpithet);
408
409
410
411 String strSpecificEpithet = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "specificEpithet", rdfNamespace);
412 nonViralName.setSpecificEpithet(strSpecificEpithet);
413
414 String strInfraspecificEpithet = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "infraspecificEpithet", rdfNamespace);
415 nonViralName.setInfraSpecificEpithet(strInfraspecificEpithet);
416 //AuthorTeams
417 //TODO
418 tcsElementName = "basionymAuthorship";
419 String basionymAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
420 if (basionymAuthorValue != null){
421 TeamOrPersonBase<?> basionymAuthor = Team.NewInstance();
422 basionymAuthor.setNomenclaturalTitle(basionymAuthorValue);
423 nonViralName.setBasionymAuthorTeam(basionymAuthor);
424 }
425
426 //TODO
427 tcsElementName = "combinationAuthorship";
428 String combinationAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
429 if (combinationAuthorValue != null){
430 TeamOrPersonBase<?> combinationAuthor = Team.NewInstance();
431 combinationAuthor.setNomenclaturalTitle(combinationAuthorValue);
432 nonViralName.setCombinationAuthorTeam(combinationAuthor);
433 }
434
435 //set the authorshipCache
436 tcsElementName = "authorship";
437 String authorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
438 String cache = nonViralName.getAuthorshipCache();
439 if ( authorValue != null){
440 //compare existing authorship cache with new one and check if it is necessary to
441 //make cache protected //TODO refinement
442 if (cache == null){
443 nonViralName.setAuthorshipCache(authorValue);
444 }else{
445 cache = basionymAuthorValue == null ? cache : cache.replace(basionymAuthorValue, "");
446 cache = combinationAuthorValue == null ? cache : cache.replace(combinationAuthorValue, "");
447 cache = cache.replace("\\(|\\)", "");
448 cache = cache.trim();
449 if (! cache.equals("")){
450 nonViralName.setAuthorshipCache(authorValue);
451 }
452 }
453 }
454 }
455 ImportHelper.setOriginalSource(nameBase, config.getSourceReference(), nameAbout, idNamespace);
456
457 checkAdditionalContents(elTaxonName, standardMappers, operationalMappers, unclearMappers);
458
459 //nameId
460 //TODO
461 //ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
462 //taxonNameMap.put(nameAbout, nameBase);
463 return nameBase;
464 }catch(UnknownCdmTypeException e){
465 e.printStackTrace();
466 }
467 return null;*/
468 }
469
470 protected TaxonNameBase handleNameElement(Element elTaxonName, Namespace rdfNamespace, Namespace taxonNameNamespace, TcsRdfImportConfigurator config, MapWrapper<TaxonNameBase> taxonNameMap){
471 String idNamespace = "TaxonName";
472 Attribute about = elTaxonName.getAttribute("about", rdfNamespace);
473
474 //create TaxonName element
475
476
477 String nameAbout = elTaxonName.getAttributeValue("about", rdfNamespace);
478 if (nameAbout == null){
479 nameAbout = XmlHelp.getChildAttributeValue(elTaxonName, "TaxonName", taxonNameNamespace, "about", rdfNamespace);
480 }
481
482
483 String strRank = XmlHelp.getChildAttributeValue(elTaxonName, "rankString", taxonNameNamespace, "rankString", rdfNamespace);
484 if (strRank == null){
485 strRank = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "rankString", rdfNamespace);
486
487 }
488
489 if (strRank == null){
490 strRank = XmlHelp.getChildAttributeValue(elTaxonName, "rank", taxonNameNamespace, "resource", rdfNamespace);
491
492 }
493
494 String strNomenclaturalCode = XmlHelp.getChildContentAttributeValue(elTaxonName, "TaxonName", taxonNameNamespace, "nomenclaturalCode", rdfNamespace);
495 if (strNomenclaturalCode == null){
496 strNomenclaturalCode = XmlHelp.getChildAttributeValue(elTaxonName, "nomenclaturalCode", taxonNameNamespace, "resource", rdfNamespace);
497
498 }
499 try {
500
501 Rank rank = TcsRdfTransformer.rankString2Rank(strRank);
502 NomenclaturalCode nomCode;
503 if (strNomenclaturalCode != null){
504 nomCode = TcsRdfTransformer.nomCodeString2NomCode(strNomenclaturalCode);
505 }else{
506 nomCode = NomenclaturalCode.ICNAFP;
507 }
508
509 TaxonNameBase<?,?> nameBase = nomCode.getNewTaxonNameInstance(rank);
510
511 Set<String> omitAttributes = null;
512 //makeStandardMapper(elTaxonName, nameBase, omitAttributes, standardMappers);
513
514 //name
515 String strNameComplete = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "nameComplete", rdfNamespace);
516 nameBase.setTitleCache(strNameComplete, true);
517
518 //Reference
519 //TODO
520 String tcsElementName = "publishedIn";
521 String tcsNamespace = config.getCommonNamespaceURIString();
522 /*String value = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, tcsNamespace);
523 if (value != null && value != ""){
524 IGeneric nomRef = ReferenceFactory.newGeneric(); //TODO
525 nomRef.setTitleCache(value, true);
526 nameBase.setNomenclaturalReference(nomRef);
527
528 //TODO
529 tcsElementName = "year";
530 tcsNamespace = taxonNameNamespace;
531 Integer year = null;
532 value = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, tcsNamespace);
533 if (value != null){
534 try {
535 year = Integer.valueOf(value);
536 TimePeriod timeP = TimePeriod.NewInstance(year);
537 nomRef.setDatePublished(timeP);
538 } catch (RuntimeException e) {
539 logger.warn("year could not be parsed");
540 }
541 }
542 if (config.isPublishReferences()){
543 ((Reference<?>)nomRef).addMarker(Marker.NewInstance(MarkerType.PUBLISH(), false));
544 }
545 }
546
547 //Status
548 tcsNamespace = taxonNameNamespace;
549 Element elAnnotation = elTaxonName.getChild("hasAnnotation", tcsNamespace);
550 if (elAnnotation != null){
551 Element elNomenclaturalNote = elAnnotation.getChild("NomenclaturalNote", tcsNamespace);
552 if (elNomenclaturalNote != null){
553 String statusValue = (String)ImportHelper.getXmlInputValue(elNomenclaturalNote, "note", tcsNamespace);
554 String type = XmlHelp.getChildAttributeValue(elNomenclaturalNote, "type", tcsNamespace, "resource", rdfNamespace);
555 String tdwgType = "http://rs.tdwg.org/ontology/voc/TaxonName#PublicationStatus";
556 if (tdwgType.equalsIgnoreCase(type)){
557 try {
558 NomenclaturalStatusType statusType = TcsRdfTransformer.nomStatusString2NomStatus(statusValue);
559 //NomenclaturalStatusType statusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusValue);
560 if (statusType != null){
561 nameBase.addStatus(NomenclaturalStatus.NewInstance(statusType));
562 }
563 } catch (UnknownCdmTypeException e) {
564 if (! statusValue.equals("valid")){
565 logger.warn("Unknown NomenclaturalStatusType: " + statusValue);
566 }
567 }
568 }
569 }
570 }
571
572 if (nameBase instanceof NonViralName){
573 NonViralName<?> nonViralName = (NonViralName<?>)nameBase;
574 String strGenusPart = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "genusPart", rdfNamespace);
575
576 //for names of rank genus the uninomial property should be used
577 if (strGenusPart == null){
578 strGenusPart = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "uninomial", rdfNamespace);
579 }
580 nonViralName.setGenusOrUninomial(strGenusPart);
581
582 String strInfragenericEpithet = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "infragenericEpithet", rdfNamespace);
583 nonViralName.setGenusOrUninomial(strInfragenericEpithet);
584
585
586
587 String strSpecificEpithet = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "specificEpithet", rdfNamespace);
588 nonViralName.setSpecificEpithet(strSpecificEpithet);
589
590 String strInfraspecificEpithet = XmlHelp.getChildContent(elTaxonName, "TaxonName", taxonNameNamespace, "infraspecificEpithet", rdfNamespace);
591 nonViralName.setInfraSpecificEpithet(strInfraspecificEpithet);
592 //AuthorTeams
593 //TODO
594 tcsElementName = "basionymAuthorship";
595 String basionymAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
596 if (basionymAuthorValue != null){
597 TeamOrPersonBase<?> basionymAuthor = Team.NewInstance();
598 basionymAuthor.setNomenclaturalTitle(basionymAuthorValue);
599 nonViralName.setBasionymAuthorTeam(basionymAuthor);
600 }
601
602 //TODO
603 tcsElementName = "combinationAuthorship";
604 String combinationAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
605 if (combinationAuthorValue != null){
606 TeamOrPersonBase<?> combinationAuthor = Team.NewInstance();
607 combinationAuthor.setNomenclaturalTitle(combinationAuthorValue);
608 nonViralName.setCombinationAuthorTeam(combinationAuthor);
609 }
610
611 //set the authorshipCache
612 tcsElementName = "authorship";
613 String authorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
614 String cache = nonViralName.getAuthorshipCache();
615 if ( authorValue != null){
616 //compare existing authorship cache with new one and check if it is necessary to
617 //make cache protected //TODO refinement
618 if (cache == null){
619 nonViralName.setAuthorshipCache(authorValue);
620 }else{
621 cache = basionymAuthorValue == null ? cache : cache.replace(basionymAuthorValue, "");
622 cache = combinationAuthorValue == null ? cache : cache.replace(combinationAuthorValue, "");
623 cache = cache.replace("\\(|\\)", "");
624 cache = cache.trim();
625 if (! cache.equals("")){
626 nonViralName.setAuthorshipCache(authorValue);
627 }
628 }
629 }
630 }
631 ImportHelper.setOriginalSource(nameBase, config.getSourceReference(), nameAbout, idNamespace);
632
633 checkAdditionalContents(elTaxonName, standardMappers, operationalMappers, unclearMappers);
634
635 //nameId
636 //TODO
637 //ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
638 //taxonNameMap.put(nameAbout, nameBase);
639 return nameBase;
640 */}catch(UnknownCdmTypeException e){
641 e.printStackTrace();
642 }
643 return null;
644 }
645
646 public TaxonNameBase handleRdfElementFromStream(InputStream is, TcsRdfImportConfigurator config, MapWrapper<TaxonNameBase> taxonNameMap, String uri){
647 Model model = ModelFactory.createDefaultModel();
648 try{
649 model.read(is, null);
650
651 config.makeNamespaces(model);
652
653 String rdfNamespace = config.getRdfNamespaceURIString();
654 String taxonNameNamespace = config.getTnNamespaceURIString();
655 return handleNameModel(model, config, taxonNameMap, uri);
656
657
658 }catch(com.hp.hpl.jena.shared.JenaException e){
659 logger.debug("The file was no valid rdf file");
660 }
661
662
663
664 return null;
665 }
666
667 }