Project

General

Profile

Download (7.65 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.CdmImportBase;
29
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
30
import eu.etaxonomy.cdm.model.common.Language;
31
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
32
import eu.etaxonomy.cdm.model.description.Feature;
33
import eu.etaxonomy.cdm.model.description.TaxonDescription;
34
import eu.etaxonomy.cdm.model.description.TextData;
35
import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
36
import eu.etaxonomy.cdm.model.reference.Reference;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.term.TermVocabulary;
39
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
40

    
41

    
42
/**
43
 * @author a.mueller
44
 * @since 29.07.2008
45
 */
46
@Component
47
public class TaxonXDescriptionImport
48
        extends CdmImportBase<TaxonXImportConfigurator, TaxonXImportState> {
49

    
50
    private static final long serialVersionUID = 1957356490206741364L;
51
    private static final Logger logger = Logger.getLogger(TaxonXDescriptionImport.class);
52

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

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

    
63
		return result;
64
	}
65

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

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

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

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

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

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

    
100

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

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

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

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

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

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

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

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

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

    
177

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

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

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

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

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

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

    
225
}
(2-2/7)