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 @ 44538
History | View | Annotate | Download (10.2 KB)
1 |
package org.gvsig.export.impl; |
---|---|
2 |
|
3 |
import java.util.List; |
4 |
import java.util.Objects; |
5 |
import java.util.logging.Level; |
6 |
import java.util.logging.Logger; |
7 |
import javax.swing.JOptionPane; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.cresques.cts.ICoordTrans; |
10 |
import org.cresques.cts.IProjection; |
11 |
import org.gvsig.export.ExportException; |
12 |
import org.gvsig.export.ExportLocator; |
13 |
import org.gvsig.export.ExportParameters; |
14 |
import org.gvsig.export.ExportParametersGeometry; |
15 |
import org.gvsig.export.ExportProcess; |
16 |
import org.gvsig.export.spi.ExportService; |
17 |
import org.gvsig.export.spi.ExportServiceFactory; |
18 |
import org.gvsig.export.spi.ExportServiceManager; |
19 |
import org.gvsig.expressionevaluator.Expression; |
20 |
import org.gvsig.fmap.dal.OpenDataStoreParameters; |
21 |
import org.gvsig.fmap.dal.exception.DataException; |
22 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
23 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
24 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
25 |
import org.gvsig.fmap.dal.feature.FeatureType; |
26 |
import org.gvsig.tools.ToolsLocator; |
27 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
28 |
import org.gvsig.tools.task.TaskStatus; |
29 |
|
30 |
/**
|
31 |
*
|
32 |
* @author jjdelcerro
|
33 |
*/
|
34 |
public class DefaultExportProcess |
35 |
implements ExportProcess {
|
36 |
|
37 |
private ExportServiceFactory factory;
|
38 |
private ExportService service;
|
39 |
private ExportParameters parameters;
|
40 |
private FeatureStore sourceFeatureStore;
|
41 |
|
42 |
private IProjection contextProjection;
|
43 |
private ICoordTrans sourceTransformation;
|
44 |
private IProjection sourceProjection;
|
45 |
private Object context; |
46 |
private Expression filterExpression; |
47 |
private int featuresToUse; |
48 |
|
49 |
public DefaultExportProcess() {
|
50 |
|
51 |
} |
52 |
|
53 |
@Override
|
54 |
public void setOutputFormat(String serviceName) throws ExportException { |
55 |
|
56 |
ExportServiceManager serviceManager = ExportLocator.getServiceManager(); |
57 |
this.factory = serviceManager.getServiceFactory(serviceName);
|
58 |
this.parameters = this.factory.createParameters(); |
59 |
this.service = this.factory.createService(this.parameters); |
60 |
this.parameters.setSourceFeatureStore(this.sourceFeatureStore); |
61 |
if (this.parameters instanceof ExportParametersGeometry) { |
62 |
ExportParametersGeometry params = (ExportParametersGeometry) this.parameters;
|
63 |
params.setContextProjection(this.contextProjection);
|
64 |
params.setSourceProjection(this.sourceProjection);
|
65 |
params.setSourceTransformation(this.sourceTransformation);
|
66 |
params.setTargetProjection(this.contextProjection);
|
67 |
params.setContext(this.context);
|
68 |
|
69 |
if (this.sourceFeatureStore != null) { |
70 |
try {
|
71 |
FeatureType ftype = this.sourceFeatureStore.getDefaultFeatureType();
|
72 |
if (ftype != null) { |
73 |
params.setSourceGeometryFieldName(ftype.getDefaultGeometryAttributeName()); |
74 |
} |
75 |
} catch (DataException ex) {
|
76 |
//
|
77 |
} |
78 |
} |
79 |
} |
80 |
this.setFilterExpression(this.filterExpression); //set features to use and expression |
81 |
|
82 |
} |
83 |
|
84 |
@Override
|
85 |
public void setParameters(ExportParameters params) { |
86 |
this.parameters = params;
|
87 |
this.factory = this.parameters.getFactory(); |
88 |
this.service = this.factory.createService(this.parameters); |
89 |
this.parameters.setSourceFeatureStore(this.sourceFeatureStore); |
90 |
this.parameters.setContext(this.context); |
91 |
|
92 |
this.featuresToUse = this.parameters.getFeaturesToUse(); |
93 |
if (this.filterExpression == null) { |
94 |
this.filterExpression = this.parameters.getFilterExpresion(); |
95 |
} else if (this.filterExpression.isEmpty()) { |
96 |
this.filterExpression = this.parameters.getFilterExpresion(); |
97 |
} else if (!Objects.equals(this.filterExpression, this.parameters.getFilterExpresion())) { |
98 |
StringBuilder message = new StringBuilder(); |
99 |
message.append(ToolsLocator.getI18nManager().getTranslation("_A_filter_is_already_applied_to_this_export_process"));
|
100 |
message.append(":\n");
|
101 |
message.append(StringUtils.abbreviate(this.filterExpression.getPhrase(), 30)); |
102 |
message.append("\n");
|
103 |
message.append(ToolsLocator.getI18nManager().getTranslation("_Its_going_to_be_replace_by_the_filter_in_the_parameters"));
|
104 |
message.append(":\n");
|
105 |
if (this.parameters.getFilterExpresion() != null) { |
106 |
message.append(StringUtils.abbreviate(this.parameters.getFilterExpresion().getPhrase(), 30)); |
107 |
} else {
|
108 |
message.append("empty filter");
|
109 |
} |
110 |
message.append("\n");
|
111 |
message.append(ToolsLocator.getI18nManager().getTranslation("_Do_you_want_to_keep_the_applied_filter"));
|
112 |
int keepFilter = ToolsSwingLocator.getThreadSafeDialogsManager().confirmDialog(
|
113 |
message.toString(),//ToolsLocator.getI18nManager().getTranslation("_Do_you_want_to_keep_the_applied_filter_and_not_use_the_one_in_the_parameters"),
|
114 |
ToolsLocator.getI18nManager().getTranslation("_Filter_to_apply"),
|
115 |
JOptionPane.YES_NO_OPTION,
|
116 |
JOptionPane.WARNING_MESSAGE,
|
117 |
"exporto-overwrite-expression-parameters");
|
118 |
if (keepFilter == JOptionPane.YES_OPTION) { |
119 |
this.parameters.setFilterExpresion(this.filterExpression); |
120 |
} |
121 |
} |
122 |
|
123 |
if (this.parameters instanceof ExportParametersGeometry) { |
124 |
ExportParametersGeometry pa = (ExportParametersGeometry) this.parameters;
|
125 |
this.contextProjection = pa.getContextProjection();
|
126 |
this.sourceProjection = pa.getSourceProjection();
|
127 |
this.sourceTransformation = pa.getSourceTransformation();
|
128 |
|
129 |
} |
130 |
} |
131 |
|
132 |
@Override
|
133 |
public void setSourceFeatureStore(FeatureStore store |
134 |
) { |
135 |
this.sourceFeatureStore = store;
|
136 |
if (this.parameters != null) { |
137 |
this.parameters.setSourceFeatureStore(sourceFeatureStore);
|
138 |
} |
139 |
} |
140 |
|
141 |
@Override
|
142 |
public void setContextProjection(IProjection projection |
143 |
) { |
144 |
this.contextProjection = projection;
|
145 |
if (this.parameters instanceof ExportParametersGeometry) { |
146 |
((ExportParametersGeometry) this.parameters).setContextProjection(projection);
|
147 |
} |
148 |
} |
149 |
|
150 |
@Override
|
151 |
public void setSourceTransformation(ICoordTrans transformation |
152 |
) { |
153 |
this.sourceTransformation = transformation;
|
154 |
if (this.parameters instanceof ExportParametersGeometry) { |
155 |
((ExportParametersGeometry) this.parameters).setSourceTransformation(transformation);
|
156 |
} |
157 |
} |
158 |
|
159 |
@Override
|
160 |
public void setSourceProjection(IProjection projection |
161 |
) { |
162 |
this.sourceProjection = projection;
|
163 |
if (this.parameters instanceof ExportParametersGeometry) { |
164 |
((ExportParametersGeometry) this.parameters).setSourceProjection(projection);
|
165 |
} |
166 |
} |
167 |
|
168 |
@Override
|
169 |
public Object getContext() { |
170 |
return context;
|
171 |
} |
172 |
|
173 |
@Override
|
174 |
public void setContext(Object context |
175 |
) { |
176 |
this.context = context;
|
177 |
if (this.parameters != null) { |
178 |
this.parameters.setContext(context);
|
179 |
} |
180 |
} |
181 |
|
182 |
@Override
|
183 |
public ExportParameters getParameters() {
|
184 |
return this.parameters; |
185 |
} |
186 |
|
187 |
@Override
|
188 |
public TaskStatus getTaskStatus() {
|
189 |
if (this.service == null) { |
190 |
throw new IllegalStateException("Can't access task status at this moment, need setting output format."); |
191 |
} |
192 |
return this.service.getTaskStatus(); |
193 |
} |
194 |
|
195 |
@Override
|
196 |
public void run() { |
197 |
if (this.factory == null) { |
198 |
throw new IllegalStateException("Can't start export process, need setting output format."); |
199 |
} |
200 |
if (this.parameters == null) { |
201 |
throw new IllegalStateException("Can't start export process, need setting parameters."); |
202 |
} |
203 |
try {
|
204 |
FeatureSet featureSet; |
205 |
switch (this.parameters.getFeaturesToUse()) { |
206 |
case ExportParameters.USE_ALL_FEATURES:
|
207 |
default:
|
208 |
featureSet = this.parameters.getSourceFeatureStore().getFeatureSet();
|
209 |
break;
|
210 |
case ExportParameters.USE_SELECTED_FEATURES:
|
211 |
featureSet = this.parameters.getSourceFeatureStore().getFeatureSelection();
|
212 |
break;
|
213 |
case ExportParameters.USE_FILTERED_FEATURES:
|
214 |
featureSet = this.parameters.getSourceFeatureStore().getFeatureSet(this.parameters.getFilterExpresion()); |
215 |
break;
|
216 |
} |
217 |
this.service.export(featureSet);
|
218 |
} catch (Exception ex) { |
219 |
throw new RuntimeException(ex); |
220 |
} |
221 |
} |
222 |
|
223 |
@Override
|
224 |
public List<OpenDataStoreParameters> getTargetOpenStoreParameters() { |
225 |
if (this.service == null) { |
226 |
return null; |
227 |
} |
228 |
try {
|
229 |
return this.service.getTargetOpenStoreParameters(); |
230 |
} catch (ExportException ex) { |
231 |
throw new RuntimeException("Can't create target open store parameters.", ex); |
232 |
} |
233 |
} |
234 |
|
235 |
@Override
|
236 |
public void setFilterExpression(Expression expression |
237 |
) { |
238 |
this.filterExpression = expression;
|
239 |
if (this.parameters != null) { |
240 |
this.parameters.setFilterExpresion(expression);
|
241 |
} |
242 |
if (expression == null) { |
243 |
if (this.sourceFeatureStore != null) { |
244 |
try {
|
245 |
FeatureSelection selection = this.sourceFeatureStore.getFeatureSelection();
|
246 |
if (selection != null && selection.size() > 0) { |
247 |
this.setFeaturesToUse(ExportParameters.USE_SELECTED_FEATURES);
|
248 |
} else {
|
249 |
this.setFeaturesToUse(ExportParameters.USE_ALL_FEATURES);
|
250 |
} |
251 |
} catch (DataException ex) {
|
252 |
this.setFeaturesToUse(ExportParameters.USE_ALL_FEATURES);
|
253 |
} |
254 |
} |
255 |
} else {
|
256 |
this.setFeaturesToUse(ExportParameters.USE_FILTERED_FEATURES);
|
257 |
} |
258 |
} |
259 |
|
260 |
@Override
|
261 |
public void setFeaturesToUse(int featuresToUse |
262 |
) { |
263 |
this.featuresToUse = featuresToUse;
|
264 |
if (this.parameters != null) { |
265 |
this.parameters.setFeaturesToUse(featuresToUse);
|
266 |
} |
267 |
} |
268 |
|
269 |
} |