Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.exportto / org.gvsig.exportto.lib / org.gvsig.exportto.lib.impl / src / main / java / org / gvsig / export / impl / DefaultExportProcess.java @ 44069

History | View | Annotate | Download (5.46 KB)

1
package org.gvsig.export.impl;
2

    
3
import java.util.List;
4
import org.apache.commons.lang3.StringUtils;
5
import org.cresques.cts.ICoordTrans;
6
import org.cresques.cts.IProjection;
7
import org.gvsig.export.ExportException;
8
import org.gvsig.export.ExportLocator;
9
import org.gvsig.export.ExportParameters;
10
import org.gvsig.export.ExportParametersGeometry;
11
import org.gvsig.export.ExportProcess;
12
import org.gvsig.export.spi.ExportService;
13
import org.gvsig.export.spi.ExportServiceFactory;
14
import org.gvsig.export.spi.ExportServiceManager;
15
import org.gvsig.fmap.dal.OpenDataStoreParameters;
16
import org.gvsig.fmap.dal.feature.FeatureSet;
17
import org.gvsig.fmap.dal.feature.FeatureStore;
18
import org.gvsig.tools.task.TaskStatus;
19

    
20
/**
21
 *
22
 * @author jjdelcerro
23
 */
24
public class DefaultExportProcess 
25
        implements ExportProcess 
26
    {
27
    private ExportServiceFactory factory;
28
    private ExportService service;
29
    private ExportParameters parameters;
30
    private FeatureStore sourceFeatureStore;
31
    
32
    private IProjection contextProjection;
33
    private ICoordTrans sourceTransformation;
34
    private IProjection sourceProjection;
35
    private Object context;
36
    
37
    public DefaultExportProcess() {
38
        
39
    } 
40
    
41
    @Override
42
    public void setOutputFormat(String serviceName) throws ExportException {
43
        if( this.factory!=null && StringUtils.equalsIgnoreCase(serviceName, this.factory.getName())) {
44
            return;
45
        }
46
        ExportServiceManager serviceManager = ExportLocator.getServiceManager();
47
        this.factory = serviceManager.getServiceFactory(serviceName);
48
        this.parameters = this.factory.createParameters();
49
        this.service = this.factory.createService(this.parameters);
50
        this.parameters.setSourceFeatureStore(this.sourceFeatureStore);
51
        if( this.parameters instanceof ExportParametersGeometry ) {
52
            ExportParametersGeometry params = (ExportParametersGeometry) this.parameters;
53
            params.setContextProjection(this.contextProjection);
54
            params.setSourceProjection(this.sourceProjection);
55
            params.setSourceTransformation(this.sourceTransformation);
56
            params.setContext(this.context);
57
        }
58
        
59
    }
60

    
61
    @Override
62
    public void setSourceFeatureStore(FeatureStore store) {
63
        this.sourceFeatureStore = store;
64
        if( this.parameters!= null ) {
65
            this.parameters.setSourceFeatureStore(sourceFeatureStore);
66
        }
67
    }
68

    
69
    @Override
70
    public void setContextProjection(IProjection projection) {
71
        this.contextProjection = projection;
72
        if( this.parameters instanceof ExportParametersGeometry ) {
73
            ((ExportParametersGeometry)this.parameters).setContextProjection(projection);
74
        }
75
    }
76

    
77
    @Override
78
    public void setSourceTransformation(ICoordTrans transformation) {
79
        this.sourceTransformation = transformation;
80
        if( this.parameters instanceof ExportParametersGeometry ) {
81
            ((ExportParametersGeometry)this.parameters).setSourceTransformation(transformation);
82
        }
83
    }
84

    
85
    @Override
86
    public void setSourceProjection(IProjection projection) {
87
        this.sourceProjection = projection;
88
        if( this.parameters instanceof ExportParametersGeometry ) {
89
            ((ExportParametersGeometry)this.parameters).setSourceProjection(projection);
90
        }
91
    }
92

    
93

    
94
    @Override
95
    public Object getContext() {
96
        return context;
97
    }
98

    
99
    @Override
100
    public void setContext(Object context) {
101
        this.context = context;
102
        if( this.parameters!= null ) {
103
            this.parameters.setContext(context);
104
        }
105
    }
106

    
107
    @Override
108
    public ExportParameters getParameters() {
109
        return this.parameters;
110
    }
111

    
112
    @Override
113
    public TaskStatus getTaskStatus() {
114
        if( this.service==null ) {
115
            throw new IllegalStateException("Can't access task status at this moment, need setting output format.");
116
        }
117
        return this.service.getTaskStatus();
118
    }
119
    
120
    @Override
121
    public void run() {
122
        if( this.factory == null ) {
123
            throw new IllegalStateException("Can't start export process, need setting output format.");
124
        }
125
        if( this.parameters == null ) {
126
            throw new IllegalStateException("Can't start export process, need setting parameters.");
127
        }
128
        try {
129
            FeatureSet featureSet;
130
            switch(this.parameters.getFeaturesToUse()) {
131
                case ExportParameters.USE_ALL_FEATURES:
132
                default:
133
                    featureSet = this.parameters.getSourceFeatureStore().getFeatureSet();
134
                    break;
135
                case ExportParameters.USE_SELECTED_FEATURES:
136
                    featureSet = this.parameters.getSourceFeatureStore().getFeatureSelection();
137
                    break;
138
                case ExportParameters.USE_FILTERED_FEATURES:
139
                    featureSet = this.parameters.getSourceFeatureStore().getFeatureSet(this.parameters.getFilterExpresion());
140
                    break;
141
            }
142
            this.service.export(featureSet);
143
        } catch (Exception ex) {
144
            throw new RuntimeException(ex);
145
        }
146
    }
147
    
148
    @Override
149
    public List<OpenDataStoreParameters> getTargetOpenStoreParameters() {
150
        if( this.service==null ) {
151
            return null;
152
        }
153
        try {
154
            return this.service.getTargetOpenStoreParameters();
155
        } catch (ExportException ex) {
156
            throw new RuntimeException("Can't create target open store parameters.", ex);
157
        }
158
    }
159
}