Project

General

Profile

Download (13.2 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.ICdmExport;
32
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
33
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
34
import eu.etaxonomy.cdm.model.agent.AgentBase;
35
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
36
import eu.etaxonomy.cdm.model.common.User;
37
import eu.etaxonomy.cdm.model.description.FeatureTree;
38
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
39
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
40
import eu.etaxonomy.cdm.model.reference.Reference;
41
import eu.etaxonomy.cdm.model.taxon.Classification;
42
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
44

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

    
53
    private static final Logger logger = Logger.getLogger(JaxbExport.class);
54

    
55
    private DataSet dataSet;
56

    
57

    
58
    //	/**
59
    //	 *
60
    //	 */
61
    //	public JaxbExport() {
62
    //		super();
63
    //		this.ioName = this.getClass().getSimpleName();
64
    //	}
65

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

    
80
        JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)state.getConfig();
81
        //		String dbname = jaxbExpConfig.getSource().getName();
82
        URI uri = jaxbExpConfig.getDestination();
83
        //		logger.info("Serializing DB " + dbname + " to file " + fileName);
84
        //		logger.debug("DbSchemaValidation = " + jaxbExpConfig.getDbSchemaValidation());
85

    
86
        TransactionStatus txStatus = null;
87

    
88
        txStatus = startTransaction(true);
89

    
90
        dataSet = new DataSet();
91

    
92
        // get data from DB
93

    
94
        try {
95
            logger.info("Retrieving data from DB");
96

    
97
            retrieveData(jaxbExpConfig, dataSet);
98

    
99
        } catch (Exception e) {
100
            logger.error("Error retrieving data");
101
            e.printStackTrace();
102
        }
103

    
104
        logger.info("All data retrieved");
105

    
106
        try {
107
            switch(jaxbExpConfig.getTarget()) {
108
            case FILE:
109
                writeToFile(new File(uri), dataSet);
110
                break;
111
            case EXPORT_DATA:
112
                CdmDocumentBuilder cdmDocumentBuilder = new CdmDocumentBuilder();
113
                exportStream = new ByteArrayOutputStream();
114
                cdmDocumentBuilder.marshal(dataSet, new StreamResult(exportStream));
115
                break;
116
            default:
117
                break;
118
            }
119
        } catch (Exception e) {
120
            logger.error("Marshalling error");
121
            e.printStackTrace();
122
        }
123

    
124
        commitTransaction(txStatus);
125

    
126
        return;
127

    
128
    }
129

    
130
    public static void writeToFile(File file, DataSet dataSet) throws UnsupportedEncodingException, FileNotFoundException {
131
        CdmDocumentBuilder cdmDocumentBuilder = new CdmDocumentBuilder();
132
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"), true);
133
        cdmDocumentBuilder.marshal(dataSet, writer);
134

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

    
137
        logger.info("XML file written");
138
        logger.info("Filename is: " + file.getAbsolutePath());
139
    }
