Project

General

Profile

Download (4.27 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.dwca.out;
11

    
12
import java.io.FileNotFoundException;
13
import java.io.IOException;
14
import java.io.UnsupportedEncodingException;
15
import java.util.Arrays;
16
import java.util.List;
17

    
18
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.stereotype.Component;
21

    
22
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
23
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
24
import eu.etaxonomy.cdm.filter.TaxonNodeFilter;
25
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitioner;
26
import eu.etaxonomy.cdm.io.common.XmlExportState;
27
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28

    
29
/**
30
 * @author a.mueller
31
 * @since 18.04.2011
32
 */
33
@Component
34
public class DwcaTaxExport extends DwcaExportBase {
35
    private static final long serialVersionUID = -3770976064909193441L;
36

    
37
    private static final Logger logger = LogManager.getLogger(DwcaTaxExport.class);
38

    
39

    
40
    @Autowired
41
    private ITaxonNodeService taxonNodeService;
42

    
43
	public DwcaTaxExport() {
44
		super();
45
		this.ioName = this.getClass().getSimpleName();
46
        file = DwcaTaxExportFile.TAXON;
47
	}
48

    
49

    
50
    /**
51
     * {@inheritDoc}
52
     */
53
    @Override
54
    public long countSteps(DwcaTaxExportState state) {
55
        TaxonNodeFilter filter = state.getConfig().getTaxonNodeFilter();
56
        return taxonNodeService.count(filter);
57
    }
58

    
59
	/**
60
	 * Retrieves data from a CDM DB and serializes the 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
	 * <BR>
64
	 * {@inheritDoc}
65
	 */
66
	@Override
67
	protected void doInvoke(DwcaTaxExportState state){
68

    
69
	    IProgressMonitor monitor = state.getConfig().getProgressMonitor();
70

    
71
		List<DwcaDataExportBase> exports =  Arrays.asList(new DwcaDataExportBase[]{
72
	        new DwcaTaxonExport(state),
73
	        new DwcaReferenceExport(state),
74
	        new DwcaResourceRelationExport(state),
75
	        new DwcaTypesExport(state),
76
	        new DwcaVernacularExport(state),
77
	        new DwcaDescriptionExport(state),
78
	        new DwcaDistributionExport(state),
79
	        new DwcaImageExport(state)
80
		});
81

    
82

    
83
		@SuppressWarnings("unchecked")
84
	    TaxonNodeOutStreamPartitioner<XmlExportState> partitioner
85
	          = TaxonNodeOutStreamPartitioner.NewInstance(
86
                    this, state, state.getConfig().getTaxonNodeFilter(),
87
                    100, monitor, null);
88
		try {
89

    
90
		    monitor.subTask("Start partitioning");
91

    
92
		    TaxonNode node = partitioner.next();
93
			while (node != null){
94
			    for (DwcaDataExportBase export : exports){
95
			        handleTaxonNode(export, state, node);
96
			    }
97
			    node = partitioner.next();
98
			}
99
		} catch (Exception e) {
100
		    String message = "Unexpected exception: " + e.getMessage();
101
			state.getResult().addException(e, message, "DwcaTaxExport.doInvoke()");
102
		}
103
		finally{
104
		    if(partitioner != null){
105
		        partitioner.close();
106
		    }
107
		    for (DwcaDataExportBase export : exports){
108
		        closeWriter(export, state);
109
            }
110
		}
111

    
112
		return;
113

    
114
	}
115

    
116
    /**
117
     * @param taxonExport
118
     * @param state
119
     */
120
    private void closeWriter(DwcaDataExportBase export, DwcaTaxExportState state) {
121
        if (!export.isIgnore(state)){
122
            export.closeWriter(state);
123
        }
124
    }
125

    
126
    private void handleTaxonNode(DwcaDataExportBase export, DwcaTaxExportState state, TaxonNode node) throws FileNotFoundException, UnsupportedEncodingException, IOException {
127
        if (!export.isIgnore(state)){
128
            export.handleTaxonNode(state, node);
129
        }
130
    }
131

    
132

    
133
	@Override
134
	protected boolean doCheck(DwcaTaxExportState state) {
135
		boolean result = true;
136
		logger.warn("No check implemented for " + this.ioName);
137
		return result;
138
	}
139

    
140

    
141
	@Override
142
	public boolean isIgnore(DwcaTaxExportState state) {
143
		return ! state.getConfig().isDoTaxa();
144
	}
145

    
146
}
(21-21/33)