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