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.
10 package eu
.etaxonomy
.cdm
.io
.sdd
;
13 import java
.io
.FileOutputStream
;
14 import java
.io
.OutputStreamWriter
;
15 import java
.io
.PrintWriter
;
16 import java
.util
.HashSet
;
17 import java
.util
.List
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.stereotype
.Component
;
22 import org
.springframework
.transaction
.TransactionStatus
;
24 import eu
.etaxonomy
.cdm
.io
.common
.CdmExportBase
;
25 import eu
.etaxonomy
.cdm
.io
.common
.ICdmExport
;
26 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
;
27 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
31 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
32 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
33 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
39 * @author h.fradin (from a.babadshanjan)
43 @Component("sddCdmExporter")
44 public class SDDCdmExporter
extends CdmExportBase
<SDDExportConfigurator
, SDDExportState
> implements ICdmExport
<SDDExportConfigurator
, SDDExportState
> {
45 // public class JaxbExport extends CdmIoBase implements ICdmIoExport {
46 // TODO: public class JaxbExport extends CdmIoBase implements ICdmIO {
48 private static final Logger logger
= Logger
.getLogger(SDDCdmExporter
.class);
49 private SDDDocumentBuilder sddDocumentBuilder
= null;
51 private String ioName
= null;
57 public SDDCdmExporter() {
59 this.ioName
= this.getClass().getSimpleName();
62 /** Retrieves data from a CDM DB and serializes them CDM to XML.
63 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
64 * Taxa that are not part of the taxonomic tree are not found.
71 protected boolean doInvoke(SDDExportState state
){
72 // protected boolean doInvoke(IExportConfigurator config,
73 // Map<String, MapWrapper<? extends CdmBase>> stores) {
75 SDDExportConfigurator sddExpConfig
= state
.getConfig();
76 String dbname
= sddExpConfig
.getSource().getName();
77 String fileName
= sddExpConfig
.getDestinationNameString();
78 logger
.info("Serializing DB " + dbname
+ " to file " + fileName
);
79 logger
.debug("DbSchemaValidation = " + sddExpConfig
.getDbSchemaValidation());
81 TransactionStatus txStatus
= startTransaction(true);
82 SDDDataSet dataSet
= new SDDDataSet();
83 List
<Taxon
> taxa
= null;
84 List
<DefinedTermBase
> terms
= null;
89 logger
.info("Retrieving data from DB");
91 retrieveData(sddExpConfig
, dataSet
);
93 } catch (Exception e
) {
94 logger
.error("Error retrieving data");
98 logger
.info("All data retrieved");
101 sddDocumentBuilder
= new SDDDocumentBuilder();
102 File f
= new File(fileName
);
103 // File f = new File(fileName);
104 FileOutputStream fos
= new FileOutputStream(f
);
105 PrintWriter writer
= new PrintWriter(new OutputStreamWriter(fos
, "UTF8"), true);
106 sddDocumentBuilder
.marshal(dataSet
, f
);
108 // TODO: Split into one file per data set member to see whether performance improves?
110 logger
.info("XML file written");
111 logger
.info("Filename is: " + fileName
);
113 } catch (Exception e
) {
114 logger
.error("Marshalling error");
117 commitTransaction(txStatus
);
124 private void retrieveData (IExportConfigurator config
, SDDDataSet sddDataSet
) {
126 SDDExportConfigurator sddExpConfig
= (SDDExportConfigurator
)config
;
127 final int MAX_ROWS
= 50000;
128 int numberOfRows
= sddExpConfig
.getMaxRows();
130 //CdmApplicationController appCtr = config.getCdmAppController(false, true);
132 int agentRows
= numberOfRows
;
133 int definedTermBaseRows
= numberOfRows
;
134 int referenceBaseRows
= numberOfRows
;
135 int taxonNameBaseRows
= numberOfRows
;
136 int taxonBaseRows
= numberOfRows
;
137 int relationshipRows
= numberOfRows
;
138 int occurrencesRows
= numberOfRows
;
139 int mediaRows
= numberOfRows
;
140 int featureDataRows
= numberOfRows
;
141 int languageDataRows
= numberOfRows
;
142 int termVocabularyRows
= numberOfRows
;
143 int homotypicalGroupRows
= numberOfRows
;
145 if (sddExpConfig
.isDoTermVocabularies() == true) {
146 if (termVocabularyRows
== 0) { termVocabularyRows
= MAX_ROWS
; }
147 logger
.info("# TermVocabulary");
148 sddDataSet
.setTermVocabularies((List
)getVocabularyService().list(null,MAX_ROWS
, 0,null,null));;
151 if (sddExpConfig
.isDoLanguageData() == true) {
152 if (languageDataRows
== 0) { languageDataRows
= MAX_ROWS
; }
153 logger
.info("# Representation, Language String");
154 sddDataSet
.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS
, 0));
155 sddDataSet
.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS
, 0));
158 if (sddExpConfig
.isDoTerms() == true) {
159 if (definedTermBaseRows
== 0) { definedTermBaseRows
= getTermService().count(DefinedTermBase
.class); }
160 logger
.info("# DefinedTermBase: " + definedTermBaseRows
);
161 sddDataSet
.setTerms(getTermService().list(null,definedTermBaseRows
, 0,null,null));
164 if (sddExpConfig
.isDoAuthors() == true) {
165 if (agentRows
== 0) { agentRows
= getAgentService().count(AgentBase
.class); }
166 logger
.info("# Agents: " + agentRows
);
167 //logger.info(" # Team: " + getAgentService().count(Team.class));
168 sddDataSet
.setAgents(getAgentService().list(null,agentRows
, 0,null,null));
171 if (sddExpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
172 if (referenceBaseRows
== 0) { referenceBaseRows
= getReferenceService().count(ReferenceBase
.class); }
173 logger
.info("# ReferenceBase: " + referenceBaseRows
);
174 sddDataSet
.setReferences(getReferenceService().list(null,referenceBaseRows
, 0,null,null));
177 if (sddExpConfig
.isDoTaxonNames() == true) {
178 if (taxonNameBaseRows
== 0) { taxonNameBaseRows
= getNameService().count(TaxonNameBase
.class); }
179 logger
.info("# TaxonNameBase: " + taxonNameBaseRows
);
180 //logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
181 sddDataSet
.setTaxonomicNames(getNameService().list(null,taxonNameBaseRows
, 0,null,null));
184 if (sddExpConfig
.isDoHomotypicalGroups() == true) {
185 if (homotypicalGroupRows
== 0) { homotypicalGroupRows
= MAX_ROWS
; }
186 logger
.info("# Homotypical Groups");
187 sddDataSet
.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows
, 0));
190 if (sddExpConfig
.isDoTaxa() == true) {
191 if (taxonBaseRows
== 0) { taxonBaseRows
= getTaxonService().count(TaxonBase
.class); }
192 logger
.info("# TaxonBase: " + taxonBaseRows
);
193 // dataSet.setTaxa(new ArrayList<Taxon>());
194 // dataSet.setSynonyms(new ArrayList<Synonym>());
195 List
<TaxonBase
> tb
= getTaxonService().list(null,taxonBaseRows
, 0,null,null);
196 for (TaxonBase taxonBase
: tb
) {
197 if (taxonBase
instanceof Taxon
) {
198 sddDataSet
.addTaxon((Taxon
)taxonBase
);
199 } else if (taxonBase
instanceof Synonym
) {
200 sddDataSet
.addSynonym((Synonym
)taxonBase
);
202 logger
.error("entry of wrong type: " + taxonBase
.toString());
208 // retrieve taxa and synonyms separately
209 // need correct count for taxa and synonyms
210 // if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
211 // logger.info("# Synonym: " + taxonBaseRows);
212 // dataSet.setSynonyms(new ArrayList<Synonym>());
213 // dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
215 if (sddExpConfig
.isDoRelTaxa() == true) {
216 if (relationshipRows
== 0) { relationshipRows
= MAX_ROWS
; }
217 logger
.info("# Relationships");
218 List
<RelationshipBase
> relationList
= getTaxonService().getAllRelationships(relationshipRows
, 0);
219 Set
<RelationshipBase
> relationSet
= new HashSet
<RelationshipBase
>(relationList
);
220 sddDataSet
.setRelationships(relationSet
);
223 if (sddExpConfig
.isDoReferencedEntities() == true) {
224 logger
.info("# Referenced Entities");
225 sddDataSet
.setReferencedEntities(getNameService().getAllNomenclaturalStatus(MAX_ROWS
, 0));
226 sddDataSet
.addReferencedEntities(getNameService().getAllTypeDesignations(MAX_ROWS
, 0));
229 if (sddExpConfig
.isDoOccurrence() == true) {
230 if (occurrencesRows
== 0) { occurrencesRows
= getOccurrenceService().count(SpecimenOrObservationBase
.class); }
231 logger
.info("# SpecimenOrObservationBase: " + occurrencesRows
);
232 sddDataSet
.setOccurrences(getOccurrenceService().list(null,occurrencesRows
,0, null,null));
235 if (sddExpConfig
.isDoMedia() == true) {
236 if (mediaRows
== 0) { mediaRows
= MAX_ROWS
; }
237 logger
.info("# Media");
238 sddDataSet
.setMedia(getMediaService().list(null,mediaRows
, 0,null,null));
239 // dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
240 // dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
243 if (sddExpConfig
.isDoFeatureData() == true) {
244 if (featureDataRows
== 0) { featureDataRows
= MAX_ROWS
; }
245 logger
.info("# Feature Tree, Feature Node");
246 sddDataSet
.setFeatureData(getFeatureTreeService().getFeatureNodesAll());
247 sddDataSet
.addFeatureData(getFeatureTreeService().list(null,null,null,null,null));
253 protected boolean doCheck(SDDExportState state
) {
254 boolean result
= true;
255 logger
.warn("No check implemented for Jaxb export");
261 protected boolean isIgnore(SDDExportState state
) {