Project

General

Profile

Download (13.5 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.ByteArrayOutputStream;
13
import java.io.File;
14
import java.io.FileNotFoundException;
15
import java.io.FileOutputStream;
16
import java.io.OutputStreamWriter;
17
import java.io.PrintWriter;
18
import java.io.UnsupportedEncodingException;
19
import java.net.URI;
20
import java.util.ArrayList;
21
import java.util.List;
22

    
23
import javax.xml.transform.stream.StreamResult;
24

    
25
import org.apache.log4j.Logger;
26
import org.springframework.stereotype.Component;
27
import org.springframework.transaction.TransactionStatus;
28

    
29
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
30
import eu.etaxonomy.cdm.io.common.CdmExportBase;
31
import eu.etaxonomy.cdm.io.common.ExportDataWrapper;
32
import eu.etaxonomy.cdm.io.common.ExportResult;
33
import eu.etaxonomy.cdm.io.common.ICdmExport;
34
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
35
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
36
import eu.etaxonomy.cdm.model.agent.AgentBase;
37
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
38
import eu.etaxonomy.cdm.model.common.User;
39
import eu.etaxonomy.cdm.model.description.FeatureTree;
40
import eu.etaxonomy.cdm.model.name.TaxonName;
41
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
42
import eu.etaxonomy.cdm.model.reference.Reference;
43
import eu.etaxonomy.cdm.model.taxon.Classification;
44
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
45
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
46

    
47
/**
48
 * @author a.babadshanjan
49
 * @created 25.09.2008
50
 * @version 1.0
51
 */
52
@Component
53
public class JaxbExport extends CdmExportBase<JaxbExportConfigurator, JaxbExportState, IExportTransformer> implements ICdmExport<JaxbExportConfigurator, JaxbExportState> {
54

    
55
    private static final Logger logger = Logger.getLogger(JaxbExport.class);
56

    
57
    private DataSet dataSet;
58

    
59
    /**
60
     *
61
     */
62
    public JaxbExport() {
63
        super();
64
        this.exportData = ExportDataWrapper.NewByteArrayInstance();
65
    }
66

    
67

    
68
    //	/**
69
    //	 *
70
    //	 */
71
    //	public JaxbExport() {
72
    //		super();
73
    //		this.ioName = this.getClass().getSimpleName();
74
    //	}
75

    
76
    /** Retrieves data from a CDM DB and serializes them CDM to XML.
77
     * Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.
78
     * Taxa that are not part of the classification are not found.
79
     *
80
     * @param exImpConfig
81
     * @param dbname
82
     * @param filename
83
     */
84
    //	@Override
85
    //	protected boolean doInvoke(IExportConfigurator config,
86
    //			Map<String, MapWrapper<? extends CdmBase>> stores) {
87
    @Override
88
    protected void doInvoke(JaxbExportState state) {
89

    
90
        JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)state.getConfig();
91
        //		String dbname = jaxbExpConfig.getSource().getName();
92
        URI uri = jaxbExpConfig.getDestination();
93
        //		logger.info("Serializing DB " + dbname + " to file " + fileName);
94
        //		logger.debug("DbSchemaValidation = " + jaxbExpConfig.getDbSchemaValidation());
95

    
96
        TransactionStatus txStatus = null;
97

    
98
        txStatus = startTransaction(true);
99

    
100
        dataSet = new DataSet();
101

    
102
        // get data from DB
103

    
104
        try {
105
            logger.info("Retrieving data from DB");
106

    
107
            retrieveData(jaxbExpConfig, dataSet);
108

    
109
        } catch (Exception e) {
110
            logger.error("Error retrieving data");
111
            e.printStackTrace();
112
        }
113

    
114
        logger.info("All data retrieved");
115

    
116
        try {
117
            switch(jaxbExpConfig.getTarget()) {
118
            case FILE:
119
                writeToFile(new File(uri), dataSet);
120
                break;
121
            case EXPORT_DATA:
122
                CdmDocumentBuilder cdmDocumentBuilder = new CdmDocumentBuilder();
123
                exportStream = new ByteArrayOutputStream();
124
                cdmDocumentBuilder.marshal(dataSet, new StreamResult(exportStream));
125
                ((ExportResult)state.getResult()).addExportData((byte[])this.createExportData().getExportData());
126

    
127
                break;
128
            default:
129
                break;
130
            }
131
        } catch (Exception e) {
132
            logger.error("Marshalling error");
133
            e.printStackTrace();
134
        }
135

    
136
        commitTransaction(txStatus);
137

    
138
        return;
139

    
140
    }
141

    
142
    public static void writeToFile(File file, DataSet dataSet) throws UnsupportedEncodingException, FileNotFoundException {
143
        CdmDocumentBuilder cdmDocumentBuilder = new CdmDocumentBuilder();
144
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"), true);
145
        cdmDocumentBuilder.marshal(dataSet, writer);
146

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

    
149
        logger.info("XML file written");
150
        logger.info("Filename is: " + file.getAbsolutePath());
151
    }
152

    
153
    private void retrieveData (IExportConfigurator config, DataSet dataSet) {
154

    
155
        JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)config;
156
        final int MAX_ROWS = 50000;
157
        int numberOfRows = jaxbExpConfig.getMaxRows();
158

    
159
        int agentRows = numberOfRows;
160
        int definedTermBaseRows = numberOfRows;
161
        int referenceBaseRows = numberOfRows;
162
        int taxonNameRows = numberOfRows;
163
        int taxonBaseRows = numberOfRows;
164
        int taxonNodeRows = numberOfRows;
165
        int relationshipRows = numberOfRows;
166
        int occurrencesRows = numberOfRows;
167
        int mediaRows = numberOfRows;
168
        int featureDataRows = numberOfRows;
169
        int classificationDataRows = numberOfRows;
170
        int languageDataRows = numberOfRows;
171
        int termVocabularyRows = numberOfRows;
172
        int homotypicalGroupRows = numberOfRows;
173
        int UserRows= numberOfRows;
174

    
175
        if (jaxbExpConfig.isDoUsers() == true) {
176

    
177
            if (UserRows == 0) { UserRows = MAX_ROWS; }
178
            logger.info("# User");
179
            List<User> users = getUserService().list(null, UserRows, 0, null, null);
180

    
181

    
182
            for (User user: users){
183
                dataSet.addUser(HibernateProxyHelper.deproxy(user));
184
            }
185

    
186
        }
187
        if (jaxbExpConfig.isDoTermVocabularies() == true) {
188
            if (termVocabularyRows == 0) { termVocabularyRows = MAX_ROWS; }
189
            logger.info("# TermVocabulary");
190
            dataSet.setTermVocabularies((List)getVocabularyService().list(null,termVocabularyRows, 0, null, null));
191
        }
192

    
193

    
194

    
195
        //		if (jaxbExpConfig.isDoLanguageData() == true) {
196
        //			if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
197
        //			logger.info("# Representation, Language String");
198
        //			dataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
199
        //TODO!!!
200
        dataSet.setLanguageStrings(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
201
        //		}
202

    
203
        if (jaxbExpConfig.isDoTerms() == true) {
204
            if (definedTermBaseRows == 0) { definedTermBaseRows = getTermService().count(DefinedTermBase.class); }
205
            logger.info("# DefinedTermBase: " + definedTermBaseRows);
206
            dataSet.setTerms(getTermService().list(null,definedTermBaseRows, 0,null,null));
207
        }
208

    
209
        if (jaxbExpConfig.isDoAuthors() == true) {
210
            if (agentRows == 0) { agentRows = getAgentService().count(AgentBase.class); }
211
            logger.info("# Agents: " + agentRows);
212
            //logger.info("    # Team: " + appCtr.getAgentService().count(Team.class));
213
            dataSet.setAgents(getAgentService().list(null,agentRows, 0,null,null));
214
        }
215

    
216

    
217

    
218
        if (jaxbExpConfig.getDoReferences() != IExportConfigurator.DO_REFERENCES.NONE) {
219
            if (referenceBaseRows == 0) { referenceBaseRows = getReferenceService().count(Reference.class); }
220
            logger.info("# Reference: " + referenceBaseRows);
221
            dataSet.setReferences(getReferenceService().list(null,referenceBaseRows, 0,null,null));
222
        }
223

    
224
        if (jaxbExpConfig.isDoHomotypicalGroups() == true) {
225
            if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
226
            logger.info("# Homotypical Groups");
227
            dataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
228
        }
229

    
230
        if (jaxbExpConfig.isDoTaxonNames() == true) {
231
            if (taxonNameRows == 0) { taxonNameRows = getNameService().count(TaxonName.class); }
232
            logger.info("# TaxonName: " + taxonNameRows);
233
            //logger.info("    # Taxon: " + getNameService().count(BotanicalName.class));
234
            dataSet.setTaxonomicNames(getNameService().list(null,taxonNameRows, 0,null,null));
235
        }
236

    
237

    
238

    
239
        if (jaxbExpConfig.isDoTaxa() == true) {
240
            if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
241
            logger.info("# TaxonBase: " + taxonBaseRows);
242
            //			dataSet.setTaxa(new ArrayList<Taxon>());
243
            //			dataSet.setSynonyms(new ArrayList<Synonym>());
244
            List<TaxonBase> tb = getTaxonService().list(null,taxonBaseRows, 0,null,null);
245
            for (TaxonBase taxonBase : tb) {
246
                dataSet.addTaxonBase(HibernateProxyHelper.deproxy(taxonBase));
247
            }
248
        }
249

    
250
        // TODO:
251
        // retrieve taxa and synonyms separately
252
        // need correct count for taxa and synonyms
253
        //		if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
254
        //		logger.info("# Synonym: " + taxonBaseRows);
255
        //		dataSet.setSynonyms(new ArrayList<Synonym>());
256
        //		dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
257

    
258
        //		if (jaxbExpConfig.isDoRelTaxa() == true) {
259
        //			if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
260
        //			logger.info("# Relationships");
261
        //			List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
262
        //			Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
263
        //			dataSet.setRelationships(relationSet);
264
        //		}
265
        if (jaxbExpConfig.isDoOccurrence() == true) {
266
            if (occurrencesRows == 0) { occurrencesRows = getOccurrenceService().count(SpecimenOrObservationBase.class); }
267
            logger.info("# SpecimenOrObservationBase: " + occurrencesRows);
268
            List<SpecimenOrObservationBase> occurrenceList = getOccurrenceService().list(null,occurrencesRows, 0,null,null);
269
            /*List<SpecimenOrObservationBase> noProxyList = new ArrayList<SpecimenOrObservationBase>();
270
			for (SpecimenOrObservationBase specimen : occurrenceList){
271
				specimen = (SpecimenOrObservationBase)HibernateProxyHelper.deproxy(specimen);
272
				noProxyList.add(specimen);
273
			}*/
274
            dataSet.setOccurrences(occurrenceList);
275
        }
276

    
277
        if (jaxbExpConfig.isDoTypeDesignations() == true) {
278
            logger.info("# TypeDesignations");
279
            dataSet.addTypeDesignations(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
280
        }
281

    
282

    
283

    
284
        if (jaxbExpConfig.isDoMedia() == true) {
285
            if (mediaRows == 0) { mediaRows = MAX_ROWS; }
286
            logger.info("# Media");
287
            dataSet.setMedia(getMediaService().list(null,mediaRows, 0,null,null));
288
            //			dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
289
            //			dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
290
        }
291

    
292
        if (jaxbExpConfig.isDoFeatureData() == true) {
293
            if (featureDataRows == 0) { featureDataRows = MAX_ROWS; }
294
            logger.info("# Feature Tree, Feature Node");
295
            List<FeatureTree> featureTrees = new ArrayList<FeatureTree>();
296
            featureTrees= getFeatureTreeService().list(null,featureDataRows, 0, null, null);
297
            List<FeatureTree> taxTreesdeproxy = new ArrayList<FeatureTree>();
298
            for (FeatureTree featureTree : featureTrees){
299
                HibernateProxyHelper.deproxy(featureTree);
300
                taxTreesdeproxy.add(featureTree);
301
            }
302

    
303
            dataSet.setFeatureTrees(getFeatureTreeService().list(null,null,null,null,null));
304
        }
305
        if (jaxbExpConfig.isDoClassificationData() == true) {
306
            if (classificationDataRows == 0) { classificationDataRows = MAX_ROWS; }
307
            logger.info("# Classification");
308

    
309

    
310
            List<Classification> taxTrees = new ArrayList<Classification>();
311
            taxTrees= getClassificationService().list(null,classificationDataRows, 0, null, null);
312

    
313
            List<Classification> taxTreesdeproxy = new ArrayList<Classification>();
314
            for (Classification taxTree : taxTrees){
315
                HibernateProxyHelper.deproxy(taxTree);
316
                taxTreesdeproxy.add(taxTree);
317
            }
318
            List<TaxonNode> taxNodes = new ArrayList<TaxonNode>();
319
            taxNodes= getClassificationService().getAllNodes();
320
            List<TaxonNode> taxNodesdeproxy = new ArrayList<TaxonNode>();
321
            for (TaxonNode taxNode : taxNodes){
322
                HibernateProxyHelper.deproxy(taxNode);
323
                taxNodesdeproxy.add(taxNode);
324
            }
325

    
326
            dataSet.setTaxonNodes(taxNodesdeproxy);
327
            dataSet.setClassifications(taxTreesdeproxy );
328
        }
329
        //TODO: FIXME!!!!!
330
        dataSet.setLanguageStrings(null);
331
    }
332

    
333

    
334
    @Override
335
    protected boolean doCheck(JaxbExportState state) {
336
        boolean result = true;
337
        logger.warn("No check implemented for Jaxb export");
338
        return result;
339
    }
340

    
341

    
342
    @Override
343
    protected boolean isIgnore(JaxbExportState state) {
344
        return false;
345
    }
346

    
347

    
348

    
349
}
(9-9/17)