Statistics
| Revision:

svn-gvsig-desktop / tags / Root_v061 / applications / appgvSIG / src / com / iver / cit / gvsig / project / ProjectTable.java @ 4812

History | View | Annotate | Download (22.2 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.cit.gvsig.gui.project.OpenException;
70
import com.iver.cit.gvsig.gui.project.SaveException;
71
import com.iver.utiles.XMLEntity;
72

    
73

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

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

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

    
103
        change.firePropertyChange("visibles", true, true);
104
    }
105

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

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

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

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

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

    
161
        return t;
162
    }
163

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

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

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

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

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

    
272
        
273
        modelo = original;
274
        original = null;
275
        createAlias();
276

    
277
        
278
        change.firePropertyChange("model", original, modelo);
279
    }
280

    
281
    /**
282
     * DOCUMENT ME!
283
     *
284
     * @return DOCUMENT ME!
285
     * @throws SaveException 
286
     *
287
     * @throws XMLException
288
     */
289
    public XMLEntity getXMLEntity() throws SaveException {
290
        XMLEntity xml = super.getXMLEntity();
291
        try {
292
        //xml.putProperty("nameClass", this.getClass().getName());
293
        xml.putProperty("numTables", numTables);
294

    
295
        if (getLinkTable() != null) {
296
            xml.putProperty("linkTable", linkTable);
297
            xml.putProperty("field1", field1);
298
            xml.putProperty("field2", field2);
299
        }
300

    
301
        if (getOriginal() != null) {
302
            xml.addChild(getOriginal().getXMLEntity());
303
        }
304

    
305
        xml.addChild(modelo.getXMLEntity());
306

    
307
        Object di = LayerFactory.getDataSourceFactory().getDriverInfo(modelo.getName());
308

    
309
        if (associatedTable != null) {
310
            xml.putProperty("layerName", ((FLayer) associatedTable).getName());
311
            xml.putProperty("viewName",
312
                project.getView((FLayer) associatedTable));
313
        }
314
        
315
        xml.putProperty("mapping", mapping);
316
        xml.putProperty("aliases", alias);
317
        }catch (Exception e) {
318
                        throw new SaveException(e,this.getClass().getName());
319
                }
320
        return xml;
321
    }
322

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

    
338
        if (xml.getStringProperty("type").equals("otherDriverFile")) {
339
            LayerFactory.getDataSourceFactory().addFileDataSource(xml.getStringProperty(
340
                    "driverName"), xml.getStringProperty("gdbmsname"),
341
                xml.getStringProperty("file"));
342

    
343
            setSelectableDataSource03(xml);
344
        } else if (xml.getStringProperty("type").equals("sameDriverFile")) {
345
            String layerName = xml.getStringProperty("layerName");
346

    
347
            ProjectView vista = project.getViewByName(xml.getStringProperty(
348
                        "viewName"));
349
            FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
350

    
351
            modelo = ((AlphanumericData) layer).getRecordset();
352
            associatedTable = (AlphanumericData) layer;
353

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

    
366
            setSelectableDataSource03(xml);
367
        }
368

    
369
        setName(xml.getStringProperty("name"));
370
    }
371

    
372
    /**
373
     * DOCUMENT ME!
374
     *
375
     * @param xml DOCUMENT ME!
376
     * @param p DOCUMENT ME!
377
     *
378
     * @throws XMLException
379
     * @throws DriverException
380
     * @throws OpenException 
381
     *
382
     * @see com.iver.cit.gvsig.project.ProjectElement#setXMLEntity(com.iver.utiles.XMLEntity)
383
     */
384
    public void setXMLEntity(XMLEntity xml, Project p)
385
        throws XMLException, DriverException, OpenException {
386
        try{
387
            setName(xml.getStringProperty("name"));
388
        numTables = xml.getIntProperty("numTables");
389

    
390
        setSelectableDataSource(xml);
391

    
392
        /*
393
           if (xml.getStringProperty("type").equals("otherDriverFile")) {
394
           } else if (xml.getStringProperty("type").equals("sameDriverFile")) {
395
                   String layerName = xml.getStringProperty("layerName");
396
                   ProjectView vista = project.getViewByName(xml.getStringProperty(
397
                   "viewName"));
398
                   FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
399
        
400
                   modelo = ((AlphanumericData) layer).getRecordset();
401
                   associatedTable = (AlphanumericData) layer;
402
           } else if (xml.getStringProperty("type").equals("db")) {
403
                   setSelectableDataSource(xml);
404
           }
405
         */
406
        setName(xml.getStringProperty("name"));
407

    
408
        if (xml.contains("linkTable")) {
409
            setLinkTable(xml.getStringProperty("linkTable"),
410
                xml.getStringProperty("field1"), xml.getStringProperty("field2"));
411
        }
412

    
413
        if (xml.contains("mapping")) {
414
                mapping = (int[]) xml.getIntArrayProperty("mapping");
415
                alias = (String[]) xml.getStringArrayProperty("aliases");
416
        } else {
417
                try {
418
                        createAlias();
419
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
420
                        throw new XMLException(e);
421
                }
422
        }
423
        }catch (Exception e) {
424
                throw new OpenException(e,this.getClass().getName());
425
        }
426
    }
