2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.io
.dwca
.out
;
11 import java
.io
.PrintWriter
;
13 import java
.net
.URISyntaxException
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
18 import java
.util
.UUID
;
20 import org
.apache
.commons
.lang
.StringUtils
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.joda
.time
.DateTime
;
23 import org
.joda
.time
.Partial
;
25 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
26 import eu
.etaxonomy
.cdm
.io
.dwca
.TermUri
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTerm
;
31 import eu
.etaxonomy
.cdm
.model
.common
.LSID
;
32 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
33 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
34 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
35 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
36 import eu
.etaxonomy
.cdm
.model
.location
.Point
;
37 import eu
.etaxonomy
.cdm
.model
.media
.Rights
;
38 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignationStatus
;
39 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
40 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
41 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
42 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
43 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationStatusBase
;
50 public abstract class DwcaRecordBase
{
51 private static final Logger logger
= Logger
.getLogger(DwcaRecordBase
.class);
53 //TODO Collection_SEPARATOR
54 protected static final CharSequence COLLECTION_SEPARATOR
= "@";
55 // protected static final String FIELD_ENCLOSER = "\"";
56 protected static final boolean IS_FIRST
= false;
57 protected static final boolean IS_NOT_FIRST
= true;
58 // protected static final String SEP = ",";
60 protected Map
<String
, URI
> knownFields
= new HashMap
<String
, URI
>();
61 protected Set
<TermUri
> knownTermFields
= new HashSet
<TermUri
>();
63 public abstract void write(PrintWriter writer
);
64 protected abstract void registerKnownFields();
67 private DwcaMetaDataRecord metaDataRecord
;
68 protected DwcaTaxExportConfigurator config
;
74 protected DwcaRecordBase(DwcaMetaDataRecord metaDataRecord
, DwcaTaxExportConfigurator config
){
75 this.metaDataRecord
= metaDataRecord
;
76 this.count
= metaDataRecord
.inc();
81 public void setId(Integer id
) {
85 public Integer
getId() {
89 public void setUuid(UUID uuid
) {
93 public UUID
getUuid() {
97 protected void printNotes(Set
<Annotation
> notes
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
98 printNotes(notes
, writer
, addSeparator
, fieldKey
.getUriString());
100 protected void printNotes(Set
<Annotation
> notes
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
101 //FIXME handles annotations correctly
103 print(value
, writer
, addSeparator
, fieldKey
);
106 // protected void print(Object object, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
107 // print(object == null ? null : object.toString(), writer, addSeparator, fieldKey);
109 protected void print(DwcaId dwcaId
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
110 print(dwcaId
== null ?
null : dwcaId
.getId(), writer
, addSeparator
, fieldKey
);
112 protected void print(UUID uuid
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
113 print(uuid
== null ?
null : uuid
.toString(), writer
, addSeparator
, fieldKey
);
115 protected void print(AgentBase
<?
> agent
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
116 print(agent
, writer
, addSeparator
, fieldKey
.getUriString());
118 protected void print(AgentBase
<?
> agent
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
119 print(agent
== null ?
null : getAgent(agent
), writer
, addSeparator
, fieldKey
);
123 protected void print(Language language
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
124 print(language
, writer
, addSeparator
, fieldKey
.getUriString());
126 protected void print(Language language
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
127 print(language
== null ?
null : getLanguage(language
), writer
, addSeparator
, fieldKey
);
129 protected void print(LSID lsid
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
130 print(lsid
, writer
, addSeparator
, fieldKey
.getUriString());
132 protected void print(LSID lsid
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
133 print(lsid
== null ?
null : String
.valueOf(lsid
.toString()), writer
, addSeparator
, fieldKey
);
136 protected void print(Set
<Rights
> rights
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
137 print(rights
, writer
, addSeparator
, fieldKey
.getUriString());
139 protected void print(Set
<Rights
> rights
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
140 String rightsString
= getRights(rights
);
141 print(rightsString
, writer
, addSeparator
, fieldKey
);
143 protected void print(URI uri
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
144 print(uri
, writer
, addSeparator
, fieldKey
.getUriString());
146 protected void print(URI uri
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
147 print(uri
== null ?
null : String
.valueOf(uri
), writer
, addSeparator
, fieldKey
);
150 protected void print(Point point
, PrintWriter writer
, boolean addSeparator
, TermUri latitudeKey
, TermUri longitudeKey
) {
151 print(point
, writer
, addSeparator
, latitudeKey
.getUriString(), longitudeKey
.getUriString());
154 protected void print(Point point
, PrintWriter writer
, boolean addSeparator
, String latitudeKey
, String longitudeKey
) {
156 String toPrint
= null;
157 print(toPrint
, writer
, addSeparator
, latitudeKey
);
158 print(toPrint
, writer
, addSeparator
, longitudeKey
);
160 String latitude
= point
.getLatitude().toString();
161 String longitude
= point
.getLongitude().toString();
162 print(latitude
, writer
, addSeparator
, latitudeKey
);
163 print(longitude
, writer
, addSeparator
, longitudeKey
);
166 protected void print(Boolean boolValue
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
167 print(boolValue
, writer
, addSeparator
, fieldKey
.getUriString());
169 protected void print(Boolean boolValue
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
170 print(boolValue
== null ?
null : String
.valueOf(boolValue
), writer
, addSeparator
, fieldKey
);
173 protected void print(Integer intValue
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
174 print(intValue
, writer
, addSeparator
, fieldKey
.getUriString());
176 protected void print(Integer intValue
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
177 print(intValue
== null ?
null : String
.valueOf(intValue
), writer
, addSeparator
, fieldKey
);
180 protected void printId(Integer intValue
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
181 print(intValue
== null ?
null : String
.valueOf(intValue
), writer
, addSeparator
, fieldKey
);
183 protected void printId(UUID uuid
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
184 print(uuid
== null ?
null : String
.valueOf(uuid
), writer
, addSeparator
, fieldKey
);
187 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
188 print(value
, writer
, addSeparator
, fieldKey
, null);
191 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
, String defaultValue
) {
192 print(value
, writer
, addSeparator
, fieldKey
.getUriString(), defaultValue
);
195 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
196 print(value
, writer
, addSeparator
, fieldKey
, null);
199 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, String fieldKey
, String defaultValue
) {
200 if (count
== 1 && addSeparator
== IS_NOT_FIRST
){
201 registerFieldKey(URI
.create(fieldKey
), defaultValue
);
203 if (StringUtils
.isBlank(defaultValue
)){
204 String strToPrint
= addSeparator ? config
.getFieldsTerminatedBy() : "";
205 if (StringUtils
.isNotBlank(value
)){
206 //Replace quotes by double quotes
207 value
= value
.replace("\"", "\"\"");
209 value
= value
.replace(config
.getLinesTerminatedBy(), "\\r");
211 //replace all line brakes according to best practices: http://code.google.com/p/gbif-ecat/wiki/BestPractices
212 value
= value
.replace("\r\n", "\\r");
213 value
= value
.replace("\r", "\\r");
214 value
= value
.replace("\n", "\\r");
216 strToPrint
+= config
.getFieldsEnclosedBy() + value
+ config
.getFieldsEnclosedBy();
218 writer
.print(strToPrint
);
222 private void registerFieldKey(URI key
, String defaultValue
) {
223 this.metaDataRecord
.addFieldEntry(key
, defaultValue
);
227 protected String
getRights(Rights rights
) {
232 return rights
.getAbbreviatedText();
236 protected String
getLanguage(Language language
) {
237 if (language
== null){
241 return language
.getIso639_2();
245 protected String
getDate(DateTime date
) {
250 return date
.toString();
254 protected String
getNomStatus(NomenclaturalStatusType nomStatus
) {
255 String result
= DwcaTaxExportTransformer
.transformToGbifNomStatus(nomStatus
);
257 if (nomStatus
== null){
260 return nomStatus
.getLabel();
267 protected String
getNomCode(NomenclaturalCode nomCode
) {
268 if (nomCode
== null){
272 return nomCode
.getTitleCache();
276 protected String
getRank(Rank rank
) {
277 String result
= DwcaTaxExportTransformer
.transformToGbifRank(rank
);
282 return rank
.getLabel();
289 protected String
getSex(DefinedTerm sex
) {
290 String result
= DwcaTaxExportTransformer
.transformToGbifSex(sex
);
295 return sex
.getLabel();
302 protected String
getLifeStage(DefinedTerm stage
) {
303 String result
= DwcaTaxExportTransformer
.transformToGbifLifeStage(stage
);
308 return stage
.getLabel();
315 protected String
getOccurrenceStatus(PresenceAbsenceTerm status
) {
316 String result
= DwcaTaxExportTransformer
.transformToGbifOccStatus(status
);
321 return status
.getLabel();
328 protected String
getEstablishmentMeans(PresenceAbsenceTerm status
) {
329 String result
= DwcaTaxExportTransformer
.transformToGbifEstablishmentMeans(status
);
334 return status
.getLabel();
343 protected String
getAgent(AgentBase
<?
> agent
) {
348 return agent
.getTitleCache();
353 protected String
getFeature(Feature feature
) {
354 if (feature
== null){
358 return feature
.getTitleCache();
363 protected String
getTimePeriod(TimePeriod period
) {
368 return period
.toString();
372 protected String
getTimePeriodPart(TimePeriod period
, boolean useEnd
) {
376 Partial date
= useEnd? period
.getEnd(): period
.getStart();
381 return date
.toString();
386 private String
getRights(Set
<Rights
> rights
) {
387 if (rights
== null || rights
.isEmpty()){
390 String result
= null;
391 for (Rights right
: rights
){
392 //TODO usi uri if available ??
393 String message
= "Rights not yet fully implemented";
394 logger
.warn(message
);
395 result
= CdmUtils
.concat(COLLECTION_SEPARATOR
, result
, right
.getAbbreviatedText());
402 protected String
getDesignationType(TypeDesignationStatusBase
<?
> status
) {
407 if (status
.isInstanceOf(SpecimenTypeDesignationStatus
.class)){
408 SpecimenTypeDesignationStatus specStatus
= CdmBase
.deproxy(status
, SpecimenTypeDesignationStatus
.class);
409 result
= DwcaTaxExportTransformer
.transformSpecimenTypeStatusToGbif(specStatus
);
411 NameTypeDesignationStatus nameStatus
= CdmBase
.deproxy(status
, NameTypeDesignationStatus
.class);
412 result
= DwcaTaxExportTransformer
.transformNameTypeStatusToGbif(nameStatus
);
415 return status
.getLabel();
422 protected void addKnownField(String string
, String uri
) throws URISyntaxException
{
423 this.knownFields
.put(string
, new URI(uri
));
426 protected void addKnownField(TermUri term
) throws URISyntaxException
{
427 this.knownTermFields
.add(term
);