Project

General

Profile

Download (7.63 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.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.OriginalSourceType;
33
import eu.etaxonomy.cdm.model.common.TermVocabulary;
34
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
35
import eu.etaxonomy.cdm.model.description.Feature;
36
import eu.etaxonomy.cdm.model.description.TaxonDescription;
37
import eu.etaxonomy.cdm.model.description.TextData;
38
import eu.etaxonomy.cdm.model.reference.Reference;
39
import eu.etaxonomy.cdm.model.taxon.Taxon;
40
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
41

    
42

    
43
/**
44
 * @author a.mueller
45
 * @created 29.07.2008
46
 * @version 1.0
47
 */
48
@Component
49
public class TaxonXDescriptionImport extends CdmIoBase<TaxonXImportState> implements ICdmIO<TaxonXImportState> {
50
	private static final Logger logger = Logger.getLogger(TaxonXDescriptionImport.class);
51

    
52
	public TaxonXDescriptionImport(){
53
		super();
54
	}
55

    
56
	@Override
57
    public boolean doCheck(TaxonXImportState state){
58
		boolean result = true;
59
		logger.warn("Checking for Facts not yet implemented");
60
		//result &= checkArticlesWithoutJournal(bmiConfig);
61

    
62
		return result;
63
	}
64

    
65
	public Map<Integer, Feature> fillFactCategories(IImportConfigurator config, CdmApplicationController cdmApp){
66
		logger.warn("not yet implemented");
67
		Map<Integer, Feature> featureMap = new HashMap<Integer, Feature>();
68
//		IDescriptionService descriptionService = cdmApp.getDescriptionService();
69
//		ITermService termService = cdmApp.getTermService();
70
//
71
//		Object source = config.getSource();
72
		return featureMap;
73
	}
74

    
75
	private String getDescriptionTitle(TaxonXImportState state){
76
		String result = "Untitled";
77
		Reference ref = state.getModsReference();
78
		if (ref != null){
79
			result = ref.getTitle();
80
			if ( CdmUtils.isEmpty(result)){
81
				result = ref.getTitleCache();
82
			}
83
		}
84
		return result;
85
	}
86

    
87
	@Override
88
    public void doInvoke(TaxonXImportState state){
89
		logger.debug("not yet fully implemented");
90

    
91
		TaxonXImportConfigurator txConfig = state.getConfig();
92
		Element root = txConfig.getSourceRoot();
93
		Namespace nsTaxonx = root.getNamespace();
94

    
95
		//Object source = config.getSource();
96

    
97
		logger.info("start make Descriptions ...");
98

    
99

    
100
		//for testing only
101
		Taxon taxon = getTaxon(txConfig);
102
		if (taxon == null){
103
			logger.warn("Taxon could not be found");
104
			state.setUnsuccessfull();
105
		}
106

    
107
		Reference modsReference = state.getModsReference();
108
		if (modsReference == null){
109
			modsReference = state.getConfig().getSourceReference();
110
		}
111

    
112
		//unlazyDescription(txConfig, taxon);
113
		TaxonDescription description = TaxonDescription.NewInstance();
114
		description.setTitleCache(getDescriptionTitle(state), true);
115
		if (modsReference != null){
116
			description.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, modsReference, null);
117
		}
118

    
119
		Element elTaxonBody = root.getChild("taxonxBody", nsTaxonx);
120
		Element elTreatment = elTaxonBody.getChild("treatment", nsTaxonx);
121
		List<Element> elDivs = elTreatment.getChildren("div", nsTaxonx);
122
		for (Element div : elDivs){
123
			Attribute attrType = div.getAttribute("type", nsTaxonx);
124
			String strType = attrType.getValue();
125
			Feature feature = null;
126
			try {
127
				feature = TaxonXTransformer.descriptionType2feature(strType);
128
			} catch (UnknownCdmTypeException e) {
129
				feature = handleFeatureException(strType, e, txConfig);
130
			}
131
			String text = getText(div);
132
			if (!"".equals(CdmUtils.Nz(text).trim())){
133
				// hibernate throws an exception when a string is longer than approx. 65500 chars.
134
				// for now we truncate any description text to 65500 characters.
135
				if(text.length() > 65500){
136
					text = text.substring(0, 65500) + "... [text truncated]";
137
					logger.warn("Truncation of text: description for taxon " + taxon.getTitleCache() + " was longer than 65500 characters.");
138
				}
139

    
140
				DescriptionElementBase descriptionElement = TextData.NewInstance(text, Language.ENGLISH(), null);
141
				descriptionElement.setFeature(feature);
142
				description.addElement(descriptionElement);
143

    
144
				//add reference
145
				if (modsReference != null){
146
					descriptionElement.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, modsReference, null, null, null);
147
				}
148
			}
149

    
150
		}
151
		if (description.size() >0){
152
			taxon.addDescription(description);
153
			getTaxonService().save(taxon);
154
		}
155
		return;
156
	}
