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.
9 package eu
.etaxonomy
.cdm
.io
.redlist
.bfnXml
.out
;
11 import java
.util
.Comparator
;
12 import java
.util
.List
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.jdom2
.Attribute
;
17 import org
.jdom2
.Document
;
18 import org
.jdom2
.Element
;
19 import org
.springframework
.stereotype
.Component
;
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
.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
.name
.TaxonNameBase
;
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
;
54 public class BfnXmlTaxonNameExport
extends BfnXmlExportBase
<TaxonNameBase
> {
56 private static final long serialVersionUID
= -931703660108981011L;
58 private static final Logger logger
= Logger
.getLogger(BfnXmlTaxonNameExport
.class);
60 public BfnXmlTaxonNameExport(){
65 protected void doInvoke(BfnXmlExportState state
){
66 startTransaction(true);
68 Document document
= state
.getConfig().getDocument();
70 //get all classifications
71 List
<Classification
> classifications
= getClassificationService().list(Classification
.class, null, null, null, null);
72 for (Classification classification
: classifications
) {
73 Element roteListeDaten
= new Element(BfnXmlConstants
.EL_ROTELISTEDATEN
);
74 roteListeDaten
.setAttribute(new Attribute(BfnXmlConstants
.ATT_INHALT
, classification
.getTitleCache()));
75 document
.getRootElement().addContent(roteListeDaten
);
77 exportFeatures(roteListeDaten
);
79 exportTaxonomy(classification
, roteListeDaten
);
85 private void exportTaxonomy(Classification classification
, Element roteListeDaten
) {
86 Element taxonyme
= new Element(BfnXmlConstants
.EL_TAXONYME
);
87 roteListeDaten
.addContent(taxonyme
);
88 List
<TaxonNode
> childNodes
= classification
.getChildNodes();
89 java
.util
.Collections
.sort(childNodes
, new Comparator
<TaxonNode
>() {
92 public int compare(TaxonNode o1
, TaxonNode o2
) {
93 Taxon taxon1
= o1
.getTaxon();
94 Taxon taxon2
= o2
.getTaxon();
98 Set
<IdentifiableSource
> sources1
= taxon1
.getSources();
99 for (IdentifiableSource identifiableSource
: sources1
) {
100 if(identifiableSource
.getType().equals(OriginalSourceType
.Import
)
101 && identifiableSource
.getIdNamespace().equals(BfnXmlConstants
.EL_TAXONYM
+":"
102 +BfnXmlConstants
.EL_WISSNAME
+":"+BfnXmlConstants
.EL_NANTEIL
+":"+BfnXmlConstants
.BEREICH_EINDEUTIGER_CODE
)){
103 id1
= Integer
.parseInt(identifiableSource
.getIdInSource());
106 Set
<IdentifiableSource
> sources2
= taxon2
.getSources();
107 for (IdentifiableSource identifiableSource
: sources2
) {
108 if(identifiableSource
.getType().equals(OriginalSourceType
.Import
)
109 && identifiableSource
.getIdNamespace().equals(BfnXmlConstants
.EL_TAXONYM
+":"
110 +BfnXmlConstants
.EL_WISSNAME
+":"+BfnXmlConstants
.EL_NANTEIL
+":"+BfnXmlConstants
.BEREICH_EINDEUTIGER_CODE
)){
111 id2
= Integer
.parseInt(identifiableSource
.getIdInSource());
117 for (TaxonNode taxonNode
: childNodes
) {
118 exportTaxon(taxonNode
.getTaxon(), taxonyme
);
122 private void exportFactualData(Taxon taxon
, Element parent
) {
123 Element informationen
= new Element(BfnXmlConstants
.EL_INFORMATIONEN
);
124 parent
.addContent(informationen
);
125 Element bezugsraum
= new Element(BfnXmlConstants
.EL_BEZUGSRAUM
);
126 bezugsraum
.setAttribute(new Attribute(BfnXmlConstants
.ATT_NAME
, BfnXmlConstants
.BEZUGRAUM_BUND
));
127 informationen
.addContent(bezugsraum
);
129 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
130 for (TaxonDescription taxonDescription
: descriptions
) {
131 //TODO: export only red list features ??
132 Set
<DescriptionElementBase
> descriptionElements
= taxonDescription
.getElements();
133 for (DescriptionElementBase descriptionElementBase
: descriptionElements
) {
134 if(descriptionElementBase
.isInstanceOf(CategoricalData
.class)){
135 CategoricalData categoricalData
= HibernateProxyHelper
.deproxy(descriptionElementBase
, CategoricalData
.class);
136 Feature feature
= categoricalData
.getFeature();
137 List
<StateData
> stateData
= categoricalData
.getStateData();
138 if(stateData
.size()!=1){
139 logger
.error("StateData does not have a size of 1 for feature "+feature
.getLabel()+" in taxon "+((NonViralName
<?
>) taxon
.getName()).getNameCache());
142 exportIwert(bezugsraum
, feature
, stateData
.iterator().next().getState().getLabel());
144 else if(descriptionElementBase
.isInstanceOf(TextData
.class)){
145 TextData textData
= HibernateProxyHelper
.deproxy(descriptionElementBase
, TextData
.class);
146 exportIwert(bezugsraum
, textData
.getFeature(), textData
.getText(Language
.getDefaultLanguage()));
152 private void exportIwert(Element parent
, Feature feature
, String wertString
) {
153 Element iwert
= new Element(BfnXmlConstants
.EL_IWERT
);
154 iwert
.setAttribute(new Attribute(BfnXmlConstants
.ATT_STANDARDNAME
, feature
.getLabel()));
155 parent
.addContent(iwert
);
157 Element wert
= new Element(BfnXmlConstants
.EL_WERT
);
158 wert
.addContent(wertString
);
159 iwert
.addContent(wert
);
162 private void exportFeatures(Element roteListeDaten
) {
163 Element eigenschaften
= new Element(BfnXmlConstants
.EL_EIGENSCHAFTEN
);
164 roteListeDaten
.addContent(eigenschaften
);
165 TermVocabulary
<Feature
> redListFeaturesVoc
= getVocabularyService().load(BfnXmlConstants
.vocRLFeatures
);
166 Set
<Feature
> terms
= redListFeaturesVoc
.getTerms();
167 for (Feature feature
: terms
) {
168 //export red list features
169 Element eigenschaft
= new Element(BfnXmlConstants
.EL_EIGENSCHAFT
);
170 eigenschaft
.setAttribute(new Attribute(BfnXmlConstants
.ATT_STANDARDNAME
, feature
.getLabel()));
171 eigenschaften
.addContent(eigenschaft
);
172 //export feature states
173 Element listenwerte
= new Element(BfnXmlConstants
.EL_LISTENWERTE
);
174 eigenschaften
.addContent(listenwerte
);
175 Set
<TermVocabulary
<State
>> supportedCategoricalEnumerations
= feature
.getSupportedCategoricalEnumerations();
176 for (TermVocabulary
<State
> termVocabulary
: supportedCategoricalEnumerations
) {
177 Set
<State
> featureStates
= termVocabulary
.getTerms();
178 // int reihenfolge = 1;
179 for (State featureState
: featureStates
) {
180 Element lwert
= new Element(BfnXmlConstants
.EL_LWERT
);
181 // lwert.setAttribute(new Attribute(BfnXmlConstants.ATT_REIHENFOLGE, String.valueOf(reihenfolge)));
182 lwert
.addContent(featureState
.getLabel());
183 listenwerte
.addContent(lwert
);
191 private void exportTaxon(Taxon taxon
, Element parent
) {
192 Element taxonym
= new Element(BfnXmlConstants
.EL_TAXONYM
);
193 parent
.addContent(taxonym
);
195 exportWissName(taxon
, taxonym
);
198 Set
<Synonym
> synonyms
= taxon
.getSynonyms();
199 if(synonyms
.size()>0){
200 Element synonymeElement
= new Element(BfnXmlConstants
.EL_SYNONYME
);
201 taxonym
.addContent(synonymeElement
);
202 for (Synonym synonym
: synonyms
) {
203 Element synonymElement
= new Element(BfnXmlConstants
.EL_SYNONYM
);
204 synonymeElement
.addContent(synonymElement
);
205 exportWissName(synonym
, synonymElement
);
210 exportCommonName(taxon
, taxonym
);
213 exportFactualData(taxon
, taxonym
);
218 private void exportWissName(TaxonBase
<?
> taxon
, Element parent
) {
219 Element wissName
= new Element(BfnXmlConstants
.EL_WISSNAME
);
220 parent
.addContent(wissName
);
222 NonViralName
<?
> name
= HibernateProxyHelper
.deproxy(taxon
.getName(), NonViralName
.class);
223 Rank rank
= name
.getRank();
225 addNanteil(wissName
, BfnXmlConstants
.BEREICH_WISSNAME
, name
.getTitleCache());
227 exportEpithet(taxon
, wissName
, name
, rank
);
230 addNanteil(wissName
, BfnXmlConstants
.BEREICH_RANG
, BfnXmlTransformer
.getRankmap().get(rank
));
233 addNanteil(wissName
, BfnXmlConstants
.BEREICH_AUTOREN
, name
.getAuthorshipCache());
236 private void exportEpithet(TaxonBase
<?
> taxon
, Element wissName
, NonViralName
<?
> name
, Rank rank
) {
237 addNanteil(wissName
, BfnXmlConstants
.BEREICH_EPITHETON1
, name
.getGenusOrUninomial());
238 if(rank
.isLower(Rank
.GENUS())){
239 String epitheton2
= name
.getInfraGenericEpithet();
240 if(epitheton2
==null){
241 epitheton2
= name
.getSpecificEpithet();
243 addNanteil(wissName
, BfnXmlConstants
.BEREICH_EPITHETON2
, epitheton2
);
245 if(rank
.isLower(Rank
.SPECIES())){
246 String epitheton3
= name
.getInfraSpecificEpithet();
247 if(epitheton3
==null){
248 epitheton3
= name
.getSpecificEpithet();
250 addNanteil(wissName
, BfnXmlConstants
.BEREICH_EPITHETON3
, epitheton3
);
252 Set
<IdentifiableSource
> sources
= taxon
.getSources();
253 for (IdentifiableSource identifiableSource
: sources
) {
254 if(identifiableSource
.getType().equals(OriginalSourceType
.Import
)
255 && identifiableSource
.getIdNamespace().equals(BfnXmlConstants
.EL_TAXONYM
+":"
256 +BfnXmlConstants
.EL_WISSNAME
+":"+BfnXmlConstants
.EL_NANTEIL
+":"+BfnXmlConstants
.BEREICH_EINDEUTIGER_CODE
)){
257 addNanteil(wissName
, BfnXmlConstants
.BEREICH_EINDEUTIGER_CODE
, identifiableSource
.getIdInSource());
262 private void exportCommonName(Taxon taxon
, Element taxonym
) {
263 Element deutscheNamen
= new Element(BfnXmlConstants
.EL_DEUTSCHENAMEN
);
264 taxonym
.addContent(deutscheNamen
);
267 Set
<TaxonDescription
> descriptions
= taxon
.getDescriptions();
268 for (TaxonDescription taxonDescription
: descriptions
) {
269 Set
<DescriptionElementBase
> elements
= taxonDescription
.getElements();
270 for (DescriptionElementBase descriptionElementBase
: elements
) {
271 if(descriptionElementBase
.isInstanceOf(CommonTaxonName
.class)){
272 CommonTaxonName commonName
= HibernateProxyHelper
.deproxy(descriptionElementBase
, CommonTaxonName
.class);
273 if(commonName
.getLanguage().equals(Language
.GERMAN())){
274 Element dName
= new Element(BfnXmlConstants
.EL_DNAME
);
275 Element trivialName
= new Element(BfnXmlConstants
.EL_TRIVIALNAME
);
276 deutscheNamen
.addContent(dName
);
277 dName
.addContent(trivialName
);
279 dName
.setAttribute(new Attribute(BfnXmlConstants
.ATT_SEQUENZ
, String
.valueOf(sequenz
)));
280 trivialName
.addContent(commonName
.getName());
288 private void addNanteil(Element element
, String bereich
, String textContent
) {
289 Element nanteil
= new Element(BfnXmlConstants
.EL_NANTEIL
);
290 nanteil
.setAttribute(new Attribute(BfnXmlConstants
.ATT_BEREICH
, bereich
));
291 nanteil
.addContent(textContent
);
292 element
.addContent(nanteil
);
296 protected boolean doCheck(BfnXmlExportState state
) {
301 protected boolean isIgnore(BfnXmlExportState state
) {