Project

General

Profile

Download (10.3 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.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.io.common.CdmExportBase;
23
import eu.etaxonomy.cdm.io.common.ICdmExport;
24
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
25
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
26
import eu.etaxonomy.cdm.io.sdd.out.SDDDataSet;
27
import eu.etaxonomy.cdm.model.agent.AgentBase;
28
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
29
import eu.etaxonomy.cdm.model.name.TaxonName;
30
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.taxon.Synonym;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35

    
36
/**
37
 * @author h.fradin (from a.babadshanjan)
38
 * @since 10.12.2008
39
 */
40
@Component("pilotOutputExporter")
41
public class PilotOutputExporter
42
        extends CdmExportBase<PilotOutputExportConfigurator, PilotOutputExportState, IExportTransformer, File>
43
        implements ICdmExport<PilotOutputExportConfigurator, PilotOutputExportState> {
44
// public class JaxbExport extends CdmIoBase implements ICdmIoExport {
45
// TODO: public class JaxbExport extends CdmIoBase implements ICdmIO {
46

    
47
    private static final long serialVersionUID = -5556376038773540067L;
48
    private static final Logger logger = Logger.getLogger(PilotOutputExporter.class);
49
	private PilotOutputDocumentBuilder pilotOutputDocumentBuilder = null;
50

    
51
	private boolean doOccurrence = true;
52

    
53

    
54
	public PilotOutputExporter() {
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 classification to retrieve children taxa, synonyms and relationships.
61
	 * Taxa that are not part of the classification are not found.
62
	 *
63
	 * @param exImpConfig
64
	 * @param dbname
65
	 * @param filename
66
	 */
67
	@Override
68
	protected void doInvoke(PilotOutputExportState state){
69
//		protected boolean doInvoke(IExportConfigurator config,
70
//		Map<String, MapWrapper<? extends CdmBase>> stores) {
71

    
72
		PilotOutputExportConfigurator pilotOutputExpConfig = state.getConfig();
73
		String dbname = pilotOutputExpConfig.getSource().getName();
74
    	String fileName = pilotOutputExpConfig.getDestinationNameString();
75
		logger.info("Serializing DB " + dbname + " to file " + fileName);
76
		logger.debug("DbSchemaValidation = " + pilotOutputExpConfig.getDbSchemaValidation());
77

    
78
		TransactionStatus txStatus = startTransaction(true);
79
		SDDDataSet dataSet = new SDDDataSet();
80
		List<Taxon> taxa = null;
81
		List<DefinedTermBase> terms = null;
82

    
83
		// get data from DB
84

    
85
		try {
86
			logger.info("Retrieving data from DB");
87

    
88
			retrieveData(pilotOutputExpConfig, dataSet);
89

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

    
95
		logger.info("All data retrieved");
96

    
97
		try {
98
			pilotOutputDocumentBuilder = new PilotOutputDocumentBuilder();
99
			File f = new File(fileName);
100
			FileOutputStream fos = new FileOutputStream(f);
101
			PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos, "UTF8"), true);
102
			pilotOutputDocumentBuilder.marshal(dataSet, fileName);
103

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

    
106
			logger.info("XML file written");
107
			logger.info("Filename is: " + fileName);
108

    
109
		} catch (Exception e) {
110
			logger.error("Marshalling error");
111
			e.printStackTrace();
112
		}
113
		commitTransaction(txStatus);
114

    
115
		return;
116

    
117
	}
118

    
119

    
120
	private void retrieveData (IExportConfigurator config, SDDDataSet sddDataSet) {
121

    
122
		PilotOutputExportConfigurator pilotOutputExpConfig = (PilotOutputExportConfigurator)config;
123
		final int MAX_ROWS = 50000;
124
		int numberOfRows = pilotOutputExpConfig.getMaxRows();
125
		// TODO:
126
		//CdmApplicationController appCtr = config.getCdmAppController(false, true);
127

    
128
		int agentRows = numberOfRows;
129
		int definedTermBaseRows = numberOfRows;
130
		int referenceBaseRows = numberOfRows;
131
		int taxonNameRows = numberOfRows;
132
		int taxonBaseRows = numberOfRows;
133
		int relationshipRows = numberOfRows;
134
		int occurrencesRows = numberOfRows;
135
		int mediaRows = numberOfRows;
136
		int featureDataRows = numberOfRows;
137
		int languageDataRows = numberOfRows;
138
		int termVocabularyRows = numberOfRows;
139
		int homotypicalGroupRows = numberOfRows;
140

    
141
		if (pilotOutputExpConfig.isDoTermVocabularies() == true) {
142
			if (termVocabularyRows == 0) { termVocabularyRows = MAX_ROWS; }
143
			logger.info("# TermVocabulary");
144
			sddDataSet.setTermVocabularies((List)getVocabularyService().list(null,MAX_ROWS, 0,null,null));;
145
		}
146

    
147
		if (pilotOutputExpConfig.isDoLanguageData() == true) {
148
			if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
149
			logger.info("# Representation, Language String");
150
			sddDataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
151
			sddDataSet.addLanguageData(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
152
		}
153

    
154
		if (pilotOutputExpConfig.isDoTerms() == true) {
155
			if (definedTermBaseRows == 0) { definedTermBaseRows = getTermService().count(DefinedTermBase.class); }
156
			logger.info("# DefinedTermBase: " + definedTermBaseRows);
157
			sddDataSet.setTerms(getTermService().list(null,definedTermBaseRows, 0,null,null));
158
		}
159

    
160
		if (pilotOutputExpConfig.isDoAuthors() == true) {
161
			if (agentRows == 0) { agentRows = getAgentService().count(AgentBase.class); }
162
			logger.info("# Agents: " + agentRows);
163
			//logger.info("    # Team: " + getAgentService().count(Team.class));
164
			sddDataSet.setAgents(getAgentService().list(null,agentRows, 0,null,null));
165
		}
166

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

    
173
		if (pilotOutputExpConfig.isDoTaxonNames() == true) {
174
			if (taxonNameRows == 0) { taxonNameRows = getNameService().count(TaxonName.class); }
175
			logger.info("# TaxonName: " + taxonNameRows);
176
			//logger.info("    # Taxon: " + getNameService().count(BotanicalName.class));
177
			sddDataSet.setTaxonomicNames(getNameService().list(null,taxonNameRows, 0,null,null));
178
		}
179

    
180
		if (pilotOutputExpConfig.isDoHomotypicalGroups() == true) {
181
			if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
182
			logger.info("# Homotypical Groups");
183
			sddDataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
184
		}
185

    
186
		if (pilotOutputExpConfig.isDoTaxa() == true) {
187
			if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
188
			logger.info("# TaxonBase: " + taxonBaseRows);
189
//			dataSet.setTaxa(new ArrayList<Taxon>());
190
//			dataSet.setSynonyms(new ArrayList<Synonym>());
191
			List<TaxonBase> tb = getTaxonService().list(null,taxonBaseRows, 0,null,null);
192
			for (TaxonBase taxonBase : tb) {
193
				if (taxonBase instanceof Taxon) {
194
					sddDataSet.addTaxon((Taxon)taxonBase);
195
				} else if (taxonBase instanceof Synonym) {
196
					sddDataSet.addSynonym((Synonym)taxonBase);
197
				} else {
198
					logger.error("entry of wrong type: " + taxonBase.toString());
199
				}
200
			}
201
		}
202

    
203
		// TODO:
204
		// retrieve taxa and synonyms separately
205
		// need correct count for taxa and synonyms
206
//		if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
207
//		logger.info("# Synonym: " + taxonBaseRows);
208
//		dataSet.setSynonyms(new ArrayList<Synonym>());
209
//		dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
210
//
211
//		if (pilotOutputExpConfig.isDoRelTaxa() == true) {
212
//			if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
213
//			logger.info("# Relationships");
214
//			List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
215
//			Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
216
//			sddDataSet.setRelationships(relationSet);
217
//		}
218

    
219
		if (pilotOutputExpConfig.isDoReferencedEntities() == true) {
220
			logger.info("# Referenced Entities");
221
			sddDataSet.setReferencedEntities(getNameService().getAllNomenclaturalStatus(MAX_ROWS, 0));
222
			sddDataSet.addSourcedEntities(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
223
		}
224

    
225
		if (pilotOutputExpConfig.isDoOccurrence() == true) {
226
			if (occurrencesRows == 0) { occurrencesRows = getOccurrenceService().count(SpecimenOrObservationBase.class); }
227
			logger.info("# SpecimenOrObservationBase: " + occurrencesRows);
228
			sddDataSet.setOccurrences(getOccurrenceService().list(null,occurrencesRows, 0,null,null));
229
		}
230

    
231
		if (pilotOutputExpConfig.isDoMedia() == true) {
232
			if (mediaRows == 0) { mediaRows = MAX_ROWS; }
233
			logger.info("# Media");
234
			sddDataSet.setMedia(getMediaService().list(null,mediaRows, 0,null,null));
235
//			dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
236
//			dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
237
		}
238

    
239
		if (pilotOutputExpConfig.isDoFeatureData() == true) {
240
			if (featureDataRows == 0) { featureDataRows = MAX_ROWS; }
241
			logger.info("# Feature Tree, Feature Node");
242
			sddDataSet.setFeatureData(getFeatureTreeService().getFeatureNodesAll());
243
			sddDataSet.addFeatureData(getFeatureTreeService().list(null,null,null,null,null));
244
		}
245
	}
246

    
247

    
248
	@Override
249
	protected boolean doCheck(PilotOutputExportState state) {
250
		boolean result = true;
251
		logger.warn("No check implemented for Jaxb export");
252
		return result;
253
	}
254

    
255

    
256
	@Override
257
	protected boolean isIgnore(PilotOutputExportState state) {
258
		return false;
259
	}
260

    
261
    /**
262
     * {@inheritDoc}
263
     */
264
    @Override
265
    public byte[] getByteArray() {
266
        // TODO Auto-generated method stub
267
        return null;
268
    }
269

    
270

    
271

    
272
}
(5-5/5)