Project

General

Profile

Download (4.56 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.PrintWriter;
15
import java.io.UnsupportedEncodingException;
16
import java.util.Set;
17

    
18
import org.apache.log4j.Logger;
19

    
20
import eu.etaxonomy.cdm.model.common.CdmBase;
21
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
22
import eu.etaxonomy.cdm.model.description.Distribution;
23
import eu.etaxonomy.cdm.model.description.Feature;
24
import eu.etaxonomy.cdm.model.description.TaxonDescription;
25
import eu.etaxonomy.cdm.model.taxon.Taxon;
26
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27

    
28
/**
29
 * @author a.mueller
30
 * @since 20.04.2011
31
 */
32
public class DwcaDistributionExport extends DwcaDataExportBase {
33

    
34
    private static final long serialVersionUID = -3274468345456407430L;
35

    
36
    private static final Logger logger = Logger.getLogger(DwcaDistributionExport.class);
37

    
38
	private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/Distribution";
39
	protected static final String fileName = "distribution.txt";
40

    
41
    private DwcaMetaDataRecord metaRecord;
42

    
43
	/**
44
	 * Constructor
45
	 */
46
	public DwcaDistributionExport(DwcaTaxExportState state) {
47
		super();
48
		this.ioName = this.getClass().getSimpleName();
49
        metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
50
        state.addMetaRecord(metaRecord);
51
        file = DwcaTaxExportFile.DISTRIBUTION;
52
	}
53

    
54
	@Override
55
	protected void doInvoke(DwcaTaxExportState state){}
56

    
57
    /**
58
     * @param state
59
     * @param node
60
     * @throws IOException
61
     * @throws FileNotFoundException
62
     * @throws UnsupportedEncodingException
63
     */
64
    @Override
65
    protected void handleTaxonNode(DwcaTaxExportState state,  TaxonNode node)
66
            throws IOException, FileNotFoundException, UnsupportedEncodingException {
67

    
68
        try {
69
            DwcaTaxExportConfigurator config = state.getConfig();
70

    
71
            Taxon taxon = CdmBase.deproxy(node.getTaxon());
72

    
73
            Set<TaxonDescription> descriptions = taxon.getDescriptions();
74
            for (TaxonDescription description : descriptions){
75
            	for (DescriptionElementBase el : description.getElements()){
76
            		if (el.isInstanceOf(Distribution.class) ){
77
            			if (! state.recordExists(file, el)){
78
            				DwcaDistributionRecord record = new DwcaDistributionRecord(metaRecord, config);
79
            				Distribution distribution = CdmBase.deproxy(el, Distribution.class);
80
            				handleDistribution(state, record, distribution, taxon, config);
81
            				PrintWriter writer = createPrintWriter(state, file);
82
            	            record.write(state, writer);
83
            				state.addExistingRecord(file, distribution);
84
            			}
85
            		}else if (el.getFeature().equals(Feature.DISTRIBUTION())){
86
            		    String message = "Distribution export for TextData not yet implemented";
87
            			state.getResult().addWarning(message);
88
            		}
89
            	}
90
            }
91
        } catch (Exception e) {
92
            String message = "Unexpected exception: " + e.getMessage();
93
            state.getResult().addException(e, message);
94
        }finally{
95
            flushWriter(state, file);
96
        }
97
    }
98

    
99

    
100

    
101

    
102
	private void handleDistribution(DwcaTaxExportState state, DwcaDistributionRecord record, Distribution distribution, Taxon taxon, DwcaTaxExportConfigurator config) {
103
		record.setId(taxon.getId());
104
		record.setUuid(taxon.getUuid());
105
		handleArea(state, record, distribution.getArea(), taxon, true);
106
		//TODO missing
107
		record.setLifeStage(null);
108
		record.setOccurrenceStatus(distribution.getStatus());
109
		//TODO missing
110
		record.setThreadStatus(null);
111
		record.setEstablishmentMeans(distribution.getStatus());
112
		//TODO missing
113
		record.setAppendixCITES(null);
114
		//TODO missing
115
		record.setEventDate(null);
116
		//TODO missing
117
		record.setSeasonalDate(null);
118
		//FIXME
119
		record.setSource(getSources(distribution, config));
120
		//FIXME
121
		record.setOccurrenceRemarks(null);
122

    
123
	}
124

    
125
	@Override
126
	protected boolean doCheck(DwcaTaxExportState state) {
127
		boolean result = true;
128
		logger.warn("No check implemented for " + this.ioName);
129
		return result;
130
	}
131

    
132
	@Override
133
	public boolean isIgnore(DwcaTaxExportState state) {
134
		return ! state.getConfig().isDoDistributions();
135
	}
136
}
(4-4/33)