Project

General

Profile

« Previous | Next » 

Revision f104a204

Added by Andreas Müller almost 2 years ago

cleanup

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaVernacularExport.java
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.commons.lang.StringUtils;
19
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
20

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

  
29
/**
30
 * @author a.mueller
31
 * @since 18.04.2011
32
 */
33
public class DwcaVernacularExport extends DwcaDataExportBase {
34
    private static final long serialVersionUID = 3169086545830374918L;
35

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

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

  
41
    private DwcaMetaDataRecord metaRecord;
42

  
43
	/**
44
	 * Constructor
45
	 */
46
	public DwcaVernacularExport(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.VERNACULAR;
52
	}
53

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

  
57
    /**
58
     * {@inheritDoc}
59
     */
60
    @Override
61
    protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node)
62
            throws IOException, FileNotFoundException, UnsupportedEncodingException {
63

  
64
        try {
65
            DwcaTaxExportConfigurator config = state.getConfig();
66
            Taxon taxon = CdmBase.deproxy(node.getTaxon());
67
            Set<TaxonDescription> descriptions = taxon.getDescriptions();
68
            for (TaxonDescription description : descriptions){
69
            	for (DescriptionElementBase el : description.getElements()){
70
            		if (el.isInstanceOf(CommonTaxonName.class)){
71
            			DwcaVernacularRecord record = new DwcaVernacularRecord(metaRecord, config);
72
            			CommonTaxonName commonTaxonName = CdmBase.deproxy(el, CommonTaxonName.class);
73
            			if (! state.recordExists(file, commonTaxonName)){
74
            				handleCommonTaxonName(state, record, commonTaxonName, taxon, config);
75
            				PrintWriter writer = createPrintWriter(state, file);
76
            				record.write(state, writer);
77
            				state.addExistingRecord(file, commonTaxonName);
78
            			}
79
            		}else if (el.getFeature().equals(Feature.COMMON_NAME())){
80
            			//TODO
81
            			String message = "Vernacular name export for TextData not yet implemented";
82
            			state.getResult().addError(message, this, "doInvoke()");
83
            			logger.warn(message);
84
            		}
85
            	}
86
            }
87

  
88
        } catch (Exception e) {
89
            String message = "Unexpected exception: " + e.getMessage();
90
            state.getResult().addException(e, message);
91
        }finally{
92
            flushWriter(state, file);
93
        }
94
    }
95

  
96

  
97

  
98

  
99
	private void handleCommonTaxonName(DwcaTaxExportState state, DwcaVernacularRecord record, CommonTaxonName commonTaxonName, Taxon taxon, DwcaTaxExportConfigurator config) {
100
		record.setId(taxon.getId());
101
		record.setUuid(taxon.getUuid());
102
		if (StringUtils.isBlank(commonTaxonName.getName())){
103
			String message = "'Name' is required field for vernacular name but does not exist for taxon " + getTaxonLogString(taxon);
104
			state.getResult().addWarning(message);
105
		}else{
106
			record.setVernacularName(commonTaxonName.getName());
107
		}
108
		//sources
109
		record.setSource(getSources(commonTaxonName, config));
110
		record.setLanguage(commonTaxonName.getLanguage());
111
		// does not exist in CDM
112
		record.setTemporal(null);
113

  
114
		handleArea(state, record, commonTaxonName.getArea(), taxon, false);
115
	}
116

  
117
	@Override
118
	protected boolean doCheck(DwcaTaxExportState state) {
119
		boolean result = true;
120
		logger.warn("No check implemented for " + this.ioName);
121
		return result;
122
	}
123

  
124
	@Override
125
	public boolean isIgnore(DwcaTaxExportState state) {
126
		return ! state.getConfig().isDoVernacularNames();
127
	}
