2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.redlist
.bfnXml
.in
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.List
;
15 import java
.util
.UUID
;
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
;
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
;
42 public class BfnXmlImportFeature
extends BfnXmlImportBase
implements ICdmIO
<BfnXmlImportState
> {
43 private static final Logger logger
= Logger
.getLogger(BfnXmlImportFeature
.class);
45 public BfnXmlImportFeature(){
50 /** Hibernate classification vocabulary initialisation strategy */
51 private static final List
<String
> VOC_CLASSIFICATION_INIT_STRATEGY
= Arrays
.asList(new String
[] {
53 "classification.rootNodes",
56 "childNodes.taxon.name",
63 "taxon.synonymRelations",
70 @SuppressWarnings({ "unchecked", "rawtypes" })
71 public void doInvoke(BfnXmlImportState state
){
73 IVocabularyService vocabularyService
= getVocabularyService();
76 logger
.warn("start create Features in CDM...");
77 ResultWrapper
<Boolean
> success
= ResultWrapper
.NewInstance(true);
80 BfnXmlImportConfigurator config
= state
.getConfig();
81 Element elDataSet
= getDataSetElement(config
);
82 Namespace bfnNamespace
= config
.getBfnXmlNamespace();
84 List contentXML
= elDataSet
.getContent();
85 Element currentElement
= null;
86 for(Object object
:contentXML
){
88 if(object
instanceof Element
){
89 currentElement
= (Element
)object
;
91 if(currentElement
.getName().equalsIgnoreCase("ROTELISTEDATEN")){
93 TransactionStatus tx
= startTransaction();
95 childName
= "EIGENSCHAFTEN";
97 Element elFeatureNames
= XmlHelp
.getSingleChildElement(success
, currentElement
, childName
, bfnNamespace
, obligatory
);
99 String bfnElementName
= "EIGENSCHAFT";
100 List
<Element
> elFeatureList
= elFeatureNames
.getChildren(bfnElementName
, bfnNamespace
);
101 List
<Feature
> featureList
= new ArrayList
<Feature
>();
103 for (Element elFeature
: elFeatureList
){
105 if(elFeature
.getAttributeValue("standardname", bfnNamespace
).equalsIgnoreCase("RL Kat.")){
106 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
108 String featureLabel
= "Kat. +/-";
109 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase(featureLabel
)){
110 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
112 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("aktuelle Bestandsstituation")){
113 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
115 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("langfristiger Bestandstrend")){
116 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
118 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("kurzfristiger Bestandstrend")){
119 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
121 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("Risikofaktoren")){
122 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
124 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("Verantwortlichkeit")){
125 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
127 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("alte RL- Kat.")){
128 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
130 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("Neobiota")){
131 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
133 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("Eindeutiger Code")){
134 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
136 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("Kommentar zur Taxonomie")){
137 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
139 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("Kommentar zur Gefährdung")){
140 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
142 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("Sonderfälle")){
143 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
145 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("Letzter Nachweis")){
146 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
148 if(elFeature
.getAttributeValue("standardname").equalsIgnoreCase("Weitere Kommentare")){
149 makeFeature(vocabularyService
, featureList
,success
, obligatory
, bfnNamespace
,elFeature
, state
);
152 createFeatureTree(featureList
);
153 commitTransaction(tx
);
155 logger
.info("end create features ...");
157 if (!success
.getValue()){
158 state
.setUnsuccessfull();
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
);
180 * @param vocabularyService
184 * @param bfnNamespace
188 private void makeFeature(IVocabularyService vocabularyService
,
189 List
<Feature
> featureList
,
190 ResultWrapper
<Boolean
> success
, boolean obligatory
,
191 Namespace bfnNamespace
, Element elFeature
, BfnXmlImportState state
) {
193 String strRlKat
= elFeature
.getAttributeValue("standardname");
194 UUID featureUUID
= null;
196 featureUUID
= BfnXmlTransformer
.getRedlistFeatureUUID(strRlKat
);
197 } catch (UnknownCdmTypeException e
) {
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
);
211 createOrUpdateTermVocabulary(TermType
.Feature
, vocabularyService
, redListCat
, "RedList Feature");
215 * @param vocabularyService
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
)){
227 if(termVocabulary
== null){
228 termVocabulary
= TermVocabulary
.NewInstance(termType
, strTermVocabulary
, strTermVocabulary
, strTermVocabulary
, null);
230 termVocabulary
.addTerm(term
);
231 vocabularyService
.saveOrUpdate(termVocabulary
);
233 return termVocabulary
;
239 * @param bfnNamespace
240 * @param elListValues
241 * @param childElementName
245 @SuppressWarnings({ "unchecked", "rawtypes"})
246 private void createOrUpdateStates(Namespace bfnNamespace
,
247 Element elListValues
,
248 String childElementName
,
250 BfnXmlImportState state
) {
252 List
<Element
> elListValueList
= elListValues
.getChildren(childElementName
, bfnNamespace
);
253 // List<StateData> stateList = new ArrayList<StateData>();
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;
262 vocabularyStateUuid
= BfnXmlTransformer
.getRedlistVocabularyUUID(redListCat
.toString());
263 } catch (UnknownCdmTypeException e1
) {
264 vocabularyStateUuid
= UUID
.randomUUID();
265 logger
.warn("Element: " + listValue
+ "\n"+ e1
);
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. ");
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
);
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
);
284 if(termVocabulary
!= null){
285 redListCat
.addSupportedCategoricalEnumeration(termVocabulary
);
286 getTermService().saveOrUpdate(redListCat
);
293 public boolean doCheck(BfnXmlImportState state
){
294 boolean result
= true;
295 //TODO needs to be implemented
300 protected boolean isIgnore(BfnXmlImportState state
){
301 return ! state
.getConfig().isDoFeature();