Revision f104a204
Added by Andreas Müller almost 2 years ago
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/dwca/out/DwcaTaxExport.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.dwca.out; |
|
11 |
|
|
12 |
import java.io.FileNotFoundException; |
|
13 |
import java.io.IOException; |
|
14 |
import java.io.UnsupportedEncodingException; |
|
15 |
import java.util.Arrays; |
|
16 |
import java.util.List; |
|
17 |
|
|
18 |
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger; |
|
19 |
import org.springframework.beans.factory.annotation.Autowired; |
|
20 |
import org.springframework.stereotype.Component; |
|
21 |
|
|
22 |
import eu.etaxonomy.cdm.api.service.ITaxonNodeService; |
|
23 |
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor; |
|
24 |
import eu.etaxonomy.cdm.filter.TaxonNodeFilter; |
|
25 |
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitioner; |
|
26 |
import eu.etaxonomy.cdm.io.common.XmlExportState; |
|
27 |
import eu.etaxonomy.cdm.model.taxon.TaxonNode; |
|
28 |
|
|
29 |
/** |
|
30 |
* @author a.mueller |
|
31 |
* @since 18.04.2011 |
|
32 |
*/ |
|
33 |
@Component |
|
34 |
public class DwcaTaxExport extends DwcaExportBase { |
|
35 |
private static final long serialVersionUID = -3770976064909193441L; |
|
36 |
|
|
37 |
private static final Logger logger = LogManager.getLogger(DwcaTaxExport.class); |
|
38 |
|
|
39 |
|
|
40 |
@Autowired |
|
41 |
private ITaxonNodeService taxonNodeService; |
|
42 |
|
|
43 |
public DwcaTaxExport() { |
|
44 |
super(); |
|
45 |
this.ioName = this.getClass().getSimpleName(); |
|
46 |
file = DwcaTaxExportFile.TAXON; |
|
47 |
} |
|
48 |
|
|
49 |
|
|
50 |
/** |
|
51 |
* {@inheritDoc} |
|
52 |
*/ |
|
53 |
@Override |
|
54 |
public long countSteps(DwcaTaxExportState state) { |
|
55 |
TaxonNodeFilter filter = state.getConfig().getTaxonNodeFilter(); |
|
56 |
return taxonNodeService.count(filter); |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* Retrieves data from a CDM DB and serializes the CDM to XML. |
|
61 |
* Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships. |
|
62 |
* Taxa that are not part of the classification are not found. |
|
63 |
* <BR> |
|
64 |
* {@inheritDoc} |
|
65 |
*/ |
|
66 |
@Override |
|
67 |
protected void doInvoke(DwcaTaxExportState state){ |
|
68 |
|
|
69 |
IProgressMonitor monitor = state.getConfig().getProgressMonitor(); |
|
70 |
|
|
71 |
List<DwcaDataExportBase> exports = Arrays.asList(new DwcaDataExportBase[]{ |
|
72 |
new DwcaTaxonExport(state), |
|
73 |
new DwcaReferenceExport(state), |
|
74 |
new DwcaResourceRelationExport(state), |
|
75 |
new DwcaTypesExport(state), |
|
76 |
new DwcaVernacularExport(state), |
|
77 |
new DwcaDescriptionExport(state), |
|
78 |
new DwcaDistributionExport(state), |
|
79 |
new DwcaImageExport(state) |
|
80 |
}); |
|
81 |
|
|
82 |
|
|
83 |
@SuppressWarnings("unchecked") |
|
84 |
TaxonNodeOutStreamPartitioner<XmlExportState> partitioner |
|
85 |
= TaxonNodeOutStreamPartitioner.NewInstance( |
|
86 |
this, state, state.getConfig().getTaxonNodeFilter(), |
|
87 |
100, monitor, null); |
|
88 |
try { |
|
89 |
|
|
90 |
monitor.subTask("Start partitioning"); |
|
91 |
|
|
92 |
TaxonNode node = partitioner.next(); |
|
93 |
while (node != null){ |
|
94 |
for (DwcaDataExportBase export : exports){ |
|
95 |
handleTaxonNode(export, state, node); |
|
96 |
} |
|
97 |
node = partitioner.next(); |
|
98 |
} |
|
99 |
} catch (Exception e) { |
|
100 |
String message = "Unexpected exception: " + e.getMessage(); |
|
101 |
state.getResult().addException(e, message, "DwcaTaxExport.doInvoke()"); |
|
102 |
} |
|
103 |
finally{ |
|
104 |
if(partitioner != null){ |
|
105 |
partitioner.close(); |
|
106 |
} |
|
107 |
for (DwcaDataExportBase export : exports){ |
|
108 |
closeWriter(export, state); |
|
109 |
} |
|
110 |
} |
|
111 |
|
|
112 |
return; |
|
113 |
|
|
114 |
} |
|
115 |
|
|
116 |
/** |
|
117 |
* @param taxonExport |
|
118 |
* @param state |
|
119 |
*/ |
|
120 |
private void closeWriter(DwcaDataExportBase export, DwcaTaxExportState state) { |
|
121 |
if (!export.isIgnore(state)){ |
|
122 |
export.closeWriter(state); |
|
123 |
} |
|
124 |
} |
|
125 |
|
|
126 |
private void handleTaxonNode(DwcaDataExportBase export, DwcaTaxExportState state, TaxonNode node) throws FileNotFoundException, UnsupportedEncodingException, IOException { |
|
127 |
if (!export.isIgnore(state)){ |
|
128 |
export.handleTaxonNode(state, node); |
|
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
|
|
133 |
@Override |
|
134 |
protected boolean doCheck(DwcaTaxExportState state) { |
|
135 |
boolean result = true; |
|
136 |
logger.warn("No check implemented for " + this.ioName); |
|
137 |
return result; |
|
138 |
} |
|
139 |
|
|
140 |
|
|
141 |
@Override |
|
142 |
public boolean isIgnore(DwcaTaxExportState state) { |
|
143 |
return ! state.getConfig().isDoTaxa(); |
|
144 |
} |
|
145 |
|
|
146 |
} |
|
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.dwca.out; |
|
11 |
|
|
12 |
import java.io.FileNotFoundException; |
|
13 |
import java.io.IOException; |
|
14 |
import java.io.UnsupportedEncodingException; |
|
15 |
import java.util.Arrays; |
|
16 |
import java.util.List; |
|
17 |
|
|
18 |
import org.apache.logging.log4j.LogManager; |
|
19 |
import org.apache.logging.log4j.Logger; |
|
20 |
import org.springframework.beans.factory.annotation.Autowired; |
|
21 |
import org.springframework.stereotype.Component; |
|
22 |
|
|
23 |
import eu.etaxonomy.cdm.api.service.ITaxonNodeService; |
|
24 |
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor; |
|
25 |
import eu.etaxonomy.cdm.filter.TaxonNodeFilter; |
|
26 |
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitioner; |
|
27 |
import eu.etaxonomy.cdm.io.common.XmlExportState; |
|
28 |
import eu.etaxonomy.cdm.model.taxon.TaxonNode; |
|
29 |
|
|
30 |
/** |
|
31 |
* @author a.mueller |
|
32 |
* @since 18.04.2011 |
|
33 |
*/ |
|
34 |
@Component |
|
35 |
public class DwcaTaxExport extends DwcaExportBase { |
|
36 |
private static final long serialVersionUID = -3770976064909193441L; |
|
37 |
|
|
38 |
private static final Logger logger = LogManager.getLogger(DwcaTaxExport.class); |
|
39 |
|
|
40 |
|
|
41 |
@Autowired |
|
42 |
private ITaxonNodeService taxonNodeService; |
|
43 |
|
|
44 |
public DwcaTaxExport() { |
|
45 |
super(); |
|
46 |
this.ioName = this.getClass().getSimpleName(); |
|
47 |
file = DwcaTaxExportFile.TAXON; |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public long countSteps(DwcaTaxExportState state) { |
|
52 |
TaxonNodeFilter filter = state.getConfig().getTaxonNodeFilter(); |
|
53 |
return taxonNodeService.count(filter); |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* Retrieves data from a CDM DB and serializes the CDM to XML. |
|
58 |
* Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships. |
|
59 |
* Taxa that are not part of the classification are not found. |
|
60 |
* <BR> |
|
61 |
* {@inheritDoc} |
|
62 |
*/ |
|
63 |
@Override |
|
64 |
protected void doInvoke(DwcaTaxExportState state){ |
|
65 |
|
|
66 |
IProgressMonitor monitor = state.getConfig().getProgressMonitor(); |
|
67 |
|
|
68 |
List<DwcaDataExportBase> exports = Arrays.asList(new DwcaDataExportBase[]{ |
|
69 |
new DwcaTaxonExport(state), |
|
70 |
new DwcaReferenceExport(state), |
|
71 |
new DwcaResourceRelationExport(state), |
|
72 |
new DwcaTypesExport(state), |
|
73 |
new DwcaVernacularExport(state), |
|
74 |
new DwcaDescriptionExport(state), |
|
75 |
new DwcaDistributionExport(state), |
|
76 |
new DwcaImageExport(state) |
|
77 |
}); |
|
78 |
|
|
79 |
|
|
80 |
@SuppressWarnings("unchecked") |
|
81 |
TaxonNodeOutStreamPartitioner<XmlExportState> partitioner |
|
82 |
= TaxonNodeOutStreamPartitioner.NewInstance( |
|
83 |
this, state, state.getConfig().getTaxonNodeFilter(), |
|
84 |
100, monitor, null); |
|
85 |
try { |
|
86 |
|
|
87 |
monitor.subTask("Start partitioning"); |
|
88 |
|
|
89 |
TaxonNode node = partitioner.next(); |
|
90 |
while (node != null){ |
|
91 |
for (DwcaDataExportBase export : exports){ |
|
92 |
handleTaxonNode(export, state, node); |
|
93 |
} |
|
94 |
node = partitioner.next(); |
|
95 |
} |
|
96 |
} catch (Exception e) { |
|
97 |
String message = "Unexpected exception: " + e.getMessage(); |
|
98 |
state.getResult().addException(e, message, "DwcaTaxExport.doInvoke()"); |
|
99 |
} |
|
100 |
finally{ |
|
101 |
if(partitioner != null){ |
|
102 |
partitioner.close(); |
|
103 |
} |
|
104 |
for (DwcaDataExportBase export : exports){ |
|
105 |
closeWriter(export, state); |
|
106 |
} |
|
107 |
} |
|
108 |
|
|
109 |
return; |
|
110 |
|
|
111 |
} |
|
112 |
|
|
113 |
/** |
|
114 |
* @param taxonExport |
|
115 |
* @param state |
|
116 |
*/ |
|
117 |
private void closeWriter(DwcaDataExportBase export, DwcaTaxExportState state) { |
|
118 |
if (!export.isIgnore(state)){ |
|
119 |
export.closeWriter(state); |
|
120 |
} |
|
121 |
} |
|
122 |
|
|
123 |
private void handleTaxonNode(DwcaDataExportBase export, DwcaTaxExportState state, TaxonNode node) throws FileNotFoundException, UnsupportedEncodingException, IOException { |
|
124 |
if (!export.isIgnore(state)){ |
|
125 |
export.handleTaxonNode(state, node); |
|
126 |
} |
|
127 |
} |
|
128 |
|
|
129 |
|
|
130 |
@Override |
|
131 |
protected boolean doCheck(DwcaTaxExportState state) { |
|
132 |
boolean result = true; |
|
133 |
logger.warn("No check implemented for " + this.ioName); |
|
134 |
return result; |
|
135 |
} |
|
136 |
|
|
137 |
|
|
138 |
@Override |
|
139 |
public boolean isIgnore(DwcaTaxExportState state) { |
|
140 |
return ! state.getConfig().isDoTaxa(); |
|
141 |
} |
|
142 |
|
|
143 |
} |
Also available in: Unified diff
cleanup