Project

General

Profile

Download (8.71 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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
package eu.etaxonomy.cdm.io.dwca.out;
10

    
11
import java.io.PrintWriter;
12
import java.net.URISyntaxException;
13

    
14
import org.apache.commons.lang.StringUtils;
15
import org.apache.log4j.Logger;
16

    
17
import eu.etaxonomy.cdm.io.dwca.TermUri;
18
import eu.etaxonomy.cdm.model.common.CdmBase;
19
import eu.etaxonomy.cdm.model.common.DefinedTerm;
20
import eu.etaxonomy.cdm.model.common.TimePeriod;
21
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
22
import eu.etaxonomy.cdm.model.location.Country;
23
import eu.etaxonomy.cdm.model.location.NamedArea;
24

    
25
/**
26
 * @author a.mueller
27
 * @date 20.04.2011
28
 *
29
 */
30
public class DwcaDistributionRecord extends DwcaRecordBase implements IDwcaAreaRecord{
31
	@SuppressWarnings("unused")
32
	private static final Logger logger = Logger.getLogger(DwcaDistributionRecord.class);
33

    
34
	private DwcaId locationId;
35
	private String locationIdString;
36
	private String locality;
37
	private String countryCode;
38
	private DefinedTerm lifeStage;
39
	private PresenceAbsenceTerm occurrenceStatus;
40
	private String threadStatus;
41

    
42
	private PresenceAbsenceTerm establishmentMeans;
43
	private String appendixCITES;
44
	private TimePeriod eventDate;
45

    
46
	private TimePeriod seasonalDate; //startDayOfYear and endDayOfYear
47
	private String source;
48
	private String occurrenceRemarks;
49

    
50

    
51
	public DwcaDistributionRecord(DwcaMetaDataRecord metaDataRecord, DwcaTaxExportConfigurator config){
52
		super(metaDataRecord, config);
53
		locationId = new DwcaId(config);
54
	}
55

    
56

    
57
	@Override
58
    protected void registerKnownFields(){
59
		try {
60
			addKnownField(TermUri.DWC_LOCATION_ID);
61
			addKnownField(TermUri.DWC_LOCALITY);
62
			addKnownField(TermUri.DWC_COUNTRY_CODE);
63
			addKnownField(TermUri.DWC_LIFESTAGE);
64
			addKnownField(TermUri.IUCN_THREAD_STATUS);
65

    
66
            addKnownField(TermUri.DWC_OCCURRENCE_STATUS);
67
            addKnownField(TermUri.DWC_ESTABLISHMENT_MEANS);
68
            addKnownField(TermUri.GBIF_APPENDIX_CITES);
69
            addKnownField(TermUri.DWC_EVENT_DATE);
70
            addKnownField(TermUri.DWC_START_DAY_OF_YEAR);
71
            addKnownField(TermUri.DC_SOURCE);
72
            addKnownField(TermUri.DWC_OCCURRENCE_REMARKS);
73
		} catch (URISyntaxException e) {
74
			throw new RuntimeException(e);
75
		}
76
	}
77

    
78
//	@Override
79
//	public List<String> getHeaderList() {
80
//		String[] result = new String[]{"coreid",
81
//				"locationId",
82
//				"locality",
83
//				"countryCode",
84
//				"lifeStage",
85
//				"occurrenceStatus",
86
//				"threadStatus",
87
//				"establishmentMeans",
88
//				"appendixCITES",
89
//				"eventDate",
90
//				"startDayOfYear",
91
//				"endDayOfYear",
92
//				"source",
93
//				"occurrenceRemarks"};
94
//		return Arrays.asList(result);
95
//	}
96

    
97
    @Override
98
    public void writeCsv(DwcaTaxExportState state) {
99
        try {
100
            DwcaTaxExportFile table = DwcaTaxExportFile.DISTRIBUTION;
101
            String[] csvLine = new String[table.getSize()];
102

    
103
            //
104
            line(state, csvLine, table, TermUri.DC_IDENTIFIER, getUuid());
105
            if (StringUtils.isNotBlank(locationIdString)){
106
                line(state, csvLine, table, TermUri.DWC_LOCATION_ID, locationIdString);
107
            }else{
108
                line(state, csvLine, table, TermUri.DWC_LOCATION_ID, locationId);
109
            }
110
            line(state, csvLine, table, TermUri.DWC_LOCALITY, locality);
111
            line(state, csvLine, table, TermUri.DWC_COUNTRY_CODE, countryCode);
112
            line(state, csvLine, table, TermUri.DWC_LIFESTAGE, getLifeStage(lifeStage));
113
            line(state, csvLine, table, TermUri.IUCN_THREAD_STATUS, getOccurrenceStatus(occurrenceStatus));
114
            line(state, csvLine, table, TermUri.DWC_OCCURRENCE_STATUS, threadStatus);
115
            line(state, csvLine, table, TermUri.DWC_ESTABLISHMENT_MEANS, getEstablishmentMeans(establishmentMeans));
116
            line(state, csvLine, table, TermUri.GBIF_APPENDIX_CITES, appendixCITES);
117
            line(state, csvLine, table, TermUri.DWC_EVENT_DATE, getTimePeriod(eventDate));
118
            line(state, csvLine, table, TermUri.DWC_START_DAY_OF_YEAR, getTimePeriodPart(seasonalDate, false));
119
            line(state, csvLine, table, TermUri.DWC_END_DAY_OF_YEAR, getTimePeriodPart(seasonalDate, true));
120
            line(state, csvLine, table, TermUri.DC_SOURCE, source);
121
            line(state, csvLine, table, TermUri.DWC_OCCURRENCE_REMARKS, occurrenceRemarks);
122

    
123
            state.getProcessor().put(table, getId().toString(), csvLine);
124
        } catch (Exception e) {
125
            String message = "Unhandled exception when writing distribution record: " + e.getMessage();
126
            state.getResult().addException(e, message);
127
        }
128

    
129
    }
130

    
131
    @Override
132
    public void write(DwcaTaxExportState state, PrintWriter writer) {
133
        if(writer == null){
134
            writeCsv(state);
135
            return;
136
        }
137
		printId(getUuid(), writer, IS_FIRST, "coreid");
138
		if (StringUtils.isNotBlank(locationIdString)){
139
			print(locationIdString, writer, IS_NOT_FIRST, TermUri.DWC_LOCATION_ID);
140
		}else{
141
			print(locationId, writer, IS_NOT_FIRST, TermUri.DWC_LOCATION_ID);
142
		}
143
		print(locality, writer, IS_NOT_FIRST, TermUri.DWC_LOCALITY);
144
		print(countryCode, writer, IS_NOT_FIRST, TermUri.DWC_COUNTRY_CODE);
145
		print(getLifeStage(lifeStage), writer, IS_NOT_FIRST, TermUri.DWC_LIFESTAGE);
146
		print(getOccurrenceStatus(occurrenceStatus), writer, IS_NOT_FIRST, TermUri.DWC_OCCURRENCE_STATUS);
147
		print(threadStatus, writer, IS_NOT_FIRST, TermUri.IUCN_THREAD_STATUS);
148
		print(getEstablishmentMeans(establishmentMeans), writer, IS_NOT_FIRST, TermUri.DWC_ESTABLISHMENT_MEANS);
149
		print(appendixCITES, writer, IS_NOT_FIRST, TermUri.GBIF_APPENDIX_CITES);
150
		print(getTimePeriod(eventDate),writer, IS_NOT_FIRST, TermUri.DWC_EVENT_DATE);
151
		print(getTimePeriodPart(seasonalDate, false),writer, IS_NOT_FIRST, TermUri.DWC_START_DAY_OF_YEAR);
152
		//TODO
153
		print(getTimePeriodPart(seasonalDate, true),writer, IS_NOT_FIRST, TermUri.DWC_END_DAY_OF_YEAR);
154
		print(source, writer, IS_NOT_FIRST, TermUri.DC_SOURCE);
155
		print(occurrenceRemarks, writer, IS_NOT_FIRST, TermUri.DWC_OCCURRENCE_REMARKS);
156
		writer.println();
157
	}
158

    
159

    
160
	public String getSource() {
161
		return source;
162
	}
163
	public void setSource(String source) {
164
		this.source = source;
165
	}
166

    
167
	public String getLocality() {
168
		return locality;
169
	}
170

    
171

    
172
	@Override
173
    public void setLocality(String locality) {
174
		this.locality = locality;
175
	}
176

    
177

    
178
	public String getCountryCode() {
179
		return countryCode;
180
	}
181

    
182

    
183
	@Override
184
    public void setCountryCode(String countryCode) {
185
		this.countryCode = countryCode;
186
	}
187

    
188

    
189
	public DefinedTerm getLifeStage() {
190
		return lifeStage;
191
	}
192

    
193

    
194
	public void setLifeStage(DefinedTerm lifeStage) {
195
		this.lifeStage = lifeStage;
196
	}
197

    
198

    
199
	public String getLocationId() {
200
		return locationId.getId();
201
	}
202

    
203
	@Override
204
    public void setLocationId(NamedArea area) {
205
		if (area.getVocabulary().getUuid().equals(NamedArea.uuidTdwgAreaVocabulary)){
206
			String locationId = "TDWG:" + area.getIdInVocabulary();
207
			this.locationIdString = locationId;
208
		}else if (area.isInstanceOf(Country.class)){
209
			Country country = CdmBase.deproxy(area, Country.class);
210
			String locationId = "ISO3166:" + country.getIso3166_A2();
211
			this.locationIdString = locationId;
212
		}else{
213
			this.locationId.setId(area);
214
		}
215
	}
216

    
217
	public PresenceAbsenceTerm getOccurrenceStatus() {
218
		return occurrenceStatus;
219
	}
220

    
221

    
222
	public void setOccurrenceStatus(PresenceAbsenceTerm occurrenceStatus) {
223
		this.occurrenceStatus = occurrenceStatus;
224
	}
225

    
226

    
227
	public String getThreadStatus() {
228
		return threadStatus;
229
	}
230

    
231

    
232
	public void setThreadStatus(String threadStatus) {
233
		this.threadStatus = threadStatus;
234
	}
235

    
236

    
237
	public PresenceAbsenceTerm getEstablishmentMeans() {
238
		return establishmentMeans;
239
	}
240

    
241

    
242
	public void setEstablishmentMeans(PresenceAbsenceTerm establishmentMeans) {
243
		this.establishmentMeans = establishmentMeans;
244
	}
245

    
246

    
247
	public String getAppendixCITES() {
248
		return appendixCITES;
249
	}
250

    
251

    
252
	public void setAppendixCITES(String appendixCITES) {
253
		this.appendixCITES = appendixCITES;
254
	}
255

    
256

    
257
	public TimePeriod getEventDate() {
258
		return eventDate;
259
	}
260

    
261

    
262
	public void setEventDate(TimePeriod eventDate) {
263
		this.eventDate = eventDate;
264
	}
265

    
266

    
267
	public TimePeriod getSeasonalDate() {
268
		return seasonalDate;
269
	}
270

    
271

    
272
	public void setSeasonalDate(TimePeriod seasonalDate) {
273
		this.seasonalDate = seasonalDate;
274
	}
275

    
276

    
277
	public String getOccurrenceRemarks() {
278
		return occurrenceRemarks;
279
	}
280

    
281

    
282
	public void setOccurrenceRemarks(String occurrenceRemarks) {
283
		this.occurrenceRemarks = occurrenceRemarks;
284
	}
285

    
286

    
287
}
(5-5/33)