Statistics
| Revision:

gvsig-projects-pool / org.gvsig.vcsgis / trunk / org.gvsig.vcsgis / org.gvsig.vcsgis.swing / org.gvsig.vcsgis.swing.impl / src / main / java / org / gvsig / vcsgis / swing / impl / importhistory / process / ImportHistoryWithRevision_minimize_memory.java @ 5018

History | View | Annotate | Download (4.66 KB)

1
package org.gvsig.vcsgis.swing.impl.importhistory.process;
2

    
3
import org.apache.commons.lang3.StringUtils;
4
import org.gvsig.expressionevaluator.Expression;
5
import org.gvsig.expressionevaluator.ExpressionBuilder;
6
import org.gvsig.expressionevaluator.ExpressionUtils;
7
import org.gvsig.fmap.dal.exception.DataException;
8
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
9
import org.gvsig.fmap.dal.feature.FeatureQuery;
10
import org.gvsig.fmap.dal.feature.FeatureSet;
11
import org.gvsig.fmap.dal.feature.FeatureStore;
12
import org.gvsig.tools.dispose.DisposeUtils;
13
import org.gvsig.tools.task.SimpleTaskStatus;
14
import org.gvsig.vcsgis.lib.VCSGisEntity;
15
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_EXCEPTION;
16
import static org.gvsig.vcsgis.lib.VCSGisManager.ERR_OK;
17
import org.gvsig.vcsgis.lib.workspace.VCSGisWorkspace;
18
import org.gvsig.vcsgis.swing.impl.importhistory.AbstractImportProcess;
19

    
20
/**
21
 *
22
 * @author gvSIG Team
23
 */
24
@SuppressWarnings("UseSpecificCatch")
25
public class ImportHistoryWithRevision_minimize_memory extends AbstractImportProcess {
26

    
27
    protected String fieldEfectiveDateName;
28
    protected String fieldRevisionName;
29

    
30
    private String sourceFieldRevisionName;
31
    private String sourceFieldEfectiveDateName;
32
    
33
    public ImportHistoryWithRevision_minimize_memory(
34
            boolean onlyLocal,
35
            VCSGisWorkspace workspace,
36
            FeatureStore sourceStore,
37
            VCSGisEntity targetEntity,
38
            String fieldCodeName,
39
            String fieldRevisionName,
40
            String fieldEfectiveDateName,
41
            String fieldOrderName,
42
            Expression deletedCondition,
43
            SimpleTaskStatus status
44
    ) {
45
        super(
46
                onlyLocal,
47
                workspace, 
48
                sourceStore, 
49
                targetEntity, 
50
                fieldCodeName, 
51
                fieldOrderName, 
52
                deletedCondition, 
53
                status
54
        );
55
        this.fieldEfectiveDateName = fieldEfectiveDateName;
56
        this.fieldRevisionName = fieldRevisionName;
57
    }
58

    
59
    @Override
60
    protected FeatureQuery getSourceQuery() throws DataException {
61
        FeatureQuery sourceQuery = sourceStore.createFeatureQuery();
62
        ExpressionBuilder expBuilder = ExpressionUtils.createExpressionBuilder();
63
        sourceQuery.getOrder().add(sourceFieldRevisionName, true);
64
        
65
        String sourceFieldOrderName = this.getSourceFieldOrderName();
66
        if (sourceFieldOrderName!=null ) {
67
            sourceQuery.getOrder().add(sourceFieldOrderName, true);
68
        }
69
        return sourceQuery;
70
    }
71
    
72
    @Override
73
    public int process() {
74
        if (StringUtils.isBlank(fieldRevisionName)) {
75
            LOGGER.warn("fieldRevisionName can't be blank,");
76
            return ERR_EXCEPTION;
77
        }
78
        
79
        FeatureSet sourceFeatures = null;
80
        
81
        try {
82
            status.message("Preparing data...");
83
            
84
            FeatureAttributeDescriptor sourceAttrRevisionName = getSourceFeatureType().getAttributeDescriptor(fieldRevisionName);
85
            if (sourceAttrRevisionName == null) {
86
                LOGGER.warn("Field '" + fieldRevisionName + "' (fieldRevisionName) has not been found in the source table.");
87
                return ERR_EXCEPTION;
88
            }
89
            sourceFieldRevisionName = sourceAttrRevisionName.getName();
90
            if( StringUtils.isNotBlank(fieldEfectiveDateName) ) {
91
                sourceFieldEfectiveDateName = getSourceFeatureType().getAttributeDescriptor(fieldEfectiveDateName).getName();
92
            }
93
            
94
            FeatureQuery theSourceQuery = this.getSourceQuery();
95
            sourceFeatures = sourceStore.getFeatureSet(theSourceQuery);
96

    
97
            sourceSize = sourceFeatures.size64();
98

    
99
            status.setRangeOfValues(0, sourceSize);
100
            status.setCurValue(0);
101

    
102
            int result = this.process_minimize_memory(
103
                    sourceFeatures, 
104
                    createGroupByRevisionNumber(sourceFieldRevisionName), 
105
                    sourceFieldEfectiveDateName
106
            );
107
            if (result != ERR_OK) {
108
                status.abort();
109
                return result;
110
            }            
111
            status.message("Finished.");
112
            status.terminate();
113
            if (this.hasDuplicatedDeletes()) {
114
                return ERR_EXCEPTION;
115
            }
116
            return ERR_OK;
117

    
118
        } catch (Exception ex) {
119
            LOGGER.warn("Can't import history", ex);
120
            status.abort();
121
            FeatureStore.cancelEditingQuietly(getTargetStore());
122
            return ERR_EXCEPTION;
123

    
124
        } finally {
125
            DisposeUtils.disposeQuietly(sourceFeatures);
126
            disposeTargetStore();
127
        }
128
    }
129
        
130
    
131
}