Project

General

Profile

Download (16.7 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
package eu.etaxonomy.cdm.io.redlist.bfnXml.out;
10

    
11
import java.util.Comparator;
12
import java.util.List;
13
import java.util.Set;
14
import java.util.UUID;
15

    
16
import org.jdom2.Attribute;
17
import org.jdom2.Document;
18
import org.jdom2.Element;
19
import org.springframework.stereotype.Component;
20

    
21
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
22
import eu.etaxonomy.cdm.io.redlist.bfnXml.BfnXmlConstants;
23
import eu.etaxonomy.cdm.io.redlist.bfnXml.in.BfnXmlTransformer;
24
import eu.etaxonomy.cdm.model.common.DefinedTerm;
25
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
26
import eu.etaxonomy.cdm.model.common.Language;
27
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
28
import eu.etaxonomy.cdm.model.common.TermVocabulary;
29
import eu.etaxonomy.cdm.model.description.CategoricalData;
30
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
31
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
32
import eu.etaxonomy.cdm.model.description.Feature;
33
import eu.etaxonomy.cdm.model.description.State;
34
import eu.etaxonomy.cdm.model.description.StateData;
35
import eu.etaxonomy.cdm.model.description.TaxonDescription;
36
import eu.etaxonomy.cdm.model.description.TextData;
37
import eu.etaxonomy.cdm.model.name.NonViralName;
38
import eu.etaxonomy.cdm.model.name.Rank;
39
import eu.etaxonomy.cdm.model.taxon.Classification;
40
import eu.etaxonomy.cdm.model.taxon.Synonym;
41
import eu.etaxonomy.cdm.model.taxon.Taxon;
42
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
44

    
45

    
46

    
47
/**
48
 *
49
 * @author pplitzner
50
 * @date May 3, 2016
51
 *
52
 */
53
@Component
54
public class BfnXmlTaxonNameExport extends BfnXmlExportBase {
55

    
56
    private static final long serialVersionUID = -931703660108981011L;
57

    
58
	public BfnXmlTaxonNameExport(){
59
		super();
60
	}
61

    
62
	@Override
63
    protected void doInvoke(BfnXmlExportState state){
64
	    startTransaction(true);
65

    
66
	    Document document = state.getConfig().getDocument();
67

    
68
	    //get all classifications
69
	    List<Classification> classifications = getClassificationService().list(Classification.class, null, null, null, null);
70
	    for (Classification classification : classifications) {
71
            Element roteListeDaten = new Element(BfnXmlConstants.EL_ROTELISTEDATEN);
72
            roteListeDaten.setAttribute(new Attribute(BfnXmlConstants.ATT_INHALT, classification.getTitleCache()));
73
            document.getRootElement().addContent(roteListeDaten);
74

    
75
            exportFeatures(roteListeDaten);
76

    
77
            exportTaxonomy(classification, roteListeDaten, state);
78

    
79
        }
80

    
81
	}
82

    
83
    private void exportTaxonomy(Classification classification, Element roteListeDaten, BfnXmlExportState state) {
84
        Element taxonyme = new Element(BfnXmlConstants.EL_TAXONYME);
85
        roteListeDaten.addContent(taxonyme);
86
        List<TaxonNode> childNodes = classification.getChildNodes();
87
        java.util.Collections.sort(childNodes, new TaxonComparator());
88
        for (TaxonNode taxonNode : childNodes) {
89
            exportTaxon(taxonNode.getTaxon(), taxonyme, state);
90
        }
91
    }
92

    
93
    private void exportFactualData(Taxon taxon, Element parent) {
94
        Element informationen = new Element(BfnXmlConstants.EL_INFORMATIONEN);
95
        parent.addContent(informationen);
96
        Element bezugsraum = new Element(BfnXmlConstants.EL_BEZUGSRAUM);
97
        bezugsraum.setAttribute(new Attribute(BfnXmlConstants.ATT_NAME, BfnXmlConstants.BEZUGRAUM_BUND));
98
        informationen.addContent(bezugsraum);
99

    
100
        Set<TaxonDescription> descriptions = taxon.getDescriptions();
101
        for (TaxonDescription taxonDescription : descriptions) {
102
            //TODO: export only red list features ??
103
            Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
104
            exportCategoricalData(BfnXmlConstants.VOC_RL_KAT, descriptionElements, taxon, parent);
105
            exportCategoricalData(BfnXmlConstants.VOC_KAT, descriptionElements, taxon, parent);
106
            exportCategoricalData(BfnXmlConstants.VOC_NEOBIOTA, descriptionElements, taxon, parent);
107
            exportCategoricalData(BfnXmlConstants.VOC_AKTUELLE_BESTANDSSTITUATION, descriptionElements, taxon, parent);
108
            exportCategoricalData(BfnXmlConstants.VOC_LANGFRISTIGER_BESTANDSTREND, descriptionElements, taxon, parent);
109
            exportCategoricalData(BfnXmlConstants.VOC_KURZFRISTIGER_BESTANDSTREND, descriptionElements, taxon, parent);
110
            exportCategoricalData(BfnXmlConstants.VOC_RISIKOFAKTOREN, descriptionElements, taxon, parent);
111
            exportCategoricalData(BfnXmlConstants.VOC_SONDERFAELLE, descriptionElements, taxon, parent);
112
            exportTextData(BfnXmlConstants.FEAT_LETZTER_NACHWEIS, descriptionElements, taxon, parent);
113
            exportCategoricalData(BfnXmlConstants.VOC_VERANTWORTLICHKEIT, descriptionElements, taxon, parent);
114
            exportTextData(BfnXmlConstants.FEAT_KOMMENTAR_TAXONOMIE, descriptionElements, taxon, parent);
115
            exportTextData(BfnXmlConstants.FEAT_KOMMENTAR_GEFAEHRDUNG, descriptionElements, taxon, parent);
116
            exportTextData(BfnXmlConstants.FEAT_WEITERE_KOMMENTARE, descriptionElements, taxon, parent);
117
            exportCategoricalData(BfnXmlConstants.VOC_ALTE_RL_KAT, descriptionElements, taxon, parent);
118

    
119

    
120
//            for (DescriptionElementBase descriptionElementBase : descriptionElements) {
121
//                if(descriptionElementBase.isInstanceOf(CategoricalData.class)){
122
//                    CategoricalData categoricalData = HibernateProxyHelper.deproxy(descriptionElementBase, CategoricalData.class);
123
//                    Feature feature = categoricalData.getFeature();
124
//                    List<StateData> stateData = categoricalData.getStateData();
125
//                    if(stateData.size()!=1){
126
//                        logger.error("StateData does not have a size of 1 for feature "+feature.getLabel()+" in taxon "+taxon.getTitleCache());
127
//                        continue;
128
//                    }
129
//                    addIwert(bezugsraum, feature.getLabel(), stateData.iterator().next().getState().getLabel());
130
//                }
131
//                else if(descriptionElementBase.isInstanceOf(TextData.class)){
132
//                    TextData textData = HibernateProxyHelper.deproxy(descriptionElementBase, TextData.class);
133
//                    addIwert(bezugsraum, textData.getFeature().getLabel(), textData.getLanguageText(Language.GERMAN()).getText());
134
//                }
135
//            }
136
        }
137
    }
138

    
139
    private void exportTextData(String featureLabel, Set<DescriptionElementBase> descriptionElements, Taxon taxon, Element parent){
140
        for (DescriptionElementBase descriptionElementBase : descriptionElements) {
141
            if(descriptionElementBase.getFeature().getLabel().equals(featureLabel)){
142
              TextData textData = HibernateProxyHelper.deproxy(descriptionElementBase, TextData.class);
143
              addIwert(parent, textData.getFeature().getLabel(), textData.getLanguageText(Language.GERMAN()).getText());
144
            }
145
        }
146
    }
147

    
148
    private void exportCategoricalData(String featureLabel, Set<DescriptionElementBase> descriptionElements, Taxon taxon, Element parent){
149
        for (DescriptionElementBase descriptionElementBase : descriptionElements) {
150
            if(descriptionElementBase.getFeature().getLabel().equals(featureLabel)){
151
                CategoricalData categoricalData = HibernateProxyHelper.deproxy(descriptionElementBase, CategoricalData.class);
152
                Feature feature = categoricalData.getFeature();
153
                List<StateData> stateData = categoricalData.getStateData();
154
                if(stateData.size()!=1){
155
                    logger.error("StateData does not have a size of 1 for feature "+feature.getLabel()+" in taxon "+taxon.getTitleCache());
156
                    continue;
157
                }
158
                addIwert(parent, feature.getLabel(), stateData.iterator().next().getState().getLabel());
159
            }
160
        }
161
    }
162

    
163
    private void exportFeatures(Element roteListeDaten) {
164
        Element eigenschaften = new Element(BfnXmlConstants.EL_EIGENSCHAFTEN);
165
        roteListeDaten.addContent(eigenschaften);
166
        TermVocabulary<Feature> redListFeaturesVoc = getVocabularyService().load(BfnXmlConstants.vocRLFeatures);
167
        Set<Feature> terms = redListFeaturesVoc.getTerms();
168
        for (Feature feature : terms) {
169
            //export red list features
170
            Element eigenschaft = new Element(BfnXmlConstants.EL_EIGENSCHAFT);
171
            eigenschaft.setAttribute(new Attribute(BfnXmlConstants.ATT_STANDARDNAME, feature.getLabel()));
172
            eigenschaften.addContent(eigenschaft);
173
            if(feature.isSupportsCategoricalData()){
174
                //export feature states
175
                Element listenwerte = new Element(BfnXmlConstants.EL_LISTENWERTE);
176
                eigenschaft.addContent(listenwerte);
177
                Set<TermVocabulary<State>> supportedCategoricalEnumerations = feature.getSupportedCategoricalEnumerations();
178
                for (TermVocabulary<State> termVocabulary : supportedCategoricalEnumerations) {
179
                    Set<State> featureStates = termVocabulary.getTerms();
180
                    //                    int reihenfolge = 1;
181
                    for (State featureState : featureStates) {
182
                        Element lwert = new Element(BfnXmlConstants.EL_LWERT);
183
                        //                        lwert.setAttribute(new Attribute(BfnXmlConstants.ATT_REIHENFOLGE, String.valueOf(reihenfolge)));
184
                        lwert.addContent(featureState.getLabel());
185
                        listenwerte.addContent(lwert);
186

    
187
                        //                        reihenfolge++;
188
                    }
189
                }
190
            }
191
        }
192
    }
193

    
194
    private void exportTaxon(Taxon taxon, Element parent, BfnXmlExportState state) {
195
        Element taxonym = new Element(BfnXmlConstants.EL_TAXONYM);
196
        parent.addContent(taxonym);
197

    
198
        //reihenfolge attribute
199
        taxonym.setAttribute(BfnXmlConstants.ATT_REIHENFOLGE, getIdentifier(taxon, BfnXmlConstants.UUID_REIHENFOLGE_IDENTIFIER_TYPE));
200

    
201
        //taxNr attribute
202
        taxonym.setAttribute(BfnXmlConstants.ATT_TAXNR, getIdentifier(taxon, BfnXmlConstants.UUID_TAX_NR_IDENTIFIER_TYPE));
203

    
204

    
205
        exportWissName(taxon, taxonym);
206

    
207
        //synonyms
208
        Set<Synonym> synonyms = taxon.getSynonyms();
209
        if(synonyms.size()>0){
210
            Element synonymeElement = new Element(BfnXmlConstants.EL_SYNONYME);
211
            taxonym.addContent(synonymeElement);
212
            for (Synonym synonym : synonyms) {
213
                Element synonymElement = new Element(BfnXmlConstants.EL_SYNONYM);
214
                synonymeElement.addContent(synonymElement);
215
                exportWissName(synonym, synonymElement);
216
            }
217
        }
218

    
219
        //common name
220
        exportCommonName(taxon, taxonym);
221

    
222
        //factual data
223
        exportFactualData(taxon, taxonym);
224

    
225

    
226
    }
227

    
228
    private String getIdentifier(Taxon taxon, UUID identifierUuid) {
229
        DefinedTerm identifierType = HibernateProxyHelper.deproxy(getTermService().load(identifierUuid), DefinedTerm.class);
230
        Set<String> identfiers = taxon.getIdentifiers(identifierType);
231
        if(identfiers.size()==1){
232
            return identfiers.iterator().next();
233
        }
234
        else{
235
            logger.error("Taxon "+taxon.getTitleCache()+" has none or multiple identifiers of type '"+identifierType.getLabel()+"'");
236
            return null;
237
        }
238
    }
239

    
240
    private void exportWissName(TaxonBase<?> taxon, Element parent) {
241
        Element wissName = new Element(BfnXmlConstants.EL_WISSNAME);
242
        parent.addContent(wissName);
243

    
244
        NonViralName<?> name = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
245
        Rank rank = name.getRank();
246
        //epithet 1,2,3
247
        exportEpithet(taxon, wissName, name, rank);
248

    
249
        //rank
250
        addNanteil(wissName, BfnXmlConstants.BEREICH_RANG, BfnXmlTransformer.getRankCodeForRank(rank));
251

    
252

    
253
        addNanteil(wissName, BfnXmlConstants.BEREICH_ORDNUNGSZAHL, null);//TODO
254
        addNanteil(wissName, BfnXmlConstants.BEREICH_AUTONYM, null);//TODO
255
        addNanteil(wissName, BfnXmlConstants.BEREICH_REICH, null);//TODO
256
        addNanteil(wissName, BfnXmlConstants.BEREICH_BASTARD, null);//TODO
257

    
258
        //authors
259
        addNanteil(wissName, BfnXmlConstants.BEREICH_AUTOREN, name.getAuthorshipCache());
260

    
261
        addNanteil(wissName, BfnXmlConstants.BEREICH_ZUSAETZE, null);//TODO
262
        addNanteil(wissName, "SortWissName", null);//TODO
263
        addNanteil(wissName, "SortArtEpi", null);//TODO
264
        addNanteil(wissName, "SortDeutName", null);//TODO
265

    
266
        //wissName
267
        addNanteil(wissName, BfnXmlConstants.BEREICH_WISSNAME, name.getTitleCache());
268
    }
269

    
270
    private void exportEpithet(TaxonBase<?> taxon, Element wissName, NonViralName<?> name, Rank rank) {
271
        //eindeutiger Code
272
        Set<IdentifiableSource> sources = taxon.getSources();
273
        for (IdentifiableSource identifiableSource : sources) {
274
            if(identifiableSource.getType().equals(OriginalSourceType.Import)
275
                    && identifiableSource.getIdNamespace().equals(BfnXmlConstants.EL_TAXONYM+":"
276
                            +BfnXmlConstants.EL_WISSNAME+":"+BfnXmlConstants.EL_NANTEIL+":"+BfnXmlConstants.BEREICH_EINDEUTIGER_CODE)){
277
                addNanteil(wissName, BfnXmlConstants.BEREICH_EINDEUTIGER_CODE, identifiableSource.getIdInSource());
278
            }
279
        }
280

    
281
        //epitheton1-2
282
        addNanteil(wissName, BfnXmlConstants.BEREICH_EPITHETON1, name.getGenusOrUninomial());
283
        if(rank.isLower(Rank.GENUS())){
284
            String epitheton2 = name.getInfraGenericEpithet();
285
            if(epitheton2==null){
286
                epitheton2 = name.getSpecificEpithet();
287
            }
288
            addNanteil(wissName, BfnXmlConstants.BEREICH_EPITHETON2, epitheton2);
289
        }
290
        //epitheton3
291
        String epitheton3 = null;
292
        if(rank.isLower(Rank.SPECIES())){
293
            epitheton3 = name.getInfraSpecificEpithet();
294
        }
295
        if(epitheton3==null){
296
            epitheton3 = name.getSpecificEpithet();
297
        }
298
        addNanteil(wissName, BfnXmlConstants.BEREICH_EPITHETON3, epitheton3);
299

    
300
        //epitheton4-5
301
        addNanteil(wissName, BfnXmlConstants.BEREICH_EPITHETON4, null);
302
        addNanteil(wissName, BfnXmlConstants.BEREICH_EPITHETON5, null);
303
    }
304

    
305
    private void exportCommonName(Taxon taxon, Element taxonym) {
306
        Element deutscheNamen = new Element(BfnXmlConstants.EL_DEUTSCHENAMEN);
307
        taxonym.addContent(deutscheNamen);
308

    
309
        int sequenz = 1;
310
        Set<TaxonDescription> descriptions = taxon.getDescriptions();
311
        for (TaxonDescription taxonDescription : descriptions) {
312
            Set<DescriptionElementBase> elements = taxonDescription.getElements();
313
            for (DescriptionElementBase descriptionElementBase : elements) {
314
                if(descriptionElementBase.isInstanceOf(CommonTaxonName.class)){
315
                    CommonTaxonName commonName = HibernateProxyHelper.deproxy(descriptionElementBase, CommonTaxonName.class);
316
                    if(commonName.getLanguage().equals(Language.GERMAN())){
317
                        Element dName = new Element(BfnXmlConstants.EL_DNAME);
318
                        Element trivialName = new Element(BfnXmlConstants.EL_TRIVIALNAME);
319
                        deutscheNamen.addContent(dName);
320
                        dName.addContent(trivialName);
321

    
322
                        dName.setAttribute(new Attribute(BfnXmlConstants.ATT_SEQUENZ, String.valueOf(sequenz)));
323
                        trivialName.addContent(commonName.getName());
324
                    }
325
                }
326
            }
327
            sequenz++;
328
        }
329
    }
330

    
331
    private void addNanteil(Element element, String bereich, String textContent) {
332
        Element nanteil = new Element(BfnXmlConstants.EL_NANTEIL);
333
        nanteil.setAttribute(new Attribute(BfnXmlConstants.ATT_BEREICH, bereich));
334
        if(textContent!=null){
335
            nanteil.addContent(textContent);
336
        }
337
        element.addContent(nanteil);
338
    }
339

    
340
    @Override
341
    protected boolean doCheck(BfnXmlExportState state) {
342
        return false;
343
    }
344

    
345
    @Override
346
    protected boolean isIgnore(BfnXmlExportState state) {
347
        return false;
348
    }
349

    
350
    private final class TaxonComparator implements Comparator<TaxonNode> {
351
        @Override
352
        public int compare(TaxonNode o1, TaxonNode o2) {
353
            Taxon taxon1 = o1.getTaxon();
354
            Taxon taxon2 = o2.getTaxon();
355

    
356
            int reihenfolge1 = Integer.parseInt(getIdentifier(taxon1, BfnXmlConstants.UUID_REIHENFOLGE_IDENTIFIER_TYPE));
357
            int reihenfolge2 = Integer.parseInt(getIdentifier(taxon2, BfnXmlConstants.UUID_REIHENFOLGE_IDENTIFIER_TYPE));
358

    
359
            return reihenfolge1-reihenfolge2;
360
        }
361
    }
362

    
363
}
(6-6/6)