140

    
141
    private void retrieveData (IExportConfigurator config, DataSet dataSet) {
142

    
143
        JaxbExportConfigurator jaxbExpConfig = (JaxbExportConfigurator)config;
144
        final int MAX_ROWS = 50000;
145
        int numberOfRows = jaxbExpConfig.getMaxRows();
146

    
147
        int agentRows = numberOfRows;
148
        int definedTermBaseRows = numberOfRows;
149
        int referenceBaseRows = numberOfRows;
150
        int taxonNameBaseRows = numberOfRows;
151
        int taxonBaseRows = numberOfRows;
152
        int taxonNodeRows = numberOfRows;
153
        int relationshipRows = numberOfRows;
154
        int occurrencesRows = numberOfRows;
155
        int mediaRows = numberOfRows;
156
        int featureDataRows = numberOfRows;
157
        int classificationDataRows = numberOfRows;
158
        int languageDataRows = numberOfRows;
159
        int termVocabularyRows = numberOfRows;
160
        int homotypicalGroupRows = numberOfRows;
161
        int UserRows= numberOfRows;
162

    
163
        if (jaxbExpConfig.isDoUsers() == true) {
164

    
165
            if (UserRows == 0) { UserRows = MAX_ROWS; }
166
            logger.info("# User");
167
            List<User> users = getUserService().list(null, UserRows, 0, null, null);
168

    
169

    
170
            for (User user: users){
171
                dataSet.addUser(HibernateProxyHelper.deproxy(user));
172
            }
173

    
174
        }
175
        if (jaxbExpConfig.isDoTermVocabularies() == true) {
176
            if (termVocabularyRows == 0) { termVocabularyRows = MAX_ROWS; }
177
            logger.info("# TermVocabulary");
178
            dataSet.setTermVocabularies((List)getVocabularyService().list(null,termVocabularyRows, 0, null, null));
179
        }
180

    
181

    
182

    
183
        //		if (jaxbExpConfig.isDoLanguageData() == true) {
184
        //			if (languageDataRows == 0) { languageDataRows = MAX_ROWS; }
185
        //			logger.info("# Representation, Language String");
186
        //			dataSet.setLanguageData(getTermService().getAllRepresentations(MAX_ROWS, 0));
187
        //TODO!!!
188
        dataSet.setLanguageStrings(getTermService().getAllLanguageStrings(MAX_ROWS, 0));
189
        //		}
190

    
191
        if (jaxbExpConfig.isDoTerms() == true) {
192
            if (definedTermBaseRows == 0) { definedTermBaseRows = getTermService().count(DefinedTermBase.class); }
193
            logger.info("# DefinedTermBase: " + definedTermBaseRows);
194
            dataSet.setTerms(getTermService().list(null,definedTermBaseRows, 0,null,null));
195
        }
196

    
197
        if (jaxbExpConfig.isDoAuthors() == true) {
198
            if (agentRows == 0) { agentRows = getAgentService().count(AgentBase.class); }
199
            logger.info("# Agents: " + agentRows);
200
            //logger.info("    # Team: " + appCtr.getAgentService().count(Team.class));
201
            dataSet.setAgents(getAgentService().list(null,agentRows, 0,null,null));
202
        }
203

    
204

    
205

    
206
        if (jaxbExpConfig.getDoReferences() != IExportConfigurator.DO_REFERENCES.NONE) {
207
            if (referenceBaseRows == 0) { referenceBaseRows = getReferenceService().count(Reference.class); }
208
            logger.info("# Reference: " + referenceBaseRows);
209
            dataSet.setReferences(getReferenceService().list(null,referenceBaseRows, 0,null,null));
210
        }
211

    
212
        if (jaxbExpConfig.isDoHomotypicalGroups() == true) {
213
            if (homotypicalGroupRows == 0) { homotypicalGroupRows = MAX_ROWS; }
214
            logger.info("# Homotypical Groups");
215
            dataSet.setHomotypicalGroups(getNameService().getAllHomotypicalGroups(homotypicalGroupRows, 0));
216
        }
217

    
218
        if (jaxbExpConfig.isDoTaxonNames() == true) {
219
            if (taxonNameBaseRows == 0) { taxonNameBaseRows = getNameService().count(TaxonNameBase.class); }
220
            logger.info("# TaxonNameBase: " + taxonNameBaseRows);
221
            //logger.info("    # Taxon: " + getNameService().count(BotanicalName.class));
222
            dataSet.setTaxonomicNames(getNameService().list(null,taxonNameBaseRows, 0,null,null));
223
        }
224

    
225

    
226

    
227
        if (jaxbExpConfig.isDoTaxa() == true) {
228
            if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
229
            logger.info("# TaxonBase: " + taxonBaseRows);
230
            //			dataSet.setTaxa(new ArrayList<Taxon>());
231
            //			dataSet.setSynonyms(new ArrayList<Synonym>());
232
            List<TaxonBase> tb = getTaxonService().list(null,taxonBaseRows, 0,null,null);
233
            for (TaxonBase taxonBase : tb) {
234
                dataSet.addTaxonBase(HibernateProxyHelper.deproxy(taxonBase));
235
            }
236
        }
237

    
238
        // TODO:
239
        // retrieve taxa and synonyms separately
240
        // need correct count for taxa and synonyms
241
        //		if (taxonBaseRows == 0) { taxonBaseRows = getTaxonService().count(TaxonBase.class); }
242
        //		logger.info("# Synonym: " + taxonBaseRows);
243
        //		dataSet.setSynonyms(new ArrayList<Synonym>());
244
        //		dataSet.setSynonyms(getTaxonService().getAllSynonyms(taxonBaseRows, 0));
245

    
246
        //		if (jaxbExpConfig.isDoRelTaxa() == true) {
247
        //			if (relationshipRows == 0) { relationshipRows = MAX_ROWS; }
248
        //			logger.info("# Relationships");
249
        //			List<RelationshipBase> relationList = getTaxonService().getAllRelationships(relationshipRows, 0);
250
        //			Set<RelationshipBase> relationSet = new HashSet<RelationshipBase>(relationList);
251
        //			dataSet.setRelationships(relationSet);
252
        //		}
253
        if (jaxbExpConfig.isDoOccurrence() == true) {
254
            if (occurrencesRows == 0) { occurrencesRows = getOccurrenceService().count(SpecimenOrObservationBase.class); }
255
            logger.info("# SpecimenOrObservationBase: " + occurrencesRows);
256
            List<SpecimenOrObservationBase> occurrenceList = getOccurrenceService().list(null,occurrencesRows, 0,null,null);
257
            /*List<SpecimenOrObservationBase> noProxyList = new ArrayList<SpecimenOrObservationBase>();
258
			for (SpecimenOrObservationBase specimen : occurrenceList){
259
				specimen = (SpecimenOrObservationBase)HibernateProxyHelper.deproxy(specimen);
260
				noProxyList.add(specimen);
261
			}*/
262
            dataSet.setOccurrences(occurrenceList);
263
        }
264

    
265
        if (jaxbExpConfig.isDoTypeDesignations() == true) {
266
            logger.info("# TypeDesignations");
267
            dataSet.addTypeDesignations(getNameService().getAllTypeDesignations(MAX_ROWS, 0));
268
        }
269

    
270

    
271

    
272
        if (jaxbExpConfig.isDoMedia() == true) {
273
            if (mediaRows == 0) { mediaRows = MAX_ROWS; }
274
            logger.info("# Media");
275
            dataSet.setMedia(getMediaService().list(null,mediaRows, 0,null,null));
276
            //			dataSet.addMedia(getMediaService().getAllMediaRepresentations(mediaRows, 0));
277
            //			dataSet.addMedia(getMediaService().getAllMediaRepresentationParts(mediaRows, 0));
278
        }
279

    
280
        if (jaxbExpConfig.isDoFeatureData() == true) {
281
            if (featureDataRows == 0) { featureDataRows = MAX_ROWS; }
282
            logger.info("# Feature Tree, Feature Node");
283
            List<FeatureTree> featureTrees = new ArrayList<FeatureTree>();
284
            featureTrees= getFeatureTreeService().list(null,featureDataRows, 0, null, null);
285
            List<FeatureTree> taxTreesdeproxy = new ArrayList<FeatureTree>();
286
            for (FeatureTree featureTree : featureTrees){
287
                HibernateProxyHelper.deproxy(featureTree);
288
                taxTreesdeproxy.add(featureTree);
289
            }
290

    
291
            dataSet.setFeatureTrees(getFeatureTreeService().list(null,null,null,null,null));
292
        }
293
        if (jaxbExpConfig.isDoClassificationData() == true) {
294
            if (classificationDataRows == 0) { classificationDataRows = MAX_ROWS; }
295
            logger.info("# Classification");
296

    
297

    
298
            List<Classification> taxTrees = new ArrayList<Classification>();
299
            taxTrees= getClassificationService().list(null,classificationDataRows, 0, null, null);
300

    
301
            List<Classification> taxTreesdeproxy = new ArrayList<Classification>();
302
            for (Classification taxTree : taxTrees){
303
                HibernateProxyHelper.deproxy(taxTree);
304
                taxTreesdeproxy.add(taxTree);
305
            }
306
            List<TaxonNode> taxNodes = new ArrayList<TaxonNode>();
307
            taxNodes= getClassificationService().getAllNodes();
308
            List<TaxonNode> taxNodesdeproxy = new ArrayList<TaxonNode>();
309
            for (TaxonNode taxNode : taxNodes){
310
                HibernateProxyHelper.deproxy(taxNode);
311
                taxNodesdeproxy.add(taxNode);
312
            }
313

    
314
            dataSet.setTaxonNodes(taxNodesdeproxy);
315
            dataSet.setClassifications(taxTreesdeproxy );
316
        }
317
        //TODO: FIXME!!!!!
318
        dataSet.setLanguageStrings(null);
319
    }
320

    
321

    
322
    @Override
323
    protected boolean doCheck(JaxbExportState state) {
324
        boolean result = true;
325
        logger.warn("No check implemented for Jaxb export");
326
        return result;
327
    }
328

    
329

    
330
    @Override
331
    protected boolean isIgnore(JaxbExportState state) {
332
        return false;
333
    }
334

    
335
}
(9-9/17)