Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_910 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialFileAdapter.java @ 11275

History | View | Annotate | Download (7.94 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.io.File;
44
import java.io.IOException;
45

    
46
import com.hardcode.driverManager.DriverLoadException;
47
import com.hardcode.gdbms.engine.data.DataSource;
48
import com.hardcode.gdbms.engine.data.DataSourceFactory;
49
import com.hardcode.gdbms.engine.data.NoSuchTableException;
50
import com.hardcode.gdbms.engine.data.driver.DriverException;
51
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
52
import com.hardcode.gdbms.engine.values.Value;
53
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
54
import com.iver.cit.gvsig.fmap.core.IFeature;
55
import com.iver.cit.gvsig.fmap.core.IGeometry;
56
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
57
import com.iver.cit.gvsig.fmap.drivers.ExternalData;
58
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
59

    
60

    
61
/**
62
 * Adapta un driver de fichero vectorial a la interfaz vectorial, manteniendo
63
 * adem?s el estado necesario por una capa vectorial de fichero (el nombre del
64
 * fichero)
65
 */
66
public class VectorialFileAdapter extends VectorialAdapter {
67
        private boolean driverInitialized = false;
68
        private File file;
69
        private SelectableDataSource ds;
70
        private String dataSourceName;
71

    
72
        /**
73
         * <code>reference_count</code> lleva un contador de referencias a este
74
         * adaptador, de forma que si la cuenta de referencias no es cero, no
75
         * abrimos otra vez el adaptador porque se supone que est? abierto.
76
         */
77
        private int reference_count = 0;
78

    
79
        /**
80
         * Crea un nuevo VectorialFileAdapter.
81
         *
82
         * @param file Fichero.
83
         */
84
        public VectorialFileAdapter(File file) {
85
                this.file = file;
86
        }
87

    
88
        /**
89
         * Devuelve driver.
90
         *
91
         * @return VectorialFileDriver.
92
         */
93
        VectorialFileDriver getFileDriver() {
94
                return (VectorialFileDriver) getDriver();
95
        }
96

    
97
        /**
98
         * incrementa el contador de las veces que se ha abierto el fichero.
99
         * Solamente cuando el contador est? a cero pide al driver que abra el
100
         * fichero
101
         *
102
         * @throws DriverIOException
103
         */
104
        public synchronized void start() throws DriverIOException {
105
                try {
106
                    if (reference_count == 0)
107
                    {
108
                            //System.out.println("====" + hashCode() + " Abrir el fichero: ["+ reference_count + "]"+ file.getAbsolutePath() );
109
                            getFileDriver().open(file);
110

    
111
                                if (!driverInitialized) {
112
                                        getFileDriver().initialize();
113
                                        driverInitialized = true;
114
                                }
115
                                getRecordset().start();
116
                    }
117
                        reference_count++;
118

    
119
                } catch (IOException e) {
120
                        throw new DriverIOException(e);
121
                } catch (DriverException e) {
122
                        // TODO Auto-generated catch block
123
                        throw new DriverIOException(e);
124
                }
125
        }
126

    
127
        /**
128
         * decrementa el contador de n?mero de aperturas y cuando llega a cero pide
129
         * al driver que cierre el fichero
130
         *
131
         * @throws DriverIOException
132
         */
133
        public synchronized void stop() throws DriverIOException {
134
                try {
135
                    if (reference_count == 1)
136
                    {
137
                            //System.out.println("====" + hashCode() + " Cerrar el fichero: ["+ reference_count + "]"+ file.getAbsolutePath() );
138
                        getFileDriver().close();
139
                        getRecordset().stop();
140
                    }
141
                    else
142
                        if (reference_count < 0)
143
                            throw new RuntimeException("Contador de referencias de driver ="
144
                                    + reference_count + ". Demasiados stop().");
145
                    reference_count--;
146
                } catch (IOException e) {
147
                        throw new DriverIOException(e);
148
                } catch (DriverLoadException e) {
149
                        // TODO Auto-generated catch block
150
                        e.printStackTrace();
151
                } catch (DriverException e) {
152
                        // TODO Auto-generated catch block
153
                        e.printStackTrace();
154
                }
155
        }
156

    
157
        /**
158
         * Is synchronized to allow thread safe access to features stored
159
         * in files.
160
         * 
161
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
162
         */
163
        public synchronized IGeometry getShape(int index) throws DriverIOException {
164
                try {
165
                        return getFileDriver().getShape(index);
166
                } catch (IOException e) {
167
                        throw new DriverIOException(e);
168
                }
169
        }
170

    
171
        /**
172
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
173
         */
174
        public int getShapeType() throws DriverIOException {
175
                return getFileDriver().getShapeType();
176
        }
177

    
178
        /**
179
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
180
         */
181
        public SelectableDataSource getRecordset() throws DriverLoadException {
182
                try {
183
                        if (ds == null) {
184
                                VectorialFileDriver driver = (VectorialFileDriver) getDriver();
185

    
186
                                if (driver instanceof ExternalData) {
187
                                        ExternalData ed = (ExternalData) driver;
188
                                        File dataFile = ed.getDataFile(file);
189
                                        String driverName = ed.getDataDriverName();
190

    
191
                                        String name = LayerFactory.getDataSourceFactory().addFileDataSource(driverName,
192
                                                dataFile.getAbsolutePath());
193
//                                         CHEMA: AUTOMATIC DATA SOURCE
194
                                        //ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.MANUAL_OPENING));
195
                                        ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING));
196
                                } else if (driver instanceof ObjectDriver) {
197
                                        String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver);
198
//                                         CHEMA: AUTOMATIC DATA SOURCE
199
                                        //ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.MANUAL_OPENING));
200
                                        ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING));
201
                                } else {
202
                                        return null;
203
                                }
204
                        }
205
                } catch (NoSuchTableException e) {
206
                        throw new RuntimeException(
207
                                "Error de implementaci?n, se ha a?adido una tabla y luego esa tabla no ha podido ser cargada");
208
                } catch (DriverException e) {
209
                        throw new RuntimeException(e);
210
                }
211

    
212
                return ds;
213
        }
214

    
215
        /**
216
         * Devuelve el fichero.
217
         *
218
         * @return Fichero.
219
         */
220
        public File getFile() {
221
                return file;
222
        }
223
        /**
224
         * Returns the feature whose index is numReg
225
         * <br>
226
         * Is synchronized to do thread safe accessing to features
227
         * stored in files.
228
         * @param numReg index of feature
229
         * @return feature
230
         * 
231
         */
232
    public synchronized IFeature getFeature(int numReg) throws com.iver.cit.gvsig.fmap.DriverException
233
    {
234
        IGeometry geom;
235
        IFeature feat = null;
236
        try {
237
            geom = getShape(numReg);
238
            DataSource rs = getRecordset();
239
            Value[] regAtt = new Value[rs.getFieldCount()];
240
            for (int fieldId=0; fieldId < rs.getFieldCount(); fieldId++ )
241
            {
242
                regAtt[fieldId] =  rs.getFieldValue(numReg, fieldId);
243
            }
244

    
245
            feat = new DefaultFeature(geom, regAtt, "" + numReg);
246
        } catch (DriverIOException e) {
247
            throw new com.iver.cit.gvsig.fmap.DriverException(e);
248
        } catch (DriverLoadException e) {
249
            throw new com.iver.cit.gvsig.fmap.DriverException(e);
250
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
251
            throw new com.iver.cit.gvsig.fmap.DriverException(e);
252
        }
253
        return feat;
254
    }
255

    
256
}