Statistics
| Revision:

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

History | View | Annotate | Download (21.5 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.Date;
46

    
47
import javax.swing.JOptionPane;
48

    
49
import com.hardcode.driverManager.DriverLoadException;
50
import com.hardcode.gdbms.engine.data.DataSource;
51
import com.hardcode.gdbms.engine.data.DataSourceFactory;
52
import com.hardcode.gdbms.engine.data.NoSuchTableException;
53
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
54
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
55
import com.hardcode.gdbms.engine.instruction.SemanticException;
56
import com.hardcode.gdbms.parser.ParseException;
57
import com.iver.andami.PluginServices;
58
import com.iver.cit.gvsig.fmap.DriverException;
59
import com.iver.cit.gvsig.fmap.layers.FLayer;
60
import com.iver.cit.gvsig.fmap.layers.FLayers;
61
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
62
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
63
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
64
import com.iver.cit.gvsig.fmap.layers.XMLException;
65
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
66
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
67
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegend;
68
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
69
import com.iver.utiles.XMLEntity;
70

    
71

    
72
/**
73
 * Tabla del proyecto
74
 *
75
 * @author Fernando Gonz?lez Cort?s
76
 */
77
public class ProjectTable extends ProjectElement {
78
    private static int numTables = 0;
79
    private SelectableDataSource modelo;
80
    private SelectableDataSource original;
81
    private String linkTable;
82
    private String field1;
83
    private String field2;
84

    
85
    /* No es necesaria para operar, s?lo para guardar el proyecto */
86
    private AlphanumericData associatedTable;
87

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

    
101
        change.firePropertyChange("visibles", true, true);
102
    }
103

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

    
118
    /**
119
     * Obtiene el modelo de la Tabla. Es decir, una clase con capacidad para
120
     * leer la informaci?n de la tabla
121
     *
122
     * @return
123
     */
124
    public SelectableDataSource getModelo() {
125
        return modelo;
126
    }
127

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

    
140
    /**
141
     * DOCUMENT ME!
142
     *
143
     * @param name DOCUMENT ME!
144
     * @param sds DOCUMENT ME!
145
     *
146
     * @return
147
     * @throws com.hardcode.gdbms.engine.data.driver.DriverException
148
     */
149
    public static ProjectTable createTable(String name, SelectableDataSource sds) throws com.hardcode.gdbms.engine.data.driver.DriverException {
150
        ProjectTable t = new ProjectTable();
151
        t.modelo = sds;
152

    
153
        t.createAlias();
154
        
155
        t.setName(name);
156
        t.setCreationDate(DateFormat.getInstance().format(new Date()));
157
        numTables++;
158

    
159
        return t;
160
    }
161

    
162
    private void createAlias() throws com.hardcode.gdbms.engine.data.driver.DriverException{
163
        mapping = new int[modelo.getFieldCount()];
164
                alias = new String[modelo.getFieldCount()];
165
                for (int i = 0; i < modelo.getFieldCount(); i++) {
166
            mapping[i] = i;
167
            alias[i] = modelo.getFieldName(i);
168
        }
169
    }
170
    
171
    /**
172
     * DOCUMENT ME!
173
     *
174
     * @param sds DOCUMENT ME!
175
     */
176
    public void setDataSource(SelectableDataSource sds) {
177
        modelo = sds;
178

    
179
        setName(sds.getName());
180
        setCreationDate(DateFormat.getInstance().format(new Date()));
181
        change.firePropertyChange("model", modelo, modelo);
182
    }
183

    
184
    /**
185
     * DOCUMENT ME!
186
     *
187
     * @param ds DOCUMENT ME!
188
     * @throws com.hardcode.gdbms.engine.data.driver.DriverException 
189
     */
190
    public void replaceDataSource(SelectableDataSource ds) throws com.hardcode.gdbms.engine.data.driver.DriverException {
191
        if (original == null){
192
            original = modelo;
193
        }
194
        modelo = ds;
195
        ds.setSelectionSupport(original.getSelectionSupport());
196
        createAlias();
197
        
198
        // FJP:
199
        // Si la tabla proviene de un layer, cambiamos su recordset
200
        if (associatedTable != null)
201
        {
202
            if (associatedTable instanceof FLyrVect)
203
            {                
204
                try {
205
                    ((FLyrVect) associatedTable).setRecordset(ds);
206
                } catch (FieldNotFoundException e) {
207
                    // TODO Auto-generated catch block
208
                    e.printStackTrace();
209
                } catch (DriverException e) {
210
                    // TODO Auto-generated catch block
211
                    e.printStackTrace();
212
                }
213
                ((FLyrVect) associatedTable).setIsJoined(true);
214
            }
215
        }
216
        
217
        change.firePropertyChange("model", original, modelo);
218
    }
219

    
220
    /**
221
     * DOCUMENT ME!
222
     * @throws com.hardcode.gdbms.engine.data.driver.DriverException 
223
     */
224
    public void restoreDataSource() throws com.hardcode.gdbms.engine.data.driver.DriverException {
225
        // FJP:
226
        // Si la tabla proviene de un layer, cambiamos su recordset
227
        if (associatedTable != null)
228
        {
229
            if (associatedTable instanceof FLyrVect)
230
            {      
231
                // Miramos si la leyenda que est? usando es una
232
                // leyenda basada en un campo de los de la uni?n.
233
                // Si lo es, no dejamos pegarle el cambiazo al recordset
234
                FLyrVect lyr = ((FLyrVect) associatedTable);
235
                if (lyr.getLegend() instanceof ClassifiedLegend)
236
                {
237
                    ClassifiedLegend legend = (ClassifiedLegend) lyr.getLegend();
238
                    VectorialLegend aux = (VectorialLegend) legend;
239
                    String fieldName = legend.getFieldName();
240
                    int idField = original.getFieldIndexByName(fieldName);
241
                    int idLabelField = -2;
242
                    if (aux.getLabelField() != null)
243
                    {
244
                        idLabelField = original.getFieldIndexByName(aux.getLabelField());
245
                    }
246
                    if ((idField == -1) || (idLabelField == -1))
247
                    {
248
                        // No se ha encontrado ese campo, o se est? etiquetando por ese campo
249
                        JOptionPane.showMessageDialog(null,
250
                                PluginServices.getText(this, "leyenda_campo_unido")
251
                                );
252

    
253
                        return;
254
                    }
255
                }
256
                
257
                try {
258
                    lyr.setRecordset(original);
259
                } catch (FieldNotFoundException e) {
260
                    // TODO Auto-generated catch block
261
                    e.printStackTrace();
262
                } catch (DriverException e) {
263
                    // TODO Auto-generated catch block
264
                    e.printStackTrace();
265
                }
266
                lyr.setIsJoined(false);
267
            }
268
        }
269

    
270
        
271
        modelo = original;
272
        original = null;
273
        createAlias();
274

    
275
        
276
        change.firePropertyChange("model", original, modelo);
277
    }
278

    
279
    /**
280
     * DOCUMENT ME!
281
     *
282
     * @return DOCUMENT ME!
283
     *
284
     * @throws XMLException
285
     */
286
    public XMLEntity getXMLEntity() throws XMLException {
287
        XMLEntity xml = super.getXMLEntity();
288

    
289
        //xml.putProperty("nameClass", this.getClass().getName());
290
        xml.putProperty("numTables", numTables);
291

    
292
        if (getLinkTable() != null) {
293
            xml.putProperty("linkTable", linkTable);
294
            xml.putProperty("field1", field1);
295
            xml.putProperty("field2", field2);
296
        }
297

    
298
        if (getOriginal() != null) {
299
            xml.addChild(getOriginal().getXMLEntity());
300
        }
301

    
302
        xml.addChild(modelo.getXMLEntity());
303

    
304
        Object di = LayerFactory.getDataSourceFactory().getDriverInfo(modelo.getName());
305

    
306
        if (associatedTable != null) {
307
            xml.putProperty("layerName", ((FLayer) associatedTable).getName());
308
            xml.putProperty("viewName",
309
                project.getView((FLayer) associatedTable));
310
        }
311
        
312
        xml.putProperty("mapping", mapping);
313
        xml.putProperty("aliases", alias);
314
        
315
        return xml;
316
    }
317

    
318
    /**
319
     * DOCUMENT ME!
320
     *
321
     * @param xml DOCUMENT ME!
322
     * @param p DOCUMENT ME!
323
     *
324
     * @throws XMLException
325
     * @throws DriverException
326
     *
327
     * @see com.iver.cit.gvsig.project.ProjectElement#setXMLEntity(com.iver.utiles.XMLEntity)
328
     */
329
    public void setXMLEntity03(XMLEntity xml, Project p)
330
        throws XMLException, DriverException {
331
        numTables = xml.getIntProperty("numTables");
332

    
333
        if (xml.getStringProperty("type").equals("otherDriverFile")) {
334
            LayerFactory.getDataSourceFactory().addFileDataSource(xml.getStringProperty(
335
                    "driverName"), xml.getStringProperty("gdbmsname"),
336
                xml.getStringProperty("file"));
337

    
338
            setSelectableDataSource03(xml);
339
        } else if (xml.getStringProperty("type").equals("sameDriverFile")) {
340
            String layerName = xml.getStringProperty("layerName");
341

    
342
            ProjectView vista = project.getViewByName(xml.getStringProperty(
343
                        "viewName"));
344
            FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
345

    
346
            modelo = ((AlphanumericData) layer).getRecordset();
347
            associatedTable = (AlphanumericData) layer;
348

    
349
            LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver) ((SingleLayer) layer).getSource()
350
                                                                              .getDriver(),
351
                xml.getStringProperty("gdbmsname"));
352
        } else if (xml.getStringProperty("type").equals("db")) {
353
            LayerFactory.getDataSourceFactory().addDBDataSourceByTable(xml.getStringProperty(
354
                    "gdbmsname"), xml.getStringProperty("host"),
355
                xml.getIntProperty("port"), xml.getStringProperty("user"),
356
                xml.getStringProperty("password"),
357
                xml.getStringProperty("dbName"),
358
                xml.getStringProperty("tableName"),
359
                xml.getStringProperty("driverInfo"));
360

    
361
            setSelectableDataSource03(xml);
362
        }
