Project

General

Profile

Download (8.96 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2008 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

    
10
package eu.etaxonomy.cdm.io.jaxb;
11

    
12
import java.io.FileOutputStream;
13
import java.io.OutputStreamWriter;
14
import java.io.PrintWriter;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19

    
20
import org.apache.log4j.Logger;
21
import org.springframework.stereotype.Component;
22
import org.springframework.transaction.TransactionStatus;
23

    
24
import eu.etaxonomy.cdm.io.common.CdmIoBase;
25
import eu.etaxonomy.cdm.io.common.ICdmIO;
26
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
27
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
28
import eu.etaxonomy.cdm.io.common.MapWrapper;
29
import eu.etaxonomy.cdm.model.agent.AgentBase;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
32
import eu.etaxonomy.cdm.model.common.RelationshipBase;
33
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
34
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
35
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
36
import eu.etaxonomy.cdm.model.taxon.Synonym;
37
import eu.etaxonomy.cdm.model.taxon.Taxon;
38
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39

    
40
/**
41
 * @author a.babadshanjan
42
 * @created 25.09.2008
43
 * @version 1.0
44
 */
45
@Component
46
public class JaxbExport extends CdmIoBase<IExportConfigurator> implements ICdmIO<IExportConfigurator> {
47

    
48
	private static final Logger logger = Logger.getLogger(JaxbExport.class);
49
	private CdmDocumentBuilder cdmDocumentBuilder = null;
50

    
51
	/**
52
	 * 
53
	 */
54
//	public JaxbExport() {
55
//		super();
56
//		this.ioName = this.getClass().getSimpleName();
57
//	}
58

    
59
	/** Retrieves data from a CDM DB and serializes them CDM to XML.
60
	 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
61
	 * Taxa that are not part of the taxonomic tree are not found.
62
	 * 
63
	 * @param exImpConfig
64
	 * @param dbname
65
	 * @param filename
66
	 */
67
	@Override
68
	protected boolean doInvoke(IExportConfigurator config,
69
			Map<String, MapWrapper<? extends CdmBase>> stores) {
70

    
71
		JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)config;
72
		String dbname = jaxbExpConfig.getSource().getName();
73
    	String fileName = jaxbExpConfig.getDestination();
74
		logger.info("Serializing DB " + dbname + " to file " + fileName);
75
		logger.debug("DbSchemaValidation = " + jaxbExpConfig.getDbSchemaValidation());
76

    
77
		TransactionStatus txStatus = startTransaction(true);
78
		DataSet dataSet = new DataSet();
79

    
80
		// get data from DB
81

    
82
		try {
83
			logger.info("Retrieving data from DB");
84

    
85
			retrieveData(config, dataSet);
86

    
87
		} catch (Exception e) {
88
			logger.error("Error retrieving data");
89
			e.printStackTrace();
90
		}
91

    
92
		logger.info("All data retrieved");
93

    
94
		try {
95
			cdmDocumentBuilder = new CdmDocumentBuilder();
96
			PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF8"), true);
97
			cdmDocumentBuilder.marshal(dataSet, writer);
98

    
99
			// TODO: Split into one file per data set member to see whether performance improves?
100

    
101
			logger.info("XML file written");
102
			logger.info("Filename is: " + fileName);
103

    
104
		} catch (Exception e) {
105
			logger.error("Marshalling error");
106
			e.printStackTrace();
107
		} 
108
		commitTransaction(txStatus);
109
		
110
		return true;
111

    
112
	}
113

    
114

    
115
	private void retrieveData (IExportConfigurator config, DataSet dataSet) {
116

    
117
		JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)config;
118
		final int MAX_ROWS = 50000;
119
		int numberOfRows = jaxbExpConfig.getMaxRows();
120

    
121
		int agentRows = numberOfRows;
122
		int definedTermBaseRows = numberOfRows;
123
		int referenceBaseRows = numberOfRows;
124
		int taxonNameBaseRows = numberOfRows;
125
		int taxonBaseRows = numberOfRows;
126
		int relationshipRows = numberOfRows;
127
		int occurrencesRows = numberOfRows;
128
		int mediaRows = numberOfRows;
129
		int featureDataRows = numberOfRows;
130
		int languageDataRows = numberOfRows;
131
		int termVocabularyRows = numberOfRows;
132
		int homotypicalGroupRows = numberOfRows;
133

    
134
		if (jaxbExpConfig.isDoTermVocabularies() == true) {
135
			if (termVocabularyRows == 0) { termVocabularyRows = MAX_ROWS; }
136
			logger.info("# TermVocabulary");
137
			dataSet.setTermVocabularies(getTermService().getAllTermVocabularies(MAX_ROWS, 0));;
138
		}
139

    
140
//		if (jaxbExpConfig.isDoLanguageData() == true) {
141
//			if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
142
//			logger.info("# Representation, Language String");
143
//			dataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
144
//			dataSet.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
145
//		}
146

    
147
		if (jaxbExpConfig.isDoTerms() == true) {
148
			if (definedTermBaseRows == 0) { definedTermBaseRows = getTermService().count(DefinedTermBase.class); }
149
			logger.info("# DefinedTermBase: " + definedTermBaseRows);
150
			dataSet.setTerms(getTermService().getAllDefinedTerms(definedTermBaseRows, 0));
151
		}
152

    
153
		if (jaxbExpConfig.isDoAuthors() == true) {
154
			if (agentRows == 0) { agentRows = getAgentService().count(AgentBase.class); }
155
			logger.info("# Agents: " + agentRows);
156
			//logger.info("    # Team: " + appCtr.getAgentService().count(Team.class));
157
			dataSet.setAgents(getAgentService().getAllAgents(agentRows, 0));
158
		}
159

    
160
		if (jaxbExpConfig.getDoReferences() != IImportConfigurator.DO_REFERENCES.NONE) {
161
			if (referenceBaseRows == 0) { referenceBaseRows = getReferenceService().count(ReferenceBase.class); }
162
			logger.info("# ReferenceBase: " + referenceBaseRows);
163
			dataSet.setReferences(getReferenceService().getAllReferences(referenceBaseRows, 0));
164
		}
165

    
166
		if (jaxbExpConfig.isDoTaxonNames() == true) {
167
			if (taxonNameBaseRows == 0) { taxonNameBaseRows = getNameService().count(TaxonNameBase.class); }
168
			logger.info("# TaxonNameBase: " + taxonNameBaseRows);
169
			//logger.info("    # Taxon: " + getNameService().count(BotanicalName.class));
170
			dataSet.setTaxonomicNames(getNameService().getAllNames(taxonNameBaseRows, 0));
171
		}
172

    
173
		if (jaxbExpConfig.isDoHomotypicalGroups() == true) {
174
			if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
175
			logger.info("# Homotypical Groups");
176
			dataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
177
		}
178

    
179
		if (jaxbExpConfig.isDoTaxa() == true) {
180
			if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
181
			logger.info("# TaxonBase: " + taxonBaseRows);
182
//			dataSet.setTaxa(new ArrayList<Taxon>());
183
//			dataSet.setSynonyms(new ArrayList<Synonym>());
184
			List<TaxonBase> tb = getTaxonService().getAllTaxonBases(taxonBaseRows, 0);
185
			for (TaxonBase taxonBase : tb) {
186
				dataSet.addTaxonBase(taxonBase);
187
			}
188
		}
189

    
190
		// TODO: 
191
		// retrieve taxa and synonyms separately
192
		// need correct count for taxa and synonyms
193
//		if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
194
//		logger.info("# Synonym: " + taxonBaseRows);
195
//		dataSet.setSynonyms(new ArrayList<Synonym>());
196
//		dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
197

    
198
//		if (jaxbExpConfig.isDoRelTaxa() == true) {
199
//			if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
200
//			logger.info("# Relationships");
201
//			List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
202
//			Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
203
//			dataSet.setRelationships(relationSet);
204
//		}
205

    
206
		if (jaxbExpConfig.isDoTypeDesignations() == true) {
207
			logger.info("# TypeDesignations");
208
			dataSet.addTypeDesignations(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
209
		}
210

    
211
		if (jaxbExpConfig.isDoOccurrence() == true) {
212
			if (occurrencesRows == 0) { occurrencesRows = getOccurrenceService().count(SpecimenOrObservationBase.class); }
213
			logger.info("# SpecimenOrObservationBase: " + occurrencesRows);
214
			dataSet.setOccurrences(getOccurrenceService().getAllSpecimenOrObservationBases(occurrencesRows, 0));
215
		}
216

    
217
		if (jaxbExpConfig.isDoMedia() == true) {
218
			if (mediaRows == 0) { mediaRows = MAX_ROWS; }
219
			logger.info("# Media");
220
			dataSet.setMedia(getMediaService().getAllMedia(mediaRows, 0));
221
//			dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
222
//			dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
223
		}
224

    
225
		if (jaxbExpConfig.isDoFeatureData() == true) {
226
			if (featureDataRows == 0) { featureDataRows = MAX_ROWS; }
227
			logger.info("# Feature Tree, Feature Node");
228
			dataSet.setFeatureTrees(getDescriptionService().getFeatureTreesAll());
229
		}
230
	}
231

    
232

    
233
	@Override
234
	protected boolean doCheck(IExportConfigurator config) {
235
		boolean result = true;
236
		logger.warn("No check implemented for Jaxb export");
237
		return result;
238
	}
239

    
240

    
241
	@Override
242
	protected boolean isIgnore(IExportConfigurator config) {
243
		return false;
244
	}
245
	
246
}
(7-7/13)