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
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.springframework
.stereotype
.Component
;
23 import org
.springframework
.transaction
.TransactionStatus
;
25 import eu
.etaxonomy
.cdm
.io
.common
.CdmIoBase
;
26 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
27 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
;
28 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
29 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
30 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
33 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
34 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
35 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
36 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
39 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
42 * @author h.fradin (from a.babadshanjan)
46 @Component("sddCdmExporter")
47 public class SDDCdmExporter
extends CdmIoBase
<IExportConfigurator
> implements ICdmIO
<IExportConfigurator
> {
48 // public class JaxbExport extends CdmIoBase implements ICdmIoExport {
49 // TODO: public class JaxbExport extends CdmIoBase implements ICdmIO {
51 private static final Logger logger
= Logger
.getLogger(SDDCdmExporter
.class);
52 private SDDDocumentBuilder sddDocumentBuilder
= null;
54 private String ioName
= null;
60 public SDDCdmExporter() {
62 this.ioName
= this.getClass().getSimpleName();
65 /** Retrieves data from a CDM DB and serializes them CDM to XML.
66 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
67 * Taxa that are not part of the taxonomic tree are not found.
74 protected boolean doInvoke(IExportConfigurator config
,
75 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
) {
77 SDDExportConfigurator sddExpConfig
= (SDDExportConfigurator
)config
;
78 String dbname
= sddExpConfig
.getSource().getName();
79 String fileName
= sddExpConfig
.getDestinationNameString();
80 logger
.info("Serializing DB " + dbname
+ " to file " + fileName
);
81 logger
.debug("DbSchemaValidation = " + sddExpConfig
.getDbSchemaValidation());
83 TransactionStatus txStatus
= startTransaction(true);
84 SDDDataSet dataSet
= new SDDDataSet();
85 List
<Taxon
> taxa
= null;
86 List
<DefinedTermBase
> terms
= null;
91 logger
.info("Retrieving data from DB");
93 retrieveData(config
, dataSet
);
95 } catch (Exception e
) {
96 logger
.error("Error retrieving data");
100 logger
.info("All data retrieved");
103 sddDocumentBuilder
= new SDDDocumentBuilder();
104 File f
= new File(fileName
);
105 // File f = new File(fileName);
106 FileOutputStream fos
= new FileOutputStream(f
);
107 PrintWriter writer
= new PrintWriter(new OutputStreamWriter(fos
, "UTF8"), true);
108 sddDocumentBuilder
.marshal(dataSet
, f
);
110 // TODO: Split into one file per data set member to see whether performance improves?
112 logger
.info("XML file written");
113 logger
.info("Filename is: " + fileName
);
115 } catch (Exception e
) {
116 logger
.error("Marshalling error");
119 commitTransaction(txStatus
);
126 private void retrieveData (IExportConfigurator config
, SDDDataSet sddDataSet
) {
128 SDDExportConfigurator sddExpConfig
= (SDDExportConfigurator
)config
;
129 final int MAX_ROWS
= 50000;
130 int numberOfRows
= sddExpConfig
.getMaxRows();
132 //CdmApplicationController appCtr = config.getCdmAppController(false, true);
134 int agentRows
= numberOfRows
;
135 int definedTermBaseRows
= numberOfRows
;
136 int referenceBaseRows
= numberOfRows
;
137 int taxonNameBaseRows
= numberOfRows
;
138 int taxonBaseRows
= numberOfRows
;
139 int relationshipRows
= numberOfRows
;
140 int occurrencesRows
= numberOfRows
;
141 int mediaRows
= numberOfRows
;
142 int featureDataRows
= numberOfRows
;
143 int languageDataRows
= numberOfRows
;
144 int termVocabularyRows
= numberOfRows
;
145 int homotypicalGroupRows
= numberOfRows
;
147 if (sddExpConfig
.isDoTermVocabularies() == true) {
148 if (termVocabularyRows
== 0) { termVocabularyRows
= MAX_ROWS
; }
149 logger
.info("# TermVocabulary");
150 sddDataSet
.setTermVocabularies(getTermService().getAllTermVocabularies(MAX_ROWS
, 0));;
153 if (sddExpConfig
.isDoLanguageData() == true) {
154 if (languageDataRows
== 0) { languageDataRows
= MAX_ROWS
; }
155 logger
.info("# Representation, Language String");
156 sddDataSet
.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS
, 0));
157 sddDataSet
.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS
, 0));
160 if (sddExpConfig
.isDoTerms() == true) {
161 if (definedTermBaseRows
== 0) { definedTermBaseRows
= getTermService().count(DefinedTermBase
.class); }
162 logger
.info("# DefinedTermBase: " + definedTermBaseRows
);
163 sddDataSet
.setTerms(getTermService().getAllDefinedTerms(definedTermBaseRows
, 0));
166 if (sddExpConfig
.isDoAuthors() == true) {
167 if (agentRows
== 0) { agentRows
= getAgentService().count(AgentBase
.class); }
168 logger
.info("# Agents: " + agentRows
);
169 //logger.info(" # Team: " + getAgentService().count(Team.class));
170 sddDataSet
.setAgents(getAgentService().getAllAgents(agentRows
, 0));
173 if (sddExpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
174 if (referenceBaseRows
== 0) { referenceBaseRows
= getReferenceService().count(ReferenceBase
.class); }
175 logger
.info("# ReferenceBase: " + referenceBaseRows
);
176 sddDataSet
.setReferences(getReferenceService().getAllReferences(referenceBaseRows
, 0));
179 if (sddExpConfig
.isDoTaxonNames() == true) {
180 if (taxonNameBaseRows
== 0) { taxonNameBaseRows
= getNameService().count(TaxonNameBase
.class); }
181 logger
.info("# TaxonNameBase: " + taxonNameBaseRows
);
182 //logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
183 sddDataSet
.setTaxonomicNames(getNameService().getAllNames(taxonNameBaseRows
, 0));
186 if (sddExpConfig
.isDoHomotypicalGroups() == true) {
187 if (homotypicalGroupRows
== 0) { homotypicalGroupRows
= MAX_ROWS
; }
188 logger
.info("# Homotypical Groups");
189 sddDataSet
.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows
, 0));
192 if (sddExpConfig
.isDoTaxa() == true) {
193 if (taxonBaseRows
== 0) { taxonBaseRows
= getTaxonService().count(TaxonBase
.class); }
194 logger
.info("# TaxonBase: " + taxonBaseRows
);
195 // dataSet.setTaxa(new ArrayList<Taxon>());
196 // dataSet.setSynonyms(new ArrayList<Synonym>());
197 List
<TaxonBase
> tb
= getTaxonService().getAllTaxonBases(taxonBaseRows
, 0);
198 for (TaxonBase taxonBase
: tb
) {
199 if (taxonBase
instanceof Taxon
) {
200 sddDataSet
.addTaxon((Taxon
)taxonBase
);
201 } else if (taxonBase
instanceof Synonym
) {
202 sddDataSet
.addSynonym((Synonym
)taxonBase
);
204 logger
.error("entry of wrong type: " + taxonBase
.toString());
210 // retrieve taxa and synonyms separately
211 // need correct count for taxa and synonyms
212 // if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
213 // logger.info("# Synonym: " + taxonBaseRows);
214 // dataSet.setSynonyms(new ArrayList<Synonym>());
215 // dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
217 if (sddExpConfig
.isDoRelTaxa() == true) {
218 if (relationshipRows
== 0) { relationshipRows
= MAX_ROWS
; }
219 logger
.info("# Relationships");
220 List
<RelationshipBase
> relationList
= getTaxonService().getAllRelationships(relationshipRows
, 0);
221 Set
<RelationshipBase
> relationSet
= new HashSet
<RelationshipBase
>(relationList
);
222 sddDataSet
.setRelationships(relationSet
);
225 if (sddExpConfig
.isDoReferencedEntities() == true) {
226 logger
.info("# Referenced Entities");
227 sddDataSet
.setReferencedEntities(getNameService().getAllNomenclaturalStatus(MAX_ROWS
, 0));
228 sddDataSet
.addReferencedEntities(getNameService().getAllTypeDesignations(MAX_ROWS
, 0));
231 if (sddExpConfig
.isDoOccurrence() == true) {
232 if (occurrencesRows
== 0) { occurrencesRows
= getOccurrenceService().count(SpecimenOrObservationBase
.class); }
233 logger
.info("# SpecimenOrObservationBase: " + occurrencesRows
);
234 sddDataSet
.setOccurrences(getOccurrenceService().getAllSpecimenOrObservationBases(occurrencesRows
, 0));
237 if (sddExpConfig
.isDoMedia() == true) {
238 if (mediaRows
== 0) { mediaRows
= MAX_ROWS
; }
239 logger
.info("# Media");
240 sddDataSet
.setMedia(getMediaService().getAllMedia(mediaRows
, 0));
241 // dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
242 // dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
245 if (sddExpConfig
.isDoFeatureData() == true) {
246 if (featureDataRows
== 0) { featureDataRows
= MAX_ROWS
; }
247 logger
.info("# Feature Tree, Feature Node");
248 sddDataSet
.setFeatureData(getDescriptionService().getFeatureNodesAll());
249 sddDataSet
.addFeatureData(getDescriptionService().getFeatureTreesAll());
255 protected boolean doCheck(IExportConfigurator config
) {
256 boolean result
= true;
257 logger
.warn("No check implemented for Jaxb export");
263 protected boolean isIgnore(IExportConfigurator config
) {