363

    
364
        setName(xml.getStringProperty("name"));
365
    }
366

    
367
    /**
368
     * DOCUMENT ME!
369
     *
370
     * @param xml DOCUMENT ME!
371
     * @param p DOCUMENT ME!
372
     *
373
     * @throws XMLException
374
     * @throws DriverException
375
     *
376
     * @see com.iver.cit.gvsig.project.ProjectElement#setXMLEntity(com.iver.utiles.XMLEntity)
377
     */
378
    public void setXMLEntity(XMLEntity xml, Project p)
379
        throws XMLException, DriverException {
380
        setName(xml.getStringProperty("name"));
381
        numTables = xml.getIntProperty("numTables");
382

    
383
        setSelectableDataSource(xml);
384

    
385
        /*
386
           if (xml.getStringProperty("type").equals("otherDriverFile")) {
387
           } else if (xml.getStringProperty("type").equals("sameDriverFile")) {
388
                   String layerName = xml.getStringProperty("layerName");
389
                   ProjectView vista = project.getViewByName(xml.getStringProperty(
390
                   "viewName"));
391
                   FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
392
        
393
                   modelo = ((AlphanumericData) layer).getRecordset();
394
                   associatedTable = (AlphanumericData) layer;
395
           } else if (xml.getStringProperty("type").equals("db")) {
396
                   setSelectableDataSource(xml);
397
           }
398
         */
399
        setName(xml.getStringProperty("name"));
400

    
401
        if (xml.contains("linkTable")) {
402
            setLinkTable(xml.getStringProperty("linkTable"),
403
                xml.getStringProperty("field1"), xml.getStringProperty("field2"));
404
        }
405

    
406
        if (xml.contains("mapping")){
407
            mapping = (int[]) xml.getIntArrayProperty("mapping");
408
            alias = (String[]) xml.getStringArrayProperty("aliases");
409
        }else{
410
            try {
411
                createAlias();
412
            } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
413
                throw new XMLException(e);
414
            }
415
        }
