Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / VectorialFileAdapter.java @ 10627

History | View | Annotate | Download (6.56 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

    
45
import com.hardcode.driverManager.DriverLoadException;
46
import com.hardcode.gdbms.driver.exceptions.CloseDriverException;
47
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
48
import com.hardcode.gdbms.driver.exceptions.OpenDriverException;
49
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
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.values.Value;
55
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
56
import com.iver.cit.gvsig.fmap.core.IFeature;
57
import com.iver.cit.gvsig.fmap.core.IGeometry;
58
import com.iver.cit.gvsig.fmap.drivers.ExternalData;
59
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
60

    
61

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

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

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

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

    
98
        /**
99
         * incrementa el contador de las veces que se ha abierto el fichero.
100
         * Solamente cuando el contador est? a cero pide al driver que abra el
101
         * fichero
102
         * @throws InitializeDriverException
103
         */
104
        public synchronized void start() throws ReadDriverException, InitializeDriverException {
105
                    if (reference_count == 0)
106
                    {
107
                                getFileDriver().open(file);
108

    
109
                                if (!driverInitialized) {
110
                                        getFileDriver().initialize();
111
                                        driverInitialized = true;
112
                                }
113
                    }
114
                        reference_count++;
115
        }
116

    
117
        /**
118
         * decrementa el contador de n?mero de aperturas y cuando llega a cero pide
119
         * al driver que cierre el fichero
120
         */
121
        public synchronized void stop() throws ReadDriverException {
122
                try {
123
                    if (reference_count == 0)
124
                    {
125
                        getFileDriver().close();
126
                    }
127
                    else
128
                        if (reference_count < 0)
129
                            throw new RuntimeException("Contador de referencias de driver ="
130
                                    + reference_count + ". Demasiados stop().");
131
                    reference_count--;
132
                } catch (CloseDriverException e) {
133
                        throw new ReadDriverException(getDriver().getName(),e);
134
                }
135
        }
136

    
137
        /**
138
         * Is synchronized to allow thread safe access to features stored
139
         * in files.
140
         *
141
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
142
         */
143
        public synchronized IGeometry getShape(int index) throws ReadDriverException {
144
                return getFileDriver().getShape(index);
145
        }
146

    
147
        /**
148
         * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
149
         */
150
        public int getShapeType() throws ReadDriverException {
151
                return getFileDriver().getShapeType();
152
        }
153

    
154
        /**
155
         * @throws ReadDriverException
156
         * @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
157
         */
158
        public SelectableDataSource getRecordset() throws DriverLoadException, ReadDriverException {
159
                try {
160
                        if (ds == null) {
161
                                VectorialFileDriver driver = (VectorialFileDriver) getDriver();
162

    
163
                                if (driver instanceof ExternalData) {
164
                                        ExternalData ed = (ExternalData) driver;
165
                                        File dataFile = ed.getDataFile(file);
166
                                        String driverName = ed.getDataDriverName();
167

    
168
                                        String name = LayerFactory.getDataSourceFactory().addFileDataSource(driverName,
169
                                                dataFile.getAbsolutePath());
170
                                        ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.MANUAL_OPENING));
171
                                } else if (driver instanceof ObjectDriver) {
172
                                        String name = LayerFactory.getDataSourceFactory().addDataSource((ObjectDriver)driver);
173
                                        ds = new SelectableDataSource(LayerFactory.getDataSourceFactory().createRandomDataSource(name, DataSourceFactory.MANUAL_OPENING));
174
                                } else {
175
                                        return null;
176
                                }
177
                        }
178
                } catch (NoSuchTableException e) {
179
                        throw new RuntimeException(
180
                                "Error de implementaci?n, se ha a?adido una tabla y luego esa tabla no ha podido ser cargada");
181
                }
182

    
183
                return ds;
184
        }
185

    
186
        /**
187
         * Devuelve el fichero.
188
         *
189
         * @return Fichero.
190
         */
191
        public File getFile() {
192
                return file;
193
        }
194
        /**
195
         * Returns the feature whose index is numReg
196
         * <br>
197
         * Is synchronized to do thread safe accessing to features
198
         * stored in files.
199
         * @param numReg index of feature
200
         * @return feature
201
         *
202
         */
203
    public synchronized IFeature getFeature(int numReg) throws ReadDriverException
204
    {
205
        IGeometry geom;
206
        IFeature feat = null;
207
        geom = getShape(numReg);
208
        DataSource rs = getRecordset();
209
        Value[] regAtt = new Value[rs.getFieldCount()];
210
        for (int fieldId=0; fieldId < rs.getFieldCount(); fieldId++ )
211
        {
212
            regAtt[fieldId] =  rs.getFieldValue(numReg, fieldId);
213
        }
214
        feat = new DefaultFeature(geom, regAtt, "" + numReg);
215
        return feat;
216
    }
217

    
218
}