Project

General

Profile

« Previous | Next » 

Revision c7fd5f24

Added by Patrick Plitzner almost 8 years ago

#5890 Use taxon nodes instead of classification for CSV export

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/csv/redlist/out/CsvExportBaseRedlist.java
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
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.
8
*/
9
package eu.etaxonomy.cdm.io.csv.redlist.out;
10

  
11
import java.io.File;
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.HashSet;
20
import java.util.List;
21
import java.util.Set;
22
import java.util.UUID;
23

  
24
import javax.xml.stream.XMLOutputFactory;
25
import javax.xml.stream.XMLStreamException;
26
import javax.xml.stream.XMLStreamWriter;
27

  
28
import org.apache.commons.lang.StringUtils;
29
import org.apache.log4j.Logger;
30

  
31
import eu.etaxonomy.cdm.common.CdmUtils;
32
import eu.etaxonomy.cdm.io.common.CdmExportBase;
33
import eu.etaxonomy.cdm.io.common.ICdmExport;
34
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
35
import eu.etaxonomy.cdm.model.common.CdmBase;
36
import eu.etaxonomy.cdm.model.common.IOriginalSource;
37
import eu.etaxonomy.cdm.model.common.ISourceable;
38
import eu.etaxonomy.cdm.model.location.Country;
39
import eu.etaxonomy.cdm.model.location.NamedArea;
40
import eu.etaxonomy.cdm.model.taxon.Taxon;
41
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
43

  
44
/**
45
 * @author a.mueller, a.oppermann
46
 * @date 18.10.2012
47
 *
48
 */
49
public abstract class CsvExportBaseRedlist extends CdmExportBase<CsvTaxExportConfiguratorRedlist, CsvTaxExportStateRedlist, IExportTransformer> implements ICdmExport<CsvTaxExportConfiguratorRedlist, CsvTaxExportStateRedlist>{
50
	private static final Logger logger = Logger.getLogger(CsvExportBaseRedlist.class);
51
	
52
	protected static final boolean IS_CORE = true;
53
	
54
	
55
	protected Set<Integer> existingRecordIds = new HashSet<Integer>();
56
	protected Set<UUID> existingRecordUuids = new HashSet<UUID>();
57
	
58
	
59

  
60
	/* (non-Javadoc)
61
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#countSteps()
62
	 */
63
	@Override
64
	public int countSteps() {
65
		List<TaxonNode> allNodes =  getClassificationService().getAllNodes();
66
		return allNodes.size();
67
	}
68

  
69
	
70
	
71
	/**
72
	 * Returns all children of the given taxon node and the node itself if the node has
73
	 *  a {@link Taxon} attached (empty taxon nodes should not but do exist in CDM databases).
74
	 * @return
75
	 */
76
	protected Set<TaxonNode> getAllNodes(Set<TaxonNode> taxonNodes) {
77
		//handle empty list as no filter defined
78
		if (taxonNodes != null && taxonNodes.isEmpty()){
79
			taxonNodes = null;
80
		}
81
		
82
		List<TaxonNode> allNodes =  getClassificationService().getAllNodes();
83
		Set<TaxonNode> result = new HashSet<TaxonNode>();
84
		for (TaxonNode node : allNodes){
85
			if (node.getClassification() == null ){
86
				continue;
87
			}else if (taxonNodes != null && ! taxonNodes.contains(node.getClassification())){
88
				continue;
89
			}else{
90
				Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
91
				if (taxon == null){
92
					String message = "There is a taxon node without taxon: " + node.getId();
93
					logger.warn(message);
94
					continue;
95
				}
96
				result.add(node);
97
			}
98
		}
99
		return result;
100
	}
101
	
102
	
103
	/**
104
	 * Creates the locationId, locality, countryCode triple
105
	 * @param record
106
	 * @param area
107
	 */
108
	protected void handleArea(ICsvAreaRecord record, NamedArea area, TaxonBase<?> taxon, boolean required) {
109
		if (area != null){
110
			record.setLocationId(area);
111
			record.setLocality(area.getLabel());
112
			if (area.isInstanceOf(Country.class)){
113
				Country country = CdmBase.deproxy(area, Country.class);
114
				record.setCountryCode(country.getIso3166_A2());
115
			}
116
		}else{
117
			if (required){
118
				String message = "Description requires area but area does not exist for taxon " + getTaxonLogString(taxon);
119
				logger.warn(message);
120
			}
121
		}
122
	}
123

  
124

  
125
	protected String getTaxonLogString(TaxonBase<?> taxon) {
126
		return taxon.getTitleCache() + "(" + taxon.getId() + ")";
127
	}
128
	
129

  
130
	/**
131
	 * @param el
132
	 * @return
133
	 */
134
	protected boolean recordExists(CdmBase el) {
135
		return existingRecordIds.contains(el.getId());
136
	}
137
	
138

  
139
	/**
140
	 * @param sec
141
	 */
142
	protected void addExistingRecord(CdmBase cdmBase) {
143
		existingRecordIds.add(cdmBase.getId());
144
	}
145
	
146
	/**
147
	 * @param el
148
	 * @return
149
	 */
150
	protected boolean recordExistsUuid(CdmBase el) {
151
		return existingRecordUuids.contains(el.getUuid());
152
	}
153
	
154
	/**
155
	 * @param sec
156
	 */
157
	protected void addExistingRecordUuid(CdmBase cdmBase) {
158
		existingRecordUuids.add(cdmBase.getUuid());
159
	}
160
	
161

  
162
	protected String getSources(ISourceable<?> sourceable, CsvTaxExportConfiguratorRedlist config) {
163
		String result = "";
164
		for (IOriginalSource source: sourceable.getSources()){
165
			if (StringUtils.isBlank(source.getIdInSource())){//idInSource indicates that this source is only data provenance, may be changed in future
166
				if (source.getCitation() != null){
167
					String ref = source.getCitation().getTitleCache();
168
					result = CdmUtils.concat(config.getSetSeparator(), result, ref);
169
				}
170
			}
171
		}
172
		return result;
173
	}
174
	
175

  
176
	/**
177
	 * @param config
178
	 * @return
179
	 * @throws IOException
180
	 * @throws FileNotFoundException
181
	 */
182
	protected FileOutputStream createFileOutputStream(CsvTaxExportConfiguratorRedlist config, String thisFileName) throws IOException, FileNotFoundException {
183
		String filePath = config.getDestinationNameString();
184
		String fileName = filePath + File.separatorChar + thisFileName;
185
		File f = new File(fileName);
186
		if (!f.exists()){
187
			f.createNewFile();
188
		}
189
		FileOutputStream fos = new FileOutputStream(f);
190
		return fos;
191
	}
192
	
193

  
194
	/**
195
	 * @param config
196
	 * @param factory
197
	 * @return
198
	 * @throws IOException
199
	 * @throws FileNotFoundException
200
	 * @throws XMLStreamException
201
	 */
202
	protected XMLStreamWriter createXmlStreamWriter(CsvTaxExportStateRedlist state, String fileName)
203
			throws IOException, FileNotFoundException, XMLStreamException {
204
		XMLOutputFactory factory = XMLOutputFactory.newInstance(); 
205
		OutputStream os;
206
		boolean useZip = state.isZip();
207
		if (useZip){
208
			os = state.getZipStream(fileName);
209
		}else{
210
			os = createFileOutputStream(state.getConfig(), fileName);
211
		}
212
		XMLStreamWriter  writer = factory.createXMLStreamWriter(os);
213
		return writer;
214
	}
215
	
216

  
217
	/**
218
	 * @param coreTaxFileName
219
	 * @param config
220
	 * @return
221
	 * @throws IOException
222
	 * @throws FileNotFoundException
223
	 * @throws UnsupportedEncodingException
224
	 */
225
	protected PrintWriter createPrintWriter(final String fileName, CsvTaxExportStateRedlist state) 
226
					throws IOException, FileNotFoundException, UnsupportedEncodingException {
227
		
228
		OutputStream os;
229
		boolean useZip = state.isZip();
230
		if (useZip){
231
			os = state.getZipStream(fileName);
232
		}else{
233
			os = createFileOutputStream(state.getConfig(), fileName);
234
		}
235
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF8"), true);
236
		
237
		return writer;
238
	}
239
	
240

  
241

  
242
	
243
	/**
244
	 * Closes the writer
245
	 * @param writer
246
	 * @param state
247
	 */
248
	protected void closeWriter(PrintWriter writer, CsvTaxExportStateRedlist state) {
249
		if (writer != null && state.isZip() == false){
250
			writer.close();
251
		}
252
	}
253
	
254

  
255
	
256
	/**
257
	 * Closes the writer.
258
	 * Note: XMLStreamWriter does not close the underlying stream.
259
	 * @param writer
260
	 * @param state
261
	 */
262
	protected void closeWriter(XMLStreamWriter writer, CsvTaxExportStateRedlist state) {
263
		if (writer != null && state.isZip() == false){
264
			try {
265
				writer.close();
266
			} catch (XMLStreamException e) {
267
				throw new RuntimeException(e);
268
			}
269
		}
270
	}
271
	protected void clearExistingRecordIds(){
272
		existingRecordIds.clear();
273
	}
274
}
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9
package eu.etaxonomy.cdm.io.csv.redlist.out;
10

  
11
import java.io.File;
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.HashSet;
20
import java.util.Set;
21
import java.util.UUID;
22

  
23
import javax.xml.stream.XMLOutputFactory;
24
import javax.xml.stream.XMLStreamException;
25
import javax.xml.stream.XMLStreamWriter;
26

  
27
import org.apache.commons.lang.StringUtils;
28
import org.apache.log4j.Logger;
29

  
30
import eu.etaxonomy.cdm.common.CdmUtils;
31
import eu.etaxonomy.cdm.io.common.CdmExportBase;
32
import eu.etaxonomy.cdm.io.common.ICdmExport;
33
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.common.IOriginalSource;
36
import eu.etaxonomy.cdm.model.common.ISourceable;
37
import eu.etaxonomy.cdm.model.location.Country;
38
import eu.etaxonomy.cdm.model.location.NamedArea;
39
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
40

  
41
/**
42
 * @author a.mueller, a.oppermann
43
 * @date 18.10.2012
44
 *
45
 */
