Project

General

Profile

Download (7.56 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.DescriptionElementSource;
32
import eu.etaxonomy.cdm.model.common.Language;
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
	public boolean doCheck(TaxonXImportState state){
57
		boolean result = true;
58
		logger.warn("Checking for Facts not yet implemented");
59
		//result &= checkArticlesWithoutJournal(bmiConfig);
60
		
61
		return result;
62
	}
63

    
64
	public Map<Integer, Feature> fillFactCategories(IImportConfigurator config, CdmApplicationController cdmApp){
65
		logger.warn("not yet implemented");
66
		Map<Integer, Feature> featureMap = new HashMap<Integer, Feature>();
67
//		IDescriptionService descriptionService = cdmApp.getDescriptionService();
68
//		ITermService termService = cdmApp.getTermService();
69
//
70
//		Object source = config.getSource();
71
		return featureMap;
72
	}
73
	
74
	private String getDescriptionTitle(TaxonXImportState state){
75
		String result = "Untitled";
76
		Reference<?> ref = state.getModsReference();
77
		if (ref != null){
78
			result = ref.getTitle();
79
			if ( CdmUtils.isEmpty(result)){
80
				result = ref.getTitleCache();
81
			}
82
		}
83
		return result;
84
	}
85
	
86
	public void doInvoke(TaxonXImportState state){
87
		logger.debug("not yet fully implemented");
88
		
89
		TaxonXImportConfigurator txConfig = state.getConfig();
90
		Element root = txConfig.getSourceRoot();
91
		Namespace nsTaxonx = root.getNamespace();
92
		
93
		//Object source = config.getSource();
94
		
95
		logger.info("start make Descriptions ...");
96
		
97
		
98
		//for testing only
99
		Taxon taxon = getTaxon(txConfig);
100
		if (taxon == null){
101
			logger.warn("Taxon could not be found");
102
			state.setUnsuccessfull();
103
		}
104
		
105
		Reference modsReference = state.getModsReference();
106
		if (modsReference == null){
107
			modsReference = state.getConfig().getSourceReference();
108
		}
109
		
110
		//unlazyDescription(txConfig, taxon);
111
		TaxonDescription description = TaxonDescription.NewInstance();
112
		description.setTitleCache(getDescriptionTitle(state), true);
113
		if (modsReference != null){
114
			description.addSource(null, null, modsReference, null);
115
		}
116
		
117
		Element elTaxonBody = root.getChild("taxonxBody", nsTaxonx);
118
		Element elTreatment = elTaxonBody.getChild("treatment", nsTaxonx);
119
		List<Element> elDivs = elTreatment.getChildren("div", nsTaxonx);
120
		for (Element div : elDivs){
121
			Attribute attrType = div.getAttribute("type", nsTaxonx);
122
			String strType = attrType.getValue();
123
			Feature feature = null;
124
			try {
125
				feature = TaxonXTransformer.descriptionType2feature(strType);
126
			} catch (UnknownCdmTypeException e) {
127
				feature = handleFeatureException(strType, e, txConfig);
128
			}
129
			String text = getText(div);
130
			if (!"".equals(CdmUtils.Nz(text).trim())){
131
				// hibernate throws an exception when a string is longer than approx. 65500 chars.
132
				// for now we truncate any description text to 65500 characters.
133
				if(text.length() > 65500){
134
					text = text.substring(0, 65500) + "... [text truncated]";
135
					logger.warn("Truncation of text: description for taxon " + taxon.getTitleCache() + " was longer than 65500 characters.");
136
				}
137
				
138
				DescriptionElementBase descriptionElement = TextData.NewInstance(text, Language.ENGLISH(), null);
139
				descriptionElement.setFeature(feature);
140
				description.addElement(descriptionElement);
141
				
142
				//add reference
143
				if (modsReference != null){
144
					descriptionElement.addSource(null, null, modsReference, null, null, null);
145
				}
146
			}
147

    
148
		}
149
		if (description.size() >0){
150
			taxon.addDescription(description);
151
			getTaxonService().save(taxon);
152
		}
153
		return;
154
	}
155
	
156
	private Feature handleFeatureException(String strType, UnknownCdmTypeException e, TaxonXImportConfigurator txConfig){
157
		Feature feature = null;
158
		TermVocabulary<Feature> featureVoc = Feature.BIOLOGY_ECOLOGY().getVocabulary();
159
		for (Feature oneFeature : featureVoc.getTerms()){
160
			if (strType.equals(oneFeature.getLabel()) || strType.equals(oneFeature.getRepresentation(Language.DEFAULT() ).getText() )){
161
				feature = oneFeature;
162
			}
163
		}
164
		
165
		if (feature == null){
166
			feature = Feature.NewInstance(strType, strType, null);
167
			featureVoc.addTerm(feature);
168
			getTermService().save(feature);
169
			logger.warn(e.getMessage() + ". Feature was added to the feature vocabulary. " + getBracketSourceName(txConfig));
170
		}
171
		return feature;
172
	}
173
	
174
	
175
	private String getText(Element div){
176
		String result = "";
177
		Iterator<Content> it =div.getDescendants(); 
178
		while (it.hasNext()){
179
			Content next = (Content)it.next();
180
			if (next instanceof Text){
181
				result += ((Text)next).getText();
182
			}
183
		}
184
		return result;
185
	}
186
	
187
	private Taxon getTaxon(TaxonXImportConfigurator config){
188
		Taxon result;
189
//		result =  Taxon.NewInstance(BotanicalName.NewInstance(null), null);
190
		ICommonService commonService = getCommonService();
191
		
192
		String originalSourceId = config.getOriginalSourceId();
193
		String namespace = config.getOriginalSourceTaxonNamespace();
194
		result = (Taxon)commonService.getSourcedObjectByIdInSource(Taxon.class, originalSourceId , namespace);
195
		if (result == null){
196
			logger.warn("Taxon (id: " + originalSourceId + ", namespace: " + namespace + ") could not be found");
197
		}
198
		return result;
199
	}
200
	
201
	private void unlazyDescription(TaxonXImportConfigurator config, Taxon taxon){
202
//		logger.warn("Preliminary commented");  //used single Transaction for all import instead !
203
//		TransactionStatus txStatus = config.getCdmAppController().startTransaction();
204
//		ITaxonService taxonService = config.getCdmAppController().getTaxonService();
205
//		taxonService.saveTaxon(taxon);
206
//		taxon.getDescriptions().size();
207
//		config.getCdmAppController().commitTransaction(txStatus);
208
	}
209
	
210
	/* (non-Javadoc)
211
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
212
	 */
213
	protected boolean isIgnore(TaxonXImportState state){
214
		return ! state.getConfig().isDoFacts();
215
	}
216
	
217
	private String getBracketSourceName(TaxonXImportConfigurator config){
218
		return "(" + config.getSourceNameString() + ")";
219
	}
220

    
221
}
(2-2/7)