Project

General

Profile

Download (4.85 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.List;
17
import java.util.Set;
18

    
19
import org.apache.commons.lang.StringUtils;
20
import org.apache.log4j.Logger;
21
import org.springframework.stereotype.Component;
22
import org.springframework.transaction.TransactionStatus;
23

    
24
import eu.etaxonomy.cdm.common.CdmUtils;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.common.IOriginalSource;
27
import eu.etaxonomy.cdm.model.common.ISourceable;
28
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
29
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
30
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
31
import eu.etaxonomy.cdm.model.description.Feature;
32
import eu.etaxonomy.cdm.model.description.TaxonDescription;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
35

    
36
/**
37
 * @author a.mueller
38
 * @created 18.04.2011
39
 */
40
@Component
41
public class DwcaVernacularExport extends DwcaExportBase {
42
	private static final Logger logger = Logger.getLogger(DwcaVernacularExport.class);
43

    
44
	private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/VernacularName";
45
	private static final String fileName = "vernacular.txt";
46
	
47
	
48
	/**
49
	 * Constructor
50
	 */
51
	public DwcaVernacularExport() {
52
		super();
53
		this.ioName = this.getClass().getSimpleName();
54
	}
55

    
56
	/** Retrieves data from a CDM DB and serializes them CDM to XML.
57
	 * Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.
58
	 * Taxa that are not part of the classification are not found.
59
	 * 
60
	 * @param exImpConfig
61
	 * @param dbname
62
	 * @param filename
63
	 */
64
	@Override
65
	protected void doInvoke(DwcaTaxExportState state){
66
		DwcaTaxExportConfigurator config = state.getConfig();
67
		TransactionStatus txStatus = startTransaction(true);
68

    
69
		PrintWriter writer = null;
70
		try {
71
			writer = createPrintWriter(fileName, state);
72
			
73
			DwcaMetaDataRecord metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, ROW_TYPE);
74
			state.addMetaRecord(metaRecord);
75
			
76
			List<TaxonNode> allNodes =  getAllNodes(null);
77
			for (TaxonNode node : allNodes){
78
				Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
79
				Set<TaxonDescription> descriptions = taxon.getDescriptions();
80
				for (TaxonDescription description : descriptions){
81
					for (DescriptionElementBase el : description.getElements()){
82
						if (el.isInstanceOf(CommonTaxonName.class)){
83
							DwcaVernacularRecord record = new DwcaVernacularRecord(metaRecord, config);
84
							CommonTaxonName commonTaxonName = CdmBase.deproxy(el, CommonTaxonName.class);
85
							if (! this.recordExists(commonTaxonName)){
86
								handleCommonTaxonName(record, commonTaxonName, taxon, config);
87
								record.write(writer);
88
								this.addExistingRecord(commonTaxonName);
89
							}
90
						}else if (el.getFeature().equals(Feature.COMMON_NAME())){
91
							//TODO
92
							String message = "Vernacular name export for TextData not yet implemented";
93
							logger.warn(message);
94
						}
95
					}
96
				}
97

    
98
				writer.flush();
99
				
100
			}
101
		} catch (FileNotFoundException e) {
102
			e.printStackTrace();
103
		} catch (UnsupportedEncodingException e) {
104
			e.printStackTrace();
105
		} catch (ClassCastException e) {
106
			e.printStackTrace();
107
		} catch (IOException e) {
108
			e.printStackTrace();
109
		} finally{
110
			closeWriter(writer, state);
111
		}
112
		commitTransaction(txStatus);
113
		return;
114
	}
115
	
116

    
117

    
118

    
119
	private void handleCommonTaxonName(DwcaVernacularRecord record, CommonTaxonName commonTaxonName, Taxon taxon, DwcaTaxExportConfigurator config) {
120
		record.setId(taxon.getId());
121
		record.setUuid(taxon.getUuid());
122
		if (StringUtils.isBlank(commonTaxonName.getName())){
123
			String message = "'Name' is required field for vernacular name but does not exist for taxon " + getTaxonLogString(taxon);
124
			logger.warn(message);
125
		}else{
126
			record.setVernacularName(commonTaxonName.getName());
127
		}
128
		//sources
129
		record.setSource(getSources(commonTaxonName, config));
130
		record.setLanguage(commonTaxonName.getLanguage());
131
		// does not exist in CDM
132
		record.setTemporal(null);
133
		
134
		handleArea(record, commonTaxonName.getArea(), taxon, false);
135
	}
136

    
137

    
138
	@Override
139
	protected boolean doCheck(DwcaTaxExportState state) {
140
		boolean result = true;
141
		logger.warn("No check implemented for " + this.ioName);
142
		return result;
143
	}
144

    
145

    
146
	@Override
147
	protected boolean isIgnore(DwcaTaxExportState state) {
148
		return ! state.getConfig().isDoVernacularNames();
149
	}
150
	
151
}
(25-25/30)