416

    
417
    }
418

    
419
    /**
420
     * DOCUMENT ME!
421
     *
422
     * @param xml DOCUMENT ME!
423
     *
424
     * @throws XMLException DOCUMENT ME!
425
     * @throws DriverException DOCUMENT ME!
426
     */
427
    private void setSelectableDataSource03(XMLEntity xml)
428
        throws XMLException, DriverException {
429
        String layerName = null;
430

    
431
        if (xml.contains("layerName")) {
432
            layerName = xml.getStringProperty("layerName");
433
        }
434

    
435
        if (layerName == null) {
436
            DataSource dataSource;
437

    
438
            try {
439
                dataSource = LayerFactory.getDataSourceFactory()
440
                                         .createRandomDataSource(xml.getStringProperty(
441
                            "gdbmsname"),
442
                        DataSourceFactory.AUTOMATIC_OPENING);
443

    
444
                SelectableDataSource sds = new SelectableDataSource(dataSource);
445

    
446
                sds.setXMLEntity03(xml.getChild(0));
447

    
448
                setDataSource(sds);
449
            } catch (NoSuchTableException e) {
450
                throw new XMLException(e);
451
            } catch (DriverLoadException e) {
452
                throw new XMLException(e);
453
            } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
454
                throw new XMLException(e);
455
            }
456

    
457
        } else {
458
            ProjectView vista = project.getViewByName(xml.getStringProperty(
459
                        "viewName"));
460
            FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
461

    
462
            modelo = ((AlphanumericData) layer).getRecordset();
463
            associatedTable = (AlphanumericData) layer;
464
        }
