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
 * @created 10.12.2008
39
 */
40
@Component("pilotOutputExporter")
41
public class PilotOutputExporter extends CdmExportBase<PilotOutputExportConfigurator, PilotOutputExportState, IExportTransformer> implements ICdmExport<PilotOutputExportConfigurator, PilotOutputExportState> {
42
// public class JaxbExport extends CdmIoBase implements ICdmIoExport {
43
// TODO: public class JaxbExport extends CdmIoBase implements ICdmIO {
44

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

    
49
	private boolean doOccurrence = true;
50

    
51

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

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

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

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

    
84
		// get data from DB
85

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

    
89
			retrieveData(pilotOutputExpConfig, dataSet);
90

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

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

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

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

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

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

    
116
		return;
117

    
118
	}
119

    
120

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
248

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

    
256

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

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

    
271

    
272

    
273
}
(5-5/5)