427

    
428
    /**
429
     * DOCUMENT ME!
430
     *
431
     * @param xml DOCUMENT ME!
432
     *
433
     * @throws XMLException DOCUMENT ME!
434
     * @throws DriverException DOCUMENT ME!
435
     */
436
    private void setSelectableDataSource03(XMLEntity xml)
437
        throws XMLException, DriverException {
438
        String layerName = null;
439

    
440
        if (xml.contains("layerName")) {
441
            layerName = xml.getStringProperty("layerName");
442
        }
443

    
444
        if (layerName == null) {
445
            DataSource dataSource;
446

    
447
            try {
448
                dataSource = LayerFactory.getDataSourceFactory()
449
                                         .createRandomDataSource(xml.getStringProperty(
450
                            "gdbmsname"),
451
                        DataSourceFactory.AUTOMATIC_OPENING);
452

    
453
                SelectableDataSource sds = new SelectableDataSource(dataSource);
454

    
455
                sds.setXMLEntity03(xml.getChild(0));
456

    
457
                setDataSource(sds);
458
            } catch (NoSuchTableException e) {
459
                throw new XMLException(e);
460
            } catch (DriverLoadException e) {
461
                throw new XMLException(e);
462
            } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
463
                throw new XMLException(e);
464
            }
465

    
466
        } else {
467
            ProjectView vista = project.getViewByName(xml.getStringProperty(
468
                        "viewName"));
469
            FLayer layer = vista.getMapContext().getLayers().getLayer(layerName);
470

    
471
            modelo = ((AlphanumericData) layer).getRecordset();
472
            associatedTable = (AlphanumericData) layer;
473
        }
474
    }
475

    
476
    /**
477
     * DOCUMENT ME!
478
     *
479
     * @param xml DOCUMENT ME!
480
     *
481
     * @throws XMLException DOCUMENT ME!
482
     * @throws DriverException DOCUMENT ME!
483
     */
484
    private void setSelectableDataSource(XMLEntity xml)
485
        throws XMLException, DriverException {
486
        String layerName = null;
487
        boolean bNeedToReplace = false;
488
        XMLEntity xmlAux = null;
489

    
490
        try {
491
            SelectableDataSource sds;
492

    
493
            if (xml.contains("layerName")) {
494
                layerName = xml.getStringProperty("layerName");
495

    
496
                ProjectView vista = project.getViewByName(xml.getStringProperty(
497
                            "viewName"));
498
                       FLayer layer=getLayer(vista.getMapContext().getLayers(),layerName);
499
                sds = ((AlphanumericData) layer).getRecordset();
500
            } else {
501
                sds = SelectableDataSource.createSelectableDataSource(xml.getChild(
502
                            0));
503
            }
504

    
505
            setDataSource(sds);
506

    
507
            if (xml.getNumChild() == 2) {
508
                bNeedToReplace = true;
509
                xmlAux = xml.getChild(1);
510
            }
511
            
512
            if (bNeedToReplace) {
513
                                if (layerName != null) {
514
                                        ProjectView vista = project.getViewByName(xml
515
                                                        .getStringProperty("viewName"));
516
                                        FLayer layer = getLayer(vista.getMapContext().getLayers(),
517
                                                        layerName);
518

    
519
                                        // modelo = ((AlphanumericData) layer).getRecordset();
520
                                        associatedTable = (AlphanumericData) layer;
521
                                }
522
                                replaceDataSource(SelectableDataSource.createSelectableDataSource(xmlAux));
523
                        }
524
        } catch (NoSuchTableException e) {
525
            throw new XMLException(e);
526
        } catch (ParseException e) {
527
            throw new XMLException(e);
528
        } catch (DriverLoadException e) {
529
            throw new XMLException(e);
530
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
531
            throw new XMLException(e);
532
        } catch (SemanticException e) {
533
            throw new XMLException(e);
534
        } catch (IOException e) {
535
            throw new XMLException(e);
536
        }
537

    
538
        if (layerName != null) {
539
            ProjectView vista = project.getViewByName(xml.getStringProperty(
540
                        "viewName"));
541
                FLayer layer=getLayer(vista.getMapContext().getLayers(),layerName);
542

    
543
            //                        modelo = ((AlphanumericData) layer).getRecordset();
544
            associatedTable = (AlphanumericData) layer;
545
            
546
            if (bNeedToReplace)
547
            {
548
                try {
549
                    replaceDataSource(SelectableDataSource.createSelectableDataSource(xmlAux));
550
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
551
                    // TODO Auto-generated catch block
552
                    e.printStackTrace();
553
                } catch (NoSuchTableException e) {
554
                    // TODO Auto-generated catch block
555
                    e.printStackTrace();
556
                } catch (ParseException e) {
557
                    // TODO Auto-generated catch block
558
                    e.printStackTrace();
559
                } catch (DriverLoadException e) {
560
                    // TODO Auto-generated catch block
561
                    e.printStackTrace();
562
                } catch (SemanticException e) {
563
                    // TODO Auto-generated catch block
564
                    e.printStackTrace();
565
                } catch (IOException e) {
566
                    // TODO Auto-generated catch block
567
                    e.printStackTrace();
568
                } catch (XMLException e) {
569
                    // TODO Auto-generated catch block
570
                    e.printStackTrace();
571
                }
572
            }
573
        }
574
    }
