Export epithet, rank and scientific name
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / redlist / bfnXml / out / BfnXmlTaxonNameExport.java
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.io.FileWriter;
12 import java.io.IOException;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.apache.log4j.Logger;
17 import org.jdom2.Attribute;
18 import org.jdom2.Document;
19 import org.jdom2.Element;
20 import org.jdom2.output.Format;
21 import org.jdom2.output.XMLOutputter;
22 import org.springframework.stereotype.Component;
23
24 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
25 import eu.etaxonomy.cdm.io.redlist.bfnXml.BfnXmlConstants;
26 import eu.etaxonomy.cdm.io.redlist.bfnXml.in.BfnXmlTransformer;
27 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
28 import eu.etaxonomy.cdm.model.common.OriginalSourceType;
29 import eu.etaxonomy.cdm.model.name.NonViralName;
30 import eu.etaxonomy.cdm.model.name.Rank;
31 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
32 import eu.etaxonomy.cdm.model.taxon.Classification;
33 import eu.etaxonomy.cdm.model.taxon.Taxon;
34 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
35
36
37
38 /**
39 *
40 * @author pplitzner
41 * @date May 3, 2016
42 *
43 */
44 @Component
45 public class BfnXmlTaxonNameExport extends BfnXmlExportBase<TaxonNameBase> {
46
47 private static final long serialVersionUID = -931703660108981011L;
48
49 private static final Logger logger = Logger.getLogger(BfnXmlTaxonNameExport.class);
50
51 public BfnXmlTaxonNameExport(){
52 super();
53 }
54
55 @Override
56 protected void doInvoke(BfnXmlExportState state){
57 Document document = state.getConfig().getDocument();
58
59 startTransaction(true);
60
61 //get all classifications
62 List<Classification> classifications = getClassificationService().list(Classification.class, null, null, null, null);
63 for (Classification classification : classifications) {
64 Element roteListeDaten = new Element(BfnXmlConstants.EL_ROTELISTEDATEN);
65 roteListeDaten.setAttribute(new Attribute(BfnXmlConstants.ATT_INHALT, classification.getTitleCache()));
66 document.getRootElement().addContent(roteListeDaten);
67
68 //export taxonomy
69 Element taxonyme = new Element(BfnXmlConstants.EL_TAXONYME);
70 roteListeDaten.addContent(taxonyme);
71 List<TaxonNode> childNodes = classification.getChildNodes();
72 for (TaxonNode taxonNode : childNodes) {
73 exportTaxon(taxonNode, taxonyme);
74 }
75 }
76
77 XMLOutputter outputter = new XMLOutputter();
78 outputter.setFormat(Format.getPrettyFormat());
79 try {
80 outputter.output(document, new FileWriter(state.getConfig().getDestination()));
81 } catch (IOException e) {
82 logger.error("Error writing file", e);
83 }
84
85 }
86
87 private void exportTaxon(TaxonNode taxonNode, Element parent) {
88 Element taxonym = new Element(BfnXmlConstants.EL_TAXONYM);
89 parent.addContent(taxonym);
90
91 Taxon taxon = taxonNode.getTaxon();
92 NonViralName name = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
93 Rank rank = name.getRank();
94 //wissName
95 addNanteil(taxonym, BfnXmlConstants.BEREICH_WISSNAME, taxon.getTitleCache());
96 //epitithon 1,2,3
97 addNanteil(taxonym, BfnXmlConstants.BEREICH_EPITHETON1, name.getGenusOrUninomial());
98 if(rank.isLower(Rank.GENUS())){
99 String epitheton2 = name.getInfraGenericEpithet();
100 if(epitheton2==null){
101 epitheton2 = name.getSpecificEpithet();
102 }
103 addNanteil(taxonym, BfnXmlConstants.BEREICH_EPITHETON2, epitheton2);
104 }
105 if(rank.isLower(Rank.SPECIES())){
106 String epitheton3 = name.getInfraSpecificEpithet();
107 if(epitheton3==null){
108 epitheton3 = name.getSpecificEpithet();
109 }
110 addNanteil(taxonym, BfnXmlConstants.BEREICH_EPITHETON3, epitheton3);
111 }
112 Set<IdentifiableSource> sources = taxon.getSources();
113 for (IdentifiableSource identifiableSource : sources) {
114 if(identifiableSource.getType().equals(OriginalSourceType.Import)
115 && identifiableSource.getIdNamespace().equals(BfnXmlConstants.EL_TAXONYM+":"
116 +BfnXmlConstants.EL_WISSNAME+":"+BfnXmlConstants.EL_NANTEIL+":"+BfnXmlConstants.BEREICH_EINDEUTIGER_CODE)){
117 addNanteil(taxonym, BfnXmlConstants.BEREICH_EINDEUTIGER_CODE, identifiableSource.getIdInSource());
118 }
119 }
120 //rank
121 addNanteil(taxonym, BfnXmlConstants.BEREICH_RANG, BfnXmlTransformer.getRankmap().get(rank));
122
123 }
124
125 private void addNanteil(Element element, String bereich, String textContent) {
126 Element nanteil = new Element(BfnXmlConstants.EL_NANTEIL);
127 nanteil.setAttribute(new Attribute(BfnXmlConstants.ATT_BEREICH, bereich));
128 nanteil.addContent(textContent);
129 element.addContent(nanteil);
130 }
131
132 @Override
133 protected boolean doCheck(BfnXmlExportState state) {
134 return false;
135 }
136
137 @Override
138 protected boolean isIgnore(BfnXmlExportState state) {
139 return false;
140 }
141
142 }