Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / ProjectTable.java @ 2567

History | View | Annotate | Download (16.4 KB)

1
/* 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
package com.iver.cit.gvsig.project;
42

    
43
import java.io.IOException;
44
import java.text.DateFormat;
45
import java.util.BitSet;
46
import java.util.Date;
47

    
48
import com.hardcode.driverManager.DriverLoadException;
49
import com.hardcode.gdbms.engine.data.DataSource;
50
import com.hardcode.gdbms.engine.data.DataSourceFactory;
51
import com.hardcode.gdbms.engine.data.NoSuchTableException;
52
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
53
import com.hardcode.gdbms.engine.instruction.SemanticException;
54
import com.hardcode.gdbms.parser.ParseException;
55
import com.iver.cit.gvsig.fmap.DriverException;
56
import com.iver.cit.gvsig.fmap.layers.FLayer;
57
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
58
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
59
import com.iver.cit.gvsig.fmap.layers.XMLException;
60
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
61
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
62
import com.iver.utiles.XMLEntity;
63

    
64

    
65
/**
66
 * Tabla del proyecto
67
 *
68
 * @author Fernando Gonz?lez Cort?s
69
 */
70
public class ProjectTable extends ProjectElement {
71
    private static int numTables = 0;
72
    private SelectableDataSource modelo;
73
    private SelectableDataSource original;
74
    private String linkTable;
75
    private String field1;
76
    private String field2;
77
    
78
    private BitSet visibles;
79

    
80
    /* No es necesaria para operar, s?lo para guardar el proyecto */
81
    private AlphanumericData associatedTable;
82

    
83
        private int[] mapping;
84
        private String[] alias;
85
    
86
    /**
87
     * Establece a true el bit index-?simo del bitmap de campos visibles. Los
88
     * campos cuyos bits est?n a true se considerar?n visibles y viceversa
89
     *
90
     * @param index indice del bit que se quiere establecer a true
91
     */
92
    public void set(int index) {
93
        //TODO implementar bien
94
        //        modelo.set(index);
95
        change.firePropertyChange("visibles", true, true);
96
    }
97

    
98
    /**
99
     * Obtiene el valor del bit index-?simo del bitmap de campos visibles
100
     *
101
     * @param index indice del bit que se quiere obtener
102
     *
103
     * @return devuelve true si el campo index-?simo es visible y false en caso
104
     *         contrario
105
     */
106
    public boolean get(int index) {
107
        //TODO implementar bien
108
        //        return modelo.get(index);
109
        return false;
110
    }
111

    
112
    /**
113
     * Obtiene el modelo de la Tabla. Es decir, una clase con capacidad para
114
     * leer la informaci?n de la tabla
115
     *
116
     * @return
117
     */
118
    public SelectableDataSource getModelo() {
119
        return modelo;
120
    }
121

    
122
    /**
123
     * Establece el valor del bit index-?simo al valor  'value'
124
     *
125
     * @param bitIndex indice del bit que se quiere tocar
126
     * @param value valor que se quiere establecer en el bit indicado
127
     */
128
    public void set(int bitIndex, boolean value) {
129
        //TODO implementar bien
130
        //        modelo.set(bitIndex, value);
131
        change.firePropertyChange("visibles", value, value);
132
    }
133

    
134
    /**
135
     * DOCUMENT ME!
136
     *
137
     * @param name DOCUMENT ME!
138
     * @param sds DOCUMENT ME!
139
     *
140
     * @return
141
     * @throws com.hardcode.gdbms.engine.data.driver.DriverException
142
     */
143
    public static ProjectTable createTable(String name, SelectableDataSource sds) throws com.hardcode.gdbms.engine.data.driver.DriverException {
144
        ProjectTable t = new ProjectTable();
145
        t.modelo = sds;
146

    
147
        t.setName(name);
148
        t.setCreationDate(DateFormat.getInstance().format(new Date()));
149
        numTables++;
150

    
151
        return t;
152
    }
153

    
154
    private void createAlias() throws com.hardcode.gdbms.engine.data.driver.DriverException{
155
        mapping = new int[modelo.getFieldCount()];
156
                alias = new String[modelo.getFieldCount()];
157
                for (int i = 0; i < modelo.getFieldCount(); i++) {
158
            mapping[i] = i;
159
            alias[i] = modelo.getFieldName(i);
160
        }
161
    }
162
    
163
    /**
164
     * DOCUMENT ME!
165
     *
166
     * @param sds DOCUMENT ME!
167
     */
168
    public void setDataSource(SelectableDataSource sds) {
169
        modelo = sds;
170

    
171
        setName(sds.getName());
172
        setCreationDate(DateFormat.getInstance().format(new Date()));
173
        change.firePropertyChange("model", modelo, modelo);
174
    }
175

    
176
    /**
177
     * DOCUMENT ME!
178
     *
179
     * @param ds DOCUMENT ME!
180
     */
181
    public void replaceDataSource(SelectableDataSource ds) {
182
        original = modelo;
183
        modelo = ds;
184
        change.firePropertyChange("model", original, modelo);
185
    }
186

    
187
    /**
188
     * DOCUMENT ME!
189
     */
190
    public void restoreDataSource() {
191
        modelo = original;
192
        original = null;
193
        change.firePropertyChange("model", original, modelo);
194
    }
195

    
196
    /**
197
     * DOCUMENT ME!
198
     *
199
     * @return DOCUMENT ME!
200
     *
201
     * @throws XMLException
202
     */
203
    public XMLEntity getXMLEntity() throws XMLException {
204
        XMLEntity xml = super.getXMLEntity();
205

    
206
        //xml.putProperty("nameClass", this.getClass().getName());
207
        xml.putProperty("numTables", numTables);
208

    
209
        if (getLinkTable() != null) {
210
            xml.putProperty("linkTable", linkTable);
211
            xml.putProperty("field1", field1);
212
            xml.putProperty("field2", field2);
213
        }
214

    
215
        if (getOriginal() != null) {
216
            xml.addChild(getOriginal().getXMLEntity());
217
        }
218

    
219
        xml.addChild(modelo.getXMLEntity());
220

    
221
        Object di = LayerFactory.getDataSourceFactory().getDriverInfo(modelo.getName());
222

    
223
        if (associatedTable != null) {
224
            xml.putProperty("layerName", ((FLayer) associatedTable).getName());
225
            xml.putProperty("viewName",
226
                project.getView((FLayer) associatedTable));
227
        }
228
        
229
        xml.putProperty("mapping", mapping);
230
        xml.putProperty("aliases", alias);
231
        
232
        return xml;
233
    }
234

    
235
    /**
236
     * DOCUMENT ME!
237
     *
238
     * @param xml DOCUMENT ME!
239
     * @param p DOCUMENT ME!
240
     *
241
     * @throws XMLException
242
     * @throws DriverException
243
     *
244
     * @see com.iver.cit.gvsig.project.ProjectElement#setXMLEntity(com.iver.utiles.XMLEntity)
245
     */
246
    public void setXMLEntity03(XMLEntity xml, Project p)
247
        throws XMLException, DriverException {
248
        numTables = xml.getIntProperty("numTables");
249

    
250
        if (xml.getStringProperty("type").equals("otherDriverFile")) {
251
            LayerFactory.getDataSourceFactory().addFileDataSource(xml.getStringProperty(
252
                    "driverName"), xml.getStringProperty("gdbmsname"),
253
                xml.getStringProperty("file"));
254

    
255
            setSelectableDataSource03(xml);
256
        } else if (xml.getStringProperty("type").equals("sameDriverFile")) {
257
            String layerName = xml.getStringProperty("layerName");
258

    
259
            ProjectView vista = project.getViewByName(xml.getStringProperty(
260
                        "viewName"));
261
            FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
262

    
263
            modelo = ((AlphanumericData) layer).getRecordset();
264
            associatedTable = (AlphanumericData) layer;
265

    
266
            LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver) ((SingleLayer) layer).getSource()
267
                                                                              .getDriver(),
268
                xml.getStringProperty("gdbmsname"));
269
        } else if (xml.getStringProperty("type").equals("db")) {
270
            LayerFactory.getDataSourceFactory().addDBDataSourceByTable(xml.getStringProperty(
271
                    "gdbmsname"), xml.getStringProperty("host"),
272
                xml.getIntProperty("port"), xml.getStringProperty("user"),
273
                xml.getStringProperty("password"),
274
                xml.getStringProperty("dbName"),
275
                xml.getStringProperty("tableName"),
276
                xml.getStringProperty("driverInfo"));
277

    
278
            setSelectableDataSource03(xml);
279
        }
