adapting export to be aware of existing application context
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / taxonx / TaxonXDescriptionImport.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.taxonx;
11
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Map;
16
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;
22 import org.jdom.Text;
23 import org.springframework.stereotype.Component;
24
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;
40
41
42 /**
43 * @author a.mueller
44 * @created 29.07.2008
45 * @version 1.0
46 */
47 @Component
48 public class TaxonXDescriptionImport extends CdmIoBase<TaxonXImportState> implements ICdmIO<TaxonXImportState> {
49 private static final Logger logger = Logger.getLogger(TaxonXDescriptionImport.class);
50
51 public TaxonXDescriptionImport(){
52 super();
53 }
54
55 public boolean doCheck(TaxonXImportState state){
56 boolean result = true;
57 logger.warn("Checking for Facts not yet implemented");
58 //result &= checkArticlesWithoutJournal(bmiConfig);
59
60 return result;
61 }
62
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();
68 //
69 // Object source = config.getSource();
70 return featureMap;
71 }
72
73 private String getDescriptionTitle(TaxonXImportState state){
74 String result = "Untitled";
75 ReferenceBase<?> ref = state.getModsReference();
76 if (ref != null){
77 result = ref.getTitle();
78 if ( CdmUtils.isEmpty(result)){
79 result = ref.getTitleCache();
80 }
81 }
82 return result;
83 }
84
85 public boolean doInvoke(TaxonXImportState state){
86 logger.debug("not yet fully implemented");
87
88 TaxonXImportConfigurator txConfig = state.getConfig();
89 Element root = txConfig.getSourceRoot();
90 Namespace nsTaxonx = root.getNamespace();
91
92 //Object source = config.getSource();
93
94 logger.info("start make Descriptions ...");
95
96
97 //for testing only
98 Taxon taxon = getTaxon(txConfig);
99 if (taxon == null){
100 logger.warn("Taxon could not be found");
101 return false;
102 }
103 //unlazyDescription(txConfig, taxon);
104 TaxonDescription description = TaxonDescription.NewInstance();
105 description.setTitleCache(getDescriptionTitle(state));
106
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;
114 try {
115 feature = TaxonXTransformer.descriptionType2feature(strType);
116 } catch (UnknownCdmTypeException e) {
117 feature = handleFeatureException(strType, e, txConfig);
118 }
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.");
126 }
127
128 DescriptionElementBase descriptionElement = TextData.NewInstance(text, Language.ENGLISH(), null);
129 descriptionElement.setFeature(feature);
130 description.addElement(descriptionElement);
131
132 //add reference
133 if (state.getModsReference() != null){
134 descriptionElement.addSource(null, null, state.getModsReference(), null, null, null);
135 }
136 }
137
138 }
139 if (description.size() >0){
140 taxon.addDescription(description);
141 getTaxonService().save(taxon);
142 }
143 return true;
144 }
145
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;
152 }
153 }
154
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));
160 }
161 return feature;
162 }
163
164
165 private String getText(Element div){
166 String result = "";
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();
172 }
173 }
174 return result;
175 }
176
177 private Taxon getTaxon(TaxonXImportConfigurator config){
178 Taxon result;
179 // result = Taxon.NewInstance(BotanicalName.NewInstance(null), null);
180 ICommonService commonService = getCommonService();
181
182 String originalSourceId = config.getOriginalSourceId();
183 String namespace = config.getOriginalSourceTaxonNamespace();
184 result = (Taxon)commonService.getSourcedObjectByIdInSource(Taxon.class, originalSourceId , namespace);
185 if (result == null){
186 logger.warn("Taxon (id: " + originalSourceId + ", namespace: " + namespace + ") could not be found");
187 }
188 return result;
189 }
190
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);
198 }
199
200 /* (non-Javadoc)
201 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
202 */
203 protected boolean isIgnore(TaxonXImportState state){
204 return ! state.getConfig().isDoFacts();
205 }
206
207 private String getBracketSourceName(TaxonXImportConfigurator config){
208 return "(" + config.getSourceNameString() + ")";
209 }
210
211 }