Project

General

Profile

Download (11.6 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.model.common.DefinedTermBase;
28
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
29
import eu.etaxonomy.cdm.model.common.TermType;
30
import eu.etaxonomy.cdm.model.common.TermVocabulary;
31
import eu.etaxonomy.cdm.model.description.Feature;
32
import eu.etaxonomy.cdm.model.description.FeatureTree;
33
import eu.etaxonomy.cdm.model.description.State;
34
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
35
/**
36
 *
37
 * @author a.oppermann
38
 * @date 04.07.2013
39
 *
40
 */
41
@Component
42
public class BfnXmlImportFeature extends BfnXmlImportBase implements ICdmIO<BfnXmlImportState> {
43
	private static final Logger logger = Logger.getLogger(BfnXmlImportFeature.class);
44

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

    
49

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

    
67
    });
68

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

    
73
		IVocabularyService vocabularyService = getVocabularyService();
74

    
75

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

    
84
		List contentXML = elDataSet.getContent();
85
		Element currentElement = null;
86
		for(Object object:contentXML){
87

    
88
			if(object instanceof Element){
89
				currentElement = (Element)object;
90

    
91
				if(currentElement.getName().equalsIgnoreCase("ROTELISTEDATEN")){
92

    
93
					TransactionStatus tx = startTransaction();
94

    
95
					childName = "EIGENSCHAFTEN";
96
					obligatory = false;
97
					Element elFeatureNames = XmlHelp.getSingleChildElement(success, currentElement, childName, bfnNamespace, obligatory);
98

    
99
					String bfnElementName = "EIGENSCHAFT";
100
					List<Element> elFeatureList = elFeatureNames.getChildren(bfnElementName, bfnNamespace);
101
					List<Feature> featureList = new ArrayList<Feature>();
102
					//for each taxonName
103
					for (Element elFeature : elFeatureList){
104

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

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

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

    
166
	}
167

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

    
178
	/**
179
	 *
180
	 * @param vocabularyService
181
	 * @param featureList
182
	 * @param success
183
	 * @param obligatory
184
	 * @param bfnNamespace
185
	 * @param elFeature
186
	 * @param state
187
	 */
188
	private void makeFeature(IVocabularyService vocabularyService,
189
			List<Feature> featureList,
190
			ResultWrapper<Boolean> success, boolean obligatory,
191
			Namespace bfnNamespace, Element elFeature, BfnXmlImportState state) {
192
		String childName;
193
		String strRlKat = elFeature.getAttributeValue("standardname");
194
		UUID featureUUID = null;
195
		try {
196
			featureUUID = BfnXmlTransformer.getRedlistFeatureUUID(strRlKat);
197
		} catch (UnknownCdmTypeException e) {
198
			e.printStackTrace();
199
		}
200
		Feature redListCat = getFeature(state, featureUUID, strRlKat, strRlKat, strRlKat, null);
201
		redListCat.setSupportsCategoricalData(true);
202
		//TODO implement German, but currently titleCache generation does not yet work correctly with another language
203
//		redListCat.getRepresentation(Language.DEFAULT()).setLanguage(Language.GERMAN());
204
		featureList.add(redListCat);
205
		childName = "LISTENWERTE";
206
		Element elListValues = XmlHelp.getSingleChildElement(success, elFeature, childName, bfnNamespace, obligatory);
207
		if(elListValues != null && !elListValues.getContent().isEmpty()){
208
			String childElementName = "LWERT";
209
			createOrUpdateStates(bfnNamespace, elListValues, childElementName, redListCat, state);
210
		}
211
		createOrUpdateTermVocabulary(TermType.Feature, vocabularyService, redListCat, "RedList Feature");
212
	}
213

    
214
	/**
215
	 * @param vocabularyService
216
	 * @param term
217
	 */
218
	@SuppressWarnings({ "unchecked", "rawtypes" })
219
	private TermVocabulary createOrUpdateTermVocabulary(TermType termType, IVocabularyService vocabularyService, DefinedTermBase term, String strTermVocabulary) {
220
		TermVocabulary termVocabulary = null;
221
		List<TermVocabulary> vocList = vocabularyService.list(TermVocabulary.class, null, null, null, VOC_CLASSIFICATION_INIT_STRATEGY);
222
		for(TermVocabulary tv : vocList){
223
			if(tv.getTitleCache().equalsIgnoreCase(strTermVocabulary)){
224
				termVocabulary = tv;
225
			}
226
		}
227
		if(termVocabulary == null){
228
			termVocabulary = TermVocabulary.NewInstance(termType, strTermVocabulary, strTermVocabulary, strTermVocabulary, null);
229
		}
230
		termVocabulary.addTerm(term);
231
		vocabularyService.saveOrUpdate(termVocabulary);
232

    
233
		return termVocabulary;
234
	}
235

    
236

    
237
	/**
238
	 * @param success
239
	 * @param bfnNamespace
240
	 * @param elListValues
241
	 * @param childElementName
242
	 * @param redListCat
243
	 */
244

    
245
	@SuppressWarnings({ "unchecked", "rawtypes"})
246
	private void createOrUpdateStates(Namespace bfnNamespace,
247
	        Element elListValues,
248
	        String childElementName,
249
			Feature redListCat,
250
			BfnXmlImportState state) {
251

    
252
		List<Element> elListValueList = elListValues.getChildren(childElementName, bfnNamespace);
253
//		List<StateData> stateList = new ArrayList<StateData>();
254

    
255
		OrderedTermVocabulary termVocabulary = null;
256
		for(Element elListValue:elListValueList){
257
			String listValue = elListValue.getTextNormalize();
258
			String matchedListValue;
259
			UUID stateTermUuid = null;
260
			UUID vocabularyStateUuid = null;
261
			try {
262
				vocabularyStateUuid = BfnXmlTransformer.getRedlistVocabularyUUID(redListCat.toString());
263
			} catch (UnknownCdmTypeException e1) {
264
				vocabularyStateUuid = UUID.randomUUID();
265
				logger.warn("Element: " + listValue + "\n"+ e1);
266
			}
267
			try {
268
				matchedListValue = BfnXmlTransformer.redListString2RedListCode(listValue);
269
			} catch (UnknownCdmTypeException e) {
270
				matchedListValue = listValue;
271
				logger.warn("No matched red list code found for \""+redListCat.toString()+":" + listValue + "\". Use original label instead. ");
272
			}
273
			try {
274
				stateTermUuid = BfnXmlTransformer.getRedlistStateTermUUID(matchedListValue, redListCat.getTitleCache());
275
			} catch (UnknownCdmTypeException e) {
276
//				stateTermUuid = UUID.randomUUID();
277
				//TODO: needs to be fixed for "eindeutiger Code"
278
				logger.warn("could not finde state term uuid for " + matchedListValue + " and redlist category"+ redListCat.getTitleCache()+"\n"+e);
279
			}
280
			String vocName = redListCat.toString()+" States";
281
			termVocabulary = (OrderedTermVocabulary) getVocabulary(TermType.State, vocabularyStateUuid, vocName, vocName, vocName, null, true, null);
282
			State stateTerm = getStateTerm(state, stateTermUuid, matchedListValue, matchedListValue, matchedListValue, termVocabulary);
283
		}
284
		if(termVocabulary != null){
285
			redListCat.addSupportedCategoricalEnumeration(termVocabulary);
286
			getTermService().saveOrUpdate(redListCat);
287
		}
288

    
289
	}
290

    
291

    
292
    @Override
293
    public boolean doCheck(BfnXmlImportState state){
294
        boolean result = true;
295
        //TODO needs to be implemented
296
        return result;
297
    }
298

    
299
    @Override
300
	protected boolean isIgnore(BfnXmlImportState state){
301
		return ! state.getConfig().isDoFeature();
302
	}
303

    
304

    
305
}
(4-4/8)