Project

General

Profile

Download (3.88 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.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
 * @created 18.04.2011
33
 */
34
public class DwcaTaxExportState extends XmlExportState<DwcaTaxExportConfigurator>{
35
	@SuppressWarnings("unused")
36
	private static final Logger logger = Logger.getLogger(DwcaTaxExportState.class);
37

    
38
	private DwcaResultProcessor processor = new DwcaResultProcessor(this);
39

    
40
	private List<DwcaMetaDataRecord> metaRecords = new ArrayList<>();
41
	private boolean isZip = false;
42
	private ZipWriter zipWriter;
43
	private List<TaxonNode>  allNodes = new ArrayList<>();
44
	private Map<DwcaTaxExportFile, PrintWriter> writerMap = new HashMap<>();
45

    
46

    
47
    protected Map<DwcaTaxExportFile,Set<Integer>> existingRecordIds = new HashMap<>();
48
    protected Set<UUID> existingRecordUuids = new HashSet<>();
49

    
50
    protected boolean recordExists(DwcaTaxExportFile file, CdmBase cdmBase) {
51
        if (existingRecordIds.get(file) == null){
52
            return false;
53
        }else{
54
            return existingRecordIds.get(file).contains(cdmBase.getId());
55
        }
56
    }
57
    protected void addExistingRecord(DwcaTaxExportFile file, CdmBase cdmBase) {
58
        Set<Integer> set = existingRecordIds.get(file);
59
        if (set == null){
60
            set = new HashSet<>();
61
            existingRecordIds.put(file, set);
62
        }
63
        set.add(cdmBase.getId());
64
    }
65
    protected boolean recordExistsUuid(CdmBase cdmBase) {
66
        return existingRecordUuids.contains(cdmBase.getUuid());
67
    }
68
    protected void addExistingRecordUuid(CdmBase cdmBase) {
69
        existingRecordUuids.add(cdmBase.getUuid());
70
    }
71

    
72
	public DwcaTaxExportState(DwcaTaxExportConfigurator config) {
73
		super(config);
74
		File file = config.getDestination();
75
		if (file != null && ! file.isDirectory()){
76
			try{
77
				isZip = true;
78
				if (! file.exists()){
79
					boolean created = file.createNewFile();
80
				}
81

    
82
				zipWriter = new ZipWriter(file);
83
			} catch (IOException e) {
84
				throw new RuntimeException(e);
85
			}
86
		}
87
	}
88

    
89
	public void addMetaRecord(DwcaMetaDataRecord record){
90
		metaRecords.add(record);
91
	}
92

    
93
	public List<DwcaMetaDataRecord> getMetaRecords(){
94
		return metaRecords;
95
	}
96

    
97
	/**
98
	 * @return the isZip
99
	 */
100
	public boolean isZip() {
101
		return isZip;
102
	}
103

    
104
	public OutputStream getZipStream(String fileName){
105
		if (isZip){
106
			OutputStream os = zipWriter.getEntryStream(fileName);
107
			return os;
108
		}else{
109
			return null;
110
		}
111
	}
112

    
113
	public void closeZip() throws IOException {
114
		if (zipWriter != null){
115
		    zipWriter.close();
116
		}
117
	}
118

    
119
    /**
120
     * @return the allNodes
121
     */
122
    public List<TaxonNode> getAllNodes() {
123
        return allNodes;
124
    }
125

    
126
    /**
127
     * @param allNodes the allNodes to set
128
     */
129
    public void setAllNodes(List<TaxonNode> allNodes) {
130
        this.allNodes = allNodes;
131
    }
132

    
133
    public DwcaResultProcessor getProcessor() {
134
        return processor;
135
    }
136

    
137
    public PrintWriter getWriter(DwcaTaxExportFile file){
138
        return this.writerMap.get(file);
139
    }
140

    
141
    public PrintWriter putWriter(DwcaTaxExportFile file, PrintWriter writer){
142
        return this.writerMap.put(file, writer);
143
    }
144

    
145

    
146

    
147
}
(24-24/33)