Project

General

Profile

Download (13.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
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

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

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

    
44

    
45

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

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

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

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

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

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

    
74
            exportFeatures(roteListeDaten);
75

    
76
            exportTaxonomy(classification, roteListeDaten, state);
77

    
78
        }
79

    
80
	}
81

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

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

    
99
        Set<TaxonDescription> descriptions = taxon.getDescriptions();
100
        for (TaxonDescription taxonDescription : descriptions) {
101
            //TODO: export only red list features ??
102
            Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
103
            for (DescriptionElementBase descriptionElementBase : descriptionElements) {
104
                if(descriptionElementBase.isInstanceOf(CategoricalData.class)){
105
                    CategoricalData categoricalData = HibernateProxyHelper.deproxy(descriptionElementBase, CategoricalData.class);
106
                    Feature feature = categoricalData.getFeature();
107
                    List<StateData> stateData = categoricalData.getStateData();
108
                    if(stateData.size()!=1){
109
                        logger.error("StateData does not have a size of 1 for feature "+feature.getLabel()+" in taxon "+taxon.getTitleCache());
110
                        continue;
111
                    }
112
                    addIwert(bezugsraum, feature.getLabel(), stateData.iterator().next().getState().getLabel());
113
                }
114
                else if(descriptionElementBase.isInstanceOf(TextData.class)){
115
                    TextData textData = HibernateProxyHelper.deproxy(descriptionElementBase, TextData.class);
116
                    addIwert(bezugsraum, textData.getFeature().getLabel(), textData.getLanguageText(Language.GERMAN()).getText());
117
                }
118
            }
119
        }
120
    }
121

    
122
    private void exportFeatures(Element roteListeDaten) {
123
        Element eigenschaften = new Element(BfnXmlConstants.EL_EIGENSCHAFTEN);
124
        roteListeDaten.addContent(eigenschaften);
125
        TermVocabulary<Feature> redListFeaturesVoc = getVocabularyService().load(BfnXmlConstants.vocRLFeatures);
126
        Set<Feature> terms = redListFeaturesVoc.getTerms();
127
        for (Feature feature : terms) {
128
            //export red list features
129
            Element eigenschaft = new Element(BfnXmlConstants.EL_EIGENSCHAFT);
130
            eigenschaft.setAttribute(new Attribute(BfnXmlConstants.ATT_STANDARDNAME, feature.getLabel()));
131
            eigenschaften.addContent(eigenschaft);
132
            if(feature.isSupportsCategoricalData()){
133
                //export feature states
134
                Element listenwerte = new Element(BfnXmlConstants.EL_LISTENWERTE);
135
                eigenschaft.addContent(listenwerte);
136
                Set<TermVocabulary<State>> supportedCategoricalEnumerations = feature.getSupportedCategoricalEnumerations();
137
                for (TermVocabulary<State> termVocabulary : supportedCategoricalEnumerations) {
138
                    Set<State> featureStates = termVocabulary.getTerms();
139
                    //                    int reihenfolge = 1;
140
                    for (State featureState : featureStates) {
141
                        Element lwert = new Element(BfnXmlConstants.EL_LWERT);
142
                        //                        lwert.setAttribute(new Attribute(BfnXmlConstants.ATT_REIHENFOLGE, String.valueOf(reihenfolge)));
143
                        lwert.addContent(featureState.getLabel());
144
                        listenwerte.addContent(lwert);
145

    
146
                        //                        reihenfolge++;
147
                    }
148
                }
149
            }
150
        }
151
    }
