Project

General

Profile

Download (11.5 KB) Statistics
| Branch: | 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.redlist.bfnXml.in;
11

    
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.List;
15
import java.util.UUID;
16

    
17
import org.apache.log4j.Logger;
18
import org.jdom.Element;
19
import org.jdom.Namespace;
20
import org.springframework.stereotype.Component;
21
import org.springframework.transaction.TransactionStatus;
22

    
23
import eu.etaxonomy.cdm.api.service.IVocabularyService;
24
import eu.etaxonomy.cdm.common.ResultWrapper;
25
import eu.etaxonomy.cdm.common.XmlHelp;
26
import eu.etaxonomy.cdm.io.common.ICdmIO;
27
import eu.etaxonomy.cdm.io.redlist.bfnXml.BfnXmlConstants;
28
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
29
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
30
import eu.etaxonomy.cdm.model.common.TermType;
31
import eu.etaxonomy.cdm.model.common.TermVocabulary;
32
import eu.etaxonomy.cdm.model.description.Feature;
33
import eu.etaxonomy.cdm.model.description.FeatureTree;
34
import eu.etaxonomy.cdm.model.description.State;
35
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
36
/**
37
 *
38
 * @author a.oppermann
39
 * @date 04.07.2013
40
 *
41
 */
42
@Component
43
public class BfnXmlImportFeature extends BfnXmlImportBase implements ICdmIO<BfnXmlImportState> {
44
	private static final Logger logger = Logger.getLogger(BfnXmlImportFeature.class);
45

    
46
	public BfnXmlImportFeature(){
47
		super();
48
	}
49

    
50

    
51
    /** Hibernate classification vocabulary initialisation strategy */
52
    private static final List<String> VOC_CLASSIFICATION_INIT_STRATEGY = Arrays.asList(new String[] {
53
            "classification.$",
54
            "classification.rootNodes",
55
            "childNodes",
56
            "childNodes.taxon",
57
            "childNodes.taxon.name",
58
            "taxonNodes",
59
            "taxonNodes.taxon",
60
            "synonymRelations",
61
            "taxon.*",
62
            "taxon.sec",
63
            "taxon.name.*",
64
            "taxon.synonymRelations",
65
            "termVocabulary.*",
66
            "terms"
67

    
68
    });
69

    
70
	@Override
71
	@SuppressWarnings({ "unchecked", "rawtypes" })
72
	public void doInvoke(BfnXmlImportState state){
73

    
74
		IVocabularyService vocabularyService = getVocabularyService();
75

    
76

    
77
		logger.warn("start create Features in CDM...");
78
		ResultWrapper<Boolean> success = ResultWrapper.NewInstance(true);
79
		String childName;
80
		boolean obligatory;
81
		BfnXmlImportConfigurator config = state.getConfig();
82
		Element elDataSet = getDataSetElement(config);
83
		Namespace bfnNamespace = config.getBfnXmlNamespace();
84

    
85
		List contentXML = elDataSet.getContent();
86
		Element currentElement = null;
87

    
88
		for(Object object:contentXML){
89

    
90
			if(object instanceof Element){
91
				currentElement = (Element)object;
92

    
93
				if(currentElement.getName().equalsIgnoreCase(BfnXmlConstants.EL_ROTELISTEDATEN)){
94

    
95
					TransactionStatus tx = startTransaction();
96

    
97
					childName = BfnXmlConstants.EL_EIGENSCHAFTEN;
98
					obligatory = false;
99
					Element elFeatureNames = XmlHelp.getSingleChildElement(success, currentElement, childName, bfnNamespace, obligatory);
100

    
101
					String bfnElementName = BfnXmlConstants.EL_EIGENSCHAFT;
102
					List<Element> elFeatureList = elFeatureNames.getChildren(bfnElementName, bfnNamespace);
103
					List<Feature> featureList = new ArrayList<Feature>();
104
					//for each taxonName
105
					for (Element elFeature : elFeatureList){
106

    
107
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME, bfnNamespace).equalsIgnoreCase("RL Kat.")){
108
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
109
						}
110
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("Kat. +/-")){
111
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
112
						}else
113
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("aktuelle Bestandsstituation")){
114
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
115
						}else
116
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("langfristiger Bestandstrend")){
117
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
118
						}else
119
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("kurzfristiger Bestandstrend")){
120
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
121
						}else
122
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("Risikofaktoren")){
123
						    makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
124
						}else
125
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("Verantwortlichkeit")){
126
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
127
						}else
128
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("alte RL- Kat.")){
129
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
130
						}else
131
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("Neobiota")){
132
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
133
						}else
134
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("Eindeutiger Code")){
135
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
136
						}else
137
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("Kommentar zur Taxonomie")){
138
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, false, state);
139
						}else
140
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("Kommentar zur Gefährdung")){
141
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, false, state);
142
						}else
143
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("Sonderfälle")){
144
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
145
						}else
146
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("Letzter Nachweis")){
147
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, false, state);
148
						}else
149
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase("Weitere Kommentare")){
150
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, false, state);
151
						}
152
					}
153
					createFeatureTree(featureList);
154
					commitTransaction(tx);
155

    
156
					logger.info("end create features ...");
157

    
158
					if (!success.getValue()){
159
						state.setUnsuccessfull();
160
					}
161
					return;
162
				}
163
			}
164
		}
165
		return;
166

    
167
	}