157

    
158
	private Feature handleFeatureException(String strType, UnknownCdmTypeException e, TaxonXImportConfigurator txConfig){
159
		Feature feature = null;
160
		TermVocabulary<Feature> featureVoc = Feature.BIOLOGY_ECOLOGY().getVocabulary();
161
		for (Feature oneFeature : featureVoc.getTerms()){
162
			if (strType.equals(oneFeature.getLabel()) || strType.equals(oneFeature.getRepresentation(Language.DEFAULT() ).getText() )){
163
				feature = oneFeature;
164
			}
165
		}
166

    
167
		if (feature == null){
168
			feature = Feature.NewInstance(strType, strType, null);
169
			featureVoc.addTerm(feature);
170
			getTermService().save(feature);
171
			logger.warn(e.getMessage() + ". Feature was added to the feature vocabulary. " + getBracketSourceName(txConfig));
172
		}
173
		return feature;
174
	}
175

    
176

    
177
	private String getText(Element div){
178
		String result = "";
179
		Iterator<Content> it =div.getDescendants();
180
		while (it.hasNext()){
181
			Content next = it.next();
182
			if (next instanceof Text){
183
				result += ((Text)next).getText();
184
			}
185
		}
186
		return result;
187
	}
188

    
189
	private Taxon getTaxon(TaxonXImportConfigurator config){
190
		Taxon result;
191
//		result =  Taxon.NewInstance(TaxonNameBase.NewBotanicalInstance(null), null);
192
		ICommonService commonService = getCommonService();
193

    
194
		String originalSourceId = config.getOriginalSourceId();
195
		String namespace = config.getOriginalSourceTaxonNamespace();
196
		result = (Taxon)commonService.getSourcedObjectByIdInSource(Taxon.class, originalSourceId , namespace);
197
		if (result == null){
198
			logger.warn("Taxon (id: " + originalSourceId + ", namespace: " + namespace + ") could not be found");
199
		}
200
		return result;
201
	}
202

    
203
	private void unlazyDescription(TaxonXImportConfigurator config, Taxon taxon){
204
//		logger.warn("Preliminary commented");  //used single Transaction for all import instead !
205
//		TransactionStatus txStatus = config.getCdmAppController().startTransaction();
206
//		ITaxonService taxonService = config.getCdmAppController().getTaxonService();
207
//		taxonService.saveTaxon(taxon);
208
//		taxon.getDescriptions().size();
209
//		config.getCdmAppController().commitTransaction(txStatus);
210
	}
211

    
212
	/* (non-Javadoc)
213
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
214
	 */
215
	@Override
216
    protected boolean isIgnore(TaxonXImportState state){
217
		return ! state.getConfig().isDoFacts();
218
	}
219

    
220
	private String getBracketSourceName(TaxonXImportConfigurator config){
221
		return "(" + config.getSourceNameString() + ")";
222
	}
223

    
224
}
(2-2/7)