cleanup
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / csv / redlist / demo / CsvDemoBase.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.demo;
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.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23 import java.util.UUID;
24
25 import javax.xml.stream.XMLOutputFactory;
26 import javax.xml.stream.XMLStreamException;
27 import javax.xml.stream.XMLStreamWriter;
28
29 import org.apache.commons.lang.StringUtils;
30 import org.apache.log4j.Logger;
31
32 import eu.etaxonomy.cdm.common.CdmUtils;
33 import eu.etaxonomy.cdm.io.common.CdmExportBase;
34 import eu.etaxonomy.cdm.io.common.ICdmExport;
35 import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
36 import eu.etaxonomy.cdm.io.csv.redlist.out.CsvTaxExportConfiguratorRedlist;
37 import eu.etaxonomy.cdm.io.csv.redlist.out.CsvTaxExportStateRedlist;
38 import eu.etaxonomy.cdm.model.common.CdmBase;
39 import eu.etaxonomy.cdm.model.common.IOriginalSource;
40 import eu.etaxonomy.cdm.model.common.ISourceable;
41 import eu.etaxonomy.cdm.model.location.Country;
42 import eu.etaxonomy.cdm.model.location.NamedArea;
43 import eu.etaxonomy.cdm.model.taxon.Classification;
44 import eu.etaxonomy.cdm.model.taxon.Taxon;
45 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
46 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
47
48 /**
49 * @author a.mueller, a.oppermann
50 * @date 18.10.2012
51 *
52 */
53 public abstract class CsvDemoBase extends CdmExportBase<CsvDemoExportConfigurator, CsvDemoExportState, IExportTransformer> implements ICdmExport<CsvDemoExportConfigurator, CsvDemoExportState>{
54 private static final long serialVersionUID = -2962456879635841019L;
55
56 private static final Logger logger = Logger.getLogger(CsvDemoBase.class);
57
58 protected static final boolean IS_CORE = true;
59
60
61 protected Set<Integer> existingRecordIds = new HashSet<>();
62 protected Set<UUID> existingRecordUuids = new HashSet<>();
63
64
65 @Override
66 public int countSteps() {
67 List<TaxonNode> allNodes = getClassificationService().getAllNodes();
68 return allNodes.size();
69 }
70
71
72
73 /**
74 * Returns the list of {@link TaxonNode taxon nodes} that are part in one of the given {@link Classification classifications}
75 * and do have a {@link Taxon} attached (empty taxon nodes should not but do exist in CDM databases).
76 * If <code>classificationList</code> is <code>null</code> or empty then all {@link TaxonNode taxon nodes} of all
77 * {@link Classification classifications} are returned.<BR>
78 * Preliminary implementation. Better implement API method for this.
79 * @return
80 */
81 protected List<TaxonNode> getAllNodes(Set<Classification> classificationList) {
82 //handle empty list as no filter defined
83 if (classificationList != null && classificationList.isEmpty()){
84 classificationList = null;
85 }
86
87 List<TaxonNode> allNodes = getClassificationService().getAllNodes();
88 List<TaxonNode> result = new ArrayList<TaxonNode>();
89 for (TaxonNode node : allNodes){
90 if (node.getClassification() == null ){
91 continue;
92 }else if (classificationList != null && ! classificationList.contains(node.getClassification())){
93 continue;
94 }else{
95 Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
96 if (taxon == null){
97 String message = "There is a taxon node without taxon: " + node.getId();
98 logger.warn(message);
99 continue;
100 }
101 result.add(node);
102 }
103 }
104 return result;
105 }
106
107
108 /**
109 * Creates the locationId, locality, countryCode triple
110 * @param record
111 * @param area
112 */
113 protected void handleArea(ICsvAreaRecord record, NamedArea area, TaxonBase<?> taxon, boolean required) {
114 if (area != null){
115 record.setLocationId(area);
116 record.setLocality(area.getLabel());
117 if (area.isInstanceOf(Country.class)){
118 Country country = CdmBase.deproxy(area, Country.class);
119 record.setCountryCode(country.getIso3166_A2());
120 }
121 }else{
122 if (required){
123 String message = "Description requires area but area does not exist for taxon " + getTaxonLogString(taxon);
124 logger.warn(message);
125 }
126 }
127 }
128
129
130 protected String getTaxonLogString(TaxonBase<?> taxon) {
131 return taxon.getTitleCache() + "(" + taxon.getId() + ")";
132 }
133
134
135 /**
136 * @param el
137 * @return
138 */
139 protected boolean recordExists(CdmBase el) {
140 return existingRecordIds.contains(el.getId());
141 }
142
143
144 /**
145 * @param sec
146 */
147 protected void addExistingRecord(CdmBase cdmBase) {
148 existingRecordIds.add(cdmBase.getId());
149 }
150
151 /**
152 * @param el
153 * @return
154 */
155 protected boolean recordExistsUuid(CdmBase el) {
156 return existingRecordUuids.contains(el.getUuid());
157 }
158
159 /**
160 * @param sec
161 */
162 protected void addExistingRecordUuid(CdmBase cdmBase) {
163 existingRecordUuids.add(cdmBase.getUuid());
164 }
165
166
167 protected String getSources(ISourceable<?> sourceable, CsvTaxExportConfiguratorRedlist config) {
168 String result = "";
169 for (IOriginalSource source: sourceable.getSources()){
170 if (StringUtils.isBlank(source.getIdInSource())){//idInSource indicates that this source is only data provenance, may be changed in future
171 if (source.getCitation() != null){
172 String ref = source.getCitation().getTitleCache();
173 result = CdmUtils.concat(config.getSetSeparator(), result, ref);
174 }
175 }
176 }
177 return result;
178 }
179
180
181 /**
182 * @param config
183 * @return
184 * @throws IOException
185 * @throws FileNotFoundException
186 */
187 protected FileOutputStream createFileOutputStream(CsvTaxExportConfiguratorRedlist config, String thisFileName) throws IOException, FileNotFoundException {
188 String filePath = config.getDestinationNameString();
189 String fileName = filePath + File.separatorChar + thisFileName;
190 File f = new File(fileName);
191 if (!f.exists()){
192 f.createNewFile();
193 }
194 FileOutputStream fos = new FileOutputStream(f);
195 return fos;
196 }
197
198
199 /**
200 * @param config
201 * @param factory
202 * @return
203 * @throws IOException
204 * @throws FileNotFoundException
205 * @throws XMLStreamException
206 */
207 protected XMLStreamWriter createXmlStreamWriter(CsvTaxExportStateRedlist state, String fileName)
208 throws IOException, FileNotFoundException, XMLStreamException {
209 XMLOutputFactory factory = XMLOutputFactory.newInstance();
210 OutputStream os;
211 boolean useZip = state.isZip();
212 if (useZip){
213 os = state.getZipStream(fileName);
214 }else{
215 os = createFileOutputStream(state.getConfig(), fileName);
216 }
217 XMLStreamWriter writer = factory.createXMLStreamWriter(os);
218 return writer;
219 }
220
221
222 /**
223 * @param coreTaxFileName
224 * @param config
225 * @return
226 * @throws IOException
227 * @throws FileNotFoundException
228 * @throws UnsupportedEncodingException
229 */
230 protected PrintWriter createPrintWriter(final String fileName, CsvTaxExportStateRedlist state)
231 throws IOException, FileNotFoundException, UnsupportedEncodingException {
232
233 OutputStream os;
234 boolean useZip = state.isZip();
235 if (useZip){
236 os = state.getZipStream(fileName);
237 }else{
238 os = createFileOutputStream(state.getConfig(), fileName);
239 }
240 PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF8"), true);
241
242 return writer;
243 }
244
245
246
247
248 /**
249 * Closes the writer
250 * @param writer
251 * @param state
252 */
253 protected void closeWriter(PrintWriter writer, CsvTaxExportStateRedlist state) {
254 if (writer != null && state.isZip() == false){
255 writer.close();
256 }
257 }
258
259
260
261 /**
262 * Closes the writer.
263 * Note: XMLStreamWriter does not close the underlying stream.
264 * @param writer
265 * @param state
266 */
267 protected void closeWriter(XMLStreamWriter writer, CsvTaxExportStateRedlist state) {
268 if (writer != null && state.isZip() == false){
269 try {
270 writer.close();
271 } catch (XMLStreamException e) {
272 throw new RuntimeException(e);
273 }
274 }
275 }
276 protected void clearExistingRecordIds(){
277 existingRecordIds.clear();
278 }
279 }