168

    
169
	private void createFeatureTree(List<Feature> featureList) {
170
		FeatureTree featureTree = FeatureTree.NewInstance(featureList);
171
		String featureTreeName = "RedListFeatureTree";
172
		featureTree.setTitleCache(featureTreeName, true);
173
		getFeatureTreeService().save(featureTree);
174
	}
175

    
176
	private void makeFeature(IVocabularyService vocabularyService,
177
			List<Feature> featureList,
178
			ResultWrapper<Boolean> success, boolean obligatory,
179
			Namespace bfnNamespace, Element elFeature, boolean supportsCategoricalData, BfnXmlImportState state) {
180
		String childName;
181
		String strRlKat = elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME);
182
		UUID featureUUID = null;
183
		try {
184
			featureUUID = BfnXmlTransformer.getRedlistFeatureUUID(strRlKat);
185
		} catch (UnknownCdmTypeException e) {
186
			e.printStackTrace();
187
		}
188
		Feature redListCat = getFeature(state, featureUUID, strRlKat, strRlKat, strRlKat, null);
189
		redListCat.setSupportsCategoricalData(supportsCategoricalData);
190
		//TODO implement German, but currently titleCache generation does not yet work correctly with another language
191
//		redListCat.getRepresentation(Language.DEFAULT()).setLanguage(Language.GERMAN());
192
		featureList.add(redListCat);
193
		childName = BfnXmlConstants.EL_LISTENWERTE;
194
		Element elListValues = XmlHelp.getSingleChildElement(success, elFeature, childName, bfnNamespace, obligatory);
195
		if(elListValues != null && !elListValues.getContent().isEmpty()){
196
			String childElementName = BfnXmlConstants.EL_LWERT;
197
			createOrUpdateStates(bfnNamespace, elListValues, childElementName, redListCat, state);
198
		}
199
		createOrUpdateTermVocabulary(TermType.Feature, vocabularyService, redListCat, BfnXmlConstants.VOC_REDLIST_FEATURES);
200
	}
201

    
202
	@SuppressWarnings({ "rawtypes" })
203
	private TermVocabulary createOrUpdateTermVocabulary(TermType termType, IVocabularyService vocabularyService, DefinedTermBase term, String strTermVocabulary) {
204

    
205
        //create/get red list feature vocabulary
206
        TermVocabulary<DefinedTermBase> termVocabulary = getVocabulary(termType, BfnXmlConstants.vocRLFeatures, BfnXmlConstants.VOC_REDLIST_FEATURES, BfnXmlConstants.VOC_REDLIST_FEATURES, BfnXmlConstants.VOC_REDLIST_FEATURES, null, false, null);
207
		termVocabulary.addTerm(term);
208
		vocabularyService.saveOrUpdate(termVocabulary);
209

    
210
		return termVocabulary;
211
	}
212

    
213
	@SuppressWarnings({ "unchecked", "rawtypes"})
214
	private void createOrUpdateStates(Namespace bfnNamespace,
215
	        Element elListValues,
216
	        String childElementName,
217
			Feature redListCat,
218
			BfnXmlImportState state) {
219

    
220
		List<Element> elListValueList = elListValues.getChildren(childElementName, bfnNamespace);
221

    
222
		OrderedTermVocabulary termVocabulary = null;
223
		for(Element elListValue:elListValueList){
224
			String listValue = elListValue.getTextNormalize();
225
			String matchedListValue;
226
			UUID stateTermUuid = null;
227
			UUID vocabularyStateUuid = null;
228
			try {
229
				vocabularyStateUuid = BfnXmlTransformer.getRedlistVocabularyUUID(redListCat.getLabel());
230
			} catch (UnknownCdmTypeException e1) {
231
				vocabularyStateUuid = UUID.randomUUID();
232
				logger.warn("Element: " + listValue + "\n"+ e1);
233
			}
234
			try {
235
				matchedListValue = BfnXmlTransformer.redListString2RedListCode(listValue);
236
			} catch (UnknownCdmTypeException e) {
237
				matchedListValue = listValue;
238
				logger.warn("No matched red list code found for \""+redListCat.toString()+":" + listValue + "\". Use original label instead. ");
239
			}
240
			try {
241
				stateTermUuid = BfnXmlTransformer.getRedlistStateTermUUID(matchedListValue, redListCat.getTitleCache());
242
			} catch (UnknownCdmTypeException e) {
243
//				stateTermUuid = UUID.randomUUID();
244
				//TODO: needs to be fixed for "eindeutiger Code"
245
				logger.warn("could not finde state term uuid for " + matchedListValue + " and redlist category"+ redListCat.getTitleCache()+"\n"+e);
246
			}
247
			String vocName = redListCat.toString()+" States";
248
			termVocabulary = (OrderedTermVocabulary) getVocabulary(TermType.State, vocabularyStateUuid, vocName, vocName, vocName, null, true, null);
249
			State stateTerm = getStateTerm(state, stateTermUuid, matchedListValue, matchedListValue, matchedListValue, termVocabulary);
250
		}
251
		if(termVocabulary != null){
252
			redListCat.addSupportedCategoricalEnumeration(termVocabulary);
253
			getTermService().saveOrUpdate(redListCat);
254
		}
255

    
256
	}
257

    
258

    
259
    @Override
260
    public boolean doCheck(BfnXmlImportState state){
261
        boolean result = true;
262
        //TODO needs to be implemented
263
        return result;
264
    }
265

    
266
    @Override
267
	protected boolean isIgnore(BfnXmlImportState state){
268
		return ! state.getConfig().isDoFeature();
269
	}
270

    
271

    
272
}
(4-4/8)