280

    
281
        setName(xml.getStringProperty("name"));
282
    }
283

    
284
    /**
285
     * DOCUMENT ME!
286
     *
287
     * @param xml DOCUMENT ME!
288
     * @param p DOCUMENT ME!
289
     *
290
     * @throws XMLException
291
     * @throws DriverException
292
     *
293
     * @see com.iver.cit.gvsig.project.ProjectElement#setXMLEntity(com.iver.utiles.XMLEntity)
294
     */
295
    public void setXMLEntity(XMLEntity xml, Project p)
296
        throws XMLException, DriverException {
297
        setName(xml.getStringProperty("name"));
298
        numTables = xml.getIntProperty("numTables");
299

    
300
        setSelectableDataSource(xml);
301

    
302
        /*
303
           if (xml.getStringProperty("type").equals("otherDriverFile")) {
304
           } else if (xml.getStringProperty("type").equals("sameDriverFile")) {
305
                   String layerName = xml.getStringProperty("layerName");
306
                   ProjectView vista = project.getViewByName(xml.getStringProperty(
307
                   "viewName"));
308
                   FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
309
        
310
                   modelo = ((AlphanumericData) layer).getRecordset();
311
                   associatedTable = (AlphanumericData) layer;
312
           } else if (xml.getStringProperty("type").equals("db")) {
313
                   setSelectableDataSource(xml);
314
           }
315
         */
316
        setName(xml.getStringProperty("name"));
317

    
318
        if (xml.contains("linkTable")) {
319
            setLinkTable(xml.getStringProperty("linkTable"),
320
                xml.getStringProperty("field1"), xml.getStringProperty("field2"));
321
        }
322

    
323
        if (xml.contains("mapping")){
324
            mapping = (int[]) xml.getIntArrayProperty("mapping");
325
            alias = (String[]) xml.getStringArrayProperty("aliases");
326
        }else{
327
            try {
328
                createAlias();
329
            } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
330
                throw new XMLException(e);
331
            }
332
        }