152

    
153
    private void exportTaxon(Taxon taxon, Element parent, BfnXmlExportState state) {
154
        Element taxonym = new Element(BfnXmlConstants.EL_TAXONYM);
155
        parent.addContent(taxonym);
156

    
157
        DefinedTerm taxNrIdentifierType = HibernateProxyHelper.deproxy(getTermService().load(BfnXmlConstants.UUID_TAX_NR_IDENTIFIER_TYPE), DefinedTerm.class);
158
        Set<String> identifiers = taxon.getIdentifiers(taxNrIdentifierType);
159
        if(identifiers.size()==1){
160
            String taxNr = identifiers.iterator().next();
161
            taxonym.setAttribute(BfnXmlConstants.ATT_TAXNR, taxNr);
162
        }
163
        else{
164
            logger.error("Taxon "+taxon.getTitleCache()+" has none or multiple identifiers of type 'taxNr'");
165
        }
166

    
167
        exportWissName(taxon, taxonym);
168

    
169
        //synonyms
170
        Set<Synonym> synonyms = taxon.getSynonyms();
171
        if(synonyms.size()>0){
172
            Element synonymeElement = new Element(BfnXmlConstants.EL_SYNONYME);
173
            taxonym.addContent(synonymeElement);
174
            for (Synonym synonym : synonyms) {
175
                Element synonymElement = new Element(BfnXmlConstants.EL_SYNONYM);
176
                synonymeElement.addContent(synonymElement);
177
                exportWissName(synonym, synonymElement);
178
            }
179
        }
180

    
181
        //common name
182
        exportCommonName(taxon, taxonym);
183

    
184
        //factual data
185
        exportFactualData(taxon, taxonym);
186

    
187

    
188
    }
189

    
190
    private void exportWissName(TaxonBase<?> taxon, Element parent) {
191
        Element wissName = new Element(BfnXmlConstants.EL_WISSNAME);
192
        parent.addContent(wissName);
193

    
194
        NonViralName<?> name = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
195
        Rank rank = name.getRank();
196
        //wissName
197
        addNanteil(wissName, BfnXmlConstants.BEREICH_WISSNAME, name.getTitleCache());
198
        //epithet 1,2,3
199
        exportEpithet(taxon, wissName, name, rank);
200

    
201
        //rank
202
        addNanteil(wissName, BfnXmlConstants.BEREICH_RANG, BfnXmlTransformer.getRankCodeForRank(rank));
203

    
204
        //authors
205
        addNanteil(wissName, BfnXmlConstants.BEREICH_AUTOREN, name.getAuthorshipCache());
206
    }
207

    
208
    private void exportEpithet(TaxonBase<?> taxon, Element wissName, NonViralName<?> name, Rank rank) {
209
        addNanteil(wissName, BfnXmlConstants.BEREICH_EPITHETON1, name.getGenusOrUninomial());
210
        if(rank.isLower(Rank.GENUS())){
211
            String epitheton2 = name.getInfraGenericEpithet();
212
            if(epitheton2==null){
213
                epitheton2 = name.getSpecificEpithet();
214
            }
215
            addNanteil(wissName, BfnXmlConstants.BEREICH_EPITHETON2, epitheton2);
216
        }
217
        if(rank.isLower(Rank.SPECIES())){
218
            String epitheton3 = name.getInfraSpecificEpithet();
219
            if(epitheton3==null){
220
                epitheton3 = name.getSpecificEpithet();
221
            }
222
            addNanteil(wissName, BfnXmlConstants.BEREICH_EPITHETON3, epitheton3);
223
        }
224
        Set<IdentifiableSource> sources = taxon.getSources();
225
        for (IdentifiableSource identifiableSource : sources) {
226
            if(identifiableSource.getType().equals(OriginalSourceType.Import)
227
                    && identifiableSource.getIdNamespace().equals(BfnXmlConstants.EL_TAXONYM+":"
228
            +BfnXmlConstants.EL_WISSNAME+":"+BfnXmlConstants.EL_NANTEIL+":"+BfnXmlConstants.BEREICH_EINDEUTIGER_CODE)){
229
                addNanteil(wissName, BfnXmlConstants.BEREICH_EINDEUTIGER_CODE, identifiableSource.getIdInSource());
230
            }
231
        }
232
    }