575
    private FLayer getLayer(FLayers layers,String name){
576
            for (int i=0;i<layers.getLayersCount();i++){
577
                    if (layers.getLayer(i) instanceof FLayers){
578
                            return getLayer((FLayers)layers.getLayer(i),name);
579
                    }else if (layers.getLayer(i).getName().equals(name)){
580
                            return layers.getLayer(i);
581
                    }
582
            }
583
    return null;
584
    }
585
    /**
586
     * DOCUMENT ME!
587
     *
588
     * @return DOCUMENT ME!
589
     */
590
    public AlphanumericData getAssociatedTable() {
591
        return associatedTable;
592
    }
593

    
594
    /**
595
     * DOCUMENT ME!
596
     *
597
     * @param associatedTable DOCUMENT ME!
598
     */
599
    public void setAssociatedTable(AlphanumericData associatedTable) {
600
        this.associatedTable = associatedTable;
601
    }
602

    
603
    /**
604
     * Obtiene la fuente de datos original de la tabla si se ha invocado
605
     * replaceDataSource. Si no se invoc? este m?todo o se invoc?
606
     * posteriormente restoreDataSource se devuelve null
607
     *
608
     * @return Returns the original.
609
     */
610
    public SelectableDataSource getOriginal() {
611
        return original;
612
    }
613

    
614
    /**
615
     * Devuelve el identificador de la tabla que contiene el link.
616
     *
617
     * @return identificador ?nico de la tabla.
618
     */
619
    public String getLinkTable() {
620
        return linkTable;
621
    }
622

    
623
    /**
624
     * Devuelve el nombre del campo de la tabla a enlazar.
625
     *
626
     * @return Nombre del campo de la tabla a enlazar.
627
     */
628
    public String getField1() {
629
        return field1;
630
    }
631

    
632
    /**
633
     * Devuelve el nombre del campo de la tabla enlazada.
634
     *
635
     * @return Nombre del campo de la tabla enlazada.
636
     */
637
    public String getField2() {
638
        return field2;
639
    }
640

    
641
    /**
642
     * Inserta el identificador de la tabla, el campo de la primera tabla y el
643
     * campo de la segunda tabla.
644
     *
645
     * @param lt identificado de la tabla.
646
     * @param f1 nombre del campo de la primera tabla.
647
     * @param f2 nombre del campo de la segunda tabla.
648
     */
649
    public void setLinkTable(String lt, String f1, String f2) {
650
        linkTable = lt;
651
        field1 = f1;
652
        field2 = f2;
653
    }
654

    
655
    /**
656
     * Borra el identificador de la tabla y elimina del array de listener los
657
     * listener que sean del tipo: LinkSelectionListenr
658
     */
659
    public void removeLinkTable() {
660
        linkTable = null;
661
        getModelo().removeLinksSelectionListener();
662
    }
663
    public String[] getAliases() {
664
        return alias;
665
    }
666
    public void setAliases(String[] alias) {
667
        this.alias = alias;
668
    }
669
    public int[] getMapping() {
670
        return mapping;
671
    }
672
    public void setMapping(int[] mapping) {
673
        this.mapping = mapping;
674
    }
675
}