Project

General

Profile

Download (7.71 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.CdmApplicationDefaultController;
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.DescriptionElementSource;
36
import eu.etaxonomy.cdm.model.description.Feature;
37
import eu.etaxonomy.cdm.model.description.TaxonDescription;
38
import eu.etaxonomy.cdm.model.description.TextData;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
42

    
43

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

    
53
	public TaxonXDescriptionImport(){
54
		super();
55
	}
56
	
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, CdmApplicationDefaultController 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
	public void doInvoke(TaxonXImportState state){
88
		logger.debug("not yet fully implemented");
89
		
90
		TaxonXImportConfigurator txConfig = state.getConfig();
91
		Element root = txConfig.getSourceRoot();
92
		Namespace nsTaxonx = root.getNamespace();
93
		
94
		//Object source = config.getSource();
95
		
96
		logger.info("start make Descriptions ...");
97
		
98
		
99
		//for testing only
100
		Taxon taxon = getTaxon(txConfig);
101
		if (taxon == null){
102
			logger.warn("Taxon could not be found");
103
			state.setUnsuccessfull();
104
		}
105
		
106
		Reference modsReference = state.getModsReference();
107
		if (modsReference == null){
108
			modsReference = state.getConfig().getSourceReference();
109
		}
110
		
111
		//unlazyDescription(txConfig, taxon);
112
		TaxonDescription description = TaxonDescription.NewInstance();
113
		description.setTitleCache(getDescriptionTitle(state), true);
114
		if (modsReference != null){
115
			description.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, modsReference, null);
116
		}
117
		
118
		Element elTaxonBody = root.getChild("taxonxBody", nsTaxonx);
119
		Element elTreatment = elTaxonBody.getChild("treatment", nsTaxonx);
120
		List<Element> elDivs = elTreatment.getChildren("div", nsTaxonx);
121
		for (Element div : elDivs){
122
			Attribute attrType = div.getAttribute("type", nsTaxonx);
123
			String strType = attrType.getValue();
124
			Feature feature = null;
125
			try {
126
				feature = TaxonXTransformer.descriptionType2feature(strType);
127
			} catch (UnknownCdmTypeException e) {
128
				feature = handleFeatureException(strType, e, txConfig);
129
			}
130
			String text = getText(div);
131
			if (!"".equals(CdmUtils.Nz(text).trim())){
132
				// hibernate throws an exception when a string is longer than approx. 65500 chars.
133
				// for now we truncate any description text to 65500 characters.
134
				if(text.length() > 65500){
135
					text = text.substring(0, 65500) + "... [text truncated]";
136
					logger.warn("Truncation of text: description for taxon " + taxon.getTitleCache() + " was longer than 65500 characters.");
137
				}
138
				
139
				DescriptionElementBase descriptionElement = TextData.NewInstance(text, Language.ENGLISH(), null);
140
				descriptionElement.setFeature(feature);
141
				description.addElement(descriptionElement);
142
				
143
				//add reference
144
				if (modsReference != null){
145
					descriptionElement.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, modsReference, null, null, null);
146
				}
147
			}
148

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

    
222
}
(2-2/7)