233

    
234
    private void exportCommonName(Taxon taxon, Element taxonym) {
235
        Element deutscheNamen = new Element(BfnXmlConstants.EL_DEUTSCHENAMEN);
236
        taxonym.addContent(deutscheNamen);
237

    
238
        int sequenz = 1;
239
        Set<TaxonDescription> descriptions = taxon.getDescriptions();
240
        for (TaxonDescription taxonDescription : descriptions) {
241
            Set<DescriptionElementBase> elements = taxonDescription.getElements();
242
            for (DescriptionElementBase descriptionElementBase : elements) {
243
                if(descriptionElementBase.isInstanceOf(CommonTaxonName.class)){
244
                    CommonTaxonName commonName = HibernateProxyHelper.deproxy(descriptionElementBase, CommonTaxonName.class);
245
                    if(commonName.getLanguage().equals(Language.GERMAN())){
246
                        Element dName = new Element(BfnXmlConstants.EL_DNAME);
247
                        Element trivialName = new Element(BfnXmlConstants.EL_TRIVIALNAME);
248
                        deutscheNamen.addContent(dName);
249
                        dName.addContent(trivialName);
250

    
251
                        dName.setAttribute(new Attribute(BfnXmlConstants.ATT_SEQUENZ, String.valueOf(sequenz)));
252
                        trivialName.addContent(commonName.getName());
253
                    }
254
                }
255
            }
256
            sequenz++;
257
        }
258
    }
259

    
260
    private void addNanteil(Element element, String bereich, String textContent) {
261
        Element nanteil = new Element(BfnXmlConstants.EL_NANTEIL);
262
        nanteil.setAttribute(new Attribute(BfnXmlConstants.ATT_BEREICH, bereich));
263
        nanteil.addContent(textContent);
264
        element.addContent(nanteil);
265
    }
266

    
267
    @Override
268
    protected boolean doCheck(BfnXmlExportState state) {
269
        return false;
270
    }
271

    
272
    @Override
273
    protected boolean isIgnore(BfnXmlExportState state) {
274
        return false;
275
    }
276

    
277
    private final class OriginalSourceComparator implements Comparator<TaxonNode> {
278
        @Override
279
        public int compare(TaxonNode o1, TaxonNode o2) {
280
            Taxon taxon1 = o1.getTaxon();
281
            Taxon taxon2 = o2.getTaxon();
282
            int id1 = 0;
283
            int id2 = 0;
284

    
285
            Set<IdentifiableSource> sources1 = taxon1.getSources();
286
            for (IdentifiableSource identifiableSource : sources1) {
287
                if(identifiableSource.getType().equals(OriginalSourceType.Import)
288
                        && identifiableSource.getIdNamespace().equals(BfnXmlConstants.EL_TAXONYM+":"
289
                +BfnXmlConstants.EL_WISSNAME+":"+BfnXmlConstants.EL_NANTEIL+":"+BfnXmlConstants.BEREICH_EINDEUTIGER_CODE)){
290
                    id1 = Integer.parseInt(identifiableSource.getIdInSource());
291
                }
292
            }
293
            Set<IdentifiableSource> sources2 = taxon2.getSources();
294
            for (IdentifiableSource identifiableSource : sources2) {
295
                if(identifiableSource.getType().equals(OriginalSourceType.Import)
296
                        && identifiableSource.getIdNamespace().equals(BfnXmlConstants.EL_TAXONYM+":"
297
                                +BfnXmlConstants.EL_WISSNAME+":"+BfnXmlConstants.EL_NANTEIL+":"+BfnXmlConstants.BEREICH_EINDEUTIGER_CODE)){
298
                    id2 = Integer.parseInt(identifiableSource.getIdInSource());
299
                }
300
            }
301
            return id1-id2;
302
        }
303
    }
304

    
305
}
(6-6/6)