Project

General

Profile

Download (11.6 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.ArrayList;
16
import java.util.List;
17

    
18
import org.apache.log4j.Logger;
19
import org.springframework.stereotype.Component;
20
import org.springframework.transaction.TransactionStatus;
21

    
22
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
23
import eu.etaxonomy.cdm.io.common.CdmExportBase;
24
import eu.etaxonomy.cdm.io.common.ICdmExport;
25
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
26
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
27
import eu.etaxonomy.cdm.model.agent.AgentBase;
28
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
29
import eu.etaxonomy.cdm.model.common.User;
30
import eu.etaxonomy.cdm.model.description.FeatureNode;
31
import eu.etaxonomy.cdm.model.description.FeatureTree;
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.TaxonBase;
36
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37
import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
38

    
39
/**
40
 * @author a.babadshanjan
41
 * @created 25.09.2008
42
 * @version 1.0
43
 */
44
@Component
45
public class JaxbExport extends CdmExportBase<JaxbExportConfigurator, JaxbExportState> implements ICdmExport<JaxbExportConfigurator, JaxbExportState> {
46

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

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

    
58
	/** Retrieves data from a CDM DB and serializes them CDM to XML.
59
	 * Starts with root taxa and traverses the taxonomic tree to retrieve children taxa, synonyms and relationships.
60
	 * Taxa that are not part of the taxonomic tree are not found.
61
	 * 
62
	 * @param exImpConfig
63
	 * @param dbname
64
	 * @param filename
65
	 */
66
//	@Override
67
//	protected boolean doInvoke(IExportConfigurator config,
68
//			Map<String, MapWrapper<? extends CdmBase>> stores) {
69
	@Override
70
	protected boolean doInvoke(JaxbExportState state) {
71
		
72
		JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)state.getConfig();
73
//		String dbname = jaxbExpConfig.getSource().getName();
74
    	String fileName = jaxbExpConfig.getDestination();
75
//		logger.info("Serializing DB " + dbname + " to file " + fileName);
76
//		logger.debug("DbSchemaValidation = " + jaxbExpConfig.getDbSchemaValidation());
77

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

    
81
		// get data from DB
82

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

    
86
			retrieveData(jaxbExpConfig, dataSet);
87

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

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

    
95
		try {
96
			cdmDocumentBuilder = new CdmDocumentBuilder();
97
			PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF8"), true);
98
			
99
			/*SAXResult result = new SAXResult();
100
			ContentHandler handler = new DefaultHandler();
101
			
102
			result.setHandler(handler);*/
103
			
104
			//cdmDocumentBuilder.marshal(dataSet, result);
105
			cdmDocumentBuilder.marshal(dataSet, writer);
106
			
107
			// TODO: Split into one file per data set member to see whether performance improves?
108

    
109
			logger.info("XML file written");
110
			logger.info("Filename is: " + fileName);
111

    
112
		} catch (Exception e) {
113
			logger.error("Marshalling error");
114
			e.printStackTrace();
115
		} 
116
		commitTransaction(txStatus);
117
		
118
		return true;
119

    
120
	}
121

    
122

    
123
	private void retrieveData (IExportConfigurator config, DataSet dataSet) {
124

    
125
		JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)config;
126
		final int MAX_ROWS = 50000;
127
		int numberOfRows = jaxbExpConfig.getMaxRows();
128

    
129
		int agentRows = numberOfRows;
130
		int definedTermBaseRows = numberOfRows;
131
		int referenceBaseRows = numberOfRows;
132
		int taxonNameBaseRows = numberOfRows;
133
		int taxonBaseRows = numberOfRows;
134
		int taxonNodeRows = numberOfRows;
135
		int relationshipRows = numberOfRows;
136
		int occurrencesRows = numberOfRows;
137
		int mediaRows = numberOfRows;
138
		int featureDataRows = numberOfRows;
139
		int taxonomicTreeDataRows = numberOfRows;
140
		int languageDataRows = numberOfRows;
141
		int termVocabularyRows = numberOfRows;
142
		int homotypicalGroupRows = numberOfRows;
143
		int UserRows= numberOfRows;
144

    
145
		if (jaxbExpConfig.isDoUsers() == true) {
146
			
147
			if (UserRows == 0) { UserRows = MAX_ROWS; }
148
			logger.info("# User");
149
			List<User> users = getUserService().list(null, UserRows, 0, null, null);
150
		
151
			
152
			for (User user: users){
153
				dataSet.addUser( (User)HibernateProxyHelper.deproxy(user));
154
			}
155
			
156
		}
157
		if (jaxbExpConfig.isDoTermVocabularies() == true) {
158
			if (termVocabularyRows == 0) { termVocabularyRows = MAX_ROWS; }
159
			logger.info("# TermVocabulary");
160
			dataSet.setTermVocabularies((List)getVocabularyService().list(null,termVocabularyRows, 0, null, null));
161
		}
162
		
163
		
164

    
165
//		if (jaxbExpConfig.isDoLanguageData() == true) {
166
//			if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
167
//			logger.info("# Representation, Language String");
168
//			dataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
169
		//TODO!!!	
170
		dataSet.setLanguageStrings(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
171
//		}
172

    
173
		if (jaxbExpConfig.isDoTerms() == true) {
174
			if (definedTermBaseRows == 0) { definedTermBaseRows = getTermService().count(DefinedTermBase.class); }
175
			logger.info("# DefinedTermBase: " + definedTermBaseRows);
176
			dataSet.setTerms(getTermService().list(null,definedTermBaseRows, 0,null,null));
177
		}
178
		
179
		if (jaxbExpConfig.isDoAuthors() == true) {
180
			if (agentRows == 0) { agentRows = getAgentService().count(AgentBase.class); }
181
			logger.info("# Agents: " + agentRows);
182
			//logger.info("    # Team: " + appCtr.getAgentService().count(Team.class));
183
			dataSet.setAgents(getAgentService().list(null,agentRows, 0,null,null));
184
		}
185
		
186
		
187

    
188
		if (jaxbExpConfig.getDoReferences() != IImportConfigurator.DO_REFERENCES.NONE) {
189
			if (referenceBaseRows == 0) { referenceBaseRows = getReferenceService().count(ReferenceBase.class); }
190
			logger.info("# ReferenceBase: " + referenceBaseRows);
191
			dataSet.setReferences(getReferenceService().list(null,referenceBaseRows, 0,null,null));
192
		}
193
		
194
		if (jaxbExpConfig.isDoHomotypicalGroups() == true) {
195
			if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
196
			logger.info("# Homotypical Groups");
197
			dataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
198
		}
199

    
200
		if (jaxbExpConfig.isDoTaxonNames() == true) {
201
			if (taxonNameBaseRows == 0) { taxonNameBaseRows = getNameService().count(TaxonNameBase.class); }
202
			logger.info("# TaxonNameBase: " + taxonNameBaseRows);
203
			//logger.info("    # Taxon: " + getNameService().count(BotanicalName.class));
204
			dataSet.setTaxonomicNames(getNameService().list(null,taxonNameBaseRows, 0,null,null));
205
		}
206

    
207
		
208

    
209
		if (jaxbExpConfig.isDoTaxa() == true) {
210
			if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
211
			logger.info("# TaxonBase: " + taxonBaseRows);
212
//			dataSet.setTaxa(new ArrayList<Taxon>());
213
//			dataSet.setSynonyms(new ArrayList<Synonym>());
214
			List<TaxonBase> tb = getTaxonService().list(null,taxonBaseRows, 0,null,null);
215
			for (TaxonBase taxonBase : tb) {
216
				dataSet.addTaxonBase((TaxonBase)HibernateProxyHelper.deproxy(taxonBase));
217
			}
218
		}
219

    
220
		// TODO: 
221
		// retrieve taxa and synonyms separately
222
		// need correct count for taxa and synonyms
223
//		if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
224
//		logger.info("# Synonym: " + taxonBaseRows);
225
//		dataSet.setSynonyms(new ArrayList<Synonym>());
226
//		dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
227

    
228
//		if (jaxbExpConfig.isDoRelTaxa() == true) {
229
//			if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
230
//			logger.info("# Relationships");
231
//			List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
232
//			Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
233
//			dataSet.setRelationships(relationSet);
234
//		}
235
		if (jaxbExpConfig.isDoOccurrence() == true) {
236
			if (occurrencesRows == 0) { occurrencesRows = getOccurrenceService().count(SpecimenOrObservationBase.class); }
237
			logger.info("# SpecimenOrObservationBase: " + occurrencesRows);
238
			List<SpecimenOrObservationBase> occurrenceList = getOccurrenceService().list(null,occurrencesRows, 0,null,null);
239
			/*List<SpecimenOrObservationBase> noProxyList = new ArrayList<SpecimenOrObservationBase>();
240
			for (SpecimenOrObservationBase specimen : occurrenceList){
241
				specimen = (SpecimenOrObservationBase)HibernateProxyHelper.deproxy(specimen);
242
				noProxyList.add(specimen);
243
			}*/
244
			dataSet.setOccurrences(occurrenceList);
245
		}
246
		
247
		if (jaxbExpConfig.isDoTypeDesignations() == true) {
248
			logger.info("# TypeDesignations");
249
			dataSet.addTypeDesignations(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
250
		}
251

    
252
		
253

    
254
		if (jaxbExpConfig.isDoMedia() == true) {
255
			if (mediaRows == 0) { mediaRows = MAX_ROWS; }
256
			logger.info("# Media");
257
			dataSet.setMedia(getMediaService().list(null,mediaRows, 0,null,null));
258
//			dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
259
//			dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
260
		}
261

    
262
		if (jaxbExpConfig.isDoFeatureData() == true) {
263
			if (featureDataRows == 0) { featureDataRows = MAX_ROWS; }
264
			logger.info("# Feature Tree, Feature Node");
265
			List<FeatureTree> featureTrees = new ArrayList<FeatureTree>();
266
			featureTrees= getFeatureTreeService().list(null,featureDataRows, 0, null, null);
267
			List<FeatureTree> taxTreesdeproxy = new ArrayList<FeatureTree>();
268
			for (FeatureTree featureTree : featureTrees){
269
				HibernateProxyHelper.deproxy(featureTree);
270
				taxTreesdeproxy.add(featureTree);
271
			}
272
			
273
			dataSet.setFeatureTrees(getFeatureTreeService().list(null,null,null,null,null));
274
		}
275
		if (jaxbExpConfig.isDoTaxonomicTreeData() == true) {
276
			if (taxonomicTreeDataRows == 0) { taxonomicTreeDataRows = MAX_ROWS; }
277
			logger.info("# Taxonomic Tree");
278
			
279
			
280
			List<TaxonomicTree> taxTrees = new ArrayList<TaxonomicTree>();
281
			taxTrees= getTaxonTreeService().list(null,taxonomicTreeDataRows, 0, null, null);
282
			
283
			List<TaxonomicTree> taxTreesdeproxy = new ArrayList<TaxonomicTree>();
284
			for (TaxonomicTree taxTree : taxTrees){
285
				HibernateProxyHelper.deproxy(taxTree);
286
				taxTreesdeproxy.add(taxTree);
287
			}
288
			List<TaxonNode> taxNodes = new ArrayList<TaxonNode>();
289
			taxNodes= getTaxonTreeService().getAllNodes();
290
			List<TaxonNode> taxNodesdeproxy = new ArrayList<TaxonNode>();
291
			for (TaxonNode taxNode : taxNodes){
292
				HibernateProxyHelper.deproxy(taxNode);
293
				taxNodesdeproxy.add(taxNode);
294
			}
295
			
296
			dataSet.setTaxonNodes(taxNodesdeproxy);
297
			dataSet.setTaxonomicTrees(taxTreesdeproxy );
298
		}
299
		//TODO: FIXME!!!!!
300
		dataSet.setLanguageStrings(null);
301
	}
302

    
303

    
304
	@Override
305
	protected boolean doCheck(JaxbExportState state) {
306
		boolean result = true;
307
		logger.warn("No check implemented for Jaxb export");
308
		return result;
309
	}
310

    
311

    
312
	@Override
313
	protected boolean isIgnore(JaxbExportState state) {
314
		return false;
315
	}
316
	
317
}
(9-9/17)