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
.csv
.redlist
.demo
;
11 import java
.io
.PrintWriter
;
13 import java
.net
.URISyntaxException
;
14 import java
.util
.ArrayList
;
15 import java
.util
.HashMap
;
16 import java
.util
.HashSet
;
19 import java
.util
.UUID
;
21 import org
.apache
.commons
.lang
.StringUtils
;
22 import org
.apache
.log4j
.Logger
;
23 import org
.joda
.time
.DateTime
;
24 import org
.joda
.time
.Partial
;
26 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
27 import eu
.etaxonomy
.cdm
.io
.stream
.terms
.TermUri
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
30 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
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
;
44 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTerm
;
47 * @author a.mueller, a.oppermann
50 public abstract class CsvDemoRecordBase
{
52 private static final Logger logger
= Logger
.getLogger(CsvDemoRecordBase
.class);
54 //TODO Collection_SEPARATOR
55 protected static final CharSequence COLLECTION_SEPARATOR
= "@";
56 // protected static final String FIELD_ENCLOSER = "\"";
57 protected static final boolean IS_FIRST
= false;
58 protected static final boolean IS_NOT_FIRST
= true;
59 // protected static final String SEP = ",";
61 protected Map
<String
, URI
> knownFields
= new HashMap
<String
, URI
>();
62 protected Set
<TermUri
> knownTermFields
= new HashSet
<TermUri
>();
64 public abstract void write(PrintWriter writer
);
65 protected abstract void registerKnownFields();
68 private final CsvDemoMetaDataRecord metaDataRecord
;
69 protected CsvDemoExportConfigurator config
;
74 protected CsvDemoRecordBase(CsvDemoMetaDataRecord metaDataRecord
, CsvDemoExportConfigurator config
){
75 this.metaDataRecord
= metaDataRecord
;
76 this.count
= metaDataRecord
.inc();
80 public void setId(Integer id
) {
84 public Integer
getId() {
88 public void setUuid(UUID uuid
) {
92 /* public UUID getUuid() {
96 protected void printNotes(Set
<Annotation
> notes
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
97 printNotes(notes
, writer
, addSeparator
, fieldKey
.getUriString());
99 protected void printNotes(Set
<Annotation
> notes
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
100 //FIXME handles annotations correctly
102 print(value
, writer
, addSeparator
, fieldKey
);
105 // protected void print(Object object, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
106 // print(object == null ? null : object.toString(), writer, addSeparator, fieldKey);
108 protected void print(CsvDemoId dwcaId
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
109 print(dwcaId
== null ?
null : dwcaId
.getId(), writer
, addSeparator
, fieldKey
);
111 protected void print(UUID uuid
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
112 print(uuid
== null ?
null : uuid
.toString(), writer
, addSeparator
, fieldKey
);
114 protected void print(AgentBase
<?
> agent
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
115 print(agent
, writer
, addSeparator
, fieldKey
.getUriString());
117 protected void print(AgentBase
<?
> agent
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
118 print(agent
== null ?
null : getAgent(agent
), writer
, addSeparator
, fieldKey
);
121 protected void print(Language language
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
122 print(language
, writer
, addSeparator
, fieldKey
.getUriString());
124 protected void print(Language language
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
125 print(language
== null ?
null : getLanguage(language
), writer
, addSeparator
, fieldKey
);
127 protected void print(LSID lsid
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
128 print(lsid
, writer
, addSeparator
, fieldKey
.getUriString());
130 protected void print(LSID lsid
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
131 print(lsid
== null ?
null : String
.valueOf(lsid
.toString()), writer
, addSeparator
, fieldKey
);
134 protected void print(Set
<Rights
> rights
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
135 print(rights
, writer
, addSeparator
, fieldKey
.getUriString());
137 protected void print(Set
<Rights
> rights
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
138 String rightsString
= getRights(rights
);
139 print(rightsString
, writer
, addSeparator
, fieldKey
);
141 protected void print(URI uri
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
142 print(uri
, writer
, addSeparator
, fieldKey
.getUriString());
144 protected void print(URI uri
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
145 print(uri
== null ?
null : String
.valueOf(uri
), writer
, addSeparator
, fieldKey
);
148 protected void print(Point point
, PrintWriter writer
, boolean addSeparator
, TermUri latitudeKey
, TermUri longitudeKey
) {
149 print(point
, writer
, addSeparator
, latitudeKey
.getUriString(), longitudeKey
.getUriString());
152 protected void print(Point point
, PrintWriter writer
, boolean addSeparator
, String latitudeKey
, String longitudeKey
) {
154 String toPrint
= null;
155 print(toPrint
, writer
, addSeparator
, latitudeKey
);
156 print(toPrint
, writer
, addSeparator
, longitudeKey
);
158 String latitude
= point
.getLatitude().toString();
159 String longitude
= point
.getLongitude().toString();
160 print(latitude
, writer
, addSeparator
, latitudeKey
);
161 print(longitude
, writer
, addSeparator
, longitudeKey
);
164 protected void print(Boolean boolValue
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
165 print(boolValue
, writer
, addSeparator
, fieldKey
.getUriString());
167 protected void print(Boolean boolValue
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
168 print(boolValue
== null ?
null : String
.valueOf(boolValue
), writer
, addSeparator
, fieldKey
);
171 protected void print(Integer intValue
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
172 print(intValue
, writer
, addSeparator
, fieldKey
.getUriString());
174 protected void print(Integer intValue
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
175 print(intValue
== null ?
null : String
.valueOf(intValue
), writer
, addSeparator
, fieldKey
);
178 protected void printId(Integer intValue
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
179 print(intValue
== null ?
null : String
.valueOf(intValue
), writer
, addSeparator
, fieldKey
);
181 protected void printId(UUID uuid
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
182 print(uuid
== null ?
null : String
.valueOf(uuid
), writer
, addSeparator
, fieldKey
);
185 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
186 print(value
, writer
, addSeparator
, fieldKey
, null);
189 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
, String defaultValue
) {
190 print(value
, writer
, addSeparator
, fieldKey
.getUriString(), defaultValue
);
193 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
194 print(value
, writer
, addSeparator
, fieldKey
, null);
197 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, String fieldKey
, String defaultValue
) {
198 if (count
== 1 && addSeparator
== IS_NOT_FIRST
){
199 registerFieldKey(URI
.create(fieldKey
), defaultValue
);
201 if (StringUtils
.isBlank(defaultValue
)){
202 String strToPrint
= addSeparator ? config
.getFieldsTerminatedBy() : "";
203 if (StringUtils
.isNotBlank(value
)){
204 //Replace quotes by double quotes
205 value
= value
.replace("\"", "\"\"");
207 value
= value
.replace(config
.getLinesTerminatedBy(), "\\r");
209 //replace all line brakes according to best practices: http://code.google.com/p/gbif-ecat/wiki/BestPractices
210 value
= value
.replace("\r\n", "\\r");
211 value
= value
.replace("\r", "\\r");
212 value
= value
.replace("\n", "\\r");
214 strToPrint
+= config
.getFieldsEnclosedBy() + value
+ config
.getFieldsEnclosedBy();
216 writer
.print(strToPrint
);
225 protected void printHeadline(PrintWriter writer
, ArrayList
<String
> list
, TermUri termUri
){
226 for(String element
:list
){
227 if(list
.get(0).equals(element
)){
228 print(element
, writer
, IS_FIRST
, termUri
);
231 print(element
, writer
, IS_NOT_FIRST
, termUri
);
242 * Method for concatenating strings, especially for the red list use case
244 protected void print(ArrayList
<String
> list
, TermUri termUri
, PrintWriter writer
){
245 if(list
== null||list
.isEmpty()){
246 print("", writer
, IS_NOT_FIRST
, termUri
);
248 for (String element
:list
){
249 if(list
.get(0).equals(element
)){
251 print(element
, writer
, IS_FIRST
, termUri
);
254 logger
.info(element
);
256 }else if(list
.get(list
.size()-1).equals(element
)){
257 print(element
, writer
, IS_FIRST
, termUri
);
259 print(element
, writer
, IS_FIRST
, termUri
);
261 logger
.info(element
);
269 private void registerFieldKey(URI key
, String defaultValue
) {
270 this.metaDataRecord
.addFieldEntry(key
, defaultValue
);
274 protected String
getRights(Rights rights
) {
279 return rights
.getAbbreviatedText();
283 protected String
getLanguage(Language language
) {
284 if (language
== null){
288 return language
.getIso639_2();
292 protected String
getDate(DateTime date
) {
297 return date
.toString();
301 protected String
getNomStatus(NomenclaturalStatusType nomStatus
) {
302 String result
= CsvDemoExportTransformer
.transformToGbifNomStatus(nomStatus
);
304 if (nomStatus
== null){
307 return nomStatus
.getLabel();
314 protected String
getNomCode(NomenclaturalCode nomCode
) {
315 if (nomCode
== null){
319 return nomCode
.getTitleCache();
323 protected String
getRank(Rank rank
) {
324 String result
= CsvDemoExportTransformer
.transformToGbifRank(rank
);
329 return rank
.getLabel();
336 protected String
getSex(DefinedTerm sex
) {
337 String result
= CsvDemoExportTransformer
.transformToGbifSex(sex
);
342 return sex
.getLabel();
349 protected String
getLifeStage(DefinedTerm stage
) {
350 String result
= CsvDemoExportTransformer
.transformToGbifLifeStage(stage
);
355 return stage
.getLabel();
362 protected String
getOccurrenceStatus(PresenceAbsenceTerm status
) {
363 String result
= CsvDemoExportTransformer
.transformToGbifOccStatus(status
);
368 return status
.getLabel();
375 protected String
getEstablishmentMeans(PresenceAbsenceTerm status
) {
376 String result
= CsvDemoExportTransformer
.transformToGbifEstablishmentMeans(status
);
381 return status
.getLabel();
390 protected String
getAgent(AgentBase
<?
> agent
) {
395 return agent
.getTitleCache();
400 protected String
getFeature(Feature feature
) {
401 if (feature
== null){
405 return feature
.getTitleCache();
410 protected String
getTimePeriod(TimePeriod period
) {
415 return period
.toString();
419 protected String
getTimePeriodPart(TimePeriod period
, boolean useEnd
) {
423 Partial date
= useEnd? period
.getEnd(): period
.getStart();
428 return date
.toString();
433 private String
getRights(Set
<Rights
> rights
) {
434 if (rights
== null || rights
.isEmpty()){
437 String result
= null;
438 for (Rights right
: rights
){
439 //TODO usi uri if available ??
440 String message
= "Rights not yet fully implemented";
441 logger
.warn(message
);
442 result
= CdmUtils
.concat(COLLECTION_SEPARATOR
, result
, right
.getAbbreviatedText());
449 protected String
getDesignationType(TypeDesignationStatusBase
<?
> status
) {
454 if (status
.isInstanceOf(SpecimenTypeDesignationStatus
.class)){
455 SpecimenTypeDesignationStatus specStatus
= CdmBase
.deproxy(status
, SpecimenTypeDesignationStatus
.class);
456 result
= CsvDemoExportTransformer
.transformSpecimenTypeStatusToGbif(specStatus
);
458 NameTypeDesignationStatus nameStatus
= CdmBase
.deproxy(status
, NameTypeDesignationStatus
.class);
459 result
= CsvDemoExportTransformer
.transformNameTypeStatusToGbif(nameStatus
);
462 return status
.getLabel();
469 protected void addKnownField(String string
, String uri
) throws URISyntaxException
{
470 this.knownFields
.put(string
, new URI(uri
));
473 protected void addKnownField(TermUri term
) throws URISyntaxException
{
474 this.knownTermFields
.add(term
);