1
|
/**
|
2
|
* Copyright (C) 2019 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.pesi.fauEu2Cdm;
|
10
|
|
11
|
import java.util.HashSet;
|
12
|
import java.util.Set;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.springframework.stereotype.Component;
|
16
|
import org.springframework.transaction.TransactionStatus;
|
17
|
|
18
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
19
|
import eu.etaxonomy.cdm.io.common.ITaxonNodeOutStreamPartitioner;
|
20
|
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitioner;
|
21
|
import eu.etaxonomy.cdm.io.common.TaxonNodeOutStreamPartitionerConcurrent;
|
22
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
23
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
24
|
|
25
|
/**
|
26
|
* @author a.mueller
|
27
|
* @since 17.08.2019
|
28
|
*/
|
29
|
@Component
|
30
|
public class FauEu2CdmDescriptionImport
|
31
|
extends FauEu2CdmImportBase {
|
32
|
|
33
|
private static final long serialVersionUID = -2111102574346601573L;
|
34
|
private static final Logger logger = Logger.getLogger(FauEu2CdmDescriptionImport.class);
|
35
|
|
36
|
|
37
|
@Override
|
38
|
protected void doInvoke(FauEu2CdmImportState state) {
|
39
|
setState(state);
|
40
|
IProgressMonitor monitor = state.getConfig().getProgressMonitor();
|
41
|
|
42
|
FauEu2CdmImportConfigurator config = state.getConfig();
|
43
|
|
44
|
ITaxonNodeOutStreamPartitioner partitioner = getPartitioner(state, monitor, config);
|
45
|
monitor.subTask("Start partitioning");
|
46
|
doData(state, partitioner);
|
47
|
}
|
48
|
|
49
|
private void doData(FauEu2CdmImportState state, ITaxonNodeOutStreamPartitioner partitioner){
|
50
|
TaxonNode node = partitioner.next();
|
51
|
int partitionSize = 100;
|
52
|
int count = 0;
|
53
|
TransactionStatus tx = startTransaction();
|
54
|
while (node != null) {
|
55
|
doSingleNode(state, node);
|
56
|
count++;
|
57
|
if (count>=partitionSize){
|
58
|
clearCache();
|
59
|
try {
|
60
|
commitTransaction(tx);
|
61
|
} catch (Exception e) {
|
62
|
logger.warn("Exception during commit node " + node.treeIndex());
|
63
|
e.printStackTrace();
|
64
|
}
|
65
|
tx = startTransaction();
|
66
|
count=0;
|
67
|
}
|
68
|
node = partitioner.next();
|
69
|
}
|
70
|
commitTransaction(tx);
|
71
|
partitioner.close();
|
72
|
}
|
73
|
|
74
|
private void doSingleNode(FauEu2CdmImportState state, TaxonNode node) {
|
75
|
Set<TaxonDescription> result = new HashSet<>();
|
76
|
logger.info(node.treeIndex());
|
77
|
try {
|
78
|
for (TaxonDescription desc : node.getTaxon().getDescriptions()){
|
79
|
result.add(detache(desc));
|
80
|
}
|
81
|
} catch (Exception e) {
|
82
|
logger.warn("Exception during detache node " + node.treeIndex());
|
83
|
e.printStackTrace();
|
84
|
}
|
85
|
try {
|
86
|
if (!result.isEmpty()){
|
87
|
getDescriptionService().saveOrUpdate((Set)result);
|
88
|
getCommonService().saveOrUpdate(toSave);
|
89
|
toSave.clear();
|
90
|
}
|
91
|
} catch (Exception e) {
|
92
|
logger.warn("Exception during save node " + node.treeIndex());
|
93
|
e.printStackTrace();
|
94
|
}
|
95
|
}
|
96
|
|
97
|
private ITaxonNodeOutStreamPartitioner getPartitioner(FauEu2CdmImportState state, IProgressMonitor monitor,
|
98
|
FauEu2CdmImportConfigurator config) {
|
99
|
ITaxonNodeOutStreamPartitioner partitioner = config.getPartitioner();
|
100
|
if (partitioner == null){
|
101
|
if(!config.isConcurrent()){
|
102
|
partitioner = TaxonNodeOutStreamPartitioner.NewInstance(sourceRepo(state), state,
|
103
|
state.getConfig().getTaxonNodeFilter(), 100,
|
104
|
monitor, 1, TaxonNodeOutStreamPartitioner.fullPropertyPaths);
|
105
|
((TaxonNodeOutStreamPartitioner)partitioner).setLastCommitManually(true);
|
106
|
}else{
|
107
|
partitioner = TaxonNodeOutStreamPartitionerConcurrent
|
108
|
.NewInstance(state.getConfig().getSource(), state.getConfig().getTaxonNodeFilter(),
|
109
|
1000, monitor, 1, TaxonNodeOutStreamPartitioner.fullPropertyPaths);
|
110
|
}
|
111
|
}
|
112
|
return partitioner;
|
113
|
}
|
114
|
|
115
|
|
116
|
@Override
|
117
|
protected boolean doDescriptions(FauEu2CdmImportState state) {
|
118
|
return true;
|
119
|
}
|
120
|
|
121
|
@Override
|
122
|
protected boolean doCheck(FauEu2CdmImportState state) {
|
123
|
return false;
|
124
|
}
|
125
|
|
126
|
@Override
|
127
|
protected boolean isIgnore(FauEu2CdmImportState state) {
|
128
|
return !state.getConfig().isDoDescriptions();
|
129
|
}
|
130
|
|
131
|
}
|