8 |
8 |
*/
|
9 |
9 |
package eu.etaxonomy.cdm.io.redlist.bfnXml.out;
|
10 |
10 |
|
11 |
|
import java.io.FileWriter;
|
12 |
|
import java.io.IOException;
|
13 |
11 |
import java.util.Comparator;
|
14 |
12 |
import java.util.List;
|
15 |
13 |
import java.util.Set;
|
... | ... | |
18 |
16 |
import org.jdom2.Attribute;
|
19 |
17 |
import org.jdom2.Document;
|
20 |
18 |
import org.jdom2.Element;
|
21 |
|
import org.jdom2.output.Format;
|
22 |
|
import org.jdom2.output.XMLOutputter;
|
23 |
19 |
import org.springframework.stereotype.Component;
|
24 |
20 |
|
25 |
21 |
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
26 |
22 |
import eu.etaxonomy.cdm.io.redlist.bfnXml.BfnXmlConstants;
|
27 |
23 |
import eu.etaxonomy.cdm.io.redlist.bfnXml.in.BfnXmlTransformer;
|
28 |
24 |
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
|
25 |
import eu.etaxonomy.cdm.model.common.Language;
|
29 |
26 |
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
|
30 |
27 |
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
|
28 |
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
31 |
29 |
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
|
32 |
30 |
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
33 |
31 |
import eu.etaxonomy.cdm.model.description.Feature;
|
34 |
32 |
import eu.etaxonomy.cdm.model.description.State;
|
|
33 |
import eu.etaxonomy.cdm.model.description.StateData;
|
35 |
34 |
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
|
35 |
import eu.etaxonomy.cdm.model.description.TextData;
|
36 |
36 |
import eu.etaxonomy.cdm.model.name.NonViralName;
|
37 |
37 |
import eu.etaxonomy.cdm.model.name.Rank;
|
38 |
38 |
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
39 |
39 |
import eu.etaxonomy.cdm.model.taxon.Classification;
|
|
40 |
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
40 |
41 |
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
|
42 |
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
41 |
43 |
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
42 |
44 |
|
43 |
45 |
|
... | ... | |
65 |
67 |
|
66 |
68 |
Document document = state.getConfig().getDocument();
|
67 |
69 |
|
68 |
|
|
69 |
70 |
//get all classifications
|
70 |
71 |
List<Classification> classifications = getClassificationService().list(Classification.class, null, null, null, null);
|
71 |
72 |
for (Classification classification : classifications) {
|
... | ... | |
73 |
74 |
roteListeDaten.setAttribute(new Attribute(BfnXmlConstants.ATT_INHALT, classification.getTitleCache()));
|
74 |
75 |
document.getRootElement().addContent(roteListeDaten);
|
75 |
76 |
|
76 |
|
//export features
|
77 |
|
Element eigenschaften = new Element(BfnXmlConstants.EL_EIGENSCHAFTEN);
|
78 |
|
roteListeDaten.addContent(eigenschaften);
|
79 |
|
TermVocabulary<Feature> redListFeaturesVoc = getVocabularyService().load(BfnXmlConstants.vocRLFeatures);
|
80 |
|
Set<Feature> terms = redListFeaturesVoc.getTerms();
|
81 |
|
for (Feature feature : terms) {
|
82 |
|
//export red list features
|
83 |
|
Element eigenschaft = new Element(BfnXmlConstants.EL_EIGENSCHAFT);
|
84 |
|
eigenschaft.setAttribute(new Attribute(BfnXmlConstants.ATT_STANDARDNAME, feature.getLabel()));
|
85 |
|
eigenschaften.addContent(eigenschaft);
|
86 |
|
//export feature states
|
87 |
|
Element listenwerte = new Element(BfnXmlConstants.EL_LISTENWERTE);
|
88 |
|
eigenschaften.addContent(listenwerte);
|
89 |
|
Set<TermVocabulary<State>> supportedCategoricalEnumerations = feature.getSupportedCategoricalEnumerations();
|
90 |
|
for (TermVocabulary<State> termVocabulary : supportedCategoricalEnumerations) {
|
91 |
|
Set<State> featureStates = termVocabulary.getTerms();
|
92 |
|
// int reihenfolge = 1;
|
93 |
|
for (State featureState : featureStates) {
|
94 |
|
Element lwert = new Element(BfnXmlConstants.EL_LWERT);
|
95 |
|
// lwert.setAttribute(new Attribute(BfnXmlConstants.ATT_REIHENFOLGE, String.valueOf(reihenfolge)));
|
96 |
|
lwert.addContent(featureState.getLabel());
|
97 |
|
listenwerte.addContent(lwert);
|
|
77 |
exportFeatures(roteListeDaten);
|
98 |
78 |
|
99 |
|
// reihenfolge++;
|
|
79 |
exportTaxonomy(classification, roteListeDaten);
|
|
80 |
|
|
81 |
}
|
|
82 |
|
|
83 |
}
|
|
84 |
|
|
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>() {
|
|
90 |
|
|
91 |
@Override
|
|
92 |
public int compare(TaxonNode o1, TaxonNode o2) {
|
|
93 |
Taxon taxon1 = o1.getTaxon();
|
|
94 |
Taxon taxon2 = o2.getTaxon();
|
|
95 |
int id1 = 0;
|
|
96 |
int id2 = 0;
|
|
97 |
|
|
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());
|
100 |
104 |
}
|
101 |
105 |
}
|
|
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());
|
|
112 |
}
|
|
113 |
}
|
|
114 |
return id1-id2;
|
102 |
115 |
}
|
|
116 |
});
|
|
117 |
for (TaxonNode taxonNode : childNodes) {
|
|
118 |
exportTaxon(taxonNode.getTaxon(), taxonyme);
|
|
119 |
}
|
|
120 |
}
|
103 |
121 |
|
104 |
|
//export taxonomy
|
105 |
|
Element taxonyme = new Element(BfnXmlConstants.EL_TAXONYME);
|
106 |
|
roteListeDaten.addContent(taxonyme);
|
107 |
|
List<TaxonNode> childNodes = classification.getChildNodes();
|
108 |
|
java.util.Collections.sort(childNodes, new Comparator<TaxonNode>() {
|
109 |
|
|
110 |
|
@Override
|
111 |
|
public int compare(TaxonNode o1, TaxonNode o2) {
|
112 |
|
Taxon taxon1 = o1.getTaxon();
|
113 |
|
Taxon taxon2 = o2.getTaxon();
|
114 |
|
int id1 = 0;
|
115 |
|
int id2 = 0;
|
116 |
|
|
117 |
|
Set<IdentifiableSource> sources1 = taxon1.getSources();
|
118 |
|
for (IdentifiableSource identifiableSource : sources1) {
|
119 |
|
if(identifiableSource.getType().equals(OriginalSourceType.Import)
|
120 |
|
&& identifiableSource.getIdNamespace().equals(BfnXmlConstants.EL_TAXONYM+":"
|
121 |
|
+BfnXmlConstants.EL_WISSNAME+":"+BfnXmlConstants.EL_NANTEIL+":"+BfnXmlConstants.BEREICH_EINDEUTIGER_CODE)){
|
122 |
|
id1 = Integer.parseInt(identifiableSource.getIdInSource());
|
123 |
|
}
|
124 |
|
}
|
125 |
|
Set<IdentifiableSource> sources2 = taxon2.getSources();
|
126 |
|
for (IdentifiableSource identifiableSource : sources2) {
|
127 |
|
if(identifiableSource.getType().equals(OriginalSourceType.Import)
|
128 |
|
&& identifiableSource.getIdNamespace().equals(BfnXmlConstants.EL_TAXONYM+":"
|
129 |
|
+BfnXmlConstants.EL_WISSNAME+":"+BfnXmlConstants.EL_NANTEIL+":"+BfnXmlConstants.BEREICH_EINDEUTIGER_CODE)){
|
130 |
|
id2 = Integer.parseInt(identifiableSource.getIdInSource());
|
131 |
|
}
|
|
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);
|
|
128 |
|
|
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());
|
|
140 |
continue;
|
132 |
141 |
}
|
133 |
|
return id1-id2;
|
|
142 |
exportIwert(bezugsraum, feature, stateData.iterator().next().getState().getLabel());
|
|
143 |
}
|
|
144 |
else if(descriptionElementBase.isInstanceOf(TextData.class)){
|
|
145 |
TextData textData = HibernateProxyHelper.deproxy(descriptionElementBase, TextData.class);
|
|
146 |
exportIwert(bezugsraum, textData.getFeature(), textData.getText(Language.getDefaultLanguage()));
|
134 |
147 |
}
|
135 |
|
});
|
136 |
|
for (TaxonNode taxonNode : childNodes) {
|
137 |
|
exportTaxon(taxonNode.getTaxon(), taxonyme);
|
138 |
148 |
}
|
139 |
149 |
}
|
|
150 |
}
|
140 |
151 |
|
141 |
|
XMLOutputter outputter = new XMLOutputter();
|
142 |
|
outputter.setFormat(Format.getPrettyFormat());
|
143 |
|
try {
|
144 |
|
outputter.output(document, new FileWriter(state.getConfig().getDestination()));
|
145 |
|
} catch (IOException e) {
|
146 |
|
logger.error("Error writing file", e);
|
147 |
|
}
|
|
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);
|
148 |
156 |
|
149 |
|
}
|
|
157 |
Element wert = new Element(BfnXmlConstants.EL_WERT);
|
|
158 |
wert.addContent(wertString);
|
|
159 |
iwert.addContent(wert);
|
|
160 |
}
|
|
161 |
|
|
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);
|
|
184 |
|
|
185 |
// reihenfolge++;
|
|
186 |
}
|
|
187 |
}
|
|
188 |
}
|
|
189 |
}
|
150 |
190 |
|
151 |
191 |
private void exportTaxon(Taxon taxon, Element parent) {
|
152 |
192 |
Element taxonym = new Element(BfnXmlConstants.EL_TAXONYM);
|
153 |
193 |
parent.addContent(taxonym);
|
154 |
194 |
|
155 |
|
NonViralName name = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
|
|
195 |
exportWissName(taxon, taxonym);
|
|
196 |
|
|
197 |
//synonyms
|
|
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);
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
//common name
|
|
210 |
exportCommonName(taxon, taxonym);
|
|
211 |
|
|
212 |
//factual data
|
|
213 |
exportFactualData(taxon, taxonym);
|
|
214 |
|
|
215 |
|
|
216 |
}
|
|
217 |
|
|
218 |
private void exportWissName(TaxonBase<?> taxon, Element parent) {
|
|
219 |
Element wissName = new Element(BfnXmlConstants.EL_WISSNAME);
|
|
220 |
parent.addContent(wissName);
|
|
221 |
|
|
222 |
NonViralName<?> name = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
|
156 |
223 |
Rank rank = name.getRank();
|
157 |
224 |
//wissName
|
158 |
|
addNanteil(taxonym, BfnXmlConstants.BEREICH_WISSNAME, taxon.getTitleCache());
|
159 |
|
//epitithon 1,2,3
|
160 |
|
addNanteil(taxonym, BfnXmlConstants.BEREICH_EPITHETON1, name.getGenusOrUninomial());
|
|
225 |
addNanteil(wissName, BfnXmlConstants.BEREICH_WISSNAME, name.getTitleCache());
|
|
226 |
//epithet 1,2,3
|
|
227 |
exportEpithet(taxon, wissName, name, rank);
|
|
228 |
|
|
229 |
//rank
|
|
230 |
addNanteil(wissName, BfnXmlConstants.BEREICH_RANG, BfnXmlTransformer.getRankmap().get(rank));
|
|
231 |
|
|
232 |
//authors
|
|
233 |
addNanteil(wissName, BfnXmlConstants.BEREICH_AUTOREN, name.getAuthorshipCache());
|
|
234 |
}
|
|
235 |
|
|
236 |
private void exportEpithet(TaxonBase<?> taxon, Element wissName, NonViralName<?> name, Rank rank) {
|
|
237 |
addNanteil(wissName, BfnXmlConstants.BEREICH_EPITHETON1, name.getGenusOrUninomial());
|
161 |
238 |
if(rank.isLower(Rank.GENUS())){
|
162 |
239 |
String epitheton2 = name.getInfraGenericEpithet();
|
163 |
240 |
if(epitheton2==null){
|
164 |
241 |
epitheton2 = name.getSpecificEpithet();
|
165 |
242 |
}
|
166 |
|
addNanteil(taxonym, BfnXmlConstants.BEREICH_EPITHETON2, epitheton2);
|
|
243 |
addNanteil(wissName, BfnXmlConstants.BEREICH_EPITHETON2, epitheton2);
|
167 |
244 |
}
|
168 |
245 |
if(rank.isLower(Rank.SPECIES())){
|
169 |
246 |
String epitheton3 = name.getInfraSpecificEpithet();
|
170 |
247 |
if(epitheton3==null){
|
171 |
248 |
epitheton3 = name.getSpecificEpithet();
|
172 |
249 |
}
|
173 |
|
addNanteil(taxonym, BfnXmlConstants.BEREICH_EPITHETON3, epitheton3);
|
|
250 |
addNanteil(wissName, BfnXmlConstants.BEREICH_EPITHETON3, epitheton3);
|
174 |
251 |
}
|
175 |
252 |
Set<IdentifiableSource> sources = taxon.getSources();
|
176 |
253 |
for (IdentifiableSource identifiableSource : sources) {
|
177 |
254 |
if(identifiableSource.getType().equals(OriginalSourceType.Import)
|
178 |
255 |
&& identifiableSource.getIdNamespace().equals(BfnXmlConstants.EL_TAXONYM+":"
|
179 |
256 |
+BfnXmlConstants.EL_WISSNAME+":"+BfnXmlConstants.EL_NANTEIL+":"+BfnXmlConstants.BEREICH_EINDEUTIGER_CODE)){
|
180 |
|
addNanteil(taxonym, BfnXmlConstants.BEREICH_EINDEUTIGER_CODE, identifiableSource.getIdInSource());
|
|
257 |
addNanteil(wissName, BfnXmlConstants.BEREICH_EINDEUTIGER_CODE, identifiableSource.getIdInSource());
|
181 |
258 |
}
|
182 |
259 |
}
|
183 |
|
//rank
|
184 |
|
addNanteil(taxonym, BfnXmlConstants.BEREICH_RANG, BfnXmlTransformer.getRankmap().get(rank));
|
|
260 |
}
|
185 |
261 |
|
186 |
|
//common name
|
|
262 |
private void exportCommonName(Taxon taxon, Element taxonym) {
|
187 |
263 |
Element deutscheNamen = new Element(BfnXmlConstants.EL_DEUTSCHENAMEN);
|
188 |
264 |
taxonym.addContent(deutscheNamen);
|
189 |
265 |
|
... | ... | |
193 |
269 |
Set<DescriptionElementBase> elements = taxonDescription.getElements();
|
194 |
270 |
for (DescriptionElementBase descriptionElementBase : elements) {
|
195 |
271 |
if(descriptionElementBase.isInstanceOf(CommonTaxonName.class)){
|
196 |
|
Element dName = new Element(BfnXmlConstants.EL_DNAME);
|
197 |
|
Element trivialName = new Element(BfnXmlConstants.EL_TRIVIALNAME);
|
198 |
|
deutscheNamen.addContent(dName);
|
199 |
|
dName.addContent(trivialName);
|
|
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);
|
200 |
278 |
|
201 |
|
dName.setAttribute(new Attribute(BfnXmlConstants.ATT_SEQUENZ, String.valueOf(sequenz)));
|
202 |
|
trivialName.addContent(((CommonTaxonName)descriptionElementBase).getName());
|
|
279 |
dName.setAttribute(new Attribute(BfnXmlConstants.ATT_SEQUENZ, String.valueOf(sequenz)));
|
|
280 |
trivialName.addContent(commonName.getName());
|
|
281 |
}
|
203 |
282 |
}
|
204 |
283 |
}
|
205 |
284 |
sequenz++;
|
206 |
285 |
}
|
207 |
|
|
208 |
286 |
}
|
209 |
287 |
|
210 |
288 |
private void addNanteil(Element element, String bereich, String textContent) {
|
Export synonymy and TextData