465
    }
466

    
467
    /**
468
     * DOCUMENT ME!
469
     *
470
     * @param xml DOCUMENT ME!
471
     *
472
     * @throws XMLException DOCUMENT ME!
473
     * @throws DriverException DOCUMENT ME!
474
     */
475
    private void setSelectableDataSource(XMLEntity xml)
476
        throws XMLException, DriverException {
477
        String layerName = null;
478
        boolean bNeedToReplace = false;
479
        XMLEntity xmlAux = null;
480

    
481
        try {
482
            SelectableDataSource sds;
483

    
484
            if (xml.contains("layerName")) {
485
                layerName = xml.getStringProperty("layerName");
486

    
487
                ProjectView vista = project.getViewByName(xml.getStringProperty(
488
                            "viewName"));
489
                       FLayer layer=getLayer(vista.getMapContext().getLayers(),layerName);
490
                sds = ((AlphanumericData) layer).getRecordset();
491
            } else {
492
                sds = SelectableDataSource.createSelectableDataSource(xml.getChild(
493
                            0));
494
            }
495

    
496
            setDataSource(sds);
497

    
498
            if (xml.getNumChild() == 2) {
499
                bNeedToReplace = true;
500
                xmlAux = xml.getChild(1);
501
                // replaceDataSource(SelectableDataSource.createSelectableDataSource(xml.getChild(1)));
502
            }
503
        } catch (NoSuchTableException e) {
504
            throw new XMLException(e);
505
        } catch (ParseException e) {
506
            throw new XMLException(e);
507
        } catch (DriverLoadException e) {
508
            throw new XMLException(e);
509
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
510
            throw new XMLException(e);
511
        } catch (SemanticException e) {
512
            throw new XMLException(e);
513
        } catch (IOException e) {
514
            throw new XMLException(e);
515
        }
516

    
517
        if (layerName != null) {
518
            ProjectView vista = project.getViewByName(xml.getStringProperty(
519
                        "viewName"));
520
                FLayer layer=getLayer(vista.getMapContext().getLayers(),layerName);
521

    
522
            //                        modelo = ((AlphanumericData) layer).getRecordset();
523
            associatedTable = (AlphanumericData) layer;
524
            
525
            if (bNeedToReplace)
526
            {
527
                try {
528
                    replaceDataSource(SelectableDataSource.createSelectableDataSource(xmlAux));
529
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
530
                    // TODO Auto-generated catch block
531
                    e.printStackTrace();
532
                } catch (NoSuchTableException e) {
533
                    // TODO Auto-generated catch block
534
                    e.printStackTrace();
535
                } catch (ParseException e) {
536
                    // TODO Auto-generated catch block
537
                    e.printStackTrace();
538
                } catch (DriverLoadException e) {
539
                    // TODO Auto-generated catch block
540
                    e.printStackTrace();
541
                } catch (SemanticException e) {
542
                    // TODO Auto-generated catch block
543
                    e.printStackTrace();
544
                } catch (IOException e) {
545
                    // TODO Auto-generated catch block
546
                    e.printStackTrace();
547
                } catch (XMLException e) {
548
                    // TODO Auto-generated catch block
549
                    e.printStackTrace();
550
                }
551
            }
552
        }
553
    }
