Revision 22477 branches/v2_0_0_prep/applications/appgvSIG/src/com/iver/cit/gvsig/TableOperations.java
TableOperations.java | ||
---|---|---|
40 | 40 |
*/ |
41 | 41 |
package com.iver.cit.gvsig; |
42 | 42 |
|
43 |
import java.awt.Component; |
|
43 | 44 |
import java.awt.Dimension; |
44 | 45 |
import java.io.IOException; |
46 |
import java.util.Iterator; |
|
45 | 47 |
|
48 |
import javax.swing.JOptionPane; |
|
49 |
|
|
46 | 50 |
import org.gvsig.fmap.data.DataException; |
51 |
import org.gvsig.fmap.data.DataManager; |
|
47 | 52 |
import org.gvsig.fmap.data.ReadException; |
53 |
import org.gvsig.fmap.data.feature.Feature; |
|
54 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
|
55 |
import org.gvsig.fmap.data.feature.FeatureCollection; |
|
48 | 56 |
import org.gvsig.fmap.data.feature.FeatureStore; |
57 |
import org.gvsig.fmap.data.feature.MemoryFeatureCollection; |
|
58 |
import org.gvsig.fmap.data.feature.joinstore.JoinFeatureStore; |
|
59 |
import org.gvsig.fmap.data.feature.joinstore.JoinFeatureStoreParameters; |
|
49 | 60 |
import org.gvsig.fmap.mapcontext.layers.FBitSet; |
50 | 61 |
import org.gvsig.fmap.mapcontext.layers.LayerFactory; |
51 | 62 |
|
... | ... | |
113 | 124 |
PluginServices.getMDIManager().closeWindow(wiz); |
114 | 125 |
|
115 | 126 |
ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected(); |
116 |
FeatureStore sds=null;
|
|
117 |
try { |
|
118 |
sds = sourceProjectTable.getModelo();
|
|
119 |
} catch (ReadException e) { |
|
120 |
e.printStackTrace(); |
|
121 |
} |
|
122 |
String tableName1 = sds.getName();
|
|
127 |
FeatureStore fs1=null;
|
|
128 |
// try {
|
|
129 |
fs1 = sourceProjectTable.getModelo();
|
|
130 |
// } catch (ReadException e) {
|
|
131 |
// e.printStackTrace();
|
|
132 |
// }
|
|
133 |
String tableName1 = fs1.getName();
|
|
123 | 134 |
|
124 | 135 |
ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected(); |
125 |
try { |
|
126 |
sds = targetProjectTable.getModelo(); |
|
127 |
} catch (ReadException e) { |
|
128 |
e.printStackTrace(); |
|
129 |
} |
|
136 |
FeatureStore fs2=null; |
|
137 |
// try { |
|
138 |
fs2 = targetProjectTable.getModelo(); |
|
139 |
// } catch (ReadException e) { |
|
140 |
// e.printStackTrace(); |
|
141 |
// } |
|
130 | 142 |
|
131 |
String tableName2 = sds.getName();
|
|
143 |
String tableName2 = fs2.getName();
|
|
132 | 144 |
|
133 | 145 |
String field1 = (String) firstTableField.getSelected(); |
134 | 146 |
String field2 = (String) secondTableField.getSelected(); |
... | ... | |
140 | 152 |
System.out.println(sql); |
141 | 153 |
|
142 | 154 |
try { |
143 |
FeatureStore result = new SelectableDataSource(LayerFactory.getDataSourceFactory() |
|
144 |
.executeSQL(sql, |
|
145 |
DataSourceFactory.AUTOMATIC_OPENING)); |
|
155 |
DataManager dm=DataManager.getManager(); |
|
156 |
JoinFeatureStoreParameters jfsp=(JoinFeatureStoreParameters)dm.createDataStoreParameters(JoinFeatureStore.DATASTORE_NAME); |
|
157 |
jfsp.setStorePrimary(fs1.getParameters()); |
|
158 |
jfsp.setStoreSecondary(fs2.getParameters()); |
|
159 |
jfsp.setDefaultGeometry(fs1.getDefaultFeatureType().getDefaultGeometry()); |
|
160 |
jfsp.setFieldNamePrefix("Join_"); |
|
161 |
jfsp.setLinkFieldPrimary(field1); |
|
162 |
jfsp.setLinkFieldSecondary(field2); |
|
163 |
|
|
164 |
JoinFeatureStore jfs=(JoinFeatureStore)dm.createDataStore(jfsp); |
|
165 |
featureStore=jfs; |
|
166 |
// FeatureStore result = new SelectableDataSource(LayerFactory.getDataSourceFactory() |
|
167 |
// .executeSQL(sql, |
|
168 |
// DataSourceFactory.AUTOMATIC_OPENING)); |
|
146 | 169 |
ProjectTable projectTable = (ProjectTable) sourceTable.getSelected(); |
147 |
EditableAdapter auxea=new EditableAdapter(); |
|
148 |
auxea.setOriginalDataSource(result); |
|
149 |
projectTable.replaceDataSource(auxea); |
|
150 |
} catch (ParseException e) { |
|
151 |
throw new RuntimeException(e); |
|
152 |
} catch (DriverLoadException e) { |
|
153 |
NotificationManager.addError("Error con la carga de drivers", |
|
154 |
e); |
|
170 |
// EditableAdapter auxea=new EditableAdapter(); |
|
171 |
// auxea.setOriginalDataSource(result); |
|
172 |
projectTable.setModel(jfs); |
|
155 | 173 |
} catch (ReadException e) { |
156 | 174 |
NotificationManager.addError("Error leyendo del driver", |
157 | 175 |
e); |
158 |
} catch (SemanticException e) { |
|
159 |
throw new RuntimeException(e); |
|
160 |
} catch (EvaluationException e) { |
|
161 |
NotificationManager.addError("Error de evaluaci?n de la expresi?n", |
|
162 |
e); |
|
163 |
} |
|
176 |
} |
|
164 | 177 |
} |
165 | 178 |
|
166 | 179 |
public void next(WizardEvent w) { |
... | ... | |
173 | 186 |
|
174 | 187 |
try { |
175 | 188 |
firstTableField.setModel(new FieldSelectionModel( |
176 |
pt.getModelo().getRecordset(),
|
|
189 |
pt.getModelo(), |
|
177 | 190 |
PluginServices.getText(this, "seleccione_campo_enlace"), |
178 |
-1));
|
|
191 |
null));
|
|
179 | 192 |
} catch (SelectionException e) { |
180 | 193 |
NotificationManager.addError("Error obteniendo los campos de la tabla", |
181 | 194 |
e); |
182 |
} catch (ReadException e) { |
|
183 |
NotificationManager.addError("Error obteniendo los campos de la tabla", |
|
184 |
e); |
|
185 | 195 |
} |
186 | 196 |
} else if (w.currentStep == 3) { |
187 | 197 |
try { |
... | ... | |
189 | 199 |
ProjectTable pt = (ProjectTable) sourceTable.getSelected(); |
190 | 200 |
|
191 | 201 |
//?ndice del campo |
192 |
FeatureStore sds = pt.getModelo();
|
|
202 |
FeatureStore fs = pt.getModelo();
|
|
193 | 203 |
String fieldName = (String) firstTableField.getSelected(); |
194 |
int fieldIndex = sds.getFieldIndexByName(fieldName); |
|
195 |
int type = sds.getFieldType(fieldIndex); |
|
204 |
String type = fs.getDefaultFeatureType().get(fieldName).getDataType(); |
|
196 | 205 |
|
206 |
|
|
197 | 207 |
secondTableField.setModel(new FieldSelectionModel( |
198 |
((ProjectTable) targetTable.getSelected()).getModelo().getRecordset(),
|
|
208 |
((ProjectTable) targetTable.getSelected()).getModelo(), |
|
199 | 209 |
PluginServices.getText(this, "seleccione_campo_enlace"), |
200 | 210 |
type)); |
201 | 211 |
} catch (SelectionException e) { |
202 | 212 |
NotificationManager.addError("Error obteniendo los campos de la tabla", |
203 | 213 |
e); |
204 |
} catch (ReadException e) { |
|
205 |
NotificationManager.addError("Error obteniendo los campos de la tabla", |
|
206 |
e); |
|
207 | 214 |
} |
208 | 215 |
} |
209 | 216 |
} |
... | ... | |
248 | 255 |
|
249 | 256 |
ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected(); |
250 | 257 |
FeatureStore sds1=null;; |
251 |
try { |
|
258 |
// try {
|
|
252 | 259 |
sds1 = sourceProjectTable.getModelo(); |
253 |
} catch (ReadException e) { |
|
254 |
e.printStackTrace(); |
|
255 |
} |
|
260 |
// } catch (ReadException e) {
|
|
261 |
// e.printStackTrace();
|
|
262 |
// }
|
|
256 | 263 |
//String tableName1 = sds1.getName(); |
257 | 264 |
|
258 | 265 |
ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected(); |
259 | 266 |
FeatureStore sds2=null; |
260 |
try { |
|
267 |
// try {
|
|
261 | 268 |
sds2 = targetProjectTable.getModelo(); |
262 |
} catch (ReadException e) { |
|
263 |
e.printStackTrace(); |
|
264 |
} |
|
269 |
// } catch (ReadException e) {
|
|
270 |
// e.printStackTrace();
|
|
271 |
// }
|
|
265 | 272 |
|
266 | 273 |
//String tableName2 = sds2.getName(); |
267 | 274 |
|
... | ... | |
281 | 288 |
|
282 | 289 |
try { |
283 | 290 |
firstTableField.setModel(new FieldSelectionModel( |
284 |
pt.getModelo().getRecordset(),
|
|
291 |
pt.getModelo(), |
|
285 | 292 |
PluginServices.getText(this, "seleccione_campo_enlace"), |
286 |
-1));
|
|
293 |
null));
|
|
287 | 294 |
} catch (SelectionException e) { |
288 | 295 |
NotificationManager.addError("Error obteniendo los campos de la tabla", |
289 | 296 |
e); |
290 |
} catch (ReadException e) { |
|
291 |
e.printStackTrace(); |
|
292 | 297 |
} |
293 | 298 |
} else if (w.currentStep == 3) { |
294 | 299 |
try { |
... | ... | |
296 | 301 |
ProjectTable pt = (ProjectTable) sourceTable.getSelected(); |
297 | 302 |
|
298 | 303 |
//?ndice del campo |
299 |
FeatureStore sds = pt.getModelo();
|
|
304 |
FeatureStore fs = pt.getModelo();
|
|
300 | 305 |
String fieldName = (String) firstTableField.getSelected(); |
301 |
int fieldIndex = sds.getFieldIndexByName(fieldName); |
|
302 |
int type = sds.getFieldType(fieldIndex); |
|
306 |
String type = fs.getDefaultFeatureType().get(fieldName).getDataType(); |
|
303 | 307 |
|
304 | 308 |
secondTableField.setModel(new FieldSelectionModel( |
305 |
((ProjectTable) targetTable.getSelected()).getModelo().getRecordset(),
|
|
309 |
((ProjectTable) targetTable.getSelected()).getModelo(), |
|
306 | 310 |
PluginServices.getText(this, "seleccione_campo_enlace"), |
307 | 311 |
type)); |
308 | 312 |
} catch (SelectionException e) { |
309 | 313 |
NotificationManager.addError("Error obteniendo los campos de la tabla", |
310 | 314 |
e); |
311 |
} catch (ReadException e) { |
|
312 |
NotificationManager.addError("Error obteniendo los campos de la tabla", |
|
313 |
e); |
|
314 | 315 |
} |
315 | 316 |
} |
316 | 317 |
} |
... | ... | |
333 | 334 |
* @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String) |
334 | 335 |
*/ |
335 | 336 |
public void newSet(String expression) throws DataException { |
336 |
// By Pablo: if no expression -> no element selected |
|
337 |
// By Pablo: if no filter expression -> no element selected
|
|
337 | 338 |
if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
338 |
long[] sel = doSet(expression); |
|
339 |
try { |
|
340 |
FeatureCollection sel = doSet(expression); |
|
339 | 341 |
|
340 |
if (sel == null) { |
|
341 |
throw new RuntimeException("Not a 'where' clause?"); |
|
342 |
if (sel == null) { |
|
343 |
throw new RuntimeException("Not a 'where' clause?"); |
|
344 |
} |
|
345 |
featureStore.setSelection(sel); |
|
346 |
}catch(Exception e){ |
|
347 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), "Asegurate de que la consulta es correcta."); |
|
342 | 348 |
} |
343 |
|
|
344 |
FBitSet selection = new FBitSet(); |
|
345 |
|
|
346 |
for (int i = 0; i < sel.length; i++) { |
|
347 |
selection.set((int) sel[i]); |
|
348 |
} |
|
349 |
|
|
350 |
featureStore.getSelection().clear(); |
|
351 |
featureStore.setSelection(selection); |
|
352 | 349 |
} |
353 | 350 |
else { |
354 | 351 |
// By Pablo: if no expression -> no element selected |
... | ... | |
359 | 356 |
/** |
360 | 357 |
* @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String) |
361 | 358 |
*/ |
362 |
private long[] doSet(String expression) { |
|
363 |
try { |
|
364 |
DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression, |
|
365 |
DataSourceFactory.MANUAL_OPENING); |
|
366 |
|
|
367 |
return ds.getWhereFilter(); |
|
368 |
} catch (DriverLoadException e) { |
|
369 |
NotificationManager.addError("Error cargando el driver", e); |
|
370 |
} catch (ReadException e) { |
|
371 |
NotificationManager.addError("Error accediendo al driver", e); |
|
372 |
} catch (ParseException e) { |
|
373 |
NotificationManager.addError("Parse error", e); |
|
374 |
} catch (SemanticException e) { |
|
375 |
NotificationManager.addError(e.getMessage(), e); |
|
376 |
} catch (IOException e) { |
|
377 |
NotificationManager.addError("GDBMS internal error", e); |
|
378 |
} catch (EvaluationException e) { |
|
379 |
NotificationManager.addError("Error con la expresi?n", e); |
|
380 |
} |
|
381 |
|
|
382 |
return null; |
|
359 |
private FeatureCollection doSet(String expression) throws ReadException { |
|
360 |
return (FeatureCollection)featureStore.getDataCollection(featureStore.getDefaultFeatureType(),expression,null); |
|
383 | 361 |
} |
384 |
|
|
385 | 362 |
/** |
386 | 363 |
* @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String) |
387 | 364 |
*/ |
388 | 365 |
public void addToSet(String expression) throws DataException { |
389 |
// By Pablo: if no expression -> don't add more elements to set |
|
366 |
// By Pablo: if no filter expression -> don't add more elements to set
|
|
390 | 367 |
if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
391 |
long[] sel = doSet(expression); |
|
368 |
FeatureCollection sel=null; |
|
369 |
try { |
|
370 |
sel = doSet(expression); |
|
371 |
} catch (ReadException e) { |
|
372 |
throw new DataException("Expression",e); |
|
373 |
} |
|
392 | 374 |
|
393 | 375 |
if (sel == null) { |
394 | 376 |
throw new RuntimeException("Not a 'where' clause?"); |
395 | 377 |
} |
396 |
|
|
397 |
FBitSet selection = new FBitSet(); |
|
398 |
|
|
399 |
for (int i = 0; i < sel.length; i++) { |
|
400 |
selection.set((int) sel[i]); |
|
401 |
} |
|
402 |
|
|
403 |
FBitSet fbs = featureStore.getSelection(); |
|
404 |
fbs.or(selection); |
|
405 |
featureStore.setSelection(fbs); |
|
378 |
FeatureCollection oldSelection=(FeatureCollection)featureStore.getSelection(); |
|
379 |
sel.addAll(oldSelection); |
|
380 |
featureStore.setSelection(sel); |
|
406 | 381 |
} |
407 | 382 |
} |
408 | 383 |
|
... | ... | |
410 | 385 |
* @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String) |
411 | 386 |
*/ |
412 | 387 |
public void fromSet(String expression) throws DataException { |
413 |
// By Pablo: if no expression -> no element selected |
|
414 |
if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
|
415 |
long[] sel = doSet(expression); |
|
388 |
// By Pablo: if no filter expression -> no element selected |
|
389 |
if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
|
416 | 390 |
|
417 |
if (sel == null) { |
|
418 |
throw new RuntimeException("Not a 'where' clause?"); |
|
419 |
} |
|
391 |
FeatureCollection sel=null; |
|
392 |
sel = doSet(expression); |
|
420 | 393 |
|
421 |
FBitSet selection = new FBitSet(); |
|
394 |
if (sel == null) { |
|
395 |
throw new RuntimeException("Not a 'where' clause?"); |
|
396 |
} |
|
397 |
FeatureCollection oldSelection=(FeatureCollection)featureStore.getSelection(); |
|
422 | 398 |
|
423 |
for (int i = 0; i < sel.length; i++) { |
|
424 |
selection.set((int) sel[i]); |
|
425 |
} |
|
426 |
|
|
427 |
FBitSet fbs = featureStore.getSelection(); |
|
428 |
fbs.and(selection); |
|
429 |
featureStore.setSelection(fbs); |
|
430 |
} |
|
431 |
else { |
|
432 |
// By Pablo: if no expression -> no element selected |
|
433 |
featureStore.getSelection().clear(); |
|
434 |
} |
|
399 |
MemoryFeatureCollection mfc=new MemoryFeatureCollection(featureStore); |
|
400 |
Iterator iterator=sel.iterator(); |
|
401 |
while (iterator.hasNext()) { |
|
402 |
Feature feature = (Feature) iterator.next(); |
|
403 |
if (oldSelection.contains(feature)){ |
|
404 |
mfc.add(feature); |
|
405 |
} |
|
406 |
} |
|
407 |
featureStore.setSelection(mfc); |
|
408 |
} else { |
|
409 |
// By Pablo: if no expression -> no element selected |
|
410 |
featureStore.setSelection(featureStore.createSelection()); |
|
411 |
} |
|
435 | 412 |
} |
436 | 413 |
|
437 | 414 |
/** |
... | ... | |
496 | 473 |
*/ |
497 | 474 |
public void initialize() { |
498 | 475 |
registerIcons(); |
476 |
org.gvsig.fmap.data.feature.joinstore.Register.selfRegister(); |
|
477 |
|
|
478 |
|
|
499 | 479 |
} |
500 | 480 |
|
501 | 481 |
private void registerIcons(){ |
Also available in: Unified diff