Project

General

Profile

Download (3.42 KB) Statistics
| Branch: | Tag: | Revision:
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.cdm2cdm;
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.model.description.TaxonDescription;
21
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
22

    
23
/**
24
 * @author a.mueller
25
 * @since 17.08.2019
26
 */
27
@Component
28
public class Cdm2CdmDescriptionImport
29
        extends Cdm2CdmImportBase {
30

    
31
    private static final long serialVersionUID = -2111102574346601573L;
32
    private static final Logger logger = Logger.getLogger(Cdm2CdmDescriptionImport.class);
33

    
34
    @Override
35
    protected void doInvoke(Cdm2CdmImportState state) {
36
        IProgressMonitor monitor = state.getConfig().getProgressMonitor();
37

    
38
        Cdm2CdmImportConfigurator config = state.getConfig();
39

    
40
        ITaxonNodeOutStreamPartitioner partitioner = getTaxonNodePartitioner(state, monitor, config);
41
        monitor.subTask("Start partitioning");
42
        doData(state, partitioner);
43
    }
44

    
45
    private void doData(Cdm2CdmImportState state, ITaxonNodeOutStreamPartitioner partitioner){
46
        TaxonNode node = partitioner.next();
47
        int partitionSize = 100;
48
        int count = 0;
49
        TransactionStatus tx = startTransaction();
50
        while (node != null) {
51
            doSingleNode(state, node);
52
            count++;
53
            if (count>=partitionSize){
54
                state.clearSessionCache();
55
                try {
56
                    commitTransaction(tx);
57
                } catch (Exception e) {
58
                    logger.warn("Exception during commit node " + node.treeIndex());
59
                    e.printStackTrace();
60
                }
61
                tx = startTransaction();
62
                count=0;
63
            }
64
            node = partitioner.next();
65
        }
66
        commitTransaction(tx);
67
        partitioner.close();
68
    }
69

    
70
    private void doSingleNode(Cdm2CdmImportState state, TaxonNode node) {
71
        Set<TaxonDescription> result = new HashSet<>();
72
        logger.info(node.treeIndex());
73
        try {
74
            for (TaxonDescription desc : node.getTaxon().getDescriptions()){
75
                result.add(detache(desc, state));
76
            }
77
        } catch (Exception e) {
78
            logger.warn("Exception during detache node " + node.treeIndex());
79
            e.printStackTrace();
80
        }
81
        try {
82
            if (!result.isEmpty()){
83
                getDescriptionService().saveOrUpdate((Set)result);
84
                getCommonService().saveOrUpdate(state.getToSave());
85
                state.clearToSave();
86
            }
87
        } catch (Exception e) {
88
            logger.warn("Exception during save node " + node.treeIndex());
89
             e.printStackTrace();
90
        }
91
    }
92

    
93

    
94

    
95
    @Override
96
    protected boolean doDescriptions(Cdm2CdmImportState state) {
97
        return true;
98
    }
99

    
100
    @Override
101
    protected boolean doCheck(Cdm2CdmImportState state) {
102
        return true;
103
    }
104

    
105
    @Override
106
    protected boolean isIgnore(Cdm2CdmImportState state) {
107
        return !state.getConfig().isDoDescriptions();
108
    }
109

    
110
}
(1-1/6)