Project

General

Profile

Download (11.8 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.List;
14
import java.util.UUID;
15

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

    
22
import eu.etaxonomy.cdm.api.service.IVocabularyService;
23
import eu.etaxonomy.cdm.common.ResultWrapper;
24
import eu.etaxonomy.cdm.common.XmlHelp;
25
import eu.etaxonomy.cdm.io.common.ICdmIO;
26
import eu.etaxonomy.cdm.io.redlist.bfnXml.BfnXmlConstants;
27
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
28
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
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 long serialVersionUID = 3545757825059662424L;
45
    private static final Logger logger = Logger.getLogger(BfnXmlImportFeature.class);
46

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

    
51

    
52
	@Override
53
	@SuppressWarnings({ "unchecked", "rawtypes" })
54
	public void doInvoke(BfnXmlImportState state){
55
	    logger.info("start create Features in CDM...");
56

    
57
		IVocabularyService vocabularyService = getVocabularyService();
58

    
59
		ResultWrapper<Boolean> success = ResultWrapper.NewInstance(true);
60
		String childName;
61
		boolean obligatory;
62
		BfnXmlImportConfigurator config = state.getConfig();
63
		Element elDataSet = getDataSetElement(config);
64
		Namespace bfnNamespace = config.getBfnXmlNamespace();
65

    
66
		List contentXML = elDataSet.getContent();
67
		Element currentElement = null;
68

    
69
		for(Object object:contentXML){
70

    
71
			if(object instanceof Element){
72
				currentElement = (Element)object;
73

    
74
				if(currentElement.getName().equalsIgnoreCase(BfnXmlConstants.EL_ROTELISTEDATEN)){
75

    
76
					TransactionStatus tx = startTransaction();
77

    
78
					childName = BfnXmlConstants.EL_EIGENSCHAFTEN;
79
					obligatory = false;
80
					Element elFeatureNames = XmlHelp.getSingleChildElement(success, currentElement, childName, bfnNamespace, obligatory);
81

    
82
					String bfnElementName = BfnXmlConstants.EL_EIGENSCHAFT;
83
					List<Element> elFeatureList = elFeatureNames.getChildren(bfnElementName, bfnNamespace);
84
					List<Feature> featureList = new ArrayList<>();
85
					//for each taxonName
86
					for (Element elFeature : elFeatureList){
87

    
88
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME, bfnNamespace).equalsIgnoreCase(BfnXmlConstants.VOC_RL_KAT)){
89
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
90
						}
91
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.VOC_KAT)){
92
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
93
						}else
94
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.VOC_AKTUELLE_BESTANDSSTITUATION)){
95
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
96
						}else
97
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.VOC_LANGFRISTIGER_BESTANDSTREND)){
98
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
99
						}else
100
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.VOC_KURZFRISTIGER_BESTANDSTREND)){
101
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
102
						}else
103
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.VOC_RISIKOFAKTOREN)){
104
						    makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
105
						}else
106
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.VOC_VERANTWORTLICHKEIT)){
107
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
108
						}else
109
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.VOC_ALTE_RL_KAT)){
110
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
111
						}else
112
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.VOC_NEOBIOTA)){
113
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
114
						}else
115
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.VOC_EINDEUTIGER_CODE)){
116
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
117
						}else
118
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.FEAT_KOMMENTAR_TAXONOMIE)){
119
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, false, state);
120
						}else
121
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.FEAT_KOMMENTAR_GEFAEHRDUNG)){
122
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, false, state);
123
						}else
124
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.VOC_SONDERFAELLE)){
125
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, true, state);
126
						}else
127
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.FEAT_LETZTER_NACHWEIS)){
128
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, false, state);
129
						}else
130
						if(elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME).equalsIgnoreCase(BfnXmlConstants.FEAT_WEITERE_KOMMENTARE)){
131
							makeFeature(vocabularyService, featureList,success, obligatory, bfnNamespace,elFeature, false, state);
132
						}
133
					}
134
					createFeatureTree(featureList);
135
					commitTransaction(tx);
136

    
137
					logger.info("end create features ...");
138

    
139
					if (!success.getValue()){
140
						state.setUnsuccessfull();
141
					}
142
					return;
143
				}
144
			}
145
		}
146
		return;
147

    
148
	}
149

    
150
	private void createFeatureTree(List<Feature> featureList) {
151
		FeatureTree featureTree = FeatureTree.NewInstance(featureList);
152
		String featureTreeName = "RedListFeatureTree";
153
		featureTree.setTitleCache(featureTreeName, true);
154
		getFeatureTreeService().save(featureTree);
155
	}
156

    
157
	private void makeFeature(IVocabularyService vocabularyService,
158
			List<Feature> featureList,
159
			ResultWrapper<Boolean> success, boolean obligatory,
160
			Namespace bfnNamespace, Element elFeature, boolean supportsCategoricalData, BfnXmlImportState state) {
161
		String childName;
162
		String strRlKat = elFeature.getAttributeValue(BfnXmlConstants.ATT_STANDARDNAME);
163
		UUID featureUUID = null;
164
		try {
165
			featureUUID = BfnXmlTransformer.getRedlistFeatureUUID(strRlKat);
166
		} catch (UnknownCdmTypeException e) {
167
			e.printStackTrace();
168
		}
169
		Feature redListCat = getFeature(state, featureUUID, strRlKat, strRlKat, strRlKat, null);
170
		addSource(redListCat, state);
171
		redListCat.setSupportsCategoricalData(supportsCategoricalData);
172
		//TODO implement German, but currently titleCache generation does not yet work correctly with another language
173
//		redListCat.getRepresentation(Language.DEFAULT()).setLanguage(Language.GERMAN());
174
		featureList.add(redListCat);
175
		childName = BfnXmlConstants.EL_LISTENWERTE;
176
		Element elListValues = XmlHelp.getSingleChildElement(success, elFeature, childName, bfnNamespace, obligatory);
177
		if(elListValues != null && !elListValues.getContent().isEmpty()){
178
			String childElementName = BfnXmlConstants.EL_LWERT;
179
			createOrUpdateStates(bfnNamespace, elListValues, childElementName, redListCat, state);
180
		}
181
		TermVocabulary<?> voc = createOrUpdateTermVocabulary(TermType.Feature, vocabularyService, redListCat, BfnXmlConstants.VOC_REDLIST_FEATURES);
182
	    addSource(voc, state);
183
	}
184

    
185
	/**
186
     * @param redListCat
187
     * @param state
188
     */
189
    private void addSource(IdentifiableEntity<?> redListCat, BfnXmlImportState state) {
190
        if (redListCat.getSources().isEmpty()){
191
            String id = null;
192
            String idNamespace = null;
193
            String detail = null;
194
            redListCat.addImportSource(id, idNamespace, state.getCompleteSourceRef(), detail);
195
        }
196
    }
197

    
198

    
199
    @SuppressWarnings({ "rawtypes" })
200
	private TermVocabulary createOrUpdateTermVocabulary(TermType termType, IVocabularyService vocabularyService, DefinedTermBase term, String strTermVocabulary) {
201

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

    
207
		return termVocabulary;
208
	}
209

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

    
217
		List<Element> elListValueList = elListValues.getChildren(childElementName, bfnNamespace);
218

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

    
257

    
258
    @Override
259
    public boolean doCheck(BfnXmlImportState state){
260
        boolean result = true;
261
        return result;
262
    }
263

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

    
269
}
(4-4/9)