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
package eu.etaxonomy.cdm.io.pilotOutputHtml;
10

    
11
import java.io.File;
12
import java.io.FileOutputStream;
13
import java.io.OutputStreamWriter;
14
import java.io.PrintWriter;
15
import java.util.List;
16

    
17
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
18
import org.springframework.stereotype.Component;
19
import org.springframework.transaction.TransactionStatus;
20

    
21
import eu.etaxonomy.cdm.io.common.CdmExportBase;
22
import eu.etaxonomy.cdm.io.common.ICdmExport;
23
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
24
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
25
import eu.etaxonomy.cdm.io.sdd.out.SDDDataSet;
26
import eu.etaxonomy.cdm.model.agent.AgentBase;
27
import eu.etaxonomy.cdm.model.name.TaxonName;
28
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.taxon.Synonym;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
33
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
34
import eu.etaxonomy.cdm.model.term.TermType;
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 = LogManager.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
		return;
115
	}
116

    
117
	private void retrieveData (IExportConfigurator config, SDDDataSet sddDataSet) {
118

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
236
		if (pilotOutputExpConfig.isDoFeatureData() == true) {
237
			if (featureDataRows == 0) { featureDataRows = MAX_ROWS; }
238
			logger.info("# Feature Tree, Feature Node");
239
			sddDataSet.setFeatureData(getTermNodeService().list(TermType.Feature,null,null,null,null));
240
			sddDataSet.addFeatureData(getTermTreeService().list(TermType.Feature,null,null,null,null));
241
		}
242
	}
243

    
244
	@Override
245
	protected boolean doCheck(PilotOutputExportState state) {
246
		boolean result = true;
247
		logger.warn("No check implemented for Jaxb export");
248
		return result;
249
	}
250

    
251
	@Override
252
	protected boolean isIgnore(PilotOutputExportState state) {
253
		return false;
254
	}
255

    
256
    @Override
257
    public byte[] getByteArray() {
258
        // TODO Auto-generated method stub
259
        return null;
260
    }
261
}
(5-5/5)