Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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.pilotOutputHtml;
11

    
12
import java.io.File;
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;
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.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.out.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.Reference;
35
import eu.etaxonomy.cdm.model.taxon.Synonym;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38

    
39
/**
40
 * @author h.fradin (from a.babadshanjan)
41
 * @created 10.12.2008
42
 * @versoin 1.0
43
 */
44
@Component("pilotOutputExporter")
45
public class PilotOutputExporter 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 {
48

    
49
	private static final Logger logger = Logger.getLogger(PilotOutputExporter.class);
50
	private PilotOutputDocumentBuilder pilotOutputDocumentBuilder = null;
51

    
52
	private boolean doOccurrence = true;
53

    
54
	
55
	/**
56
	 * 
57
	 */
58
	public PilotOutputExporter() {
59
		super();
60
		this.ioName = this.getClass().getSimpleName();
61
	}
62

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

    
82
		TransactionStatus txStatus = startTransaction(true);
83
		SDDDataSet dataSet = new SDDDataSet();
84
		List<Taxon> taxa = null;
85
		List<DefinedTermBase> terms = null;
86

    
87
		// get data from DB
88

    
89
		try {
90
			logger.info("Retrieving data from DB");
91

    
92
			retrieveData(pilotOutputExpConfig, dataSet);
93

    
94
		} catch (Exception e) {
95
			logger.error("Error retrieving data");
96
			e.printStackTrace();
97
		}
98

    
99
		logger.info("All data retrieved");
100

    
101
		try {
102
			pilotOutputDocumentBuilder = new PilotOutputDocumentBuilder();
103
			File f = new File(fileName);
104
			FileOutputStream fos = new FileOutputStream(f);
105
			PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos, "UTF8"), true);
106
			pilotOutputDocumentBuilder.marshal(dataSet, fileName);
107

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

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

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

    
121
	}
122

    
123

    
124
	private void retrieveData (IExportConfigurator config, SDDDataSet sddDataSet) {
125

    
126
		PilotOutputExportConfigurator pilotOutputExpConfig = (PilotOutputExportConfigurator)config;
127
		final int MAX_ROWS = 50000;
128
		int numberOfRows = pilotOutputExpConfig.getMaxRows();
129
		// TODO: 
130
		//CdmApplicationController appCtr = config.getCdmAppController(false, true);
131

    
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;
144

    
145
		if (pilotOutputExpConfig.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));;
149
		}
150

    
151
		if (pilotOutputExpConfig.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));
156
		}
157

    
158
		if (pilotOutputExpConfig.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));
162
		}
163

    
164
		if (pilotOutputExpConfig.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));
169
		}
170

    
171
		if (pilotOutputExpConfig.getDoReferences() != IExportConfigurator.DO_REFERENCES.NONE) {
172
			if (referenceBaseRows == 0) { referenceBaseRows = getReferenceService().count(Reference.class); }
173
			logger.info("# Reference: " + referenceBaseRows);
174
			sddDataSet.setReferences(getReferenceService().list(null,referenceBaseRows, 0,null,null));
175
		}
176

    
177
		if (pilotOutputExpConfig.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));
182
		}
183

    
184
		if (pilotOutputExpConfig.isDoHomotypicalGroups() == true) {
185
			if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
186
			logger.info("# Homotypical Groups");
187
			sddDataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
188
		}
189

    
190
		if (pilotOutputExpConfig.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);
201
				} else {
202
					logger.error("entry of wrong type: " + taxonBase.toString());
203
				}
204
			}
205
		}
206

    
207
		// TODO: 
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));
214

    
215
		if (pilotOutputExpConfig.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);
221
		}
222

    
223
		if (pilotOutputExpConfig.isDoReferencedEntities() == true) {
224
			logger.info("# Referenced Entities");
225
			sddDataSet.setReferencedEntities(getNameService().getAllNomenclaturalStatus(MAX_ROWS, 0));
226
			sddDataSet.addReferencedEntities(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
227
		}
228

    
229
		if (pilotOutputExpConfig.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));
233
		}
234

    
235
		if (pilotOutputExpConfig.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));
241
		}
242

    
243
		if (pilotOutputExpConfig.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));
248
		}
249
	}
250

    
251

    
252
	@Override
253
	protected boolean doCheck(PilotOutputExportState state) {
254
		boolean result = true;
255
		logger.warn("No check implemented for Jaxb export");
256
		return result;
257
	}
258

    
259

    
260
	@Override
261
	protected boolean isIgnore(PilotOutputExportState state) {
262
		return false;
263
	}
264

    
265

    
266
	
267
}
(5-5/5)