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.dwca.out;
|
10
|
|
11
|
import java.io.File;
|
12
|
import java.io.IOException;
|
13
|
import java.io.OutputStream;
|
14
|
import java.io.PrintWriter;
|
15
|
import java.util.ArrayList;
|
16
|
import java.util.HashMap;
|
17
|
import java.util.HashSet;
|
18
|
import java.util.List;
|
19
|
import java.util.Map;
|
20
|
import java.util.Set;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
import org.apache.log4j.Logger;
|
24
|
|
25
|
import eu.etaxonomy.cdm.io.common.XmlExportState;
|
26
|
import eu.etaxonomy.cdm.io.common.ZipWriter;
|
27
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
28
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
29
|
|
30
|
/**
|
31
|
* @author a.mueller
|
32
|
* @since 18.04.2011
|
33
|
*/
|
34
|
public class DwcaTaxExportState extends XmlExportState<DwcaTaxExportConfigurator>{
|
35
|
|
36
|
@SuppressWarnings("unused")
|
37
|
private static final Logger logger = Logger.getLogger(DwcaTaxExportState.class);
|
38
|
|
39
|
private DwcaResultProcessor processor = new DwcaResultProcessor(this);
|
40
|
|
41
|
private List<DwcaMetaDataRecord> metaRecords = new ArrayList<>();
|
42
|
private boolean isZip = false;
|
43
|
private ZipWriter zipWriter;
|
44
|
private List<TaxonNode> allNodes = new ArrayList<>();
|
45
|
private Map<DwcaTaxExportFile, PrintWriter> writerMap = new HashMap<>();
|
46
|
|
47
|
|
48
|
protected Map<DwcaTaxExportFile,Set<Integer>> existingRecordIds = new HashMap<>();
|
49
|
protected Set<UUID> existingRecordUuids = new HashSet<>();
|
50
|
|
51
|
protected boolean recordExists(DwcaTaxExportFile file, CdmBase cdmBase) {
|
52
|
if (existingRecordIds.get(file) == null){
|
53
|
return false;
|
54
|
}else{
|
55
|
return existingRecordIds.get(file).contains(cdmBase.getId());
|
56
|
}
|
57
|
}
|
58
|
protected void addExistingRecord(DwcaTaxExportFile file, CdmBase cdmBase) {
|
59
|
Set<Integer> set = existingRecordIds.get(file);
|
60
|
if (set == null){
|
61
|
set = new HashSet<>();
|
62
|
existingRecordIds.put(file, set);
|
63
|
}
|
64
|
set.add(cdmBase.getId());
|
65
|
}
|
66
|
protected boolean recordExistsUuid(CdmBase cdmBase) {
|
67
|
return existingRecordUuids.contains(cdmBase.getUuid());
|
68
|
}
|
69
|
protected void addExistingRecordUuid(CdmBase cdmBase) {
|
70
|
existingRecordUuids.add(cdmBase.getUuid());
|
71
|
}
|
72
|
|
73
|
public DwcaTaxExportState(DwcaTaxExportConfigurator config) {
|
74
|
super(config);
|
75
|
File file = config.getDestination();
|
76
|
if (file != null && ! file.isDirectory()){
|
77
|
try{
|
78
|
isZip = true;
|
79
|
if (! file.exists()){
|
80
|
boolean created = file.createNewFile();
|
81
|
}
|
82
|
|
83
|
zipWriter = new ZipWriter(file);
|
84
|
} catch (IOException e) {
|
85
|
throw new RuntimeException(e);
|
86
|
}
|
87
|
}
|
88
|
}
|
89
|
|
90
|
public void addMetaRecord(DwcaMetaDataRecord record){
|
91
|
metaRecords.add(record);
|
92
|
}
|
93
|
|
94
|
public List<DwcaMetaDataRecord> getMetaRecords(){
|
95
|
return metaRecords;
|
96
|
}
|
97
|
|
98
|
/**
|
99
|
* @return the isZip
|
100
|
*/
|
101
|
public boolean isZip() {
|
102
|
return isZip;
|
103
|
}
|
104
|
|
105
|
public OutputStream getZipStream(String fileName){
|
106
|
if (isZip){
|
107
|
OutputStream os = zipWriter.getEntryStream(fileName);
|
108
|
return os;
|
109
|
}else{
|
110
|
return null;
|
111
|
}
|
112
|
}
|
113
|
|
114
|
public void closeZip() throws IOException {
|
115
|
if (zipWriter != null){
|
116
|
zipWriter.close();
|
117
|
}
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* @return the allNodes
|
122
|
*/
|
123
|
public List<TaxonNode> getAllNodes() {
|
124
|
return allNodes;
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* @param allNodes the allNodes to set
|
129
|
*/
|
130
|
public void setAllNodes(List<TaxonNode> allNodes) {
|
131
|
this.allNodes = allNodes;
|
132
|
}
|
133
|
|
134
|
public DwcaResultProcessor getProcessor() {
|
135
|
return processor;
|
136
|
}
|
137
|
|
138
|
public PrintWriter getWriter(DwcaTaxExportFile file){
|
139
|
return this.writerMap.get(file);
|
140
|
}
|
141
|
|
142
|
public PrintWriter putWriter(DwcaTaxExportFile file, PrintWriter writer){
|
143
|
return this.writerMap.put(file, writer);
|
144
|
}
|
145
|
|
146
|
|
147
|
|
148
|
}
|