46
public abstract class CsvExportBaseRedlist extends CdmExportBase<CsvTaxExportConfiguratorRedlist, CsvTaxExportStateRedlist, IExportTransformer> implements ICdmExport<CsvTaxExportConfiguratorRedlist, CsvTaxExportStateRedlist>{
47
	private static final Logger logger = Logger.getLogger(CsvExportBaseRedlist.class);
48

  
49
	protected static final boolean IS_CORE = true;
50

  
51

  
52
	protected Set<Integer> existingRecordIds = new HashSet<Integer>();
53
	protected Set<UUID> existingRecordUuids = new HashSet<UUID>();
54

  
55
	/**
56
	 * Creates the locationId, locality, countryCode triple
57
	 * @param record
58
	 * @param area
59
	 */
60
	protected void handleArea(ICsvAreaRecord record, NamedArea area, TaxonBase<?> taxon, boolean required) {
61
		if (area != null){
62
			record.setLocationId(area);
63
			record.setLocality(area.getLabel());
64
			if (area.isInstanceOf(Country.class)){
65
				Country country = CdmBase.deproxy(area, Country.class);
66
				record.setCountryCode(country.getIso3166_A2());
67
			}
68
		}else{
69
			if (required){
70
				String message = "Description requires area but area does not exist for taxon " + getTaxonLogString(taxon);
71
				logger.warn(message);
72
			}
73
		}
74
	}
75

  
76

  
77
	protected String getTaxonLogString(TaxonBase<?> taxon) {
78
		return taxon.getTitleCache() + "(" + taxon.getId() + ")";
79
	}
80

  
81

  
82
	/**
83
	 * @param el
84
	 * @return
85
	 */
86
	protected boolean recordExists(CdmBase el) {
87
		return existingRecordIds.contains(el.getId());
88
	}
89

  
90

  
91
	/**
92
	 * @param sec
93
	 */
94
	protected void addExistingRecord(CdmBase cdmBase) {
95
		existingRecordIds.add(cdmBase.getId());
96
	}
97

  
98
	/**
99
	 * @param el
100
	 * @return
101
	 */
102
	protected boolean recordExistsUuid(CdmBase el) {
103
		return existingRecordUuids.contains(el.getUuid());
104
	}
105

  
106
	/**
107
	 * @param sec
108
	 */
109
	protected void addExistingRecordUuid(CdmBase cdmBase) {
110
		existingRecordUuids.add(cdmBase.getUuid());
111
	}
112

  
113

  
114
	protected String getSources(ISourceable<?> sourceable, CsvTaxExportConfiguratorRedlist config) {
115
		String result = "";
116
		for (IOriginalSource source: sourceable.getSources()){
117
			if (StringUtils.isBlank(source.getIdInSource())){//idInSource indicates that this source is only data provenance, may be changed in future
118
				if (source.getCitation() != null){
119
					String ref = source.getCitation().getTitleCache();
120
					result = CdmUtils.concat(config.getSetSeparator(), result, ref);
121
				}
122
			}
123
		}
124
		return result;
125
	}
126

  
127

  
128
	/**
129
	 * @param config
130
	 * @return
131
	 * @throws IOException
132
	 * @throws FileNotFoundException
133
	 */
134
	protected FileOutputStream createFileOutputStream(CsvTaxExportConfiguratorRedlist config, String thisFileName) throws IOException, FileNotFoundException {
135
		String filePath = config.getDestinationNameString();
136
		String fileName = filePath + File.separatorChar + thisFileName;
137
		File f = new File(fileName);
138
		if (!f.exists()){
139
			f.createNewFile();
140
		}
141
		FileOutputStream fos = new FileOutputStream(f);
142
		return fos;
143
	}
144

  
145

  
146
	/**
147
	 * @param config
148
	 * @param factory
149
	 * @return
150
	 * @throws IOException
151
	 * @throws FileNotFoundException
152
	 * @throws XMLStreamException
153
	 */
154
	protected XMLStreamWriter createXmlStreamWriter(CsvTaxExportStateRedlist state, String fileName)
155
			throws IOException, FileNotFoundException, XMLStreamException {
156
		XMLOutputFactory factory = XMLOutputFactory.newInstance();
157
		OutputStream os;
158
		boolean useZip = state.isZip();
159
		if (useZip){
160
			os = state.getZipStream(fileName);
161
		}else{
162
			os = createFileOutputStream(state.getConfig(), fileName);
163
		}
164
		XMLStreamWriter  writer = factory.createXMLStreamWriter(os);
165
		return writer;
166
	}
167

  
168

  
169
	/**
170
	 * @param coreTaxFileName
171
	 * @param config
172
	 * @return
173
	 * @throws IOException
174
	 * @throws FileNotFoundException
175
	 * @throws UnsupportedEncodingException
176
	 */
177
	protected PrintWriter createPrintWriter(final String fileName, CsvTaxExportStateRedlist state)
178
					throws IOException, FileNotFoundException, UnsupportedEncodingException {
179

  
180
		OutputStream os;
181
		boolean useZip = state.isZip();
182
		if (useZip){
183
			os = state.getZipStream(fileName);
184
		}else{
185
			os = createFileOutputStream(state.getConfig(), fileName);
186
		}
187
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF8"), true);
188

  
189
		return writer;
190
	}
191

  
192

  
193

  
194

  
195
	/**
196
	 * Closes the writer
197
	 * @param writer
198
	 * @param state
199
	 */
200
	protected void closeWriter(PrintWriter writer, CsvTaxExportStateRedlist state) {
201
		if (writer != null && state.isZip() == false){
202
			writer.close();
203
		}
204
	}
205

  
206

  
207

  
208
	/**
209
	 * Closes the writer.
210
	 * Note: XMLStreamWriter does not close the underlying stream.
211
	 * @param writer
212
	 * @param state
213
	 */
214
	protected void closeWriter(XMLStreamWriter writer, CsvTaxExportStateRedlist state) {
215
		if (writer != null && state.isZip() == false){
216
			try {
217
				writer.close();
218
			} catch (XMLStreamException e) {
219
				throw new RuntimeException(e);
220
			}
221
		}
222
	}
223
	protected void clearExistingRecordIds(){
224
		existingRecordIds.clear();
225
	}
226
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/csv/redlist/out/CsvTaxExportConfiguratorRedlist.java
1
/**

2
 * Copyright (C) 2007 EDIT

3
 * European Distributed Institute of Taxonomy 

4
 * http://www.e-taxonomy.eu

5
 * 

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.

8
 */

9

  
10
package eu.etaxonomy.cdm.io.csv.redlist.out;

11

  
12
import java.io.ByteArrayOutputStream;

13
import java.io.File;

14
import java.util.ArrayList;

15
import java.util.HashSet;

16
import java.util.List;

17
import java.util.Set;

18
import java.util.UUID;

19

  
20
import org.apache.log4j.Logger;

21

  
22
import eu.etaxonomy.cdm.database.ICdmDataSource;

23
import eu.etaxonomy.cdm.io.common.XmlExportConfiguratorBase;

24
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;

25
import eu.etaxonomy.cdm.model.description.Feature;

26
import eu.etaxonomy.cdm.model.location.NamedArea;

27

  
28

  
29
/**

30
 * @author a.oppermann

31
 * @created 17.10.2012

32
 */

33
public class CsvTaxExportConfiguratorRedlist extends XmlExportConfiguratorBase<CsvTaxExportStateRedlist> {

34
	@SuppressWarnings("unused")

35
	private static final Logger logger = Logger.getLogger(CsvTaxExportConfiguratorRedlist.class);

36

  
37
	private String encoding = "UTF-8";

38
	private String linesTerminatedBy = "\r\n";

39
	private String fieldsEnclosedBy = "\"";

40
	private boolean hasHeaderLines = true;

41
	private String fieldsTerminatedBy=",";

42
	private boolean doTaxa = true;

43
	private boolean doDistributions = false;

44
	private ByteArrayOutputStream baos;

45
	private boolean isUseIdWherePossible = false;

46
	private boolean includeBasionymsInResourceRelations;

47
	private boolean includeMisappliedNamesInResourceRelations;

48
	private String defaultBibliographicCitation = null;

49
	private List<UUID> featureExclusions = new ArrayList<UUID>();

50
	//filter on the classifications to be exported

51
	private Set<UUID> classificationUuids = new HashSet<UUID>();   

52
	private boolean withHigherClassification = false;

53
	private String setSeparator = ";";

54

  
55
	private List<Feature> features;

56

  
57
	private String classificationTitleCache;

58

  
59
	private List<NamedArea> areas;

60

  
61
	//TODO

62
	private static IExportTransformer defaultTransformer = null;

63

  
64
	public static CsvTaxExportConfiguratorRedlist NewInstance(ICdmDataSource source, File destinationFolder) { 

65
		return new CsvTaxExportConfiguratorRedlist(source, destinationFolder);

66
	}

67

  
68
	@Override

69
	@SuppressWarnings("unchecked")

70
	protected void makeIoClassList() {

71
		ioClassList = new Class[] {

72
				CsvTaxExportRedlist.class

73
		};

74
	}

75

  
76
	/**

77
	 * @param url

78
	 * @param destination

79
	 */

80
	private CsvTaxExportConfiguratorRedlist(ICdmDataSource source, File destination) {

81
		super(destination, source, defaultTransformer);

82
	}

83

  
84
	/* (non-Javadoc)

85
	 * @see eu.etaxonomy.cdm.io.common.ImportConfiguratorBase#getSource()

86
	 */

87
	@Override

88
	public File getDestination() {

89
		return super.getDestination();

90
	}

91

  
92
	/**

93
	 * @param file

94
	 */

95
	@Override

96
	public void setDestination(File fileName) {

97
		super.setDestination(fileName);

98
	}

99

  
100
	/* (non-Javadoc)

101
	 * @see eu.etaxonomy.cdm.io.common.IExportConfigurator#getDestinationNameString()

102
	 */

103
	@Override

104
	public String getDestinationNameString() {

105
		if (this.getDestination() == null) {

106
			return null;

107
		} else {

108
			return this.getDestination().toString();

109
		}

110
	}

111

  
112
	/* (non-Javadoc)

113
	 * @see eu.etaxonomy.cdm.io.common.IExportConfigurator#getNewState()

114
	 */

115
	public CsvTaxExportStateRedlist getNewState() {

116
		return new CsvTaxExportStateRedlist(this);

117
	}

118

  
119
	public boolean isDoTaxa() {

120
		return doTaxa;

121
	}

122

  
123
	public void setDoTaxa(boolean doTaxa) {

124
		this.doTaxa = doTaxa;

125
	}

126

  
127

  
128
	public boolean isDoDistributions() {

129
		return doDistributions;

130
	}

131

  
132
	public void setDoDistributions(boolean doDistributions) {

133
		this.doDistributions = doDistributions;

134
	}

135

  
136
	public void setFeatureExclusions(List<UUID> featureExclusions) {

137
		this.featureExclusions = featureExclusions;

138
	}

139

  
140
	public List<UUID> getFeatureExclusions() {

141
		return featureExclusions;

142
	}

143

  
144
	public String getEncoding() {

145
		return encoding;

146
	}

147

  
148
	public void setEncoding(String encoding) {

149
		this.encoding = encoding;

150
	}

151

  
152
	public String getLinesTerminatedBy() {

153
		return linesTerminatedBy;

154
	}

155

  
156
	public void setLinesTerminatedBy(String linesTerminatedBy) {

157
		this.linesTerminatedBy = linesTerminatedBy;

158
	}

159

  
160
	public String getFieldsEnclosedBy() {

161
		return fieldsEnclosedBy;

162
	}

163

  
164
	public void setFieldsEnclosedBy(String fieldsEnclosedBy) {

165
		this.fieldsEnclosedBy = fieldsEnclosedBy;

166
	}

167

  
168
	/**

169
	 * Equals darwin core archive ignoreHeaderLines attribute

170
	 * @return

171
	 */

172
	public boolean isHasHeaderLines() {

173
		return hasHeaderLines;

174
	}

175

  
176
	public void setHasHeaderLines(boolean hasHeaderLines) {

177
		this.hasHeaderLines = hasHeaderLines;

178
	}

179

  
180
	public boolean isIncludeBasionymsInResourceRelations() {

181
		return includeBasionymsInResourceRelations;

182
	}

183

  
184
	public void setIncludeBasionymsInResourceRelations(boolean includeBasionymsInResourceRelations) {

185
		this.includeBasionymsInResourceRelations = includeBasionymsInResourceRelations;

186
	}

187

  
188
	public boolean isIncludeMisappliedNamesInResourceRelations() {

189
		return includeMisappliedNamesInResourceRelations;

190
	}

191

  
192
	public void setIncludeMisappliedNamesInResourceRelations(boolean includeMisappliedNamesInResourceRelations) {

193
		this.includeMisappliedNamesInResourceRelations = includeMisappliedNamesInResourceRelations;

194
	}

195

  
196
	public boolean isUseIdWherePossible() {

197
		return this.isUseIdWherePossible;

198
	}

199

  
200
	public void setUseIdWherePossible(boolean isUseIdWherePossible) {

201
		this.isUseIdWherePossible = isUseIdWherePossible;

202
	}

203

  
204
	public void setDefaultBibliographicCitation(String defaultBibliographicCitation) {

205
		this.defaultBibliographicCitation = defaultBibliographicCitation;

206
	}

207

  
208

  
209
	public String getDefaultBibliographicCitation() {

210
		return defaultBibliographicCitation;

211
	}

212

  
213
	/**

214
	 * The default value for the taxon.source column. This may be a column linking to a url that provides 

215
	 * data about the given taxon. The id is replaced by a placeholder, 

216
	 * e.g. http://wp6-cichorieae.e-taxonomy.eu/portal/?q=cdm_dataportal/taxon/{id}.

217
	 * NOTE: This may be replaced in future versions by concrete CDM server implementations.

218
	 * 

219
	 * @return the taxonSourceDefault

220
	 */

221

  
222
	public boolean isWithHigherClassification() {

223
		return withHigherClassification;

224
	}

225

  
226
	public void setWithHigherClassification(boolean withHigherClassification) {

227
		this.withHigherClassification = withHigherClassification;

228
	}

229

  
230
	/**

231
	 * @return the setSeparator

232
	 */

233
	public String getSetSeparator() {

234
		return setSeparator;

235
	}

236

  
237
	/**

238
	 * @param setSeparator the setSeparator to set

239
	 */

240
	public void setSetSeparator(String setSeparator) {

241
		this.setSeparator = setSeparator;

242
	}

243

  
244
	public void setFieldsTerminatedBy(String fieldsTerminatedBy) {

245
		this.fieldsTerminatedBy = fieldsTerminatedBy;

246
	}

247

  
248
	public String getFieldsTerminatedBy() {

249
		return fieldsTerminatedBy;

250
	}

251

  
252
	public Set<UUID> getTaxonNodeUuids() {

253
		return classificationUuids;

254
	}

255

  
256
	public void setTaxonNodeUuids(Set<UUID> classificationUuids) {

257
		this.classificationUuids = classificationUuids;

258
	}

259

  
260
	public ByteArrayOutputStream getByteArrayOutputStream() {

261
		return baos;

262
	}

263

  
264
	public void setByteArrayOutputStream(ByteArrayOutputStream baos) {

265
		this.baos = baos;

266
	}

267

  
268
	public void setFeatures(List<Feature> features) {

269
		this.features = features;

270
		
271
	}

272
	
273
	public List<Feature>  getFeatures() {

274
		return features;

275
		
276
	}

277

  
278
	public void setClassificationTitleCache(String classificationTitleCache) {

279
		this.classificationTitleCache = classificationTitleCache;

280
	}

281
	
282
	public String getClassificationTitleCache() {

283
		return classificationTitleCache;

284
	}

285

  
286
	/**

287
	 * @param areas

288
	 */

289
	public void setNamedAreas(List<NamedArea> areas) {

290
		// TODO Auto-generated method stub

291
		this.areas = areas;

292
		
293
	}

294
	public List<NamedArea> getNamedAreas(){

295
		return areas;

296
	}

297
	
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy 
4
 * http://www.e-taxonomy.eu
5
 * 
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.
8
 */
9

  
10
package eu.etaxonomy.cdm.io.csv.redlist.out;
11

  
12
import java.io.ByteArrayOutputStream;
13
import java.io.File;
14
import java.util.ArrayList;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Set;
18
import java.util.UUID;
19

  
20
import org.apache.log4j.Logger;
21

  
22
import eu.etaxonomy.cdm.database.ICdmDataSource;
23
import eu.etaxonomy.cdm.io.common.XmlExportConfiguratorBase;
24
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
25
import eu.etaxonomy.cdm.model.description.Feature;
26
import eu.etaxonomy.cdm.model.location.NamedArea;
27

  
28

  
29
/**
30
 * @author a.oppermann
31
 * @created 17.10.2012
32
 */
33
public class CsvTaxExportConfiguratorRedlist extends XmlExportConfiguratorBase<CsvTaxExportStateRedlist> {
34
	@SuppressWarnings("unused")
35
	private static final Logger logger = Logger.getLogger(CsvTaxExportConfiguratorRedlist.class);
36

  
37
	private String encoding = "UTF-8";
38
	private String linesTerminatedBy = "\r\n";
39
	private String fieldsEnclosedBy = "\"";
40
	private boolean hasHeaderLines = true;
41
	private String fieldsTerminatedBy=",";
42
	private boolean doTaxa = true;
43
	private boolean doDistributions = false;
44
	private ByteArrayOutputStream baos;
45
	private boolean isUseIdWherePossible = false;
46
	private boolean includeBasionymsInResourceRelations;
47
	private boolean includeMisappliedNamesInResourceRelations;
48
	private String defaultBibliographicCitation = null;
49
	private List<UUID> featureExclusions = new ArrayList<UUID>();
50
	//filter on the classifications to be exported
51
	private Set<UUID> taxonNodeUuids = new HashSet<UUID>();   
52
	private boolean withHigherClassification = false;
53
	private String setSeparator = ";";
54

  
55
	private List<Feature> features;
56

  
57
	private String classificationTitleCache;
58

  
59
	private List<NamedArea> areas;
60

  
61
	//TODO
62
	private static IExportTransformer defaultTransformer = null;
63

  
64
	public static CsvTaxExportConfiguratorRedlist NewInstance(ICdmDataSource source, File destinationFolder) { 
65
		return new CsvTaxExportConfiguratorRedlist(source, destinationFolder);
66
	}
67

  
68
	@Override
69
	@SuppressWarnings("unchecked")
70
	protected void makeIoClassList() {
71
		ioClassList = new Class[] {
72
				CsvTaxExportRedlist.class
73
		};
74
	}
75

  
76
	/**
77
	 * @param url
78
	 * @param destination
79
	 */
80
	private CsvTaxExportConfiguratorRedlist(ICdmDataSource source, File destination) {
81
		super(destination, source, defaultTransformer);
82
	}
83

  
84
	/* (non-Javadoc)
85
	 * @see eu.etaxonomy.cdm.io.common.ImportConfiguratorBase#getSource()
86
	 */
87
	@Override
88
	public File getDestination() {
89
		return super.getDestination();
90
	}
91

  
92
	/**
93
	 * @param file
94
	 */
95
	@Override
96
	public void setDestination(File fileName) {
97
		super.setDestination(fileName);
98
	}
99

  
100
	/* (non-Javadoc)
101
	 * @see eu.etaxonomy.cdm.io.common.IExportConfigurator#getDestinationNameString()
102
	 */
103
	@Override
104
	public String getDestinationNameString() {
105
		if (this.getDestination() == null) {
106
			return null;
107
		} else {
108
			return this.getDestination().toString();
109
		}
110
	}
111

  
112
	/* (non-Javadoc)
113
	 * @see eu.etaxonomy.cdm.io.common.IExportConfigurator#getNewState()
114
	 */
115
	public CsvTaxExportStateRedlist getNewState() {
116
		return new CsvTaxExportStateRedlist(this);
117
	}
118

  
119
	public boolean isDoTaxa() {
120
		return doTaxa;
121
	}
122

  
123
	public void setDoTaxa(boolean doTaxa) {
124
		this.doTaxa = doTaxa;
125
	}
126

  
127

  
128
	public boolean isDoDistributions() {
129
		return doDistributions;
130
	}
131

  
132
	public void setDoDistributions(boolean doDistributions) {
133
		this.doDistributions = doDistributions;
134
	}
135

  
136
	public void setFeatureExclusions(List<UUID> featureExclusions) {
137
		this.featureExclusions = featureExclusions;
138
	}
139

  
140
	public List<UUID> getFeatureExclusions() {
141
		return featureExclusions;
142
	}
143

  
144
	public String getEncoding() {
145
		return encoding;
146
	}
147

  
148
	public void setEncoding(String encoding) {
149
		this.encoding = encoding;
150
	}
151

  
152
	public String getLinesTerminatedBy() {
153
		return linesTerminatedBy;
154
	}
155

  
156
	public void setLinesTerminatedBy(String linesTerminatedBy) {
157
		this.linesTerminatedBy = linesTerminatedBy;
158
	}
159

  
160
	public String getFieldsEnclosedBy() {
161
		return fieldsEnclosedBy;
162
	}
163

  
164
	public void setFieldsEnclosedBy(String fieldsEnclosedBy) {
165
		this.fieldsEnclosedBy = fieldsEnclosedBy;
166
	}
167

  
168
	/**
169
	 * Equals darwin core archive ignoreHeaderLines attribute
170
	 * @return
171
	 */
172
	public boolean isHasHeaderLines() {
173
		return hasHeaderLines;
174
	}
175

  
176
	public void setHasHeaderLines(boolean hasHeaderLines) {
177
		this.hasHeaderLines = hasHeaderLines;
178
	}
179

  
180
	public boolean isIncludeBasionymsInResourceRelations() {
181
		return includeBasionymsInResourceRelations;
182
	}
183

  
184
	public void setIncludeBasionymsInResourceRelations(boolean includeBasionymsInResourceRelations) {
185
		this.includeBasionymsInResourceRelations = includeBasionymsInResourceRelations;
186
	}
187

  
188
	public boolean isIncludeMisappliedNamesInResourceRelations() {
189
		return includeMisappliedNamesInResourceRelations;
190
	}
191

  
192
	public void setIncludeMisappliedNamesInResourceRelations(boolean includeMisappliedNamesInResourceRelations) {
193
		this.includeMisappliedNamesInResourceRelations = includeMisappliedNamesInResourceRelations;
194
	}
195

  
196
	public boolean isUseIdWherePossible() {
197
		return this.isUseIdWherePossible;
198
	}
199

  
200
	public void setUseIdWherePossible(boolean isUseIdWherePossible) {
201
		this.isUseIdWherePossible = isUseIdWherePossible;
202
	}
203

  
204
	public void setDefaultBibliographicCitation(String defaultBibliographicCitation) {
205
		this.defaultBibliographicCitation = defaultBibliographicCitation;
206
	}
207

  
208

  
209
	public String getDefaultBibliographicCitation() {
210
		return defaultBibliographicCitation;
211
	}
212

  
213
	/**
214
	 * The default value for the taxon.source column. This may be a column linking to a url that provides 
215
	 * data about the given taxon. The id is replaced by a placeholder, 
216
	 * e.g. http://wp6-cichorieae.e-taxonomy.eu/portal/?q=cdm_dataportal/taxon/{id}.
217
	 * NOTE: This may be replaced in future versions by concrete CDM server implementations.
218
	 * 
219
	 * @return the taxonSourceDefault
220
	 */
221

  
222
	public boolean isWithHigherClassification() {
223
		return withHigherClassification;
224
	}
225

  
226
	public void setWithHigherClassification(boolean withHigherClassification) {
227
		this.withHigherClassification = withHigherClassification;
228
	}
229

  
230
	/**
231
	 * @return the setSeparator
232
	 */
233
	public String getSetSeparator() {
234
		return setSeparator;
235
	}
236

  
237
	/**
238
	 * @param setSeparator the setSeparator to set
239
	 */
240
	public void setSetSeparator(String setSeparator) {
241
		this.setSeparator = setSeparator;
242
	}
243

  
244
	public void setFieldsTerminatedBy(String fieldsTerminatedBy) {
245
		this.fieldsTerminatedBy = fieldsTerminatedBy;
246
	}
247

  
248
	public String getFieldsTerminatedBy() {
249
		return fieldsTerminatedBy;
250
	}
251

  
252
	public Set<UUID> getTaxonNodeUuids() {
253
		return taxonNodeUuids;
254
	}
255

  
256
	public void setTaxonNodeUuids(Set<UUID> taxonNodeUuids) {
257
		this.taxonNodeUuids = taxonNodeUuids;
258
	}
259

  
260
	public ByteArrayOutputStream getByteArrayOutputStream() {
261
		return baos;
262
	}
263

  
264
	public void setByteArrayOutputStream(ByteArrayOutputStream baos) {
265
		this.baos = baos;
266
	}
267

  
268
	public void setFeatures(List<Feature> features) {
269
		this.features = features;
270
		
271
	}
272
	
273
	public List<Feature>  getFeatures() {
274
		return features;
275
		
276
	}
277

  
278
	public void setClassificationTitleCache(String classificationTitleCache) {
279
		this.classificationTitleCache = classificationTitleCache;
280
	}
281
	
282
	public String getClassificationTitleCache() {
283
		return classificationTitleCache;
284
	}
285

  
286
	/**
287
	 * @param areas
288
	 */
289
	public void setNamedAreas(List<NamedArea> areas) {
290
		// TODO Auto-generated method stub
291
		this.areas = areas;
292
		
293
	}
294
	public List<NamedArea> getNamedAreas(){
295
		return areas;
296
	}
297
	
298 298
}
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/csv/redlist/out/CsvTaxExportRedlist.java
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
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.
8
*/
9

  
10
package eu.etaxonomy.cdm.io.csv.redlist.out;
11

  
12
import java.io.ByteArrayOutputStream;
13
import java.io.PrintWriter;
14
import java.util.ArrayList;
15
import java.util.Collections;
16
import java.util.Comparator;
17
import java.util.HashSet;
18
import java.util.List;
19
import java.util.Set;
20
import java.util.UUID;
21

  
22
import org.apache.log4j.Logger;
23
import org.springframework.stereotype.Component;
24
import org.springframework.transaction.TransactionStatus;
25

  
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27
import eu.etaxonomy.cdm.model.common.Language;
28
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
29
import eu.etaxonomy.cdm.model.description.CategoricalData;
30
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
31
import eu.etaxonomy.cdm.model.description.Distribution;
32
import eu.etaxonomy.cdm.model.description.Feature;
33
import eu.etaxonomy.cdm.model.description.State;
34
import eu.etaxonomy.cdm.model.description.TaxonDescription;
35
import eu.etaxonomy.cdm.model.description.TextData;
36
import eu.etaxonomy.cdm.model.location.NamedArea;
37
import eu.etaxonomy.cdm.model.name.NonViralName;
38
import eu.etaxonomy.cdm.model.taxon.Classification;
39
import eu.etaxonomy.cdm.model.taxon.Synonym;
40
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
41
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
42
import eu.etaxonomy.cdm.model.taxon.Taxon;
43
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
44
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
45
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
46
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
47

  
48

  
49
/**
50
 * @author a.oppermann
51
 * @created 18.10.2012
52
 */
53

  
54
@Component
55
public class CsvTaxExportRedlist extends CsvExportBaseRedlist {
56
	private static final Logger logger = Logger.getLogger(CsvTaxExportRedlist.class);
57

  
58
	private static final String ROW_TYPE = "http://rs.tdwg.org/dwc/terms/Taxon";
59
	private static final String fileName = "RedlistCoreTax.csv";
60

  
61
	public CsvTaxExportRedlist() {
62
		super();
63
		this.ioName = this.getClass().getSimpleName();
64
	}
65
	
66

  
67
	/** Retrieves data from a CDM DB and serializes them CDM to CSV.
68
	 * Starts with root taxa and traverses the classification to retrieve 
69
	 * children taxa, synonyms, relationships, descriptive data, red list 
70
	 * status (features). 
71
	 * Taxa that are not part of the classification are not found.
72
	 * 
73
	 * @param exImpConfig
74
	 * @param dbname
75
	 * @param filename
76
	 */
77
	@Override
78
	protected void doInvoke(CsvTaxExportStateRedlist state){
79
		CsvTaxExportConfiguratorRedlist config = state.getConfig();
80
		TransactionStatus txStatus = startTransaction(true);
81
		List<NamedArea> selectedAreas = config.getNamedAreas();
82
		Set<TaxonNode> taxonNodes = assembleTaxonNodeSet(config);
83
		
84
		PrintWriter writer = null;
85
		ByteArrayOutputStream byteArrayOutputStream;
86
		try {
87
			byteArrayOutputStream = config.getByteArrayOutputStream();
88
			writer = new PrintWriter(byteArrayOutputStream); 
89
			//geographical Filter
90
			List<TaxonNode> filteredNodes = handleGeographicalFilter(selectedAreas, taxonNodes);
91
			
92
			//sorting List
93
			Collections.sort(filteredNodes, new Comparator<TaxonNode>() {
94

  
95
				@Override
96
				public int compare(TaxonNode tn1, TaxonNode tn2) {
97
					Taxon taxon1 = tn1.getTaxon();
98
					Taxon taxon2 = tn2.getTaxon();
99
					if(taxon1 != null && taxon2 != null){
100
						return taxon1.getTitleCache().compareTo(taxon2.getTitleCache());
101
					}
102
					else{
103
						return 0;
104
					}
105
				}
106
			});
107
			for (TaxonNode node : filteredNodes){
108
				Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
109
				CsvTaxRecordRedlist record = assembleRecord(state);
110
				NonViralName<?> name = CdmBase.deproxy(taxon.getName(), NonViralName.class);
111
				Classification classification = node.getClassification();
112
				config.setClassificationTitleCache(classification.getTitleCache());	
113
				if (! this.recordExists(taxon)){
114
					handleTaxonBase(record, taxon, name, taxon, classification, null, false, false, config);
115
					record.write(writer);
116
					this.addExistingRecord(taxon);
117
				}
118
				//misapplied names
119
				handleMisapplication(taxon, writer, classification, record, config);
120
				writer.flush();
121
			}
122
		} catch (ClassCastException e) {
123
			e.printStackTrace();
124
		}
125
		finally{
126
			writer.close();
127
			this.clearExistingRecordIds();
128
		}
129
		commitTransaction(txStatus);
130
		return;
131

  
132
	}
133
	
134
	
135
	//TODO: Exception handling
136
	protected Set<TaxonNode> assembleTaxonNodeSet(CsvTaxExportConfiguratorRedlist config){
137
		Set<TaxonNode> taxonNodes = new HashSet<>();
138
		if(config != null){
139
			Set<UUID> taxonNodeUuidSet = config.getTaxonNodeUuids();
140
			taxonNodes.addAll(getTaxonNodeService().find(taxonNodeUuidSet));
141
		}
142
		return taxonNodes;
143
	}
144

  
145
	//TODO: Exception handling
146
	private CsvTaxRecordRedlist assembleRecord(CsvTaxExportStateRedlist state) {
147
		if(state!=null){
148
			CsvTaxExportConfiguratorRedlist config = state.getConfig();
149
			CsvMetaDataRecordRedlist metaRecord = new CsvMetaDataRecordRedlist(true, fileName, ROW_TYPE);
150
			state.addMetaRecord(metaRecord);
151
			CsvTaxRecordRedlist record = new CsvTaxRecordRedlist(metaRecord, config);
152
			return record;
153
		}
154
		return null;
155
	}
156

  
157
	/**
158
	 * Takes positive List of areas and iterates over a given taxon node 
159
	 * and their {@link Taxon} to return all {@link Taxon} with the desired 
160
	 * geographical attribute.
161
	 * 
162
	 * <p><p>
163
	 *
164
	 * If selectedAreas is null all child {@link TaxonNode}s of the given taxon node will be returned.
165
	 * 
166
	 * @param selectedAreas 
167
	 * @param taxonNodes
168
	 * @return
169
	 */
170
	protected List<TaxonNode> handleGeographicalFilter(List<NamedArea> selectedAreas,
171
			Set<TaxonNode> taxonNodes) {
172
		List<TaxonNode> filteredNodes = new ArrayList<TaxonNode>();
173
		List<TaxonNode> allNodes =  new ArrayList(getAllNodes(taxonNodes));
174
		//Geographical filter
175
		logger.info(selectedAreas.size());
176
		if(selectedAreas != null && !selectedAreas.isEmpty() && selectedAreas.size() < 16){
177
//				if(selectedAreas.size() == 16){
178
//					//Germany TDWG Level 3
179
//					String germany="uu7b7c2db5-aa44-4302-bdec-6556fd74b0b9id";
180
//					selectedAreas.add((NamedArea) getTermService().find(UUID.fromString(germany)));
181
//				}
182
			for (TaxonNode node : allNodes){
183
				Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
184
				Set<TaxonDescription> descriptions = taxon.getDescriptions();
185
				for (TaxonDescription description : descriptions){
186
					for (DescriptionElementBase el : description.getElements()){
187
						if (el.isInstanceOf(Distribution.class) ){
188
							Distribution distribution = CdmBase.deproxy(el, Distribution.class);
189
							NamedArea area = distribution.getArea();
190
							for(NamedArea selectedArea:selectedAreas){
191
								if(selectedArea.getUuid().equals(area.getUuid())){
192
									filteredNodes.add(node);
193
								}
194
							}
195
						}
196
					}
197
				}
198
			}
199
		}else{
200
			filteredNodes = allNodes;
201
		}
202
		return filteredNodes;
203
	}
204

  
205
	/**
206
	 * handles misapplied {@link Taxon}
207
	 * @param taxon
208
	 * @param writer
209
	 * @param classification
210
	 * @param metaRecord
211
	 * @param config
212
	 */
213
	private void handleMisapplication(Taxon taxon, PrintWriter writer, Classification classification, CsvTaxRecordRedlist record, CsvTaxExportConfiguratorRedlist config) {
214
		Set<Taxon> misappliedNames = taxon.getMisappliedNames();
215
		for (Taxon misappliedName : misappliedNames ){
216
//			CsvTaxRecordRedlist record = new CsvTaxRecordRedlist(metaRecord, config);
217
			TaxonRelationshipType relType = TaxonRelationshipType.MISAPPLIED_NAME_FOR();
218
			NonViralName<?> name = CdmBase.deproxy(misappliedName.getName(), NonViralName.class);
219
		
220
			if (! this.recordExists(misappliedName)){
221
				handleTaxonBase(record, misappliedName, name, taxon, classification, relType, false, false, config);
222
				record.write(writer);
223
				this.addExistingRecord(misappliedName);
224
			}
225
		}	
226
	}
227

  
228
	/**
229
	 * handles the information record for the actual {@link Taxon} including {@link Classification classification}, Taxon Name, Taxon ID,
230
	 * Taxon Status, Synonyms, {@link Feature features} data 
231
	 * @param record the concrete information record
232
	 * @param taxonBase {@link Taxon}
233
	 * @param name
234
	 * @param acceptedTaxon
235
	 * @param parent
236
	 * @param basionym
237
	 * @param isPartial 
238
	 * @param isProParte 
239
	 * @param config 
240
	 * @param type
241
	 */
242
	private void handleTaxonBase(CsvTaxRecordRedlist record,TaxonBase<?> taxonBase,
243
			NonViralName<?> name, Taxon acceptedTaxon, Classification classification, 
244
			RelationshipTermBase<?> relType, boolean isProParte, boolean isPartial, 
245
			CsvTaxExportConfiguratorRedlist config) {
246
		
247
		List<Feature> features = config.getFeatures();
248
		record.setHeadLinePrinted(config.isHasHeaderLines());
249
		if(features != null)record.setPrintFeatures(features);
250
		config.setHasHeaderLines(false);
251

  
252
		record.setDatasetName(classification.getTitleCache());
253
		record.setScientificName(name.getTitleCache());
254
		record.setScientificNameId(name.getUuid().toString());
255
		handleTaxonomicStatus(record, name, relType, isProParte, isPartial);
256
		//synonyms
257
		handleSynonyms(record,(Taxon) taxonBase);
258
		//distribution
259
		handleDiscriptionData(record, (Taxon) taxonBase);
260
		if(features!= null) {
261
			
262
			List<List<String>> featureCells = new ArrayList<List<String>>(features.size());
263
			for(int i = 0; i < features.size(); i++) {
264
				featureCells.add(new ArrayList<String>());
265
			}
266
			handleRelatedRedlistStatus(record, (Taxon)taxonBase, false, featureCells, features);
267
			handleRelatedRedlistStatus(record, (Taxon)taxonBase, true, featureCells, features);
268

  
269
		}
270
		return;
271
	}
272

  
273
	private void handleTaxonomicStatus(
274
			CsvTaxRecordRedlist record,
275
			NonViralName<?> name, 
276
			RelationshipTermBase<?> type,
277
			boolean isProParte,
278
			boolean isPartial) {
279
		if (type == null){
280
			record.setTaxonomicStatus(name.getNomenclaturalCode().acceptedTaxonStatusLabel());
281
		}else{
282
			String status = name.getNomenclaturalCode().synonymStatusLabel();
283
			if (type.equals(SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF())){
284
				status = "heterotypicSynonym";
285
			}else if(type.equals(SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF())){
286
				status = "homotypicSynonym";
287
			}else if(type.equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
288
				status = "misapplied";
289
			}
290
			if (isProParte){
291
				status = "proParteSynonym";
292
			}else if (isPartial){
293
				String message = "Partial synonym is not part of the gbif toxonomic status vocabulary";
294
				logger.warn(message);
295
				status = "partialSynonym";
296
			}
297
			
298
			record.setTaxonomicStatus(status);
299
		}
300
	}
301

  
302
	private void handleSynonyms(CsvTaxRecordRedlist record, Taxon taxon) {
303
		
304
		Set<SynonymRelationship> synRels = taxon.getSynonymRelations();
305
		ArrayList<String> synonyms = new ArrayList<String>(); 
306
		for (SynonymRelationship synRel :synRels ){
307
			Synonym synonym = synRel.getSynonym();
308
			SynonymRelationshipType type = synRel.getType();
309
			if (type == null){ // should not happen
310
				type = SynonymRelationshipType.SYNONYM_OF();
311
			}
312
			NonViralName<?> name = CdmBase.deproxy(synonym.getName(), NonViralName.class);
313
			synonyms.add(name.getTitleCache());
314
		}
315
		record.setSynonyms(synonyms);
316
	}
317

  
318
	private void handleDiscriptionData(CsvTaxRecordRedlist record, Taxon taxon) {
319
		
320
		Set<TaxonDescription> descriptions = taxon.getDescriptions();
321
		ArrayList<String> distributions = new ArrayList<String>();
322
		for (TaxonDescription description : descriptions){
323
			for (DescriptionElementBase el : description.getElements()){
324
				if (el.isInstanceOf(Distribution.class) ){
325
						Distribution distribution = CdmBase.deproxy(el, Distribution.class);
326
						NamedArea area = distribution.getArea();
327
						distributions.add(area.getTitleCache());
328
				}
329

  
330
			}
331
		}
332
		record.setCountryCode(distributions);
333
	}
334

  
335
	private void handleRedlistStatus(CsvTaxRecordRedlist record, Taxon taxon, List<List<String>> featureCells, List<Feature> features){
336
		Set<TaxonDescription> descriptions = taxon.getDescriptions();
337

  
338
		for (TaxonDescription description : descriptions){
339
			for (DescriptionElementBase el : description.getElements()){
340
				if(el.isInstanceOf(CategoricalData.class)){
341
					CategoricalData categoricalData = CdmBase.deproxy(el, CategoricalData.class);
342
					for(State state:categoricalData.getStatesOnly()){
343
						Feature stateFeature = categoricalData.getFeature();
344
						// find matching feature and put data into according cell
345
						for(int i = 0; i < features.size(); i++) {
346
							if(features.get(i).equals(stateFeature)){
347
								List<String> cell = featureCells.get(i);
348
								cell.add(state.toString());
349
							}
350
						}
351
					}
352
				}else if(el.isInstanceOf(TextData.class)){
353
					TextData textData = CdmBase.deproxy(el, TextData.class);
354
					Feature textFeature = textData.getFeature();
355
					// find matching feature and put data into according cell
356
					for(int i = 0; i < features.size(); i++) {
357
						if(features.get(i).equals(textFeature)){
358
							List<String> cell = featureCells.get(i);
359
							String text = textData.getText(Language.GERMAN());
360
							text = text.replaceAll(System.getProperty("line.separator"), "");
361
							text = text.replaceAll("                            ", " ");
362
							cell.add(text);
363
							
364
						}
365
					}
366
				}
367
			}
368
		}
369
		record.setFeatures(featureCells);
370
	}
371

  
372

  
373
	private void handleRelatedRedlistStatus(CsvTaxRecordRedlist record, Taxon taxon, boolean relationFrom, List<List<String>> featureCells, List<Feature> features) {
374

  
375
		if (relationFrom)handleRedlistStatus(record, taxon, featureCells, features);
376
		
377
		
378
		Set<TaxonRelationship> taxRels;
379
		if(relationFrom){
380
			taxRels = taxon.getRelationsFromThisTaxon();
381
		}else{
382
			taxRels = taxon.getRelationsToThisTaxon();
383
		}
384
		for (TaxonRelationship taxRel:taxRels){
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff