Project

General

Profile

« Previous | Next » 

Revision 3443c007

Added by Andreas Müller about 7 years ago

ref #6754 ref #6755 improve filtering and progress monitoring for DwCA export

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/common/CdmExportBase.java
10 10
package eu.etaxonomy.cdm.io.common;
11 11

  
12 12
import java.io.ByteArrayOutputStream;
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Set;
18
import java.util.UUID;
13 19

  
14 20
import org.apache.log4j.Logger;
21
import org.springframework.beans.factory.annotation.Autowired;
15 22

  
23
import eu.etaxonomy.cdm.api.service.IClassificationService;
24
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
25
import eu.etaxonomy.cdm.filter.TaxonNodeFilter;
16 26
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
27
import eu.etaxonomy.cdm.io.dwca.out.DwcaTaxExportState;
17 28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.taxon.Classification;
30
import eu.etaxonomy.cdm.model.taxon.Taxon;
31
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
32
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
18 33

  
19 34
/**
20 35
 * @author a.mueller
21 36
 * @created 01.07.2008
22 37
 */
23
public abstract class CdmExportBase<CONFIG extends IExportConfigurator<STATE, TRANSFORM>, STATE extends ExportStateBase, TRANSFORM extends IExportTransformer>
38
public abstract class CdmExportBase<CONFIG extends ExportConfiguratorBase<STATE, TRANSFORM, DEST>, STATE extends ExportStateBase, TRANSFORM extends IExportTransformer, DEST extends Object>
24 39
            extends CdmIoBase<STATE, ExportResult>
25 40
            implements ICdmExport<CONFIG, STATE>{
26 41

  
......
31 46
    protected ByteArrayOutputStream exportStream;
32 47

  
33 48

  
49
    @Autowired
50
    protected IClassificationService classificationService;
51

  
52
    @Autowired
53
    protected ITaxonNodeService taxonNodeService;
54

  
55

  
34 56
	@Override
35 57
	public  ExportDataWrapper createExportData() {
36 58
	    if (exportStream != null){
......
60 82
        return null;
61 83
    }
62 84

  
85

  
63 86
    public Object getDbId(CdmBase cdmBase, STATE state){
64 87
        logger.warn("Not yet implemented for export base class");
65 88
        return null;
66 89
    }
90

  
91

  
92
    /**
93
     * Returns the list of {@link TaxonNode taxon nodes} that correspond to the
94
     * given filter criteria (e.g. subtreeUUids). If no filter is given
95
     * all taxon nodes of all classifications are returned. If the list has been
96
     * computed before it is taken from the state cache. Nodes that do not have
97
     * a taxon attached are not returned. Instead a warning is given that the node is
98
     * ommitted (empty taxon nodes should not but do exist in CDM databases).
99
     * <BR>
100
     * Preliminary implementation. Better implement API method for this.
101
     */
102
    //TODO unify with similar methods for other exports
103
    protected List<TaxonNode> allNodes(DwcaTaxExportState state) {
104

  
105
        TaxonNodeFilter filter = state.getConfig().getTaxonNodeFilter();
106

  
107
        List<UUID> listUuid = taxonNodeService.uuidList(filter);
108

  
109
        //TODO memory critical to store ALL node
110
        if (state.getAllNodes().isEmpty()){
111
            makeAllNodes(state, listUuid);
112
        }
113
        List<TaxonNode> allNodes = state.getAllNodes();
114
        return allNodes;
115
    }
116

  
117

  
118
    protected void makeAllNodes(DwcaTaxExportState state, Collection<UUID> subtreeSet) {
119

  
120
        try {
121
            boolean doSynonyms = false;
122
            boolean recursive = true;
123
            Set<UUID> uuidSet = new HashSet<>();
124

  
125
            for (UUID subtreeUuid : subtreeSet){
126
                UUID tnUuuid = taxonNodeUuid(subtreeUuid);
127
                uuidSet.add(tnUuuid);
128
                List<TaxonNodeDto> records = getTaxonNodeService().pageChildNodesDTOs(tnUuuid,
129
                        recursive, doSynonyms, null, null, null).getRecords();
130
                for (TaxonNodeDto dto : records){
131
                    uuidSet.add(dto.getUuid());
132
                }
133
            }
134
            List<TaxonNode> allNodes =  getTaxonNodeService().find(uuidSet);
135

  
136
            List<TaxonNode> result = new ArrayList<>();
137
            for (TaxonNode node : allNodes){
138
                if(node.getParent()== null){  //root (or invalid) node
139
                    continue;
140
                }
141
                node = CdmBase.deproxy(node);
142
                Taxon taxon = CdmBase.deproxy(node.getTaxon());
143
                if (taxon == null){
144
                    String message = "There is a taxon node without taxon. id=" + node.getId();
145
                    state.getResult().addWarning(message);
146
                    continue;
147
                }
148
                result.add(node);
149
            }
150
            state.setAllNodes(result);
151
        } catch (Exception e) {
152
            String message = "Unexpected exception when trying to compute all taxon nodes";
153
            state.getResult().addException(e, message);
154
        }
155
    }
156

  
157

  
158
    /**
159
     * @param subtreeUuid
160
     * @return
161
     */
162
    private UUID taxonNodeUuid(UUID subtreeUuid) {
163
        TaxonNode node = taxonNodeService.find(subtreeUuid);
164
        if (node == null){
165
            Classification classification = classificationService.find(subtreeUuid);
166
            if (classification != null){
167
                node = classification.getRootNode();
168
            }else{
169
                throw new IllegalArgumentException("Subtree identifier does not exist: " + subtreeUuid);
170
            }
171
        }
172
        return node.getUuid();
173
    }
174

  
67 175
}

Also available in: Unified diff