333

    
334
    }
335

    
336
    /**
337
     * DOCUMENT ME!
338
     *
339
     * @param xml DOCUMENT ME!
340
     *
341
     * @throws XMLException DOCUMENT ME!
342
     * @throws DriverException DOCUMENT ME!
343
     */
344
    private void setSelectableDataSource03(XMLEntity xml)
345
        throws XMLException, DriverException {
346
        String layerName = null;
347

    
348
        if (xml.contains("layerName")) {
349
            layerName = xml.getStringProperty("layerName");
350
        }
351

    
352
        if (layerName == null) {
353
            DataSource dataSource;
354

    
355
            try {
356
                dataSource = LayerFactory.getDataSourceFactory()
357
                                         .createRandomDataSource(xml.getStringProperty(
358
                            "gdbmsname"),
359
                        DataSourceFactory.CLOSING_AUTOMATIC_MODE);
360

    
361
                SelectableDataSource sds = new SelectableDataSource(dataSource);
362

    
363
                sds.setXMLEntity03(xml.getChild(0));
364

    
365
                setDataSource(sds);
366
            } catch (NoSuchTableException e) {
367
                throw new XMLException(e);
368
            } catch (DriverLoadException e) {
369
                throw new XMLException(e);
370
            } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
371
                throw new XMLException(e);
372
            }
373

    
374
        } else {
375
            ProjectView vista = project.getViewByName(xml.getStringProperty(
376
                        "viewName"));
377
            FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
378

    
379
            modelo = ((AlphanumericData) layer).getRecordset();
380
            associatedTable = (AlphanumericData) layer;
381
        }
382
    }
383

    
384
    /**
385
     * DOCUMENT ME!
386
     *
387
     * @param xml DOCUMENT ME!
388
     *
389
     * @throws XMLException DOCUMENT ME!
390
     * @throws DriverException DOCUMENT ME!
391
     */
392
    private void setSelectableDataSource(XMLEntity xml)
