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
.CdmIoBase
;
29 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
30 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
31 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
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
.ReferenceBase
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
39 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
48 public class TaxonXDescriptionImport
extends CdmIoBase
<TaxonXImportState
> implements ICdmIO
<TaxonXImportState
> {
49 private static final Logger logger
= Logger
.getLogger(TaxonXDescriptionImport
.class);
51 public TaxonXDescriptionImport(){
55 public boolean doCheck(TaxonXImportState state
){
56 boolean result
= true;
57 logger
.warn("Checking for Facts not yet implemented");
58 //result &= checkArticlesWithoutJournal(bmiConfig);
63 public Map
<Integer
, Feature
> fillFactCategories(IImportConfigurator config
, CdmApplicationController cdmApp
){
64 logger
.warn("not yet implemented");
65 Map
<Integer
, Feature
> featureMap
= new HashMap
<Integer
, Feature
>();
66 // IDescriptionService descriptionService = cdmApp.getDescriptionService();
67 // ITermService termService = cdmApp.getTermService();
69 // Object source = config.getSource();
73 private String
getDescriptionTitle(TaxonXImportState state
){
74 String result
= "Untitled";
75 ReferenceBase
<?
> ref
= state
.getModsReference();
77 result
= ref
.getTitle();
78 if ( CdmUtils
.isEmpty(result
)){
79 result
= ref
.getTitleCache();
85 public boolean doInvoke(TaxonXImportState state
){
86 logger
.debug("not yet fully implemented");
88 TaxonXImportConfigurator txConfig
= state
.getConfig();
89 Element root
= txConfig
.getSourceRoot();
90 Namespace nsTaxonx
= root
.getNamespace();
92 //Object source = config.getSource();
94 logger
.info("start make Descriptions ...");
98 Taxon taxon
= getTaxon(txConfig
);
100 logger
.warn("Taxon could not be found");
103 //unlazyDescription(txConfig, taxon);
104 TaxonDescription description
= TaxonDescription
.NewInstance();
105 description
.setTitleCache(getDescriptionTitle(state
));
107 Element elTaxonBody
= root
.getChild("taxonxBody", nsTaxonx
);
108 Element elTreatment
= elTaxonBody
.getChild("treatment", nsTaxonx
);
109 List
<Element
> elDivs
= elTreatment
.getChildren("div", nsTaxonx
);
110 for (Element div
: elDivs
){
111 Attribute attrType
= div
.getAttribute("type", nsTaxonx
);
112 String strType
= attrType
.getValue();
113 Feature feature
= null;
115 feature
= TaxonXTransformer
.descriptionType2feature(strType
);
116 } catch (UnknownCdmTypeException e
) {
117 feature
= handleFeatureException(strType
, e
, txConfig
);
119 String text
= getText(div
);
120 if (!"".equals(CdmUtils
.Nz(text
).trim())){
121 // FIXME hibernate throws an exception when a string is longer than approx. 4000 chars.
122 // for now we truncate any description text to 4000 characters.
123 if(text
.length() > 65500){
124 text
= text
.substring(0, 65500) + "... [text truncated]";
125 logger
.warn("FIXME - Truncation of text occurred.");
128 DescriptionElementBase descriptionElement
= TextData
.NewInstance(text
, Language
.ENGLISH(), null);
129 descriptionElement
.setFeature(feature
);
130 description
.addElement(descriptionElement
);
133 if (state
.getModsReference() != null){
134 descriptionElement
.addSource(null, null, state
.getModsReference(), null, null, null);
139 if (description
.size() >0){
140 taxon
.addDescription(description
);
141 getTaxonService().save(taxon
);
146 private Feature
handleFeatureException(String strType
, UnknownCdmTypeException e
, TaxonXImportConfigurator txConfig
){
147 Feature feature
= null;
148 TermVocabulary
<Feature
> featureVoc
= Feature
.BIOLOGY_ECOLOGY().getVocabulary();
149 for (Feature oneFeature
: featureVoc
.getTerms()){
150 if (strType
.equals(oneFeature
.getLabel()) || strType
.equals(oneFeature
.getRepresentation(Language
.DEFAULT() ).getText() )){
151 feature
= oneFeature
;
155 if (feature
== null){
156 feature
= Feature
.NewInstance(strType
, strType
, null);
157 featureVoc
.addTerm(feature
);
158 getTermService().save(feature
);
159 logger
.warn(e
.getMessage() + ". Feature was added to the feature vocabulary. " + getBracketSourceName(txConfig
));
165 private String
getText(Element div
){
167 Iterator
<Content
> it
=div
.getDescendants();
168 while (it
.hasNext()){
169 Content next
= (Content
)it
.next();
170 if (next
instanceof Text
){
171 result
+= ((Text
)next
).getText();
177 private Taxon
getTaxon(TaxonXImportConfigurator config
){
179 // result = Taxon.NewInstance(BotanicalName.NewInstance(null), null);
180 ICommonService commonService
= getCommonService();
182 String originalSourceId
= config
.getOriginalSourceId();
183 String namespace
= config
.getOriginalSourceTaxonNamespace();
184 result
= (Taxon
)commonService
.getSourcedObjectByIdInSource(Taxon
.class, originalSourceId
, namespace
);
186 logger
.warn("Taxon (id: " + originalSourceId
+ ", namespace: " + namespace
+ ") could not be found");
191 private void unlazyDescription(TaxonXImportConfigurator config
, Taxon taxon
){
192 // logger.warn("Preliminary commented"); //used single Transaction for all import instead !
193 // TransactionStatus txStatus = config.getCdmAppController().startTransaction();
194 // ITaxonService taxonService = config.getCdmAppController().getTaxonService();
195 // taxonService.saveTaxon(taxon);
196 // taxon.getDescriptions().size();
197 // config.getCdmAppController().commitTransaction(txStatus);
201 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
203 protected boolean isIgnore(TaxonXImportState state
){
204 return ! state
.getConfig().isDoFacts();
207 private String
getBracketSourceName(TaxonXImportConfigurator config
){
208 return "(" + config
.getSourceNameString() + ")";