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
.pilotOutputHtml
;
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
.io
.sdd
.SDDDataSet
;
29 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
32 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
33 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
34 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
40 * @author h.fradin (from a.babadshanjan)
44 @Component("pilotOutputDescriptionExporter")
45 public class PilotOutputDescriptionExporter
extends CdmExportBase
<PilotOutputExportConfigurator
, PilotOutputExportState
> implements ICdmExport
<PilotOutputExportConfigurator
, PilotOutputExportState
> {
46 // public class JaxbExport extends CdmIoBase implements ICdmIoExport {
47 // TODO: public class JaxbExport extends CdmIoBase implements ICdmIO {
49 private static final Logger logger
= Logger
.getLogger(PilotOutputDescriptionExporter
.class);
50 private PilotOutputDocumentBuilder pilotOutputDocumentBuilder
= null;
52 private String ioName
= null;
58 public PilotOutputDescriptionExporter() {
60 this.ioName
= this.getClass().getSimpleName();
63 /** Retrieves data from a CDM DB and serializes them CDM to XML.
64 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
65 * Taxa that are not part of the taxonomic tree are not found.
72 protected boolean doInvoke(PilotOutputExportState state
){
73 // protected boolean doInvoke(IExportConfigurator config,
74 // Map<String, MapWrapper<? extends CdmBase>> stores) {
76 PilotOutputExportConfigurator sddExpConfig
= state
.getConfig();
77 String dbname
= sddExpConfig
.getSource().getName();
78 String fileName
= sddExpConfig
.getDestinationNameString();
79 logger
.info("Serializing DB " + dbname
+ " to file " + fileName
);
80 logger
.debug("DbSchemaValidation = " + sddExpConfig
.getDbSchemaValidation());
82 TransactionStatus txStatus
= startTransaction(true);
83 SDDDataSet dataSet
= new SDDDataSet();
84 List
<Taxon
> taxa
= null;
85 List
<DefinedTermBase
> terms
= null;
90 logger
.info("Retrieving data from DB");
92 retrieveData(sddExpConfig
, dataSet
);
94 } catch (Exception e
) {
95 logger
.error("Error retrieving data");
99 logger
.info("All data retrieved");
102 pilotOutputDocumentBuilder
= new PilotOutputDocumentBuilder();
103 File f
= new File(fileName
);
104 // File f = new File(fileName);
105 FileOutputStream fos
= new FileOutputStream(f
);
106 PrintWriter writer
= new PrintWriter(new OutputStreamWriter(fos
, "UTF8"), true);
107 pilotOutputDocumentBuilder
.marshal(dataSet
, fileName
);
109 // TODO: Split into one file per data set member to see whether performance improves?
111 logger
.info("XML file written");
112 logger
.info("Filename is: " + fileName
);
114 } catch (Exception e
) {
115 logger
.error("Marshalling error");
118 commitTransaction(txStatus
);
125 private void retrieveData (IExportConfigurator config
, SDDDataSet sddDataSet
) {
127 PilotOutputExportConfigurator sddExpConfig
= (PilotOutputExportConfigurator
)config
;
128 final int MAX_ROWS
= 50000;
129 int numberOfRows
= sddExpConfig
.getMaxRows();
131 //CdmApplicationController appCtr = config.getCdmAppController(false, true);
133 int agentRows
= numberOfRows
;
134 int definedTermBaseRows
= numberOfRows
;
135 int referenceBaseRows
= numberOfRows
;
136 int taxonNameBaseRows
= numberOfRows
;
137 int taxonBaseRows
= numberOfRows
;
138 int relationshipRows
= numberOfRows
;
139 int occurrencesRows
= numberOfRows
;
140 int mediaRows
= numberOfRows
;
141 int featureDataRows
= numberOfRows
;
142 int languageDataRows
= numberOfRows
;
143 int termVocabularyRows
= numberOfRows
;
144 int homotypicalGroupRows
= numberOfRows
;
146 if (sddExpConfig
.isDoTermVocabularies() == true) {
147 if (termVocabularyRows
== 0) { termVocabularyRows
= MAX_ROWS
; }
148 logger
.info("# TermVocabulary");
149 sddDataSet
.setTermVocabularies(getTermService().getAllTermVocabularies(MAX_ROWS
, 0));;
152 if (sddExpConfig
.isDoLanguageData() == true) {
153 if (languageDataRows
== 0) { languageDataRows
= MAX_ROWS
; }
154 logger
.info("# Representation, Language String");
155 sddDataSet
.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS
, 0));
156 sddDataSet
.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS
, 0));
159 if (sddExpConfig
.isDoTerms() == true) {
160 if (definedTermBaseRows
== 0) { definedTermBaseRows
= getTermService().count(DefinedTermBase
.class); }
161 logger
.info("# DefinedTermBase: " + definedTermBaseRows
);
162 sddDataSet
.setTerms(getTermService().getAllDefinedTerms(definedTermBaseRows
, 0));
165 if (sddExpConfig
.isDoAuthors() == true) {
166 if (agentRows
== 0) { agentRows
= getAgentService().count(AgentBase
.class); }
167 logger
.info("# Agents: " + agentRows
);
168 //logger.info(" # Team: " + getAgentService().count(Team.class));
169 sddDataSet
.setAgents(getAgentService().getAllAgents(agentRows
, 0));
172 if (sddExpConfig
.getDoReferences() != IImportConfigurator
.DO_REFERENCES
.NONE
) {
173 if (referenceBaseRows
== 0) { referenceBaseRows
= getReferenceService().count(ReferenceBase
.class); }
174 logger
.info("# ReferenceBase: " + referenceBaseRows
);
175 sddDataSet
.setReferences(getReferenceService().getAllReferences(referenceBaseRows
, 0));
178 if (sddExpConfig
.isDoTaxonNames() == true) {
179 if (taxonNameBaseRows
== 0) { taxonNameBaseRows
= getNameService().count(TaxonNameBase
.class); }
180 logger
.info("# TaxonNameBase: " + taxonNameBaseRows
);
181 //logger.info(" # Taxon: " + getNameService().count(BotanicalName.class));
182 sddDataSet
.setTaxonomicNames(getNameService().getAllNames(taxonNameBaseRows
, 0));
185 if (sddExpConfig
.isDoHomotypicalGroups() == true) {
186 if (homotypicalGroupRows
== 0) { homotypicalGroupRows
= MAX_ROWS
; }
187 logger
.info("# Homotypical Groups");
188 sddDataSet
.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows
, 0));
191 if (sddExpConfig
.isDoTaxa() == true) {
192 if (taxonBaseRows
== 0) { taxonBaseRows
= getTaxonService().count(TaxonBase
.class); }
193 logger
.info("# TaxonBase: " + taxonBaseRows
);
194 // dataSet.setTaxa(new ArrayList<Taxon>());
195 // dataSet.setSynonyms(new ArrayList<Synonym>());
196 List
<TaxonBase
> tb
= getTaxonService().getAllTaxonBases(taxonBaseRows
, 0);
197 for (TaxonBase taxonBase
: tb
) {
198 if (taxonBase
instanceof Taxon
) {
199 sddDataSet
.addTaxon((Taxon
)taxonBase
);
200 } else if (taxonBase
instanceof Synonym
) {
201 sddDataSet
.addSynonym((Synonym
)taxonBase
);
203 logger
.error("entry of wrong type: " + taxonBase
.toString());
209 // retrieve taxa and synonyms separately
210 // need correct count for taxa and synonyms
211 // if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
212 // logger.info("# Synonym: " + taxonBaseRows);
213 // dataSet.setSynonyms(new ArrayList<Synonym>());
214 // dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
216 if (sddExpConfig
.isDoRelTaxa() == true) {
217 if (relationshipRows
== 0) { relationshipRows
= MAX_ROWS
; }
218 logger
.info("# Relationships");
219 List
<RelationshipBase
> relationList
= getTaxonService().getAllRelationships(relationshipRows
, 0);
220 Set
<RelationshipBase
> relationSet
= new HashSet
<RelationshipBase
>(relationList
);
221 sddDataSet
.setRelationships(relationSet
);
224 if (sddExpConfig
.isDoReferencedEntities() == true) {
225 logger
.info("# Referenced Entities");
226 sddDataSet
.setReferencedEntities(getNameService().getAllNomenclaturalStatus(MAX_ROWS
, 0));
227 sddDataSet
.addReferencedEntities(getNameService().getAllTypeDesignations(MAX_ROWS
, 0));
230 if (sddExpConfig
.isDoOccurrence() == true) {
231 if (occurrencesRows
== 0) { occurrencesRows
= getOccurrenceService().count(SpecimenOrObservationBase
.class); }
232 logger
.info("# SpecimenOrObservationBase: " + occurrencesRows
);
233 sddDataSet
.setOccurrences(getOccurrenceService().getAllSpecimenOrObservationBases(occurrencesRows
, 0));
236 if (sddExpConfig
.isDoMedia() == true) {
237 if (mediaRows
== 0) { mediaRows
= MAX_ROWS
; }
238 logger
.info("# Media");
239 sddDataSet
.setMedia(getMediaService().getAllMedia(mediaRows
, 0));
240 // dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
241 // dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
244 if (sddExpConfig
.isDoFeatureData() == true) {
245 if (featureDataRows
== 0) { featureDataRows
= MAX_ROWS
; }
246 logger
.info("# Feature Tree, Feature Node");
247 sddDataSet
.setFeatureData(getDescriptionService().getFeatureNodesAll());
248 sddDataSet
.addFeatureData(getDescriptionService().getFeatureTreesAll(null));
254 protected boolean doCheck(PilotOutputExportState state
) {
255 boolean result
= true;
256 logger
.warn("No check implemented for Jaxb export");
262 protected boolean isIgnore(PilotOutputExportState state
) {