128
}
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.commons.lang.StringUtils;
19
import org.apache.logging.log4j.LogManager;
20
import org.apache.logging.log4j.Logger;
21

  
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
24
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
25
import eu.etaxonomy.cdm.model.description.Feature;
26
import eu.etaxonomy.cdm.model.description.TaxonDescription;
27
import eu.etaxonomy.cdm.model.taxon.Taxon;
28
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
29

  
30
/**
31
 * @author a.mueller
32
 * @since 18.04.2011
33
 */
34
public class DwcaVernacularExport extends DwcaDataExportBase {
35
    private static final long serialVersionUID = 3169086545830374918L;
36

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

  
39
	private static final String ROW_TYPE = "http://rs.gbif.org/terms/1.0/VernacularName";
40
	protected static final String fileName = "vernacular.txt";
41

  
42
    private DwcaMetaDataRecord metaRecord;
43

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

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

  
58
    @Override
59
    protected void handleTaxonNode(DwcaTaxExportState state, TaxonNode node)
60
            throws IOException, FileNotFoundException, UnsupportedEncodingException {
61

  
62
        try {
63
            DwcaTaxExportConfigurator config = state.getConfig();
64
            Taxon taxon = CdmBase.deproxy(node.getTaxon());
65
            Set<TaxonDescription> descriptions = taxon.getDescriptions();
66
            for (TaxonDescription description : descriptions){
67
            	for (DescriptionElementBase el : description.getElements()){
68
            		if (el.isInstanceOf(CommonTaxonName.class)){
69
            			DwcaVernacularRecord record = new DwcaVernacularRecord(metaRecord, config);
70
            			CommonTaxonName commonTaxonName = CdmBase.deproxy(el, CommonTaxonName.class);
71
            			if (! state.recordExists(file, commonTaxonName)){
72
            				handleCommonTaxonName(state, record, commonTaxonName, taxon, config);
73
            				PrintWriter writer = createPrintWriter(state, file);
74
            				record.write(state, writer);
75
            				state.addExistingRecord(file, commonTaxonName);
76
            			}
77
            		}else if (el.getFeature().equals(Feature.COMMON_NAME())){
78
            			//TODO
79
            			String message = "Vernacular name export for TextData not yet implemented";
80
            			state.getResult().addError(message, this, "doInvoke()");
81
            			logger.warn(message);
82
            		}
83
            	}
84
            }
85

  
86
        } catch (Exception e) {
87
            String message = "Unexpected exception: " + e.getMessage();
88
            state.getResult().addException(e, message);
89
        }finally{
90
            flushWriter(state, file);
91
        }
92
    }
93

  
94

  
95

  
96

  
97
	private void handleCommonTaxonName(DwcaTaxExportState state, DwcaVernacularRecord record, CommonTaxonName commonTaxonName, Taxon taxon, DwcaTaxExportConfigurator config) {
98
		record.setId(taxon.getId());
99
		record.setUuid(taxon.getUuid());
100
		if (StringUtils.isBlank(commonTaxonName.getName())){
101
			String message = "'Name' is required field for vernacular name but does not exist for taxon " + getTaxonLogString(taxon);
102
			state.getResult().addWarning(message);
103
		}else{
104
			record.setVernacularName(commonTaxonName.getName());
105
		}
106
		//sources
107
		record.setSource(getSources(commonTaxonName, config));
108
		record.setLanguage(commonTaxonName.getLanguage());
109
		// does not exist in CDM
110
		record.setTemporal(null);
111

  
112
		handleArea(state, record, commonTaxonName.getArea(), taxon, false);
113
	}
114

  
115
	@Override
116
	protected boolean doCheck(DwcaTaxExportState state) {
117
		boolean result = true;
118
		logger.warn("No check implemented for " + this.ioName);
119
		return result;
120
	}
121

  
122
	@Override
123
	public boolean isIgnore(DwcaTaxExportState state) {
124
		return ! state.getConfig().isDoVernacularNames();
125
	}
126
}

Also available in: Unified diff