Project

General

Profile

Download (6.37 KB) Statistics
| Branch: | Tag: | Revision:
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.location.Country;
36
import eu.etaxonomy.cdm.model.location.NamedArea;
37
import eu.etaxonomy.cdm.model.reference.IOriginalSource;
38
import eu.etaxonomy.cdm.model.reference.ISourceable;
39
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
40

    
41
/**
42
 * @author a.mueller, a.oppermann
43
 * @since 18.10.2012
44
 *
45
 */
46
public abstract class CsvExportBaseRedlist
47
        extends CdmExportBase<CsvTaxExportConfiguratorRedlist, CsvTaxExportStateRedlist, IExportTransformer, File>
48
        implements ICdmExport<CsvTaxExportConfiguratorRedlist, CsvTaxExportStateRedlist>{
49

    
50
    private static final long serialVersionUID = 2719567114724597599L;
51

    
52
    private static final Logger logger = Logger.getLogger(CsvExportBaseRedlist.class);
53

    
54
	protected static final boolean IS_CORE = true;
55

    
56

    
57
	protected Set<Integer> existingRecordIds = new HashSet<Integer>();
58
	protected Set<UUID> existingRecordUuids = new HashSet<UUID>();
59

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

    
81

    
82
	protected String getTaxonLogString(TaxonBase<?> taxon) {
83
		return taxon.getTitleCache() + "(" + taxon.getId() + ")";
84
	}
85

    
86

    
87
	/**
88
	 * @param el
89
	 * @return
90
	 */
91
	protected boolean recordExists(CdmBase el) {
92
		return existingRecordIds.contains(el.getId());
93
	}
94

    
95

    
96
	/**
97
	 * @param sec
98
	 */
99
	protected void addExistingRecord(CdmBase cdmBase) {
100
		existingRecordIds.add(cdmBase.getId());
101
	}
102

    
103
	/**
104
	 * @param el
105
	 * @return
106
	 */
107
	protected boolean recordExistsUuid(CdmBase el) {
108
		return existingRecordUuids.contains(el.getUuid());
109
	}
110

    
111
	/**
112
	 * @param sec
113
	 */
114
	protected void addExistingRecordUuid(CdmBase cdmBase) {
115
		existingRecordUuids.add(cdmBase.getUuid());
116
	}
117

    
118

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

    
132

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

    
150

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

    
173

    
174
	/**
175
	 * @param coreTaxFileName
176
	 * @param config
177
	 * @return
178
	 * @throws IOException
179
	 * @throws FileNotFoundException
180
	 * @throws UnsupportedEncodingException
181
	 */
182
	protected PrintWriter createPrintWriter(final String fileName, CsvTaxExportStateRedlist state)
183
					throws IOException, FileNotFoundException, UnsupportedEncodingException {
184

    
185
		OutputStream os;
186
		boolean useZip = state.isZip();
187
		if (useZip){
188
			os = state.getZipStream(fileName);
189
		}else{
190
			os = createFileOutputStream(state.getConfig(), fileName);
191
		}
192
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF8"), true);
193

    
194
		return writer;
195
	}
196

    
197

    
198

    
199

    
200
	/**
201
	 * Closes the writer
202
	 * @param writer
203
	 * @param state
204
	 */
205
	protected void closeWriter(PrintWriter writer, CsvTaxExportStateRedlist state) {
206
		if (writer != null && state.isZip() == false){
207
			writer.close();
208
		}
209
	}
210

    
211

    
212

    
213
	/**
214
	 * Closes the writer.
215
	 * Note: XMLStreamWriter does not close the underlying stream.
216
	 * @param writer
217
	 * @param state
218
	 */
219
	protected void closeWriter(XMLStreamWriter writer, CsvTaxExportStateRedlist state) {
220
		if (writer != null && state.isZip() == false){
221
			try {
222
				writer.close();
223
			} catch (XMLStreamException e) {
224
				throw new RuntimeException(e);
225
			}
226
		}
227
	}
228
	protected void clearExistingRecordIds(){
229
		existingRecordIds.clear();
230
	}
231
}
(1-1/10)