root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableOperations.java @ 27352
History | View | Annotate | Download (16.9 KB)
1 | 1103 | fjp | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 312 | fernando | package com.iver.cit.gvsig; |
42 | |||
43 | 6736 | caballero | import java.awt.Dimension; |
44 | 1830 | fernando | import java.io.IOException; |
45 | 26291 | vcaballero | import java.lang.reflect.Array; |
46 | import java.util.ArrayList; |
||
47 | import java.util.Arrays; |
||
48 | import java.util.Collection; |
||
49 | 1830 | fernando | |
50 | 1219 | vcaballero | import com.hardcode.driverManager.DriverLoadException; |
51 | 10626 | caballero | import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
52 | 884 | fernando | import com.hardcode.gdbms.engine.data.DataSource; |
53 | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
||
54 | 2217 | fernando | import com.hardcode.gdbms.engine.instruction.EvaluationException; |
55 | 884 | fernando | import com.hardcode.gdbms.engine.instruction.SemanticException; |
56 | import com.hardcode.gdbms.parser.ParseException; |
||
57 | 855 | fernando | import com.iver.andami.PluginServices; |
58 | import com.iver.andami.messages.NotificationManager; |
||
59 | 596 | fernando | import com.iver.andami.plugins.Extension; |
60 | 6877 | cesar | import com.iver.andami.ui.mdiManager.IWindow; |
61 | 26051 | vcaballero | import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException; |
62 | 3940 | caballero | import com.iver.cit.gvsig.fmap.edition.EditableAdapter; |
63 | 26051 | vcaballero | import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
64 | 884 | fernando | import com.iver.cit.gvsig.fmap.layers.FBitSet; |
65 | 1774 | fernando | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
66 | 855 | fernando | import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
67 | import com.iver.cit.gvsig.gui.filter.ExpressionListener; |
||
68 | 9532 | caballero | import com.iver.cit.gvsig.project.Project; |
69 | 7304 | caballero | import com.iver.cit.gvsig.project.documents.table.FieldSelectionModel; |
70 | import com.iver.cit.gvsig.project.documents.table.ObjectSelectionStep; |
||
71 | import com.iver.cit.gvsig.project.documents.table.ProjectTable; |
||
72 | 7379 | caballero | import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory; |
73 | 7304 | caballero | import com.iver.cit.gvsig.project.documents.table.TableSelectionModel; |
74 | import com.iver.cit.gvsig.project.documents.table.gui.AndamiWizard; |
||
75 | 26051 | vcaballero | import com.iver.cit.gvsig.project.documents.table.gui.JoinWizardController; |
76 | 7304 | caballero | import com.iver.cit.gvsig.project.documents.table.gui.Table; |
77 | 1830 | fernando | import com.iver.utiles.swing.objectSelection.SelectionException; |
78 | import com.iver.utiles.swing.wizard.WizardControl; |
||
79 | import com.iver.utiles.swing.wizard.WizardEvent; |
||
80 | import com.iver.utiles.swing.wizard.WizardListener; |
||
81 | 312 | fernando | |
82 | |||
83 | /**
|
||
84 | 1219 | vcaballero | * Extensi?n que controla las operaciones realizadas sobre las tablas.
|
85 | 312 | fernando | *
|
86 | * @author Fernando Gonz?lez Cort?s
|
||
87 | */
|
||
88 | 5005 | jorpiell | public class TableOperations extends Extension implements ExpressionListener { |
89 | 855 | fernando | private SelectableDataSource dataSource = null; |
90 | 2183 | fernando | //private Table vista;
|
91 | 312 | fernando | |
92 | 1219 | vcaballero | /**
|
93 | 5005 | jorpiell | * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
|
94 | 1219 | vcaballero | */
|
95 | public void execute(String actionCommand) { |
||
96 | 9532 | caballero | ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(ProjectExtension.class); |
97 | Project project=pe.getProject(); |
||
98 | ProjectTable[] pts = (ProjectTable[]) project.getDocumentsByType(ProjectTableFactory.registerName) |
||
99 | .toArray(new ProjectTable[0]); |
||
100 | 2183 | fernando | if ("JOIN".equals(actionCommand)) { |
101 | 26051 | vcaballero | JoinWizardController wizardController = new JoinWizardController(this); |
102 | wizardController.runWizard(pts); |
||
103 | 2183 | fernando | }else if ("LINK".equals(actionCommand)) { |
104 | try {
|
||
105 | final ObjectSelectionStep sourceTable = new ObjectSelectionStep(); |
||
106 | sourceTable.setModel(new TableSelectionModel(pts,
|
||
107 | 2337 | fernando | PluginServices.getText(this, "seleccione_tabla_origen"))); |
108 | 2183 | fernando | |
109 | final ObjectSelectionStep targetTable = new ObjectSelectionStep(); |
||
110 | targetTable.setModel(new TableSelectionModel(pts,
|
||
111 | 2337 | fernando | PluginServices.getText(this, "seleccione_tabla_a_enlazar"))); |
112 | 2183 | fernando | |
113 | final ObjectSelectionStep firstTableField = new ObjectSelectionStep(); |
||
114 | final ObjectSelectionStep secondTableField = new ObjectSelectionStep(); |
||
115 | 2337 | fernando | final AndamiWizard wiz = new AndamiWizard(PluginServices.getText(this, "back"), PluginServices.getText(this, "next"), PluginServices.getText(this, "finish"), PluginServices.getText(this, "cancel")); |
116 | 6736 | caballero | wiz.setSize(new Dimension(450,200)); |
117 | 2183 | fernando | wiz.addStep(sourceTable); |
118 | wiz.addStep(firstTableField); |
||
119 | wiz.addStep(targetTable); |
||
120 | wiz.addStep(secondTableField); |
||
121 | |||
122 | wiz.addWizardListener(new WizardListener() {
|
||
123 | public void cancel(WizardEvent w) { |
||
124 | 6880 | cesar | PluginServices.getMDIManager().closeWindow(wiz); |
125 | 2183 | fernando | } |
126 | |||
127 | public void finished(WizardEvent w) { |
||
128 | 6880 | cesar | PluginServices.getMDIManager().closeWindow(wiz); |
129 | 2183 | fernando | |
130 | ProjectTable sourceProjectTable = (ProjectTable) sourceTable.getSelected(); |
||
131 | 3940 | caballero | SelectableDataSource sds1=null;;
|
132 | try {
|
||
133 | 7304 | caballero | sds1 = sourceProjectTable.getModelo().getRecordset(); |
134 | 10626 | caballero | } catch (ReadDriverException e) {
|
135 | 3940 | caballero | e.printStackTrace(); |
136 | } |
||
137 | 7304 | caballero | //String tableName1 = sds1.getName();
|
138 | 2183 | fernando | |
139 | ProjectTable targetProjectTable = (ProjectTable) targetTable.getSelected(); |
||
140 | 3940 | caballero | SelectableDataSource sds2=null;
|
141 | try {
|
||
142 | 7304 | caballero | sds2 = targetProjectTable.getModelo().getRecordset(); |
143 | 10626 | caballero | } catch (ReadDriverException e) {
|
144 | 3940 | caballero | e.printStackTrace(); |
145 | } |
||
146 | 2183 | fernando | |
147 | 7304 | caballero | //String tableName2 = sds2.getName();
|
148 | 2183 | fernando | |
149 | String field1 = (String) firstTableField.getSelected(); |
||
150 | String field2 = (String) secondTableField.getSelected(); |
||
151 | sourceProjectTable.setLinkTable(sds2.getName(),field1,field2); |
||
152 | ((ProjectExtension)PluginServices.getExtension(ProjectExtension.class)).getProject().setLinkTable(); |
||
153 | } |
||
154 | |||
155 | public void next(WizardEvent w) { |
||
156 | WizardControl wiz = w.wizard; |
||
157 | wiz.enableBack(true);
|
||
158 | wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
|
||
159 | |||
160 | if (w.currentStep == 1) { |
||
161 | ProjectTable pt = (ProjectTable) sourceTable.getSelected(); |
||
162 | |||
163 | try {
|
||
164 | firstTableField.setModel(new FieldSelectionModel(
|
||
165 | 3940 | caballero | pt.getModelo().getRecordset(), |
166 | 2337 | fernando | PluginServices.getText(this, "seleccione_campo_enlace"), |
167 | 2183 | fernando | -1));
|
168 | } catch (SelectionException e) {
|
||
169 | NotificationManager.addError("Error obteniendo los campos de la tabla",
|
||
170 | e); |
||
171 | 10626 | caballero | } catch (ReadDriverException e) {
|
172 | 3940 | caballero | e.printStackTrace(); |
173 | 2183 | fernando | } |
174 | } else if (w.currentStep == 3) { |
||
175 | try {
|
||
176 | //tabla
|
||
177 | ProjectTable pt = (ProjectTable) sourceTable.getSelected(); |
||
178 | |||
179 | //?ndice del campo
|
||
180 | 7304 | caballero | SelectableDataSource sds = pt.getModelo().getRecordset(); |
181 | 2183 | fernando | String fieldName = (String) firstTableField.getSelected(); |
182 | int fieldIndex = sds.getFieldIndexByName(fieldName);
|
||
183 | int type = sds.getFieldType(fieldIndex);
|
||
184 | |||
185 | secondTableField.setModel(new FieldSelectionModel(
|
||
186 | 3940 | caballero | ((ProjectTable) targetTable.getSelected()).getModelo().getRecordset(), |
187 | 2337 | fernando | PluginServices.getText(this, "seleccione_campo_enlace"), |
188 | 2183 | fernando | type)); |
189 | } catch (SelectionException e) {
|
||
190 | NotificationManager.addError("Error obteniendo los campos de la tabla",
|
||
191 | e); |
||
192 | 10626 | caballero | } catch (ReadDriverException e) {
|
193 | 2183 | fernando | NotificationManager.addError("Error obteniendo los campos de la tabla",
|
194 | e); |
||
195 | } |
||
196 | } |
||
197 | } |
||
198 | |||
199 | public void back(WizardEvent w) { |
||
200 | WizardControl wiz = w.wizard; |
||
201 | wiz.enableBack(true);
|
||
202 | wiz.enableNext(((ObjectSelectionStep) wiz.getCurrentStep()).getSelectedItem() != null);
|
||
203 | } |
||
204 | }); |
||
205 | 9532 | caballero | project.setModified(true);
|
206 | 6880 | cesar | PluginServices.getMDIManager().addWindow(wiz); |
207 | 2183 | fernando | } catch (SelectionException e) {
|
208 | NotificationManager.addError("Error abriendo el asistente", e);
|
||
209 | } |
||
210 | 3940 | caballero | } |
211 | 1219 | vcaballero | } |
212 | 312 | fernando | |
213 | 1219 | vcaballero | /**
|
214 | 855 | fernando | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
|
215 | 312 | fernando | */
|
216 | public void newSet(String expression) { |
||
217 | 9494 | ppiqueras | // By Pablo: if no expression -> no element selected
|
218 | if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
||
219 | long[] sel = doSet(expression); |
||
220 | 9532 | caballero | |
221 | 9494 | ppiqueras | if (sel == null) { |
222 | throw new RuntimeException("Not a 'where' clause?"); |
||
223 | } |
||
224 | 9532 | caballero | |
225 | 9494 | ppiqueras | FBitSet selection = new FBitSet();
|
226 | 9532 | caballero | |
227 | 9494 | ppiqueras | for (int i = 0; i < sel.length; i++) { |
228 | selection.set((int) sel[i]);
|
||
229 | } |
||
230 | 9532 | caballero | |
231 | 9494 | ppiqueras | dataSource.clearSelection(); |
232 | dataSource.setSelection(selection); |
||
233 | 1219 | vcaballero | } |
234 | 9494 | ppiqueras | else {
|
235 | // By Pablo: if no expression -> no element selected
|
||
236 | dataSource.clearSelection(); |
||
237 | 884 | fernando | } |
238 | 312 | fernando | } |
239 | 1219 | vcaballero | |
240 | 312 | fernando | /**
|
241 | 855 | fernando | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
|
242 | 312 | fernando | */
|
243 | 884 | fernando | private long[] doSet(String expression) { |
244 | try {
|
||
245 | 2183 | fernando | DataSource ds = LayerFactory.getDataSourceFactory().executeSQL(expression,
|
246 | 2667 | fernando | DataSourceFactory.MANUAL_OPENING); |
247 | 1219 | vcaballero | |
248 | 884 | fernando | return ds.getWhereFilter();
|
249 | } catch (DriverLoadException e) {
|
||
250 | NotificationManager.addError("Error cargando el driver", e);
|
||
251 | 10626 | caballero | } catch (ReadDriverException e) {
|
252 | 884 | fernando | NotificationManager.addError("Error accediendo al driver", e);
|
253 | } catch (ParseException e) { |
||
254 | NotificationManager.addError("Parse error", e);
|
||
255 | } catch (SemanticException e) {
|
||
256 | NotificationManager.addError(e.getMessage(), e); |
||
257 | } catch (IOException e) { |
||
258 | NotificationManager.addError("GDBMS internal error", e);
|
||
259 | 2217 | fernando | } catch (EvaluationException e) {
|
260 | NotificationManager.addError("Error con la expresi?n", e);
|
||
261 | } |
||
262 | 1219 | vcaballero | |
263 | 884 | fernando | return null; |
264 | 312 | fernando | } |
265 | |||
266 | 1219 | vcaballero | /**
|
267 | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String)
|
||
268 | */
|
||
269 | public void addToSet(String expression) { |
||
270 | 9494 | ppiqueras | // By Pablo: if no expression -> don't add more elements to set
|
271 | if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
||
272 | long[] sel = doSet(expression); |
||
273 | 9532 | caballero | |
274 | 9494 | ppiqueras | if (sel == null) { |
275 | throw new RuntimeException("Not a 'where' clause?"); |
||
276 | } |
||
277 | 9532 | caballero | |
278 | 9494 | ppiqueras | FBitSet selection = new FBitSet();
|
279 | 9532 | caballero | |
280 | 9494 | ppiqueras | for (int i = 0; i < sel.length; i++) { |
281 | selection.set((int) sel[i]);
|
||
282 | } |
||
283 | 9532 | caballero | |
284 | 9494 | ppiqueras | FBitSet fbs = dataSource.getSelection(); |
285 | fbs.or(selection); |
||
286 | dataSource.setSelection(fbs); |
||
287 | 1219 | vcaballero | } |
288 | } |
||
289 | 312 | fernando | |
290 | 1219 | vcaballero | /**
|
291 | * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
|
||
292 | */
|
||
293 | public void fromSet(String expression) { |
||
294 | 9494 | ppiqueras | // By Pablo: if no expression -> no element selected
|
295 | if (! this.filterExpressionFromWhereIsEmpty(expression)) { |
||
296 | long[] sel = doSet(expression); |
||
297 | 9532 | caballero | |
298 | 9494 | ppiqueras | if (sel == null) { |
299 | throw new RuntimeException("Not a 'where' clause?"); |
||
300 | } |
||
301 | 9532 | caballero | |
302 | 9494 | ppiqueras | FBitSet selection = new FBitSet();
|
303 | 9532 | caballero | |
304 | 9494 | ppiqueras | for (int i = 0; i < sel.length; i++) { |
305 | selection.set((int) sel[i]);
|
||
306 | } |
||
307 | 9532 | caballero | |
308 | 9494 | ppiqueras | FBitSet fbs = dataSource.getSelection(); |
309 | fbs.and(selection); |
||
310 | dataSource.setSelection(fbs); |
||
311 | 1219 | vcaballero | } |
312 | 9494 | ppiqueras | else {
|
313 | // By Pablo: if no expression -> no element selected
|
||
314 | dataSource.clearSelection(); |
||
315 | 312 | fernando | } |
316 | 1219 | vcaballero | } |
317 | 9532 | caballero | |
318 | 9494 | ppiqueras | /**
|
319 | * Returns true if the WHERE subconsultation of the filterExpression is empty ("")
|
||
320 | 9532 | caballero | *
|
321 | 9494 | ppiqueras | * @author Pablo Piqueras Bartolom? (p_queras@hotmail.com)
|
322 | * @param expression An string
|
||
323 | 9532 | caballero | * @return A boolean value
|
324 | 9494 | ppiqueras | */
|
325 | private boolean filterExpressionFromWhereIsEmpty(String expression) { |
||
326 | String subExpression = expression.trim();
|
||
327 | 9532 | caballero | int pos;
|
328 | |||
329 | 9494 | ppiqueras | // Remove last ';' if exists
|
330 | if (subExpression.charAt(subExpression.length() -1) == ';') |
||
331 | subExpression = subExpression.substring(0, subExpression.length() -1).trim(); |
||
332 | 9532 | caballero | |
333 | 9494 | ppiqueras | // If there is no 'where' clause
|
334 | if ((pos = subExpression.indexOf("where")) == -1) |
||
335 | return false; |
||
336 | 9532 | caballero | |
337 | 9494 | ppiqueras | // If there is no subexpression in the WHERE clause -> true
|
338 | subExpression = subExpression.substring(pos + 5, subExpression.length()).trim(); // + 5 is the length of 'where' |
||
339 | if ( subExpression.length() == 0 ) |
||
340 | return true; |
||
341 | else
|
||
342 | return false; |
||
343 | } |
||
344 | 312 | fernando | |
345 | /**
|
||
346 | 5005 | jorpiell | * @see com.iver.mdiApp.plugins.IExtension#isVisible()
|
347 | 312 | fernando | */
|
348 | public boolean isVisible() { |
||
349 | 6880 | cesar | IWindow v = PluginServices.getMDIManager().getActiveWindow(); |
350 | 312 | fernando | |
351 | 855 | fernando | if (v == null) { |
352 | 312 | fernando | return false; |
353 | } |
||
354 | 1219 | vcaballero | |
355 | 5900 | jorpiell | if (v instanceof Table) { |
356 | 312 | fernando | return true; |
357 | 2183 | fernando | } /*else {
|
358 | 1219 | vcaballero | if (v instanceof com.iver.cit.gvsig.gui.View) {
|
359 | 855 | fernando | com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
|
360 | ProjectView pv = view.getModel();
|
||
361 | 1219 | vcaballero | FLayer[] seleccionadas = pv.getMapContext().getLayers()
|
362 | .getActives();
|
||
363 | |||
364 | if (seleccionadas.length == 1) {
|
||
365 | if (seleccionadas[0] instanceof AlphanumericData) {
|
||
366 | 855 | fernando | return true;
|
367 | }
|
||
368 | 312 | fernando | }
|
369 | }
|
||
370 | 2183 | fernando | */
|
371 | 312 | fernando | return false; |
372 | 2183 | fernando | //}
|
373 | 312 | fernando | } |
374 | |||
375 | 596 | fernando | /**
|
376 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#initialize()
|
377 | 596 | fernando | */
|
378 | 5005 | jorpiell | public void initialize() { |
379 | 14821 | jmvivo | registerIcons(); |
380 | 596 | fernando | } |
381 | |||
382 | 14821 | jmvivo | private void registerIcons(){ |
383 | 15647 | jmvivo | PluginServices.getIconTheme().registerDefault( |
384 | 14821 | jmvivo | "table-join",
|
385 | this.getClass().getClassLoader().getResource("images/tablejoin.png") |
||
386 | ); |
||
387 | |||
388 | 15647 | jmvivo | PluginServices.getIconTheme().registerDefault( |
389 | 14821 | jmvivo | "table-link",
|
390 | this.getClass().getClassLoader().getResource("images/tablelink.png") |
||
391 | ); |
||
392 | } |
||
393 | |||
394 | 596 | fernando | /**
|
395 | 5005 | jorpiell | * @see com.iver.andami.plugins.IExtension#isEnabled()
|
396 | 596 | fernando | */
|
397 | public boolean isEnabled() { |
||
398 | 855 | fernando | return true; |
399 | 596 | fernando | } |
400 | 2183 | fernando | |
401 | 26051 | vcaballero | public void execJoin(ProjectTable sourceProjectTable, String field1, String prefix1, |
402 | ProjectTable targetProjectTable, String field2, String prefix2) { |
||
403 | // get source table and source field
|
||
404 | SelectableDataSource sds=null;
|
||
405 | try {
|
||
406 | sds = sourceProjectTable.getModelo().getRecordset(); |
||
407 | } catch (ReadDriverException e) {
|
||
408 | e.printStackTrace(); |
||
409 | } |
||
410 | String tableName1 = sds.getName();
|
||
411 | // get target table and target field
|
||
412 | try {
|
||
413 | sds = targetProjectTable.getModelo().getRecordset(); |
||
414 | } catch (ReadDriverException e) {
|
||
415 | e.printStackTrace(); |
||
416 | } |
||
417 | String tableName2 = sds.getName();
|
||
418 | // compute the join
|
||
419 | String sql =
|
||
420 | "custom com_iver_cit_gvsig_arcjoin tables '" +
|
||
421 | tableName1 + "', '" + tableName2 + "' values(" + |
||
422 | field1 + ", " + field2 + ");"; |
||
423 | PluginServices.getLogger().debug(sql); |
||
424 | 3940 | caballero | |
425 | 26051 | vcaballero | try {
|
426 | SelectableDataSource result = new SelectableDataSource(LayerFactory.getDataSourceFactory()
|
||
427 | .executeSQL(sql, |
||
428 | DataSourceFactory.AUTOMATIC_OPENING)); |
||
429 | EditableAdapter auxea=new EditableAdapter();
|
||
430 | auxea.setOriginalDataSource(result); |
||
431 | renameFields(auxea, sourceProjectTable, sanitizeFieldName(prefix1), |
||
432 | targetProjectTable, sanitizeFieldName(prefix2)); |
||
433 | 26291 | vcaballero | String[] currentJoinedTables = sourceProjectTable.getJoinedTables(); |
434 | String[] joinedTables; |
||
435 | if (currentJoinedTables!=null) { |
||
436 | joinedTables = new String[currentJoinedTables.length+1]; |
||
437 | System.arraycopy(currentJoinedTables, 0, joinedTables, 0, currentJoinedTables.length); |
||
438 | } |
||
439 | else {
|
||
440 | joinedTables = new String[1]; |
||
441 | } |
||
442 | joinedTables[joinedTables.length-1] = targetProjectTable.getName();
|
||
443 | sourceProjectTable.setJoinTable(joinedTables); |
||
444 | 26051 | vcaballero | sourceProjectTable.replaceDataSource(auxea); |
445 | } catch (ParseException e) { |
||
446 | throw new RuntimeException(e); |
||
447 | } catch (DriverLoadException e) {
|
||
448 | NotificationManager.addError(PluginServices.getText(this, "Error_loading_driver"), |
||
449 | e); |
||
450 | } catch (ReadDriverException e) {
|
||
451 | NotificationManager.addError(PluginServices.getText(this, "Error_reading_from_the_driver"), |
||
452 | e); |
||
453 | } catch (SemanticException e) {
|
||
454 | throw new RuntimeException(e); |
||
455 | } catch (EvaluationException e) {
|
||
456 | NotificationManager.addError(PluginServices.getText(this, "Error_evaluationg_expression"), |
||
457 | e); |
||
458 | } |
||
459 | } |
||
460 | |||
461 | /**
|
||
462 | * Renames fields in a JOIN, so that fields from TABLE1
|
||
463 | * become sourcePrefix.field1, sourcePrefix.field2, etc, and fields
|
||
464 | * from TABLE2 become targetPrefix.field1, targetPrefix.field2, etc.
|
||
465 | * @param eds
|
||
466 | * @param pt1
|
||
467 | * @param pt1
|
||
468 | */
|
||
469 | private void renameFields(EditableAdapter eds, |
||
470 | ProjectTable pt1, String sourcePrefix, ProjectTable pt2, String targetPrefix) { |
||
471 | try {
|
||
472 | SelectableDataSource sourceDS1 = pt1.getModelo().getRecordset(); |
||
473 | SelectableDataSource sourceDS2 = pt2.getModelo().getRecordset(); |
||
474 | SelectableDataSource joinDS = eds.getRecordset(); |
||
475 | eds.startEdition(EditionEvent.ALPHANUMERIC); |
||
476 | // rename fields from sourceDS1
|
||
477 | for (int i=0; i<sourceDS1.getFieldCount(); i++) { |
||
478 | String fieldName = sourceDS1.getFieldName(i);
|
||
479 | |||
480 | if (joinDS.getFieldIndexByName(fieldName)!=-1) { |
||
481 | eds.renameField(fieldName, sourcePrefix+"_"+fieldName);
|
||
482 | } |
||
483 | } |
||
484 | // rename fields from sourceDS2
|
||
485 | for (int i=0; i<sourceDS2.getFieldCount(); i++) { |
||
486 | String fieldName = "link_"+sourceDS2.getFieldName(i); |
||
487 | |||
488 | if (joinDS.getFieldIndexByName(fieldName)!=-1) { |
||
489 | eds.renameField(fieldName, targetPrefix+"_"+sourceDS2.getFieldName(i));
|
||
490 | } |
||
491 | } |
||
492 | } catch (ReadDriverException e) {
|
||
493 | //just log the error, it's not bad if fields couldn't be removed
|
||
494 | PluginServices.getLogger().error("Error renaming fields in a JOIN", e);
|
||
495 | } catch (StartWriterVisitorException e) {
|
||
496 | PluginServices.getLogger().error("Error renaming fields in a JOIN", e);
|
||
497 | } |
||
498 | } |
||
499 | |||
500 | /**
|
||
501 | * Ensure that field name only has 'safe' characters
|
||
502 | * (no spaces, special characters, etc).
|
||
503 | */
|
||
504 | public String sanitizeFieldName(String fieldName) { |
||
505 | return fieldName.replaceAll("\\W", "_"); // replace any non-word character by an underscore |
||
506 | } |
||
507 | |||
508 | 312 | fernando | } |