393
        throws XMLException, DriverException {
394
        String layerName = null;
395

    
396
        try {
397
            SelectableDataSource sds;
398

    
399
            if (xml.contains("layerName")) {
400
                layerName = xml.getStringProperty("layerName");
401

    
402
                ProjectView vista = project.getViewByName(xml.getStringProperty(
403
                            "viewName"));
404
                FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
405
                sds = ((AlphanumericData) layer).getRecordset();
406
            } else {
407
                sds = SelectableDataSource.createSelectableDataSource(xml.getChild(
408
                            0));
409
            }
410

    
411
            setDataSource(sds);
412

    
413
            if (xml.getNumChild() == 2) {
414
                replaceDataSource(SelectableDataSource.createSelectableDataSource(
415
                        xml.getChild(1)));
416
            }
417
        } catch (NoSuchTableException e) {
418
            throw new XMLException(e);
419
        } catch (ParseException e) {
420
            throw new XMLException(e);
421
        } catch (DriverLoadException e) {
422
            throw new XMLException(e);
423
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
424
            throw new XMLException(e);
425
        } catch (SemanticException e) {
426
            throw new XMLException(e);
427
        } catch (IOException e) {
428
            throw new XMLException(e);
429
        }
430

    
431
        if (layerName != null) {
432
            ProjectView vista = project.getViewByName(xml.getStringProperty(
433
                        "viewName"));
434
            FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
435

    
436
            //                        modelo = ((AlphanumericData) layer).getRecordset();
437
            associatedTable = (AlphanumericData) layer;
438
        }
439
    }
440

    
441
    /**
442
     * DOCUMENT ME!
443
     *
444
     * @return DOCUMENT ME!
445
     */
446
    public AlphanumericData getAssociatedTable() {
447
        return associatedTable;
448
    }
449

    
450
    /**
451
     * DOCUMENT ME!
452
     *
453
     * @param associatedTable DOCUMENT ME!
454
     */
455
    public void setAssociatedTable(AlphanumericData associatedTable) {
456
        this.associatedTable = associatedTable;
457
    }
458

    
459
    /**
460
     * Obtiene la fuente de datos original de la tabla si se ha invocado
461
     * replaceDataSource. Si no se invoc? este m?todo o se invoc?
462
     * posteriormente restoreDataSource se devuelve null
463
     *
464
     * @return Returns the original.
465
     */
466
    public SelectableDataSource getOriginal() {
467
        return original;
468
    }
469

    
470
    /**
471
     * Devuelve el identificador de la tabla que contiene el link.
472
     *
473
     * @return identificador ?nico de la tabla.
474
     */
475
    public String getLinkTable() {
476
        return linkTable;
477
    }
478

    
479
    /**
480
     * Devuelve el nombre del campo de la tabla a enlazar.
481
     *
482
     * @return Nombre del campo de la tabla a enlazar.
483
     */
484
    public String getField1() {
485
        return field1;
486
    }
487

    
488
    /**
489
     * Devuelve el nombre del campo de la tabla enlazada.
490
     *
491
     * @return Nombre del campo de la tabla enlazada.
492
     */
493
    public String getField2() {
494
        return field2;
495
    }
496

    
497
    /**
498
     * Inserta el identificador de la tabla, el campo de la primera tabla y el
499
     * campo de la segunda tabla.
500
     *
501
     * @param lt identificado de la tabla.
502
     * @param f1 nombre del campo de la primera tabla.
503
     * @param f2 nombre del campo de la segunda tabla.
504
     */
505
    public void setLinkTable(String lt, String f1, String f2) {
506
        linkTable = lt;
507
        field1 = f1;
508
        field2 = f2;
509
    }
510

    
511
    /**
512
     * Borra el identificador de la tabla y elimina del array de listener los
513
     * listener que sean del tipo: LinkSelectionListenr
514
     */
515
    public void removeLinkTable() {
516
        linkTable = null;
517
        getModelo().removeLinksSelectionListener();
518
    }
519
    public String[] getAliases() {
520
        return alias;
521
    }
522
    public void setAliases(String[] alias) {
523
        this.alias = alias;
524
    }
525
    public int[] getMapping() {
526
        return mapping;
527
    }
528
    public void setMapping(int[] mapping) {
529
        this.mapping = mapping;
530
    }
531

    
532
}