Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialDBAdapter.java @ 3959

History | View | Annotate | Download (7.02 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.fmap.layers;
42

    
43
import java.awt.geom.Rectangle2D;
44
import java.io.IOException;
45
import java.sql.Connection;
46
import java.sql.ResultSet;
47
import java.sql.SQLException;
48
import java.sql.Statement;
49

    
50
import com.hardcode.driverManager.DriverLoadException;
51
import com.hardcode.gdbms.engine.data.DataSource;
52
import com.hardcode.gdbms.engine.data.DataSourceFactory;
53
import com.hardcode.gdbms.engine.data.NoSuchTableException;
54
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
55
import com.hardcode.gdbms.engine.values.Value;
56
import com.iver.cit.gvsig.fmap.DriverException;
57
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
58
import com.iver.cit.gvsig.fmap.core.FShape;
59
import com.iver.cit.gvsig.fmap.core.IFeature;
60
import com.iver.cit.gvsig.fmap.core.IGeometry;
61
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
62
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
63
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator;
64
import com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver;
65

    
66

    
67

    
68
/**
69
 * Adapta un driver de base de datos vectorial a la interfaz vectorial,
70
 * manteniendo adem?s el estado necesario por una capa vectorial de base de
71
 * datos (par?metros de la conexi?n)
72
 */
73
public class VectorialDBAdapter extends VectorialAdapter implements VectorialSecuencial {
74
    private int numReg=-1;
75
    private SelectableDataSource ds = null;
76
        /**
77
         * incrementa el contador de las veces que se ha abierto el fichero.
78
         * Solamente cuando el contador est? a cero pide al driver que conecte con
79
         * la base de datos
80
         */
81
        public void start() {
82
            try {
83
                ((VectorialDatabaseDriver)driver).open();
84
            } catch (DriverException e) {
85
                // TODO Auto-generated catch block
86
                e.printStackTrace();
87
            }
88
        }
89

    
90
        /**
91
         * decrementa el contador de n?mero de aperturas y cuando llega a cero pide
92
         * al driver que cierre la conexion con el servidor de base de datos
93
         */
94
        public void stop() {
95
            ((VectorialDatabaseDriver)driver).close();
96
        }
97

    
98
        /**
99
         * @return devuelve la Conexi?n a la base de datos, para que
100
         * el usuario pueda hacer la consulta que quiera, si lo desea.
101
         * Por ejemplo, esto puede ser ?til para abrir un cuadro de dialogo
102
         * avanazado y lanzar peticiones del tipo "Devuelveme un buffer
103
         * a las autopistas", y con el resultset que te venga, escribir
104
         * un shape, o cosas as?.
105
         */
106
        /* public Connection getConnection()
107
        {
108
            return ((VectorialDatabaseDriver)driver).getConnection();
109
        }*/
110
        /* public IFeatureIterator getFeatureIterator(String sql) throws DriverException
111
        {
112
            return ((VectorialDatabaseDriver)driver).getFeatureIterator(sql);
113
        }*/
114
        public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) throws DriverException
115
        {
116
            return ((VectorialDatabaseDriver)driver).getFeatureIterator(r, strEPSG);
117
        }
118

    
119
    public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, String[] alphaNumericFieldsNeeded) throws DriverException
120
    {
121
        return ((VectorialDatabaseDriver)driver).getFeatureIterator(r, strEPSG, alphaNumericFieldsNeeded);
122
    }
123

    
124
        public String[] getFields()
125
        {
126
            return ((VectorialDatabaseDriver)driver).getFields();
127
        }
128
        public String getWhereClause()
129
        {
130
            return ((VectorialDatabaseDriver)driver).getWhereClause();
131
        }
132
        public String getTableName()
133
        {
134
            return ((VectorialDatabaseDriver)driver).getTableName();
135
        }
136

    
137

    
138
        /**
139
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
140
         */
141
        public IGeometry getShape(int index) throws DriverIOException {
142
            IGeometry geom = null;
143
            try {
144
            geom = ((VectorialDatabaseDriver)driver).getShape(index);
145
        } catch (IOException e) {
146
            throw new DriverIOException(e);
147
        }
148
            return geom;
149
        }
150

    
151
        /**
152
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
153
         */
154
        public int getShapeType() throws DriverIOException {
155
                return ((VectorialDatabaseDriver)driver).getShapeType();
156
        }
157

    
158
        /**
159
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
160
         */
161
        public SelectableDataSource getRecordset() throws DriverLoadException {
162
            if (driver instanceof ObjectDriver)
163
            {
164
            if (ds == null)
165
            {
166
                            String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver);
167
                            try {
168
                    ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING));
169
                } catch (NoSuchTableException e) {
170
                    throw new RuntimeException(e);
171
                            } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
172
                                    throw new RuntimeException(e);
173
                            }
174
            }
175
            }
176
                return ds;
177
        }
178

    
179
    /* (non-Javadoc)
180
     * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getFeature(int)
181
     */
182
    public IFeature getFeature(int numReg) throws DriverException {
183
        IGeometry geom;
184
        IFeature feat = null;
185
        try {
186
            geom = getShape(numReg);
187
            DataSource rs = getRecordset();
188
            int idFieldID = getLyrDef().getIdFieldID();
189
            Value[] regAtt = new Value[rs.getFieldCount()];
190
            String theID = null;
191
            for (int fieldId=0; fieldId < rs.getFieldCount(); fieldId++ )
192
            {
193
                regAtt[fieldId] =  rs.getFieldValue(numReg, fieldId);
194
                if (fieldId == idFieldID)
195
                    theID = regAtt[fieldId].toString();
196
            }
197

    
198
            feat = new DefaultFeature(geom, regAtt, theID);
199
        } catch (DriverIOException e) {
200
            throw new DriverException(e);
201
        } catch (DriverLoadException e) {
202
            throw new DriverException(e);
203
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
204
            throw new DriverException(e);
205
        }
206
        return feat;
207
    }
208

    
209
    public DBLayerDefinition getLyrDef()
210
    {
211
        return ((VectorialDatabaseDriver)driver).getLyrDef();
212
    }
213
}