-/**\r
-* Copyright (C) 2007 EDIT\r
-* European Distributed Institute of Taxonomy \r
-* http://www.e-taxonomy.eu\r
-* \r
-* The contents of this file are subject to the Mozilla Public License Version 1.1\r
-* See LICENSE.TXT at the top of this package for the full license terms.\r
-*/\r
-package eu.etaxonomy.cdm.io.csv.redlist.out;\r
-\r
-import java.io.File;\r
-import java.io.FileNotFoundException;\r
-import java.io.FileOutputStream;\r
-import java.io.IOException;\r
-import java.io.OutputStream;\r
-import java.io.OutputStreamWriter;\r
-import java.io.PrintWriter;\r
-import java.io.UnsupportedEncodingException;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Set;\r
-import java.util.UUID;\r
-\r
-import javax.xml.stream.XMLOutputFactory;\r
-import javax.xml.stream.XMLStreamException;\r
-import javax.xml.stream.XMLStreamWriter;\r
-\r
-import org.apache.commons.lang.StringUtils;\r
-import org.apache.log4j.Logger;\r
-\r
-import eu.etaxonomy.cdm.common.CdmUtils;\r
-import eu.etaxonomy.cdm.io.common.CdmExportBase;\r
-import eu.etaxonomy.cdm.io.common.ICdmExport;\r
-import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;\r
-import eu.etaxonomy.cdm.model.common.CdmBase;\r
-import eu.etaxonomy.cdm.model.common.IOriginalSource;\r
-import eu.etaxonomy.cdm.model.common.ISourceable;\r
-import eu.etaxonomy.cdm.model.location.Country;\r
-import eu.etaxonomy.cdm.model.location.NamedArea;\r
-import eu.etaxonomy.cdm.model.taxon.Taxon;\r
-import eu.etaxonomy.cdm.model.taxon.TaxonBase;\r
-import eu.etaxonomy.cdm.model.taxon.TaxonNode;\r
-\r
-/**\r
- * @author a.mueller, a.oppermann\r
- * @date 18.10.2012\r
- *\r
- */\r
-public abstract class CsvExportBaseRedlist extends CdmExportBase<CsvTaxExportConfiguratorRedlist, CsvTaxExportStateRedlist, IExportTransformer> implements ICdmExport<CsvTaxExportConfiguratorRedlist, CsvTaxExportStateRedlist>{\r
- private static final Logger logger = Logger.getLogger(CsvExportBaseRedlist.class);\r
- \r
- protected static final boolean IS_CORE = true;\r
- \r
- \r
- protected Set<Integer> existingRecordIds = new HashSet<Integer>();\r
- protected Set<UUID> existingRecordUuids = new HashSet<UUID>();\r
- \r
- \r
-\r
- /* (non-Javadoc)\r
- * @see eu.etaxonomy.cdm.io.common.CdmIoBase#countSteps()\r
- */\r
- @Override\r
- public int countSteps() {\r
- List<TaxonNode> allNodes = getClassificationService().getAllNodes();\r
- return allNodes.size();\r
- }\r
-\r
- \r
- \r
- /**\r
- * Returns all children of the given taxon node and the node itself if the node has\r
- * a {@link Taxon} attached (empty taxon nodes should not but do exist in CDM databases).\r
- * @return\r
- */\r
- protected Set<TaxonNode> getAllNodes(Set<TaxonNode> taxonNodes) {\r
- //handle empty list as no filter defined\r
- if (taxonNodes != null && taxonNodes.isEmpty()){\r
- taxonNodes = null;\r
- }\r
- \r
- List<TaxonNode> allNodes = getClassificationService().getAllNodes();\r
- Set<TaxonNode> result = new HashSet<TaxonNode>();\r
- for (TaxonNode node : allNodes){\r
- if (node.getClassification() == null ){\r
- continue;\r
- }else if (taxonNodes != null && ! taxonNodes.contains(node.getClassification())){\r
- continue;\r
- }else{\r
- Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);\r
- if (taxon == null){\r
- String message = "There is a taxon node without taxon: " + node.getId();\r
- logger.warn(message);\r
- continue;\r
- }\r
- result.add(node);\r
- }\r
- }\r
- return result;\r
- }\r
- \r
- \r
- /**\r
- * Creates the locationId, locality, countryCode triple\r
- * @param record\r
- * @param area\r
- */\r
- protected void handleArea(ICsvAreaRecord record, NamedArea area, TaxonBase<?> taxon, boolean required) {\r
- if (area != null){\r
- record.setLocationId(area);\r
- record.setLocality(area.getLabel());\r
- if (area.isInstanceOf(Country.class)){\r
- Country country = CdmBase.deproxy(area, Country.class);\r
- record.setCountryCode(country.getIso3166_A2());\r
- }\r
- }else{\r
- if (required){\r
- String message = "Description requires area but area does not exist for taxon " + getTaxonLogString(taxon);\r
- logger.warn(message);\r
- }\r
- }\r
- }\r
-\r
-\r
- protected String getTaxonLogString(TaxonBase<?> taxon) {\r
- return taxon.getTitleCache() + "(" + taxon.getId() + ")";\r
- }\r
- \r
-\r
- /**\r
- * @param el\r
- * @return\r
- */\r
- protected boolean recordExists(CdmBase el) {\r
- return existingRecordIds.contains(el.getId());\r
- }\r
- \r
-\r
- /**\r
- * @param sec\r
- */\r
- protected void addExistingRecord(CdmBase cdmBase) {\r
- existingRecordIds.add(cdmBase.getId());\r
- }\r
- \r
- /**\r
- * @param el\r
- * @return\r
- */\r
- protected boolean recordExistsUuid(CdmBase el) {\r
- return existingRecordUuids.contains(el.getUuid());\r
- }\r
- \r
- /**\r
- * @param sec\r
- */\r
- protected void addExistingRecordUuid(CdmBase cdmBase) {\r
- existingRecordUuids.add(cdmBase.getUuid());\r
- }\r
- \r
-\r
- protected String getSources(ISourceable<?> sourceable, CsvTaxExportConfiguratorRedlist config) {\r
- String result = "";\r
- for (IOriginalSource source: sourceable.getSources()){\r
- if (StringUtils.isBlank(source.getIdInSource())){//idInSource indicates that this source is only data provenance, may be changed in future\r
- if (source.getCitation() != null){\r
- String ref = source.getCitation().getTitleCache();\r
- result = CdmUtils.concat(config.getSetSeparator(), result, ref);\r
- }\r
- }\r
- }\r
- return result;\r
- }\r
- \r
-\r
- /**\r
- * @param config\r
- * @return\r
- * @throws IOException\r
- * @throws FileNotFoundException\r
- */\r
- protected FileOutputStream createFileOutputStream(CsvTaxExportConfiguratorRedlist config, String thisFileName) throws IOException, FileNotFoundException {\r
- String filePath = config.getDestinationNameString();\r
- String fileName = filePath + File.separatorChar + thisFileName;\r
- File f = new File(fileName);\r
- if (!f.exists()){\r
- f.createNewFile();\r
- }\r
- FileOutputStream fos = new FileOutputStream(f);\r
- return fos;\r
- }\r
- \r
-\r
- /**\r
- * @param config\r
- * @param factory\r
- * @return\r
- * @throws IOException\r
- * @throws FileNotFoundException\r
- * @throws XMLStreamException\r
- */\r
- protected XMLStreamWriter createXmlStreamWriter(CsvTaxExportStateRedlist state, String fileName)\r
- throws IOException, FileNotFoundException, XMLStreamException {\r
- XMLOutputFactory factory = XMLOutputFactory.newInstance(); \r
- OutputStream os;\r
- boolean useZip = state.isZip();\r
- if (useZip){\r
- os = state.getZipStream(fileName);\r
- }else{\r
- os = createFileOutputStream(state.getConfig(), fileName);\r
- }\r
- XMLStreamWriter writer = factory.createXMLStreamWriter(os);\r
- return writer;\r
- }\r
- \r
-\r
- /**\r
- * @param coreTaxFileName\r
- * @param config\r
- * @return\r
- * @throws IOException\r
- * @throws FileNotFoundException\r
- * @throws UnsupportedEncodingException\r
- */\r
- protected PrintWriter createPrintWriter(final String fileName, CsvTaxExportStateRedlist state) \r
- throws IOException, FileNotFoundException, UnsupportedEncodingException {\r
- \r
- OutputStream os;\r
- boolean useZip = state.isZip();\r
- if (useZip){\r
- os = state.getZipStream(fileName);\r
- }else{\r
- os = createFileOutputStream(state.getConfig(), fileName);\r
- }\r
- PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF8"), true);\r
- \r
- return writer;\r
- }\r
- \r
-\r
-\r
- \r
- /**\r
- * Closes the writer\r
- * @param writer\r
- * @param state\r
- */\r
- protected void closeWriter(PrintWriter writer, CsvTaxExportStateRedlist state) {\r
- if (writer != null && state.isZip() == false){\r
- writer.close();\r
- }\r
- }\r
- \r
-\r
- \r
- /**\r
- * Closes the writer.\r
- * Note: XMLStreamWriter does not close the underlying stream.\r
- * @param writer\r
- * @param state\r
- */\r
- protected void closeWriter(XMLStreamWriter writer, CsvTaxExportStateRedlist state) {\r
- if (writer != null && state.isZip() == false){\r
- try {\r
- writer.close();\r
- } catch (XMLStreamException e) {\r
- throw new RuntimeException(e);\r
- }\r
- }\r
- }\r
- protected void clearExistingRecordIds(){\r
- existingRecordIds.clear();\r
- }\r
-}\r
+/**
+* Copyright (C) 2007 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.io.csv.redlist.out;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+
+import eu.etaxonomy.cdm.common.CdmUtils;
+import eu.etaxonomy.cdm.io.common.CdmExportBase;
+import eu.etaxonomy.cdm.io.common.ICdmExport;
+import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.common.IOriginalSource;
+import eu.etaxonomy.cdm.model.common.ISourceable;
+import eu.etaxonomy.cdm.model.location.Country;
+import eu.etaxonomy.cdm.model.location.NamedArea;
+import eu.etaxonomy.cdm.model.taxon.TaxonBase;
+
+/**
+ * @author a.mueller, a.oppermann
+ * @date 18.10.2012
+ *
+ */
+public abstract class CsvExportBaseRedlist extends CdmExportBase<CsvTaxExportConfiguratorRedlist, CsvTaxExportStateRedlist, IExportTransformer> implements ICdmExport<CsvTaxExportConfiguratorRedlist, CsvTaxExportStateRedlist>{
+ private static final Logger logger = Logger.getLogger(CsvExportBaseRedlist.class);
+
+ protected static final boolean IS_CORE = true;
+
+
+ protected Set<Integer> existingRecordIds = new HashSet<Integer>();
+ protected Set<UUID> existingRecordUuids = new HashSet<UUID>();
+
+ /**
+ * Creates the locationId, locality, countryCode triple
+ * @param record
+ * @param area
+ */
+ protected void handleArea(ICsvAreaRecord record, NamedArea area, TaxonBase<?> taxon, boolean required) {
+ if (area != null){
+ record.setLocationId(area);
+ record.setLocality(area.getLabel());
+ if (area.isInstanceOf(Country.class)){
+ Country country = CdmBase.deproxy(area, Country.class);
+ record.setCountryCode(country.getIso3166_A2());
+ }
+ }else{
+ if (required){
+ String message = "Description requires area but area does not exist for taxon " + getTaxonLogString(taxon);
+ logger.warn(message);
+ }
+ }
+ }
+
+
+ protected String getTaxonLogString(TaxonBase<?> taxon) {
+ return taxon.getTitleCache() + "(" + taxon.getId() + ")";
+ }
+
+
+ /**
+ * @param el
+ * @return
+ */
+ protected boolean recordExists(CdmBase el) {
+ return existingRecordIds.contains(el.getId());
+ }
+
+
+ /**
+ * @param sec
+ */
+ protected void addExistingRecord(CdmBase cdmBase) {
+ existingRecordIds.add(cdmBase.getId());
+ }
+
+ /**
+ * @param el
+ * @return
+ */
+ protected boolean recordExistsUuid(CdmBase el) {
+ return existingRecordUuids.contains(el.getUuid());
+ }
+
+ /**
+ * @param sec
+ */
+ protected void addExistingRecordUuid(CdmBase cdmBase) {
+ existingRecordUuids.add(cdmBase.getUuid());
+ }
+
+
+ protected String getSources(ISourceable<?> sourceable, CsvTaxExportConfiguratorRedlist config) {
+ String result = "";
+ for (IOriginalSource source: sourceable.getSources()){
+ if (StringUtils.isBlank(source.getIdInSource())){//idInSource indicates that this source is only data provenance, may be changed in future
+ if (source.getCitation() != null){
+ String ref = source.getCitation().getTitleCache();
+ result = CdmUtils.concat(config.getSetSeparator(), result, ref);
+ }
+ }
+ }
+ return result;
+ }
+
+
+ /**
+ * @param config
+ * @return
+ * @throws IOException
+ * @throws FileNotFoundException
+ */
+ protected FileOutputStream createFileOutputStream(CsvTaxExportConfiguratorRedlist config, String thisFileName) throws IOException, FileNotFoundException {
+ String filePath = config.getDestinationNameString();
+ String fileName = filePath + File.separatorChar + thisFileName;
+ File f = new File(fileName);
+ if (!f.exists()){
+ f.createNewFile();
+ }
+ FileOutputStream fos = new FileOutputStream(f);
+ return fos;
+ }
+
+
+ /**
+ * @param config
+ * @param factory
+ * @return
+ * @throws IOException
+ * @throws FileNotFoundException
+ * @throws XMLStreamException
+ */
+ protected XMLStreamWriter createXmlStreamWriter(CsvTaxExportStateRedlist state, String fileName)
+ throws IOException, FileNotFoundException, XMLStreamException {
+ XMLOutputFactory factory = XMLOutputFactory.newInstance();
+ OutputStream os;
+ boolean useZip = state.isZip();
+ if (useZip){
+ os = state.getZipStream(fileName);
+ }else{
+ os = createFileOutputStream(state.getConfig(), fileName);
+ }
+ XMLStreamWriter writer = factory.createXMLStreamWriter(os);
+ return writer;
+ }
+
+
+ /**
+ * @param coreTaxFileName
+ * @param config
+ * @return
+ * @throws IOException
+ * @throws FileNotFoundException
+ * @throws UnsupportedEncodingException
+ */
+ protected PrintWriter createPrintWriter(final String fileName, CsvTaxExportStateRedlist state)
+ throws IOException, FileNotFoundException, UnsupportedEncodingException {
+
+ OutputStream os;
+ boolean useZip = state.isZip();
+ if (useZip){
+ os = state.getZipStream(fileName);
+ }else{
+ os = createFileOutputStream(state.getConfig(), fileName);
+ }
+ PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF8"), true);
+
+ return writer;
+ }
+
+
+
+
+ /**
+ * Closes the writer
+ * @param writer
+ * @param state
+ */
+ protected void closeWriter(PrintWriter writer, CsvTaxExportStateRedlist state) {
+ if (writer != null && state.isZip() == false){
+ writer.close();
+ }
+ }
+
+
+
+ /**
+ * Closes the writer.
+ * Note: XMLStreamWriter does not close the underlying stream.
+ * @param writer
+ * @param state
+ */
+ protected void closeWriter(XMLStreamWriter writer, CsvTaxExportStateRedlist state) {
+ if (writer != null && state.isZip() == false){
+ try {
+ writer.close();
+ } catch (XMLStreamException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }
+ protected void clearExistingRecordIds(){
+ existingRecordIds.clear();
+ }
+}
-/**\r
- * Copyright (C) 2007 EDIT\r
- * European Distributed Institute of Taxonomy \r
- * http://www.e-taxonomy.eu\r
- * \r
- * The contents of this file are subject to the Mozilla Public License Version 1.1\r
- * See LICENSE.TXT at the top of this package for the full license terms.\r
- */\r
-\r
-package eu.etaxonomy.cdm.io.csv.redlist.out;\r
-\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.File;\r
-import java.util.ArrayList;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Set;\r
-import java.util.UUID;\r
-\r
-import org.apache.log4j.Logger;\r
-\r
-import eu.etaxonomy.cdm.database.ICdmDataSource;\r
-import eu.etaxonomy.cdm.io.common.XmlExportConfiguratorBase;\r
-import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;\r
-import eu.etaxonomy.cdm.model.description.Feature;\r
-import eu.etaxonomy.cdm.model.location.NamedArea;\r
-\r
-\r
-/**\r
- * @author a.oppermann\r
- * @created 17.10.2012\r
- */\r
-public class CsvTaxExportConfiguratorRedlist extends XmlExportConfiguratorBase<CsvTaxExportStateRedlist> {\r
- @SuppressWarnings("unused")\r
- private static final Logger logger = Logger.getLogger(CsvTaxExportConfiguratorRedlist.class);\r
-\r
- private String encoding = "UTF-8";\r
- private String linesTerminatedBy = "\r\n";\r
- private String fieldsEnclosedBy = "\"";\r
- private boolean hasHeaderLines = true;\r
- private String fieldsTerminatedBy=",";\r
- private boolean doTaxa = true;\r
- private boolean doDistributions = false;\r
- private ByteArrayOutputStream baos;\r
- private boolean isUseIdWherePossible = false;\r
- private boolean includeBasionymsInResourceRelations;\r
- private boolean includeMisappliedNamesInResourceRelations;\r
- private String defaultBibliographicCitation = null;\r
- private List<UUID> featureExclusions = new ArrayList<UUID>();\r
- //filter on the classifications to be exported\r
- private Set<UUID> classificationUuids = new HashSet<UUID>(); \r
- private boolean withHigherClassification = false;\r
- private String setSeparator = ";";\r
-\r
- private List<Feature> features;\r
-\r
- private String classificationTitleCache;\r
-\r
- private List<NamedArea> areas;\r
-\r
- //TODO\r
- private static IExportTransformer defaultTransformer = null;\r
-\r
- public static CsvTaxExportConfiguratorRedlist NewInstance(ICdmDataSource source, File destinationFolder) { \r
- return new CsvTaxExportConfiguratorRedlist(source, destinationFolder);\r
- }\r
-\r
- @Override\r
- @SuppressWarnings("unchecked")\r
- protected void makeIoClassList() {\r
- ioClassList = new Class[] {\r
- CsvTaxExportRedlist.class\r
- };\r
- }\r
-\r
- /**\r
- * @param url\r
- * @param destination\r
- */\r
- private CsvTaxExportConfiguratorRedlist(ICdmDataSource source, File destination) {\r
- super(destination, source, defaultTransformer);\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see eu.etaxonomy.cdm.io.common.ImportConfiguratorBase#getSource()\r
- */\r
- @Override\r
- public File getDestination() {\r
- return super.getDestination();\r
- }\r
-\r
- /**\r
- * @param file\r
- */\r
- @Override\r
- public void setDestination(File fileName) {\r
- super.setDestination(fileName);\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see eu.etaxonomy.cdm.io.common.IExportConfigurator#getDestinationNameString()\r
- */\r
- @Override\r
- public String getDestinationNameString() {\r
- if (this.getDestination() == null) {\r
- return null;\r
- } else {\r
- return this.getDestination().toString();\r
- }\r
- }\r
-\r
- /* (non-Javadoc)\r
- * @see eu.etaxonomy.cdm.io.common.IExportConfigurator#getNewState()\r
- */\r
- public CsvTaxExportStateRedlist getNewState() {\r
- return new CsvTaxExportStateRedlist(this);\r
- }\r
-\r
- public boolean isDoTaxa() {\r
- return doTaxa;\r
- }\r
-\r
- public void setDoTaxa(boolean doTaxa) {\r
- this.doTaxa = doTaxa;\r
- }\r
-\r
-\r
- public boolean isDoDistributions() {\r
- return doDistributions;\r
- }\r
-\r
- public void setDoDistributions(boolean doDistributions) {\r
- this.doDistributions = doDistributions;\r
- }\r
-\r
- public void setFeatureExclusions(List<UUID> featureExclusions) {\r
- this.featureExclusions = featureExclusions;\r
- }\r
-\r
- public List<UUID> getFeatureExclusions() {\r
- return featureExclusions;\r
- }\r
-\r
- public String getEncoding() {\r
- return encoding;\r
- }\r
-\r
- public void setEncoding(String encoding) {\r
- this.encoding = encoding;\r
- }\r
-\r
- public String getLinesTerminatedBy() {\r
- return linesTerminatedBy;\r
- }\r
-\r
- public void setLinesTerminatedBy(String linesTerminatedBy) {\r
- this.linesTerminatedBy = linesTerminatedBy;\r
- }\r
-\r
- public String getFieldsEnclosedBy() {\r
- return fieldsEnclosedBy;\r
- }\r
-\r
- public void setFieldsEnclosedBy(String fieldsEnclosedBy) {\r
- this.fieldsEnclosedBy = fieldsEnclosedBy;\r
- }\r
-\r
- /**\r
- * Equals darwin core archive ignoreHeaderLines attribute\r
- * @return\r
- */\r
- public boolean isHasHeaderLines() {\r
- return hasHeaderLines;\r
- }\r
-\r
- public void setHasHeaderLines(boolean hasHeaderLines) {\r
- this.hasHeaderLines = hasHeaderLines;\r
- }\r
-\r
- public boolean isIncludeBasionymsInResourceRelations() {\r
- return includeBasionymsInResourceRelations;\r
- }\r
-\r
- public void setIncludeBasionymsInResourceRelations(boolean includeBasionymsInResourceRelations) {\r
- this.includeBasionymsInResourceRelations = includeBasionymsInResourceRelations;\r
- }\r
-\r
- public boolean isIncludeMisappliedNamesInResourceRelations() {\r
- return includeMisappliedNamesInResourceRelations;\r
- }\r
-\r
- public void setIncludeMisappliedNamesInResourceRelations(boolean includeMisappliedNamesInResourceRelations) {\r
- this.includeMisappliedNamesInResourceRelations = includeMisappliedNamesInResourceRelations;\r
- }\r
-\r
- public boolean isUseIdWherePossible() {\r
- return this.isUseIdWherePossible;\r
- }\r
-\r
- public void setUseIdWherePossible(boolean isUseIdWherePossible) {\r
- this.isUseIdWherePossible = isUseIdWherePossible;\r
- }\r
-\r
- public void setDefaultBibliographicCitation(String defaultBibliographicCitation) {\r
- this.defaultBibliographicCitation = defaultBibliographicCitation;\r
- }\r
-\r
-\r
- public String getDefaultBibliographicCitation() {\r
- return defaultBibliographicCitation;\r
- }\r
-\r
- /**\r
- * The default value for the taxon.source column. This may be a column linking to a url that provides \r
- * data about the given taxon. The id is replaced by a placeholder, \r
- * e.g. http://wp6-cichorieae.e-taxonomy.eu/portal/?q=cdm_dataportal/taxon/{id}.\r
- * NOTE: This may be replaced in future versions by concrete CDM server implementations.\r
- * \r
- * @return the taxonSourceDefault\r
- */\r
-\r
- public boolean isWithHigherClassification() {\r
- return withHigherClassification;\r
- }\r
-\r
- public void setWithHigherClassification(boolean withHigherClassification) {\r
- this.withHigherClassification = withHigherClassification;\r
- }\r
-\r
- /**\r
- * @return the setSeparator\r
- */\r
- public String getSetSeparator() {\r
- return setSeparator;\r
- }\r
-\r
- /**\r
- * @param setSeparator the setSeparator to set\r
- */\r
- public void setSetSeparator(String setSeparator) {\r
- this.setSeparator = setSeparator;\r
- }\r
-\r
- public void setFieldsTerminatedBy(String fieldsTerminatedBy) {\r
- this.fieldsTerminatedBy = fieldsTerminatedBy;\r
- }\r
-\r
- public String getFieldsTerminatedBy() {\r
- return fieldsTerminatedBy;\r
- }\r
-\r
- public Set<UUID> getTaxonNodeUuids() {\r
- return classificationUuids;\r
- }\r
-\r
- public void setTaxonNodeUuids(Set<UUID> classificationUuids) {\r
- this.classificationUuids = classificationUuids;\r
- }\r
-\r
- public ByteArrayOutputStream getByteArrayOutputStream() {\r
- return baos;\r
- }\r
-\r
- public void setByteArrayOutputStream(ByteArrayOutputStream baos) {\r
- this.baos = baos;\r
- }\r
-\r
- public void setFeatures(List<Feature> features) {\r
- this.features = features;\r
- \r
- }\r
- \r
- public List<Feature> getFeatures() {\r
- return features;\r
- \r
- }\r
-\r
- public void setClassificationTitleCache(String classificationTitleCache) {\r
- this.classificationTitleCache = classificationTitleCache;\r
- }\r
- \r
- public String getClassificationTitleCache() {\r
- return classificationTitleCache;\r
- }\r
-\r
- /**\r
- * @param areas\r
- */\r
- public void setNamedAreas(List<NamedArea> areas) {\r
- // TODO Auto-generated method stub\r
- this.areas = areas;\r
- \r
- }\r
- public List<NamedArea> getNamedAreas(){\r
- return areas;\r
- }\r
- \r
+/**
+ * Copyright (C) 2007 EDIT
+ * European Distributed Institute of Taxonomy
+ * http://www.e-taxonomy.eu
+ *
+ * The contents of this file are subject to the Mozilla Public License Version 1.1
+ * See LICENSE.TXT at the top of this package for the full license terms.
+ */
+
+package eu.etaxonomy.cdm.io.csv.redlist.out;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+
+import org.apache.log4j.Logger;
+
+import eu.etaxonomy.cdm.database.ICdmDataSource;
+import eu.etaxonomy.cdm.io.common.XmlExportConfiguratorBase;
+import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
+import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.location.NamedArea;
+
+
+/**
+ * @author a.oppermann
+ * @created 17.10.2012
+ */
+public class CsvTaxExportConfiguratorRedlist extends XmlExportConfiguratorBase<CsvTaxExportStateRedlist> {
+ @SuppressWarnings("unused")
+ private static final Logger logger = Logger.getLogger(CsvTaxExportConfiguratorRedlist.class);
+
+ private String encoding = "UTF-8";
+ private String linesTerminatedBy = "\r\n";
+ private String fieldsEnclosedBy = "\"";
+ private boolean hasHeaderLines = true;
+ private String fieldsTerminatedBy=",";
+ private boolean doTaxa = true;
+ private boolean doDistributions = false;
+ private ByteArrayOutputStream baos;
+ private boolean isUseIdWherePossible = false;
+ private boolean includeBasionymsInResourceRelations;
+ private boolean includeMisappliedNamesInResourceRelations;
+ private String defaultBibliographicCitation = null;
+ private List<UUID> featureExclusions = new ArrayList<UUID>();
+ //filter on the classifications to be exported
+ private Set<UUID> taxonNodeUuids = new HashSet<UUID>();
+ private boolean withHigherClassification = false;
+ private String setSeparator = ";";
+
+ private List<Feature> features;
+
+ private String classificationTitleCache;
+
+ private List<NamedArea> areas;
+
+ //TODO
+ private static IExportTransformer defaultTransformer = null;
+
+ public static CsvTaxExportConfiguratorRedlist NewInstance(ICdmDataSource source, File destinationFolder) {
+ return new CsvTaxExportConfiguratorRedlist(source, destinationFolder);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected void makeIoClassList() {
+ ioClassList = new Class[] {
+ CsvTaxExportRedlist.class
+ };
+ }
+
+ /**
+ * @param url
+ * @param destination
+ */
+ private CsvTaxExportConfiguratorRedlist(ICdmDataSource source, File destination) {
+ super(destination, source, defaultTransformer);
+ }
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.io.common.ImportConfiguratorBase#getSource()
+ */
+ @Override
+ public File getDestination() {
+ return super.getDestination();
+ }
+
+ /**
+ * @param file
+ */
+ @Override
+ public void setDestination(File fileName) {
+ super.setDestination(fileName);
+ }
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.io.common.IExportConfigurator#getDestinationNameString()
+ */
+ @Override
+ public String getDestinationNameString() {
+ if (this.getDestination() == null) {
+ return null;
+ } else {
+ return this.getDestination().toString();
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.io.common.IExportConfigurator#getNewState()
+ */
+ public CsvTaxExportStateRedlist getNewState() {
+ return new CsvTaxExportStateRedlist(this);
+ }
+
+ public boolean isDoTaxa() {
+ return doTaxa;
+ }
+
+ public void setDoTaxa(boolean doTaxa) {
+ this.doTaxa = doTaxa;
+ }
+
+
+ public boolean isDoDistributions() {
+ return doDistributions;
+ }
+
+ public void setDoDistributions(boolean doDistributions) {
+ this.doDistributions = doDistributions;
+ }
+
+ public void setFeatureExclusions(List<UUID> featureExclusions) {
+ this.featureExclusions = featureExclusions;
+ }
+
+ public List<UUID> getFeatureExclusions() {
+ return featureExclusions;
+ }
+
+ public String getEncoding() {
+ return encoding;
+ }
+
+ public void setEncoding(String encoding) {
+ this.encoding = encoding;
+ }
+
+ public String getLinesTerminatedBy() {
+ return linesTerminatedBy;
+ }
+
+ public void setLinesTerminatedBy(String linesTerminatedBy) {
+ this.linesTerminatedBy = linesTerminatedBy;
+ }
+
+ public String getFieldsEnclosedBy() {
+ return fieldsEnclosedBy;
+ }
+
+ public void setFieldsEnclosedBy(String fieldsEnclosedBy) {
+ this.fieldsEnclosedBy = fieldsEnclosedBy;
+ }
+
+ /**
+ * Equals darwin core archive ignoreHeaderLines attribute
+ * @return
+ */
+ public boolean isHasHeaderLines() {
+ return hasHeaderLines;
+ }
+
+ public void setHasHeaderLines(boolean hasHeaderLines) {
+ this.hasHeaderLines = hasHeaderLines;
+ }
+
+ public boolean isIncludeBasionymsInResourceRelations() {
+ return includeBasionymsInResourceRelations;
+ }
+
+ public void setIncludeBasionymsInResourceRelations(boolean includeBasionymsInResourceRelations) {
+ this.includeBasionymsInResourceRelations = includeBasionymsInResourceRelations;
+ }
+
+ public boolean isIncludeMisappliedNamesInResourceRelations() {
+ return includeMisappliedNamesInResourceRelations;
+ }
+
+ public void setIncludeMisappliedNamesInResourceRelations(boolean includeMisappliedNamesInResourceRelations) {
+ this.includeMisappliedNamesInResourceRelations = includeMisappliedNamesInResourceRelations;
+ }
+
+ public boolean isUseIdWherePossible() {
+ return this.isUseIdWherePossible;
+ }
+
+ public void setUseIdWherePossible(boolean isUseIdWherePossible) {
+ this.isUseIdWherePossible = isUseIdWherePossible;
+ }
+
+ public void setDefaultBibliographicCitation(String defaultBibliographicCitation) {
+ this.defaultBibliographicCitation = defaultBibliographicCitation;
+ }
+
+
+ public String getDefaultBibliographicCitation() {
+ return defaultBibliographicCitation;
+ }
+
+ /**
+ * The default value for the taxon.source column. This may be a column linking to a url that provides
+ * data about the given taxon. The id is replaced by a placeholder,
+ * e.g. http://wp6-cichorieae.e-taxonomy.eu/portal/?q=cdm_dataportal/taxon/{id}.
+ * NOTE: This may be replaced in future versions by concrete CDM server implementations.
+ *
+ * @return the taxonSourceDefault
+ */
+
+ public boolean isWithHigherClassification() {
+ return withHigherClassification;
+ }
+
+ public void setWithHigherClassification(boolean withHigherClassification) {
+ this.withHigherClassification = withHigherClassification;
+ }
+
+ /**
+ * @return the setSeparator
+ */
+ public String getSetSeparator() {
+ return setSeparator;
+ }
+
+ /**
+ * @param setSeparator the setSeparator to set
+ */
+ public void setSetSeparator(String setSeparator) {
+ this.setSeparator = setSeparator;
+ }
+
+ public void setFieldsTerminatedBy(String fieldsTerminatedBy) {
+ this.fieldsTerminatedBy = fieldsTerminatedBy;
+ }
+
+ public String getFieldsTerminatedBy() {
+ return fieldsTerminatedBy;
+ }
+
+ public Set<UUID> getTaxonNodeUuids() {
+ return taxonNodeUuids;
+ }
+
+ public void setTaxonNodeUuids(Set<UUID> taxonNodeUuids) {
+ this.taxonNodeUuids = taxonNodeUuids;
+ }
+
+ public ByteArrayOutputStream getByteArrayOutputStream() {
+ return baos;
+ }
+
+ public void setByteArrayOutputStream(ByteArrayOutputStream baos) {
+ this.baos = baos;
+ }
+
+ public void setFeatures(List<Feature> features) {
+ this.features = features;
+
+ }
+
+ public List<Feature> getFeatures() {
+ return features;
+
+ }
+
+ public void setClassificationTitleCache(String classificationTitleCache) {
+ this.classificationTitleCache = classificationTitleCache;
+ }
+
+ public String getClassificationTitleCache() {
+ return classificationTitleCache;
+ }
+
+ /**
+ * @param areas
+ */
+ public void setNamedAreas(List<NamedArea> areas) {
+ // TODO Auto-generated method stub
+ this.areas = areas;
+
+ }
+ public List<NamedArea> getNamedAreas(){
+ return areas;
+ }
+
}
\ No newline at end of file
-/**\r
-* Copyright (C) 2007 EDIT\r
-* European Distributed Institute of Taxonomy \r
-* http://www.e-taxonomy.eu\r
-* \r
-* The contents of this file are subject to the Mozilla Public License Version 1.1\r
-* See LICENSE.TXT at the top of this package for the full license terms.\r
-*/\r
-\r
-package eu.etaxonomy.cdm.io.csv.redlist.out;\r
-\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.PrintWriter;\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.Comparator;\r
-import java.util.HashSet;\r
-import java.util.List;\r
-import java.util.Set;\r
-import java.util.UUID;\r
-\r
-import org.apache.log4j.Logger;\r
-import org.springframework.stereotype.Component;\r
-import org.springframework.transaction.TransactionStatus;\r
-\r
-import eu.etaxonomy.cdm.model.common.CdmBase;\r
-import eu.etaxonomy.cdm.model.common.Language;\r
-import eu.etaxonomy.cdm.model.common.RelationshipTermBase;\r
-import eu.etaxonomy.cdm.model.description.CategoricalData;\r
-import eu.etaxonomy.cdm.model.description.DescriptionElementBase;\r
-import eu.etaxonomy.cdm.model.description.Distribution;\r
-import eu.etaxonomy.cdm.model.description.Feature;\r
-import eu.etaxonomy.cdm.model.description.State;\r
-import eu.etaxonomy.cdm.model.description.TaxonDescription;\r
-import eu.etaxonomy.cdm.model.description.TextData;\r
-import eu.etaxonomy.cdm.model.location.NamedArea;\r
-import eu.etaxonomy.cdm.model.name.NonViralName;\r
-import eu.etaxonomy.cdm.model.taxon.Classification;\r
-import eu.etaxonomy.cdm.model.taxon.Synonym;\r
-import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;\r
-import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;\r
-import eu.etaxonomy.cdm.model.taxon.Taxon;\r
-import eu.etaxonomy.cdm.model.taxon.TaxonBase;\r
-import eu.etaxonomy.cdm.model.taxon.TaxonNode;\r
-import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;\r
-import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;\r
-\r
-\r
-/**\r
- * @author a.oppermann\r
- * @created 18.10.2012\r
- */\r
-\r
-@Component\r
-public class CsvTaxExportRedlist extends CsvExportBaseRedlist {\r
- private static final Logger logger = Logger.getLogger(CsvTaxExportRedlist.class);\r
-\r
- private static final String ROW_TYPE = "http://rs.tdwg.org/dwc/terms/Taxon";\r
- private static final String fileName = "RedlistCoreTax.csv";\r
-\r
- public CsvTaxExportRedlist() {\r
- super();\r
- this.ioName = this.getClass().getSimpleName();\r
- }\r
- \r
-\r
- /** Retrieves data from a CDM DB and serializes them CDM to CSV.\r
- * Starts with root taxa and traverses the classification to retrieve \r
- * children taxa, synonyms, relationships, descriptive data, red list \r
- * status (features). \r
- * Taxa that are not part of the classification are not found.\r
- * \r
- * @param exImpConfig\r
- * @param dbname\r
- * @param filename\r
- */\r
- @Override\r
- protected void doInvoke(CsvTaxExportStateRedlist state){\r
- CsvTaxExportConfiguratorRedlist config = state.getConfig();\r
- TransactionStatus txStatus = startTransaction(true);\r
- List<NamedArea> selectedAreas = config.getNamedAreas();\r
- Set<TaxonNode> taxonNodes = assembleTaxonNodeSet(config);\r
- \r
- PrintWriter writer = null;\r
- ByteArrayOutputStream byteArrayOutputStream;\r
- try {\r
- byteArrayOutputStream = config.getByteArrayOutputStream();\r
- writer = new PrintWriter(byteArrayOutputStream); \r
- //geographical Filter\r
- List<TaxonNode> filteredNodes = handleGeographicalFilter(selectedAreas, taxonNodes);\r
- \r
- //sorting List\r
- Collections.sort(filteredNodes, new Comparator<TaxonNode>() {\r
-\r
- @Override\r
- public int compare(TaxonNode tn1, TaxonNode tn2) {\r
- Taxon taxon1 = tn1.getTaxon();\r
- Taxon taxon2 = tn2.getTaxon();\r
- if(taxon1 != null && taxon2 != null){\r
- return taxon1.getTitleCache().compareTo(taxon2.getTitleCache());\r
- }\r
- else{\r
- return 0;\r
- }\r
- }\r
- });\r
- for (TaxonNode node : filteredNodes){\r
- Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);\r
- CsvTaxRecordRedlist record = assembleRecord(state);\r
- NonViralName<?> name = CdmBase.deproxy(taxon.getName(), NonViralName.class);\r
- Classification classification = node.getClassification();\r
- config.setClassificationTitleCache(classification.getTitleCache()); \r
- if (! this.recordExists(taxon)){\r
- handleTaxonBase(record, taxon, name, taxon, classification, null, false, false, config);\r
- record.write(writer);\r
- this.addExistingRecord(taxon);\r
- }\r
- //misapplied names\r
- handleMisapplication(taxon, writer, classification, record, config);\r
- writer.flush();\r
- }\r
- } catch (ClassCastException e) {\r
- e.printStackTrace();\r
- }\r
- finally{\r
- writer.close();\r
- this.clearExistingRecordIds();\r
- }\r
- commitTransaction(txStatus);\r
- return;\r
-\r
- }\r
- \r
- \r
- //TODO: Exception handling\r
- protected Set<TaxonNode> assembleTaxonNodeSet(CsvTaxExportConfiguratorRedlist config){\r
- Set<TaxonNode> taxonNodes = new HashSet<>();\r
- if(config != null){\r
- Set<UUID> taxonNodeUuidSet = config.getTaxonNodeUuids();\r
- taxonNodes.addAll(getTaxonNodeService().find(taxonNodeUuidSet));\r
- }\r
- return taxonNodes;\r
- }\r
-\r
- //TODO: Exception handling\r
- private CsvTaxRecordRedlist assembleRecord(CsvTaxExportStateRedlist state) {\r
- if(state!=null){\r
- CsvTaxExportConfiguratorRedlist config = state.getConfig();\r
- CsvMetaDataRecordRedlist metaRecord = new CsvMetaDataRecordRedlist(true, fileName, ROW_TYPE);\r
- state.addMetaRecord(metaRecord);\r
- CsvTaxRecordRedlist record = new CsvTaxRecordRedlist(metaRecord, config);\r
- return record;\r
- }\r
- return null;\r
- }\r
-\r
- /**\r
- * Takes positive List of areas and iterates over a given taxon node \r
- * and their {@link Taxon} to return all {@link Taxon} with the desired \r
- * geographical attribute.\r
- * \r
- * <p><p>\r
- *\r
- * If selectedAreas is null all child {@link TaxonNode}s of the given taxon node will be returned.\r
- * \r
- * @param selectedAreas \r
- * @param taxonNodes\r
- * @return\r
- */\r
- protected List<TaxonNode> handleGeographicalFilter(List<NamedArea> selectedAreas,\r
- Set<TaxonNode> taxonNodes) {\r
- List<TaxonNode> filteredNodes = new ArrayList<TaxonNode>();\r
- List<TaxonNode> allNodes = new ArrayList(getAllNodes(taxonNodes));\r
- //Geographical filter\r
- logger.info(selectedAreas.size());\r
- if(selectedAreas != null && !selectedAreas.isEmpty() && selectedAreas.size() < 16){\r
-// if(selectedAreas.size() == 16){\r
-// //Germany TDWG Level 3\r
-// String germany="uu7b7c2db5-aa44-4302-bdec-6556fd74b0b9id";\r
-// selectedAreas.add((NamedArea) getTermService().find(UUID.fromString(germany)));\r
-// }\r
- for (TaxonNode node : allNodes){\r
- Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);\r
- Set<TaxonDescription> descriptions = taxon.getDescriptions();\r
- for (TaxonDescription description : descriptions){\r
- for (DescriptionElementBase el : description.getElements()){\r
- if (el.isInstanceOf(Distribution.class) ){\r
- Distribution distribution = CdmBase.deproxy(el, Distribution.class);\r
- NamedArea area = distribution.getArea();\r
- for(NamedArea selectedArea:selectedAreas){\r
- if(selectedArea.getUuid().equals(area.getUuid())){\r
- filteredNodes.add(node);\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }\r
- }else{\r
- filteredNodes = allNodes;\r
- }\r
- return filteredNodes;\r
- }\r
-\r
- /**\r
- * handles misapplied {@link Taxon}\r
- * @param taxon\r
- * @param writer\r
- * @param classification\r
- * @param metaRecord\r
- * @param config\r
- */\r
- private void handleMisapplication(Taxon taxon, PrintWriter writer, Classification classification, CsvTaxRecordRedlist record, CsvTaxExportConfiguratorRedlist config) {\r
- Set<Taxon> misappliedNames = taxon.getMisappliedNames();\r
- for (Taxon misappliedName : misappliedNames ){\r
-// CsvTaxRecordRedlist record = new CsvTaxRecordRedlist(metaRecord, config);\r
- TaxonRelationshipType relType = TaxonRelationshipType.MISAPPLIED_NAME_FOR();\r
- NonViralName<?> name = CdmBase.deproxy(misappliedName.getName(), NonViralName.class);\r
- \r
- if (! this.recordExists(misappliedName)){\r
- handleTaxonBase(record, misappliedName, name, taxon, classification, relType, false, false, config);\r
- record.write(writer);\r
- this.addExistingRecord(misappliedName);\r
- }\r
- } \r
- }\r
-\r
- /**\r
- * handles the information record for the actual {@link Taxon} including {@link Classification classification}, Taxon Name, Taxon ID,\r
- * Taxon Status, Synonyms, {@link Feature features} data \r
- * @param record the concrete information record\r
- * @param taxonBase {@link Taxon}\r
- * @param name\r
- * @param acceptedTaxon\r
- * @param parent\r
- * @param basionym\r
- * @param isPartial \r
- * @param isProParte \r
- * @param config \r
- * @param type\r
- */\r
- private void handleTaxonBase(CsvTaxRecordRedlist record,TaxonBase<?> taxonBase,\r
- NonViralName<?> name, Taxon acceptedTaxon, Classification classification, \r
- RelationshipTermBase<?> relType, boolean isProParte, boolean isPartial, \r
- CsvTaxExportConfiguratorRedlist config) {\r
- \r
- List<Feature> features = config.getFeatures();\r
- record.setHeadLinePrinted(config.isHasHeaderLines());\r
- if(features != null)record.setPrintFeatures(features);\r
- config.setHasHeaderLines(false);\r
-\r
- record.setDatasetName(classification.getTitleCache());\r
- record.setScientificName(name.getTitleCache());\r
- record.setScientificNameId(name.getUuid().toString());\r
- handleTaxonomicStatus(record, name, relType, isProParte, isPartial);\r
- //synonyms\r
- handleSynonyms(record,(Taxon) taxonBase);\r
- //distribution\r
- handleDiscriptionData(record, (Taxon) taxonBase);\r
- if(features!= null) {\r
- \r
- List<List<String>> featureCells = new ArrayList<List<String>>(features.size());\r
- for(int i = 0; i < features.size(); i++) {\r
- featureCells.add(new ArrayList<String>());\r
- }\r
- handleRelatedRedlistStatus(record, (Taxon)taxonBase, false, featureCells, features);\r
- handleRelatedRedlistStatus(record, (Taxon)taxonBase, true, featureCells, features);\r
-\r
- }\r
- return;\r
- }\r
-\r
- private void handleTaxonomicStatus(\r
- CsvTaxRecordRedlist record,\r
- NonViralName<?> name, \r
- RelationshipTermBase<?> type,\r
- boolean isProParte,\r
- boolean isPartial) {\r
- if (type == null){\r
- record.setTaxonomicStatus(name.getNomenclaturalCode().acceptedTaxonStatusLabel());\r
- }else{\r
- String status = name.getNomenclaturalCode().synonymStatusLabel();\r
- if (type.equals(SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF())){\r
- status = "heterotypicSynonym";\r
- }else if(type.equals(SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF())){\r
- status = "homotypicSynonym";\r
- }else if(type.equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){\r
- status = "misapplied";\r
- }\r
- if (isProParte){\r
- status = "proParteSynonym";\r
- }else if (isPartial){\r
- String message = "Partial synonym is not part of the gbif toxonomic status vocabulary";\r
- logger.warn(message);\r
- status = "partialSynonym";\r
- }\r
- \r
- record.setTaxonomicStatus(status);\r
- }\r
- }\r
-\r
- private void handleSynonyms(CsvTaxRecordRedlist record, Taxon taxon) {\r
- \r
- Set<SynonymRelationship> synRels = taxon.getSynonymRelations();\r
- ArrayList<String> synonyms = new ArrayList<String>(); \r
- for (SynonymRelationship synRel :synRels ){\r
- Synonym synonym = synRel.getSynonym();\r
- SynonymRelationshipType type = synRel.getType();\r
- if (type == null){ // should not happen\r
- type = SynonymRelationshipType.SYNONYM_OF();\r
- }\r
- NonViralName<?> name = CdmBase.deproxy(synonym.getName(), NonViralName.class);\r
- synonyms.add(name.getTitleCache());\r
- }\r
- record.setSynonyms(synonyms);\r
- }\r
-\r
- private void handleDiscriptionData(CsvTaxRecordRedlist record, Taxon taxon) {\r
- \r
- Set<TaxonDescription> descriptions = taxon.getDescriptions();\r
- ArrayList<String> distributions = new ArrayList<String>();\r
- for (TaxonDescription description : descriptions){\r
- for (DescriptionElementBase el : description.getElements()){\r
- if (el.isInstanceOf(Distribution.class) ){\r
- Distribution distribution = CdmBase.deproxy(el, Distribution.class);\r
- NamedArea area = distribution.getArea();\r
- distributions.add(area.getTitleCache());\r
- }\r
-\r
- }\r
- }\r
- record.setCountryCode(distributions);\r
- }\r
-\r
- private void handleRedlistStatus(CsvTaxRecordRedlist record, Taxon taxon, List<List<String>> featureCells, List<Feature> features){\r
- Set<TaxonDescription> descriptions = taxon.getDescriptions();\r
-\r
- for (TaxonDescription description : descriptions){\r
- for (DescriptionElementBase el : description.getElements()){\r
- if(el.isInstanceOf(CategoricalData.class)){\r
- CategoricalData categoricalData = CdmBase.deproxy(el, CategoricalData.class);\r
- for(State state:categoricalData.getStatesOnly()){\r
- Feature stateFeature = categoricalData.getFeature();\r
- // find matching feature and put data into according cell\r
- for(int i = 0; i < features.size(); i++) {\r
- if(features.get(i).equals(stateFeature)){\r
- List<String> cell = featureCells.get(i);\r
- cell.add(state.toString());\r
- }\r
- }\r
- }\r
- }else if(el.isInstanceOf(TextData.class)){\r
- TextData textData = CdmBase.deproxy(el, TextData.class);\r
- Feature textFeature = textData.getFeature();\r
- // find matching feature and put data into according cell\r
- for(int i = 0; i < features.size(); i++) {\r
- if(features.get(i).equals(textFeature)){\r
- List<String> cell = featureCells.get(i);\r
- String text = textData.getText(Language.GERMAN());\r
- text = text.replaceAll(System.getProperty("line.separator"), "");\r
- text = text.replaceAll(" ", " ");\r
- cell.add(text);\r
- \r
- }\r
- }\r
- }\r
- }\r
- }\r
- record.setFeatures(featureCells);\r
- }\r
-\r
-\r
- private void handleRelatedRedlistStatus(CsvTaxRecordRedlist record, Taxon taxon, boolean relationFrom, List<List<String>> featureCells, List<Feature> features) {\r
-\r
- if (relationFrom)handleRedlistStatus(record, taxon, featureCells, features);\r
- \r
- \r
- Set<TaxonRelationship> taxRels;\r
- if(relationFrom){\r
- taxRels = taxon.getRelationsFromThisTaxon();\r
- }else{\r
- taxRels = taxon.getRelationsToThisTaxon();\r
- }\r
- for (TaxonRelationship taxRel:taxRels){\r
- if(taxRel.getType().equals(TaxonRelationshipType.CONGRUENT_TO())){\r
- Taxon relatedTaxon;\r
- if(relationFrom){\r
- relatedTaxon = taxRel.getToTaxon();\r
- }else{\r
- relatedTaxon = taxRel.getFromTaxon();\r
- }\r
- handleRedlistStatus(record, relatedTaxon, featureCells, features);\r
- }\r
- }\r
- }\r
-\r
- @Override\r
- protected boolean doCheck(CsvTaxExportStateRedlist state) {\r
- boolean result = true;\r
- logger.warn("No check implemented for " + this.ioName);\r
- return result;\r
- }\r
-\r
- @Override\r
- protected boolean isIgnore(CsvTaxExportStateRedlist state) {\r
- return ! state.getConfig().isDoTaxa();\r
- }\r
- \r
-}\r
+/**
+* Copyright (C) 2007 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+
+package eu.etaxonomy.cdm.io.csv.redlist.out;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.UUID;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+import org.springframework.transaction.TransactionStatus;
+
+import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.common.Language;
+import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
+import eu.etaxonomy.cdm.model.description.CategoricalData;
+import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
+import eu.etaxonomy.cdm.model.description.Distribution;
+import eu.etaxonomy.cdm.model.description.Feature;
+import eu.etaxonomy.cdm.model.description.State;
+import eu.etaxonomy.cdm.model.description.TaxonDescription;
+import eu.etaxonomy.cdm.model.description.TextData;
+import eu.etaxonomy.cdm.model.location.NamedArea;
+import eu.etaxonomy.cdm.model.name.NonViralName;
+import eu.etaxonomy.cdm.model.taxon.Classification;
+import eu.etaxonomy.cdm.model.taxon.Synonym;
+import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
+import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
+import eu.etaxonomy.cdm.model.taxon.TaxonBase;
+import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
+import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
+
+
+/**
+ * @author a.oppermann
+ * @created 18.10.2012
+ */
+
+@Component
+public class CsvTaxExportRedlist extends CsvExportBaseRedlist {
+ private static final Logger logger = Logger.getLogger(CsvTaxExportRedlist.class);
+
+ private static final String ROW_TYPE = "http://rs.tdwg.org/dwc/terms/Taxon";
+ private static final String fileName = "RedlistCoreTax.csv";
+
+ public CsvTaxExportRedlist() {
+ super();
+ this.ioName = this.getClass().getSimpleName();
+ }
+
+
+ /** Retrieves data from a CDM DB and serializes them CDM to CSV.
+ * Starts with root taxa and traverses the classification to retrieve
+ * children taxa, synonyms, relationships, descriptive data, red list
+ * status (features).
+ * Taxa that are not part of the classification are not found.
+ *
+ * @param exImpConfig
+ * @param dbname
+ * @param filename
+ */
+ @Override
+ protected void doInvoke(CsvTaxExportStateRedlist state){
+ CsvTaxExportConfiguratorRedlist config = state.getConfig();
+ TransactionStatus txStatus = startTransaction(true);
+ List<NamedArea> selectedAreas = config.getNamedAreas();
+ Set<TaxonNode> taxonNodes = assembleTaxonNodeSet(config);
+
+ PrintWriter writer = null;
+ ByteArrayOutputStream byteArrayOutputStream;
+ try {
+ byteArrayOutputStream = config.getByteArrayOutputStream();
+ writer = new PrintWriter(byteArrayOutputStream);
+ //geographical Filter
+ List<TaxonNode> filteredNodes = handleGeographicalFilter(selectedAreas, taxonNodes);
+
+ //sorting List
+ Collections.sort(filteredNodes, new Comparator<TaxonNode>() {
+
+ @Override
+ public int compare(TaxonNode tn1, TaxonNode tn2) {
+ Taxon taxon1 = tn1.getTaxon();
+ Taxon taxon2 = tn2.getTaxon();
+ if(taxon1 != null && taxon2 != null){
+ return taxon1.getTitleCache().compareTo(taxon2.getTitleCache());
+ }
+ else{
+ return 0;
+ }
+ }
+ });
+ for (TaxonNode node : filteredNodes){
+ Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
+ CsvTaxRecordRedlist record = assembleRecord(state);
+ NonViralName<?> name = CdmBase.deproxy(taxon.getName(), NonViralName.class);
+ Classification classification = node.getClassification();
+ config.setClassificationTitleCache(classification.getTitleCache());
+ if (! this.recordExists(taxon)){
+ handleTaxonBase(record, taxon, name, taxon, classification, null, false, false, config);
+ record.write(writer);
+ this.addExistingRecord(taxon);
+ }
+ //misapplied names
+ handleMisapplication(taxon, writer, classification, record, config);
+ writer.flush();
+ }
+ } catch (ClassCastException e) {
+ e.printStackTrace();
+ }
+ finally{
+ if(writer!=null){
+ writer.close();
+ }
+ this.clearExistingRecordIds();
+ }
+ commitTransaction(txStatus);
+ return;
+
+ }
+
+
+ //TODO: Exception handling
+ protected Set<TaxonNode> assembleTaxonNodeSet(CsvTaxExportConfiguratorRedlist config){
+ Set<TaxonNode> taxonNodes = new HashSet<>();
+ if(config != null){
+ Set<UUID> taxonNodeUuidSet = config.getTaxonNodeUuids();
+ taxonNodes.addAll(getTaxonNodeService().find(taxonNodeUuidSet));
+ }
+ return taxonNodes;
+ }
+
+ //TODO: Exception handling
+ private CsvTaxRecordRedlist assembleRecord(CsvTaxExportStateRedlist state) {
+ if(state!=null){
+ CsvTaxExportConfiguratorRedlist config = state.getConfig();
+ CsvMetaDataRecordRedlist metaRecord = new CsvMetaDataRecordRedlist(true, fileName, ROW_TYPE);
+ state.addMetaRecord(metaRecord);
+ CsvTaxRecordRedlist record = new CsvTaxRecordRedlist(metaRecord, config);
+ return record;
+ }
+ return null;
+ }
+
+ /**
+ * Takes positive List of areas and iterates over a given taxon node
+ * and their {@link Taxon} to return all {@link Taxon} with the desired
+ * geographical attribute.
+ *
+ * <p><p>
+ *
+ * If selectedAreas is null all child {@link TaxonNode}s of the given taxon node will be returned.
+ *
+ * @param selectedAreas
+ * @param taxonNodes
+ * @return
+ */
+ protected List<TaxonNode> handleGeographicalFilter(List<NamedArea> selectedAreas,
+ Set<TaxonNode> taxonNodes) {
+ List<TaxonNode> filteredNodes = new ArrayList<TaxonNode>();
+ List<TaxonNode> allNodes = new ArrayList<TaxonNode>();
+ for (TaxonNode node : taxonNodes){
+ allNodes.addAll(getTaxonNodeService().loadChildNodesOfTaxonNode(node, null, true, null));
+ }
+ //Geographical filter
+ if(selectedAreas != null && !selectedAreas.isEmpty() && selectedAreas.size() < 16){
+ for (TaxonNode node : allNodes){
+ Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
+ Set<TaxonDescription> descriptions = taxon.getDescriptions();
+ for (TaxonDescription description : descriptions){
+ for (DescriptionElementBase el : description.getElements()){
+ if (el.isInstanceOf(Distribution.class) ){
+ Distribution distribution = CdmBase.deproxy(el, Distribution.class);
+ NamedArea area = distribution.getArea();
+ for(NamedArea selectedArea:selectedAreas){
+ if(selectedArea.getUuid().equals(area.getUuid())){
+ filteredNodes.add(node);
+ }
+ }
+ }
+ }
+ }
+ }
+ }else{
+ filteredNodes = allNodes;
+ }
+ return filteredNodes;
+ }
+
+ /**
+ * handles misapplied {@link Taxon}
+ * @param taxon
+ * @param writer
+ * @param classification
+ * @param metaRecord
+ * @param config
+ */
+ private void handleMisapplication(Taxon taxon, PrintWriter writer, Classification classification, CsvTaxRecordRedlist record, CsvTaxExportConfiguratorRedlist config) {
+ Set<Taxon> misappliedNames = taxon.getMisappliedNames();
+ for (Taxon misappliedName : misappliedNames ){
+// CsvTaxRecordRedlist record = new CsvTaxRecordRedlist(metaRecord, config);
+ TaxonRelationshipType relType = TaxonRelationshipType.MISAPPLIED_NAME_FOR();
+ NonViralName<?> name = CdmBase.deproxy(misappliedName.getName(), NonViralName.class);
+
+ if (! this.recordExists(misappliedName)){
+ handleTaxonBase(record, misappliedName, name, taxon, classification, relType, false, false, config);
+ record.write(writer);
+ this.addExistingRecord(misappliedName);
+ }
+ }
+ }
+
+ /**
+ * handles the information record for the actual {@link Taxon} including {@link Classification classification}, Taxon Name, Taxon ID,
+ * Taxon Status, Synonyms, {@link Feature features} data
+ * @param record the concrete information record
+ * @param taxonBase {@link Taxon}
+ * @param name
+ * @param acceptedTaxon
+ * @param parent
+ * @param basionym
+ * @param isPartial
+ * @param isProParte
+ * @param config
+ * @param type
+ */
+ private void handleTaxonBase(CsvTaxRecordRedlist record,TaxonBase<?> taxonBase,
+ NonViralName<?> name, Taxon acceptedTaxon, Classification classification,
+ RelationshipTermBase<?> relType, boolean isProParte, boolean isPartial,
+ CsvTaxExportConfiguratorRedlist config) {
+
+ List<Feature> features = config.getFeatures();
+ record.setHeadLinePrinted(config.isHasHeaderLines());
+ if(features != null) {
+ record.setPrintFeatures(features);
+ }
+ config.setHasHeaderLines(false);
+
+ record.setDatasetName(classification.getTitleCache());
+ record.setScientificName(name.getTitleCache());
+ record.setScientificNameId(name.getUuid().toString());
+ handleTaxonomicStatus(record, name, relType, isProParte, isPartial);
+ //synonyms
+ handleSynonyms(record,(Taxon) taxonBase);
+ //distribution
+ handleDiscriptionData(record, (Taxon) taxonBase);
+ if(features!= null) {
+
+ List<List<String>> featureCells = new ArrayList<List<String>>(features.size());
+ for(int i = 0; i < features.size(); i++) {
+ featureCells.add(new ArrayList<String>());
+ }
+ handleRelatedRedlistStatus(record, (Taxon)taxonBase, false, featureCells, features);
+ handleRelatedRedlistStatus(record, (Taxon)taxonBase, true, featureCells, features);
+
+ }
+ return;
+ }
+
+ private void handleTaxonomicStatus(
+ CsvTaxRecordRedlist record,
+ NonViralName<?> name,
+ RelationshipTermBase<?> type,
+ boolean isProParte,
+ boolean isPartial) {
+ if (type == null){
+ record.setTaxonomicStatus(name.getNomenclaturalCode().acceptedTaxonStatusLabel());
+ }else{
+ String status = name.getNomenclaturalCode().synonymStatusLabel();
+ if (type.equals(SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF())){
+ status = "heterotypicSynonym";
+ }else if(type.equals(SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF())){
+ status = "homotypicSynonym";
+ }else if(type.equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
+ status = "misapplied";
+ }
+ if (isProParte){
+ status = "proParteSynonym";
+ }else if (isPartial){
+ String message = "Partial synonym is not part of the gbif toxonomic status vocabulary";
+ logger.warn(message);
+ status = "partialSynonym";
+ }
+
+ record.setTaxonomicStatus(status);
+ }
+ }
+
+ private void handleSynonyms(CsvTaxRecordRedlist record, Taxon taxon) {
+
+ Set<SynonymRelationship> synRels = taxon.getSynonymRelations();
+ ArrayList<String> synonyms = new ArrayList<String>();
+ for (SynonymRelationship synRel :synRels ){
+ Synonym synonym = synRel.getSynonym();
+ SynonymRelationshipType type = synRel.getType();
+ if (type == null){ // should not happen
+ type = SynonymRelationshipType.SYNONYM_OF();
+ }
+ NonViralName<?> name = CdmBase.deproxy(synonym.getName(), NonViralName.class);
+ synonyms.add(name.getTitleCache());
+ }
+ record.setSynonyms(synonyms);
+ }
+
+ private void handleDiscriptionData(CsvTaxRecordRedlist record, Taxon taxon) {
+
+ Set<TaxonDescription> descriptions = taxon.getDescriptions();
+ ArrayList<String> distributions = new ArrayList<String>();
+ for (TaxonDescription description : descriptions){
+ for (DescriptionElementBase el : description.getElements()){
+ if (el.isInstanceOf(Distribution.class) ){
+ Distribution distribution = CdmBase.deproxy(el, Distribution.class);
+ NamedArea area = distribution.getArea();
+ distributions.add(area.getTitleCache());
+ }
+
+ }
+ }
+ record.setCountryCode(distributions);
+ }
+
+ private void handleRedlistStatus(CsvTaxRecordRedlist record, Taxon taxon, List<List<String>> featureCells, List<Feature> features){
+ Set<TaxonDescription> descriptions = taxon.getDescriptions();
+
+ for (TaxonDescription description : descriptions){
+ for (DescriptionElementBase el : description.getElements()){
+ if(el.isInstanceOf(CategoricalData.class)){
+ CategoricalData categoricalData = CdmBase.deproxy(el, CategoricalData.class);
+ for(State state:categoricalData.getStatesOnly()){
+ Feature stateFeature = categoricalData.getFeature();
+ // find matching feature and put data into according cell
+ for(int i = 0; i < features.size(); i++) {
+ if(features.get(i).equals(stateFeature)){
+ List<String> cell = featureCells.get(i);
+ cell.add(state.toString());
+ }
+ }
+ }
+ }else if(el.isInstanceOf(TextData.class)){
+ TextData textData = CdmBase.deproxy(el, TextData.class);
+ Feature textFeature = textData.getFeature();
+ // find matching feature and put data into according cell
+ for(int i = 0; i < features.size(); i++) {
+ if(features.get(i).equals(textFeature)){
+ List<String> cell = featureCells.get(i);
+ String text = textData.getText(Language.GERMAN());
+ text = text.replaceAll(System.getProperty("line.separator"), "");
+ text = text.replaceAll(" ", " ");
+ cell.add(text);
+
+ }
+ }
+ }
+ }
+ }
+ record.setFeatures(featureCells);
+ }
+
+
+ private void handleRelatedRedlistStatus(CsvTaxRecordRedlist record, Taxon taxon, boolean relationFrom, List<List<String>> featureCells, List<Feature> features) {
+
+ if (relationFrom) {
+ handleRedlistStatus(record, taxon, featureCells, features);
+ }
+
+
+ Set<TaxonRelationship> taxRels;
+ if(relationFrom){
+ taxRels = taxon.getRelationsFromThisTaxon();
+ }else{
+ taxRels = taxon.getRelationsToThisTaxon();
+ }
+ for (TaxonRelationship taxRel:taxRels){
+ if(taxRel.getType().equals(TaxonRelationshipType.CONGRUENT_TO())){
+ Taxon relatedTaxon;
+ if(relationFrom){
+ relatedTaxon = taxRel.getToTaxon();
+ }else{
+ relatedTaxon = taxRel.getFromTaxon();
+ }
+ handleRedlistStatus(record, relatedTaxon, featureCells, features);
+ }
+ }
+ }
+
+ @Override
+ protected boolean doCheck(CsvTaxExportStateRedlist state) {
+ boolean result = true;
+ logger.warn("No check implemented for " + this.ioName);
+ return result;
+ }
+
+ @Override
+ protected boolean isIgnore(CsvTaxExportStateRedlist state) {
+ return ! state.getConfig().isDoTaxa();
+ }
+
+}