Statistics
| Revision:

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

History | View | Annotate | Download (6.59 KB)

1 1100 fjp
/* 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 214 fernando
package com.iver.cit.gvsig.fmap.layers;
42
43 1828 fernando
import java.io.File;
44 442 vcaballero
45 1828 fernando
import com.hardcode.driverManager.DriverLoadException;
46 10627 caballero
import com.hardcode.gdbms.driver.exceptions.CloseDriverException;
47
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
48
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
49 408 fernando
import com.hardcode.gdbms.engine.data.DataSource;
50
import com.hardcode.gdbms.engine.data.DataSourceFactory;
51
import com.hardcode.gdbms.engine.data.NoSuchTableException;
52 1828 fernando
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
53 3248 fjp
import com.hardcode.gdbms.engine.values.Value;
54
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
55
import com.iver.cit.gvsig.fmap.core.IFeature;
56 305 fjp
import com.iver.cit.gvsig.fmap.core.IGeometry;
57 504 fernando
import com.iver.cit.gvsig.fmap.drivers.ExternalData;
58 214 fernando
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
59 229 vcaballero
60
61 214 fernando
/**
62 229 vcaballero
 * 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 214 fernando
 */
66 3601 fjp
public class VectorialFileAdapter extends VectorialAdapter {
67 504 fernando
        private boolean driverInitialized = false;
68
        private File file;
69 3959 caballero
        private SelectableDataSource ds;
70 546 fernando
        private String dataSourceName;
71 3959 caballero
72 1327 fjp
        /**
73
         * <code>reference_count</code> lleva un contador de referencias a este
74 3959 caballero
         * adaptador, de forma que si la cuenta de referencias no es cero, no
75 1327 fjp
         * abrimos otra vez el adaptador porque se supone que est? abierto.
76
         */
77
        private int reference_count = 0;
78 229 vcaballero
79 504 fernando
        /**
80
         * Crea un nuevo VectorialFileAdapter.
81
         *
82 1034 vcaballero
         * @param file Fichero.
83 504 fernando
         */
84
        public VectorialFileAdapter(File file) {
85
                this.file = file;
86
        }
87 214 fernando
88 504 fernando
        /**
89 1034 vcaballero
         * Devuelve driver.
90 504 fernando
         *
91 1034 vcaballero
         * @return VectorialFileDriver.
92 504 fernando
         */
93 3936 fjp
        VectorialFileDriver getFileDriver() {
94 504 fernando
                return (VectorialFileDriver) getDriver();
95
        }
96 229 vcaballero
97 504 fernando
        /**
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 10627 caballero
         * @throws InitializeDriverException
102 504 fernando
         */
103 10627 caballero
        public synchronized void start() throws ReadDriverException, InitializeDriverException {
104 1327 fjp
                    if (reference_count == 0)
105
                    {
106
                                getFileDriver().open(file);
107 3959 caballero
108 1327 fjp
                                if (!driverInitialized) {
109
                                        getFileDriver().initialize();
110
                                        driverInitialized = true;
111
                                }
112 14588 vcaballero
                                getRecordset().start();
113 1327 fjp
                    }
114
                        reference_count++;
115 504 fernando
        }
116 214 fernando
117 504 fernando
        /**
118
         * decrementa el contador de n?mero de aperturas y cuando llega a cero pide
119
         * al driver que cierre el fichero
120
         */
121 10627 caballero
        public synchronized void stop() throws ReadDriverException {
122 504 fernando
                try {
123 1327 fjp
                    if (reference_count == 0)
124
                    {
125
                        getFileDriver().close();
126
                    }
127
                    else
128
                        if (reference_count < 0)
129 3959 caballero
                            throw new RuntimeException("Contador de referencias de driver ="
130 1327 fjp
                                    + reference_count + ". Demasiados stop().");
131
                    reference_count--;
132 10627 caballero
                } catch (CloseDriverException e) {
133
                        throw new ReadDriverException(getDriver().getName(),e);
134 504 fernando
                }
135
        }
136 214 fernando
137 504 fernando
        /**
138 4580 azabala
         * Is synchronized to allow thread safe access to features stored
139
         * in files.
140 10627 caballero
         *
141 504 fernando
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
142
         */
143 10627 caballero
        public synchronized IGeometry getShape(int index) throws ReadDriverException {
144
                return getFileDriver().getShape(index);
145 504 fernando
        }
146 214 fernando
147 504 fernando
        /**
148
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
149
         */
150 10627 caballero
        public int getShapeType() throws ReadDriverException {
151 504 fernando
                return getFileDriver().getShapeType();
152
        }
153 408 fernando
154 504 fernando
        /**
155 10627 caballero
         * @throws ReadDriverException
156 504 fernando
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
157
         */
158 10665 caballero
        public SelectableDataSource getRecordset() throws ReadDriverException {
159
                String name =null;
160 508 fernando
                try {
161 1034 vcaballero
                        if (ds == null) {
162
                                VectorialFileDriver driver = (VectorialFileDriver) getDriver();
163 442 vcaballero
164 1034 vcaballero
                                if (driver instanceof ExternalData) {
165
                                        ExternalData ed = (ExternalData) driver;
166
                                        File dataFile = ed.getDataFile(file);
167
                                        String driverName = ed.getDataDriverName();
168 442 vcaballero
169 10665 caballero
                                        name = LayerFactory.getDataSourceFactory().addFileDataSource(driverName,
170 2217 fernando
                                                dataFile.getAbsolutePath());
171 14588 vcaballero
                                        ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING));
172 1828 fernando
                                } else if (driver instanceof ObjectDriver) {
173 10665 caballero
                                        name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver);
174 14588 vcaballero
                                        ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.AUTOMATIC_OPENING));
175 1034 vcaballero
                                } else {
176
                                        return null;
177
                                }
178 463 fernando
                        }
179 504 fernando
                } catch (NoSuchTableException e) {
180
                        throw new RuntimeException(
181
                                "Error de implementaci?n, se ha a?adido una tabla y luego esa tabla no ha podido ser cargada");
182 10665 caballero
                } catch (DriverLoadException e) {
183
                        throw new ReadDriverException(name,e);
184 504 fernando
                }
185
186 508 fernando
                return ds;
187 504 fernando
        }
188 442 vcaballero
189 504 fernando
        /**
190 1034 vcaballero
         * Devuelve el fichero.
191 504 fernando
         *
192 1034 vcaballero
         * @return Fichero.
193 504 fernando
         */
194
        public File getFile() {
195
                return file;
196
        }
197 4580 azabala
        /**
198
         * Returns the feature whose index is numReg
199
         * <br>
200
         * Is synchronized to do thread safe accessing to features
201
         * stored in files.
202
         * @param numReg index of feature
203
         * @return feature
204 10627 caballero
         *
205 4580 azabala
         */
206 10627 caballero
    public synchronized IFeature getFeature(int numReg) throws ReadDriverException
207 3248 fjp
    {
208
        IGeometry geom;
209
        IFeature feat = null;
210 10627 caballero
        geom = getShape(numReg);
211
        DataSource rs = getRecordset();
212
        Value[] regAtt = new Value[rs.getFieldCount()];
213
        for (int fieldId=0; fieldId < rs.getFieldCount(); fieldId++ )
214
        {
215
            regAtt[fieldId] =  rs.getFieldValue(numReg, fieldId);
216 3248 fjp
        }
217 10627 caballero
        feat = new DefaultFeature(geom, regAtt, "" + numReg);
218 3248 fjp
        return feat;
219
    }
220 3959 caballero
221 214 fernando
}