554
    private FLayer getLayer(FLayers layers,String name){
555
            for (int i=0;i<layers.getLayersCount();i++){
556
                    if (layers.getLayer(i) instanceof FLayers){
557
                            return getLayer((FLayers)layers.getLayer(i),name);
558
                    }else if (layers.getLayer(i).getName().equals(name)){
559
                            return layers.getLayer(i);
560
                    }
561
            }
562
    return null;
563
    }
564
    /**
565
     * DOCUMENT ME!
566
     *
567
     * @return DOCUMENT ME!
568
     */
569
    public AlphanumericData getAssociatedTable() {
570
        return associatedTable;
571
    }
572

    
573
    /**
574
     * DOCUMENT ME!
575
     *
576
     * @param associatedTable DOCUMENT ME!
577
     */
578
    public void setAssociatedTable(AlphanumericData associatedTable) {
579
        this.associatedTable = associatedTable;
580
    }
581

    
582
    /**
583
     * Obtiene la fuente de datos original de la tabla si se ha invocado
584
     * replaceDataSource. Si no se invoc? este m?todo o se invoc?
585
     * posteriormente restoreDataSource se devuelve null
586
     *
587
     * @return Returns the original.
588
     */
589
    public SelectableDataSource getOriginal() {
590
        return original;
591
    }
592

    
593
    /**
594
     * Devuelve el identificador de la tabla que contiene el link.
595
     *
596
     * @return identificador ?nico de la tabla.
597
     */
598
    public String getLinkTable() {
599
        return linkTable;
600
    }
601

    
602
    /**
603
     * Devuelve el nombre del campo de la tabla a enlazar.
604
     *
605
     * @return Nombre del campo de la tabla a enlazar.
606
     */
607
    public String getField1() {
608
        return field1;
609
    }
610

    
611
    /**
612
     * Devuelve el nombre del campo de la tabla enlazada.
613
     *
614
     * @return Nombre del campo de la tabla enlazada.
615
     */
616
    public String getField2() {
617
        return field2;
618
    }
619

    
620
    /**
621
     * Inserta el identificador de la tabla, el campo de la primera tabla y el
622
     * campo de la segunda tabla.
623
     *
624
     * @param lt identificado de la tabla.
625
     * @param f1 nombre del campo de la primera tabla.
626
     * @param f2 nombre del campo de la segunda tabla.
627
     */
628
    public void setLinkTable(String lt, String f1, String f2) {
629
        linkTable = lt;
630
        field1 = f1;
631
        field2 = f2;
632
    }
633

    
634
    /**
635
     * Borra el identificador de la tabla y elimina del array de listener los
636
     * listener que sean del tipo: LinkSelectionListenr
637
     */
638
    public void removeLinkTable() {
639
        linkTable = null;
640
        getModelo().removeLinksSelectionListener();
641
    }
642
    public String[] getAliases() {
643
        return alias;
644
    }
645
    public void setAliases(String[] alias) {
646
        this.alias = alias;
647
    }
648
    public int[] getMapping() {
649
        return mapping;
650
    }
651
    public void setMapping(int[] mapping) {
652
        this.mapping = mapping;
653
    }
654
}