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
;
12 import java
.net
.URISyntaxException
;
13 import java
.util
.ArrayList
;
14 import java
.util
.HashMap
;
15 import java
.util
.HashSet
;
18 import java
.util
.UUID
;
20 import org
.apache
.commons
.lang3
.StringUtils
;
21 import org
.apache
.logging
.log4j
.LogManager
;
22 import org
.apache
.logging
.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
.common
.URI
;
28 import eu
.etaxonomy
.cdm
.io
.stream
.terms
.TermUri
;
29 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.model
.common
.LSID
;
33 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
34 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
35 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
36 import eu
.etaxonomy
.cdm
.model
.description
.PresenceAbsenceTerm
;
37 import eu
.etaxonomy
.cdm
.model
.location
.Point
;
38 import eu
.etaxonomy
.cdm
.model
.media
.Rights
;
39 import eu
.etaxonomy
.cdm
.model
.name
.NameTypeDesignationStatus
;
40 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalCode
;
41 import eu
.etaxonomy
.cdm
.model
.name
.NomenclaturalStatusType
;
42 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
43 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignationStatus
;
44 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationStatusBase
;
45 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTerm
;
48 * @author a.mueller, a.oppermann
51 public abstract class CsvDemoRecordBase
{
53 private static final Logger logger
= LogManager
.getLogger();
55 //TODO Collection_SEPARATOR
56 protected static final CharSequence COLLECTION_SEPARATOR
= "@";
57 // protected static final String FIELD_ENCLOSER = "\"";
58 protected static final boolean IS_FIRST
= false;
59 protected static final boolean IS_NOT_FIRST
= true;
60 // protected static final String SEP = ",";
62 protected Map
<String
, URI
> knownFields
= new HashMap
<String
, URI
>();
63 protected Set
<TermUri
> knownTermFields
= new HashSet
<TermUri
>();
65 public abstract void write(PrintWriter writer
);
66 protected abstract void registerKnownFields();
69 private final CsvDemoMetaDataRecord metaDataRecord
;
70 protected CsvDemoExportConfigurator config
;
75 protected CsvDemoRecordBase(CsvDemoMetaDataRecord metaDataRecord
, CsvDemoExportConfigurator config
){
76 this.metaDataRecord
= metaDataRecord
;
77 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(CsvDemoId 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
);
122 protected void print(Language language
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
123 print(language
, writer
, addSeparator
, fieldKey
.getUriString());
125 protected void print(Language language
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
126 print(language
== null ?
null : getLanguage(language
), writer
, addSeparator
, fieldKey
);
128 protected void print(LSID lsid
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
129 print(lsid
, writer
, addSeparator
, fieldKey
.getUriString());
131 protected void print(LSID lsid
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
132 print(lsid
== null ?
null : String
.valueOf(lsid
.toString()), writer
, addSeparator
, fieldKey
);
135 protected void print(Set
<Rights
> rights
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
136 print(rights
, writer
, addSeparator
, fieldKey
.getUriString());
138 protected void print(Set
<Rights
> rights
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
139 String rightsString
= getRights(rights
);
140 print(rightsString
, writer
, addSeparator
, fieldKey
);
142 protected void print(URI uri
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
143 print(uri
, writer
, addSeparator
, fieldKey
.getUriString());
145 protected void print(URI uri
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
146 print(uri
== null ?
null : String
.valueOf(uri
), writer
, addSeparator
, fieldKey
);
149 protected void print(Point point
, PrintWriter writer
, boolean addSeparator
, TermUri latitudeKey
, TermUri longitudeKey
) {
150 print(point
, writer
, addSeparator
, latitudeKey
.getUriString(), longitudeKey
.getUriString());
153 protected void print(Point point
, PrintWriter writer
, boolean addSeparator
, String latitudeKey
, String longitudeKey
) {
155 String toPrint
= null;
156 print(toPrint
, writer
, addSeparator
, latitudeKey
);
157 print(toPrint
, writer
, addSeparator
, longitudeKey
);
159 String latitude
= point
.getLatitude().toString();
160 String longitude
= point
.getLongitude().toString();
161 print(latitude
, writer
, addSeparator
, latitudeKey
);
162 print(longitude
, writer
, addSeparator
, longitudeKey
);
165 protected void print(Boolean boolValue
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
166 print(boolValue
, writer
, addSeparator
, fieldKey
.getUriString());
168 protected void print(Boolean boolValue
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
169 print(boolValue
== null ?
null : String
.valueOf(boolValue
), writer
, addSeparator
, fieldKey
);
172 protected void print(Integer intValue
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
173 print(intValue
, writer
, addSeparator
, fieldKey
.getUriString());
175 protected void print(Integer intValue
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
176 print(intValue
== null ?
null : String
.valueOf(intValue
), writer
, addSeparator
, fieldKey
);
179 protected void printId(Integer intValue
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
180 print(intValue
== null ?
null : String
.valueOf(intValue
), writer
, addSeparator
, fieldKey
);
182 protected void printId(UUID uuid
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
183 print(uuid
== null ?
null : String
.valueOf(uuid
), writer
, addSeparator
, fieldKey
);
186 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
) {
187 print(value
, writer
, addSeparator
, fieldKey
, null);
190 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, TermUri fieldKey
, String defaultValue
) {
191 print(value
, writer
, addSeparator
, fieldKey
.getUriString(), defaultValue
);
194 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, String fieldKey
) {
195 print(value
, writer
, addSeparator
, fieldKey
, null);
198 protected void print(String value
, PrintWriter writer
, boolean addSeparator
, String fieldKey
, String defaultValue
) {
199 if (count
== 1 && addSeparator
== IS_NOT_FIRST
){
200 registerFieldKey(URI
.create(fieldKey
), defaultValue
);
202 if (StringUtils
.isBlank(defaultValue
)){
203 String strToPrint
= addSeparator ? config
.getFieldsTerminatedBy() : "";
204 if (StringUtils
.isNotBlank(value
)){
205 //Replace quotes by double quotes
206 value
= value
.replace("\"", "\"\"");
208 value
= value
.replace(config
.getLinesTerminatedBy(), "\\r");
210 //replace all line brakes according to best practices: http://code.google.com/p/gbif-ecat/wiki/BestPractices
211 value
= value
.replace("\r\n", "\\r");
212 value
= value
.replace("\r", "\\r");
213 value
= value
.replace("\n", "\\r");
215 strToPrint
+= config
.getFieldsEnclosedBy() + value
+ config
.getFieldsEnclosedBy();
217 writer
.print(strToPrint
);
226 protected void printHeadline(PrintWriter writer
, ArrayList
<String
> list
, TermUri termUri
){
227 for(String element
:list
){
228 if(list
.get(0).equals(element
)){
229 print(element
, writer
, IS_FIRST
, termUri
);
232 print(element
, writer
, IS_NOT_FIRST
, termUri
);
243 * Method for concatenating strings, especially for the red list use case
245 protected void print(ArrayList
<String
> list
, TermUri termUri
, PrintWriter writer
){
246 if(list
== null||list
.isEmpty()){
247 print("", writer
, IS_NOT_FIRST
, termUri
);
249 for (String element
:list
){
250 if(list
.get(0).equals(element
)){
252 print(element
, writer
, IS_FIRST
, termUri
);
255 logger
.info(element
);
257 }else if(list
.get(list
.size()-1).equals(element
)){
258 print(element
, writer
, IS_FIRST
, termUri
);
260 print(element
, writer
, IS_FIRST
, termUri
);
262 logger
.info(element
);
270 private void registerFieldKey(URI key
, String defaultValue
) {
271 this.metaDataRecord
.addFieldEntry(key
, defaultValue
);
275 protected String
getRights(Rights rights
) {
280 return rights
.getAbbreviatedText();
284 protected String
getLanguage(Language language
) {
285 if (language
== null){
289 return language
.getIso639_2();
293 protected String
getDate(DateTime date
) {
298 return date
.toString();
302 protected String
getNomStatus(NomenclaturalStatusType nomStatus
) {
303 String result
= CsvDemoExportTransformer
.transformToGbifNomStatus(nomStatus
);
305 if (nomStatus
== null){
308 return nomStatus
.getLabel();
315 protected String
getNomCode(NomenclaturalCode nomCode
) {
316 if (nomCode
== null){
320 return nomCode
.getTitleCache();
324 protected String
getRank(Rank rank
) {
325 String result
= CsvDemoExportTransformer
.transformToGbifRank(rank
);
330 return rank
.getLabel();
337 protected String
getSex(DefinedTerm sex
) {
338 String result
= CsvDemoExportTransformer
.transformToGbifSex(sex
);
343 return sex
.getLabel();
350 protected String
getLifeStage(DefinedTerm stage
) {
351 String result
= CsvDemoExportTransformer
.transformToGbifLifeStage(stage
);
356 return stage
.getLabel();
363 protected String
getOccurrenceStatus(PresenceAbsenceTerm status
) {
364 String result
= CsvDemoExportTransformer
.transformToGbifOccStatus(status
);
369 return status
.getLabel();
376 protected String
getEstablishmentMeans(PresenceAbsenceTerm status
) {
377 String result
= CsvDemoExportTransformer
.transformToGbifEstablishmentMeans(status
);
382 return status
.getLabel();
391 protected String
getAgent(AgentBase
<?
> agent
) {
396 return agent
.getTitleCache();
401 protected String
getFeature(Feature feature
) {
402 if (feature
== null){
406 return feature
.getTitleCache();
411 protected String
getTimePeriod(TimePeriod period
) {
416 return period
.toString();
420 protected String
getTimePeriodPart(TimePeriod period
, boolean useEnd
) {
424 Partial date
= useEnd? period
.getEnd(): period
.getStart();
429 return date
.toString();
434 private String
getRights(Set
<Rights
> rights
) {
435 if (rights
== null || rights
.isEmpty()){
438 String result
= null;
439 for (Rights right
: rights
){
440 //TODO usi uri if available ??
441 String message
= "Rights not yet fully implemented";
442 logger
.warn(message
);
443 result
= CdmUtils
.concat(COLLECTION_SEPARATOR
, result
, right
.getAbbreviatedText());
450 protected String
getDesignationType(TypeDesignationStatusBase
<?
> status
) {
455 if (status
.isInstanceOf(SpecimenTypeDesignationStatus
.class)){
456 SpecimenTypeDesignationStatus specStatus
= CdmBase
.deproxy(status
, SpecimenTypeDesignationStatus
.class);
457 result
= CsvDemoExportTransformer
.transformSpecimenTypeStatusToGbif(specStatus
);
459 NameTypeDesignationStatus nameStatus
= CdmBase
.deproxy(status
, NameTypeDesignationStatus
.class);
460 result
= CsvDemoExportTransformer
.transformNameTypeStatusToGbif(nameStatus
);
463 return status
.getLabel();
470 protected void addKnownField(String string
, String uri
) throws URISyntaxException
{
471 this.knownFields
.put(string
, new URI(uri
));
474 protected void addKnownField(TermUri term
) throws URISyntaxException
{
475 this.knownTermFields
.add(term
);