2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.taxonx
;
12 import java
.util
.HashMap
;
13 import java
.util
.Iterator
;
14 import java
.util
.List
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.jdom
.Attribute
;
19 import org
.jdom
.Content
;
20 import org
.jdom
.Element
;
21 import org
.jdom
.Namespace
;
23 import org
.springframework
.stereotype
.Component
;
25 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
27 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
28 import eu
.etaxonomy
.cdm
.io
.common
.CdmImportBase
;
29 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
30 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
31 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSourceType
;
32 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
33 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
34 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
35 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
36 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
37 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
39 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
47 public class TaxonXDescriptionImport
48 extends CdmImportBase
<TaxonXImportConfigurator
, TaxonXImportState
> {
50 private static final long serialVersionUID
= 1957356490206741364L;
51 private static final Logger logger
= Logger
.getLogger(TaxonXDescriptionImport
.class);
53 public TaxonXDescriptionImport(){
58 public boolean doCheck(TaxonXImportState state
){
59 boolean result
= true;
60 logger
.warn("Checking for Facts not yet implemented");
61 //result &= checkArticlesWithoutJournal(bmiConfig);
66 public Map
<Integer
, Feature
> fillFactCategories(IImportConfigurator config
, CdmApplicationController cdmApp
){
67 logger
.warn("not yet implemented");
68 Map
<Integer
, Feature
> featureMap
= new HashMap
<Integer
, Feature
>();
69 // IDescriptionService descriptionService = cdmApp.getDescriptionService();
70 // ITermService termService = cdmApp.getTermService();
72 // Object source = config.getSource();
76 private String
getDescriptionTitle(TaxonXImportState state
){
77 String result
= "Untitled";
78 Reference ref
= state
.getModsReference();
80 result
= ref
.getTitle();
81 if ( CdmUtils
.isEmpty(result
)){
82 result
= ref
.getTitleCache();
89 public void doInvoke(TaxonXImportState state
){
90 logger
.debug("not yet fully implemented");
92 TaxonXImportConfigurator txConfig
= state
.getConfig();
93 Element root
= txConfig
.getSourceRoot();
94 Namespace nsTaxonx
= root
.getNamespace();
96 //Object source = config.getSource();
98 logger
.info("start make Descriptions ...");
102 Taxon taxon
= getTaxon(txConfig
);
104 logger
.warn("Taxon could not be found");
105 state
.setUnsuccessfull();
108 Reference modsReference
= state
.getModsReference();
109 if (modsReference
== null){
110 modsReference
= state
.getConfig().getSourceReference();
113 //unlazyDescription(txConfig, taxon);
114 TaxonDescription description
= TaxonDescription
.NewInstance();
115 description
.setTitleCache(getDescriptionTitle(state
), true);
116 if (modsReference
!= null){
117 description
.addSource(OriginalSourceType
.PrimaryTaxonomicSource
, null, null, modsReference
, null);
120 Element elTaxonBody
= root
.getChild("taxonxBody", nsTaxonx
);
121 Element elTreatment
= elTaxonBody
.getChild("treatment", nsTaxonx
);
122 List
<Element
> elDivs
= elTreatment
.getChildren("div", nsTaxonx
);
123 for (Element div
: elDivs
){
124 Attribute attrType
= div
.getAttribute("type", nsTaxonx
);
125 String strType
= attrType
.getValue();
126 Feature feature
= null;
128 feature
= TaxonXTransformer
.descriptionType2feature(strType
);
129 } catch (UnknownCdmTypeException e
) {
130 feature
= handleFeatureException(strType
, e
, txConfig
);
132 String text
= getText(div
);
133 if (!"".equals(CdmUtils
.Nz(text
).trim())){
134 // hibernate throws an exception when a string is longer than approx. 65500 chars.
135 // for now we truncate any description text to 65500 characters.
136 if(text
.length() > 65500){
137 text
= text
.substring(0, 65500) + "... [text truncated]";
138 logger
.warn("Truncation of text: description for taxon " + taxon
.getTitleCache() + " was longer than 65500 characters.");
141 DescriptionElementBase descriptionElement
= TextData
.NewInstance(text
, Language
.ENGLISH(), null);
142 descriptionElement
.setFeature(feature
);
143 description
.addElement(descriptionElement
);
146 if (modsReference
!= null){
147 descriptionElement
.addSource(OriginalSourceType
.PrimaryTaxonomicSource
, null, null, modsReference
, null, null, null);
152 if (description
.size() >0){
153 taxon
.addDescription(description
);
154 getTaxonService().save(taxon
);
159 private Feature
handleFeatureException(String strType
, UnknownCdmTypeException e
, TaxonXImportConfigurator txConfig
){
160 Feature feature
= null;
161 TermVocabulary
<Feature
> featureVoc
= Feature
.BIOLOGY_ECOLOGY().getVocabulary();
162 for (Feature oneFeature
: featureVoc
.getTerms()){
163 if (strType
.equals(oneFeature
.getLabel()) || strType
.equals(oneFeature
.getRepresentation(Language
.DEFAULT() ).getText() )){
164 feature
= oneFeature
;
168 if (feature
== null){
169 feature
= Feature
.NewInstance(strType
, strType
, null);
170 featureVoc
.addTerm(feature
);
171 getTermService().save(feature
);
172 logger
.warn(e
.getMessage() + ". Feature was added to the feature vocabulary. " + getBracketSourceName(txConfig
));
178 private String
getText(Element div
){
180 Iterator
<Content
> it
=div
.getDescendants();
181 while (it
.hasNext()){
182 Content next
= it
.next();
183 if (next
instanceof Text
){
184 result
+= ((Text
)next
).getText();
190 private Taxon
getTaxon(TaxonXImportConfigurator config
){
192 // result = Taxon.NewInstance(TaxonNameFactory.NewBotanicalInstance(null), null);
193 ICommonService commonService
= getCommonService();
195 String originalSourceId
= config
.getOriginalSourceId();
196 String namespace
= config
.getOriginalSourceTaxonNamespace();
197 result
= (Taxon
)commonService
.getSourcedObjectByIdInSource(Taxon
.class, originalSourceId
, namespace
);
199 logger
.warn("Taxon (id: " + originalSourceId
+ ", namespace: " + namespace
+ ") could not be found");
204 private void unlazyDescription(TaxonXImportConfigurator config
, Taxon taxon
){
205 // logger.warn("Preliminary commented"); //used single Transaction for all import instead !
206 // TransactionStatus txStatus = config.getCdmAppController().startTransaction();
207 // ITaxonService taxonService = config.getCdmAppController().getTaxonService();
208 // taxonService.saveTaxon(taxon);
209 // taxon.getDescriptions().size();
210 // config.getCdmAppController().commitTransaction(txStatus);
214 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
217 protected boolean isIgnore(TaxonXImportState state
){
218 return ! state
.getConfig().isDoFacts();
221 private String
getBracketSourceName(TaxonXImportConfigurator config
){
222 return "(" + config
.getSourceNameString() + ")";