2 * Copyright (C) 2007 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
;
12 import java
.io
.FileNotFoundException
;
13 import java
.io
.FileOutputStream
;
14 import java
.io
.IOException
;
15 import java
.io
.OutputStream
;
16 import java
.io
.OutputStreamWriter
;
17 import java
.io
.PrintWriter
;
18 import java
.io
.UnsupportedEncodingException
;
19 import java
.util
.ArrayList
;
20 import java
.util
.HashSet
;
21 import java
.util
.List
;
23 import java
.util
.UUID
;
25 import javax
.xml
.stream
.XMLOutputFactory
;
26 import javax
.xml
.stream
.XMLStreamException
;
27 import javax
.xml
.stream
.XMLStreamWriter
;
29 import org
.apache
.commons
.lang
.StringUtils
;
30 import org
.apache
.log4j
.Logger
;
32 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
33 import eu
.etaxonomy
.cdm
.io
.common
.CdmExportBase
;
34 import eu
.etaxonomy
.cdm
.io
.common
.ICdmExport
;
35 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
.IExportTransformer
;
36 import eu
.etaxonomy
.cdm
.io
.csv
.redlist
.out
.CsvTaxExportConfiguratorRedlist
;
37 import eu
.etaxonomy
.cdm
.io
.csv
.redlist
.out
.CsvTaxExportStateRedlist
;
38 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
39 import eu
.etaxonomy
.cdm
.model
.common
.IOriginalSource
;
40 import eu
.etaxonomy
.cdm
.model
.common
.ISourceable
;
41 import eu
.etaxonomy
.cdm
.model
.location
.Country
;
42 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
44 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
49 * @author a.mueller, a.oppermann
53 public abstract class CsvDemoBase
extends CdmExportBase
<CsvDemoExportConfigurator
, CsvDemoExportState
, IExportTransformer
> implements ICdmExport
<CsvDemoExportConfigurator
, CsvDemoExportState
>{
54 private static final long serialVersionUID
= -2962456879635841019L;
56 private static final Logger logger
= Logger
.getLogger(CsvDemoBase
.class);
58 protected static final boolean IS_CORE
= true;
61 protected Set
<Integer
> existingRecordIds
= new HashSet
<>();
62 protected Set
<UUID
> existingRecordUuids
= new HashSet
<>();
66 public int countSteps() {
67 List
<TaxonNode
> allNodes
= getClassificationService().getAllNodes();
68 return allNodes
.size();
74 * Returns the list of {@link TaxonNode taxon nodes} that are part in one of the given {@link Classification classifications}
75 * and do have a {@link Taxon} attached (empty taxon nodes should not but do exist in CDM databases).
76 * If <code>classificationList</code> is <code>null</code> or empty then all {@link TaxonNode taxon nodes} of all
77 * {@link Classification classifications} are returned.<BR>
78 * Preliminary implementation. Better implement API method for this.
81 protected List
<TaxonNode
> getAllNodes(Set
<Classification
> classificationList
) {
82 //handle empty list as no filter defined
83 if (classificationList
!= null && classificationList
.isEmpty()){
84 classificationList
= null;
87 List
<TaxonNode
> allNodes
= getClassificationService().getAllNodes();
88 List
<TaxonNode
> result
= new ArrayList
<TaxonNode
>();
89 for (TaxonNode node
: allNodes
){
90 if (node
.getClassification() == null ){
92 }else if (classificationList
!= null && ! classificationList
.contains(node
.getClassification())){
95 Taxon taxon
= CdmBase
.deproxy(node
.getTaxon(), Taxon
.class);
97 String message
= "There is a taxon node without taxon: " + node
.getId();
109 * Creates the locationId, locality, countryCode triple
113 protected void handleArea(ICsvAreaRecord record
, NamedArea area
, TaxonBase
<?
> taxon
, boolean required
) {
115 record
.setLocationId(area
);
116 record
.setLocality(area
.getLabel());
117 if (area
.isInstanceOf(Country
.class)){
118 Country country
= CdmBase
.deproxy(area
, Country
.class);
119 record
.setCountryCode(country
.getIso3166_A2());
123 String message
= "Description requires area but area does not exist for taxon " + getTaxonLogString(taxon
);
124 logger
.warn(message
);
130 protected String
getTaxonLogString(TaxonBase
<?
> taxon
) {
131 return taxon
.getTitleCache() + "(" + taxon
.getId() + ")";
139 protected boolean recordExists(CdmBase el
) {
140 return existingRecordIds
.contains(el
.getId());
147 protected void addExistingRecord(CdmBase cdmBase
) {
148 existingRecordIds
.add(cdmBase
.getId());
155 protected boolean recordExistsUuid(CdmBase el
) {
156 return existingRecordUuids
.contains(el
.getUuid());
162 protected void addExistingRecordUuid(CdmBase cdmBase
) {
163 existingRecordUuids
.add(cdmBase
.getUuid());
167 protected String
getSources(ISourceable
<?
> sourceable
, CsvTaxExportConfiguratorRedlist config
) {
169 for (IOriginalSource source
: sourceable
.getSources()){
170 if (StringUtils
.isBlank(source
.getIdInSource())){//idInSource indicates that this source is only data provenance, may be changed in future
171 if (source
.getCitation() != null){
172 String ref
= source
.getCitation().getTitleCache();
173 result
= CdmUtils
.concat(config
.getSetSeparator(), result
, ref
);
184 * @throws IOException
185 * @throws FileNotFoundException
187 protected FileOutputStream
createFileOutputStream(CsvTaxExportConfiguratorRedlist config
, String thisFileName
) throws IOException
, FileNotFoundException
{
188 String filePath
= config
.getDestinationNameString();
189 String fileName
= filePath
+ File
.separatorChar
+ thisFileName
;
190 File f
= new File(fileName
);
194 FileOutputStream fos
= new FileOutputStream(f
);
203 * @throws IOException
204 * @throws FileNotFoundException
205 * @throws XMLStreamException
207 protected XMLStreamWriter
createXmlStreamWriter(CsvTaxExportStateRedlist state
, String fileName
)
208 throws IOException
, FileNotFoundException
, XMLStreamException
{
209 XMLOutputFactory factory
= XMLOutputFactory
.newInstance();
211 boolean useZip
= state
.isZip();
213 os
= state
.getZipStream(fileName
);
215 os
= createFileOutputStream(state
.getConfig(), fileName
);
217 XMLStreamWriter writer
= factory
.createXMLStreamWriter(os
);
223 * @param coreTaxFileName
226 * @throws IOException
227 * @throws FileNotFoundException
228 * @throws UnsupportedEncodingException
230 protected PrintWriter
createPrintWriter(final String fileName
, CsvTaxExportStateRedlist state
)
231 throws IOException
, FileNotFoundException
, UnsupportedEncodingException
{
234 boolean useZip
= state
.isZip();
236 os
= state
.getZipStream(fileName
);
238 os
= createFileOutputStream(state
.getConfig(), fileName
);
240 PrintWriter writer
= new PrintWriter(new OutputStreamWriter(os
, "UTF8"), true);
253 protected void closeWriter(PrintWriter writer
, CsvTaxExportStateRedlist state
) {
254 if (writer
!= null && state
.isZip() == false){
263 * Note: XMLStreamWriter does not close the underlying stream.
267 protected void closeWriter(XMLStreamWriter writer
, CsvTaxExportStateRedlist state
) {
268 if (writer
!= null && state
.isZip() == false){
271 } catch (XMLStreamException e
) {
272 throw new RuntimeException(e
);
276 protected void clearExistingRecordIds(){
277 existingRecordIds
.clear();