root / branches / v10 / extensions / extOracleSpatial / src / es / prodevelop / cit / gvsig / fmap / drivers / jdbc / oracle / OFileDataSourceAdapter.java @ 13991
History | View | Annotate | Download (7.49 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Prodevelop 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 |
* Prodevelop Integraci?n de Tecnolog?as SL
|
34 |
* Conde Salvatierra de ?lava , 34-10
|
35 |
* 46004 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963 510 612
|
39 |
* +34 963 510 968
|
40 |
* gis@prodevelop.es
|
41 |
* http://www.prodevelop.es
|
42 |
*/
|
43 |
package es.prodevelop.cit.gvsig.fmap.drivers.jdbc.oracle; |
44 |
|
45 |
import com.hardcode.driverManager.Driver; |
46 |
import com.hardcode.driverManager.DriverLoadException; |
47 |
|
48 |
import com.hardcode.gdbms.engine.data.SourceInfo; |
49 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
50 |
import com.hardcode.gdbms.engine.data.driver.FileDriver; |
51 |
import com.hardcode.gdbms.engine.data.driver.GDBMSDriver; |
52 |
import com.hardcode.gdbms.engine.data.driver.ReadAccess; |
53 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
54 |
import com.hardcode.gdbms.engine.data.file.AbstractFileDataSource; |
55 |
import com.hardcode.gdbms.engine.data.file.FileDataSource; |
56 |
import com.hardcode.gdbms.engine.data.file.FileDataSourceFactory; |
57 |
import com.hardcode.gdbms.engine.data.file.FileDataWare; |
58 |
import com.hardcode.gdbms.engine.data.file.FileSourceInfo; |
59 |
import com.hardcode.gdbms.engine.values.Value; |
60 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
61 |
|
62 |
import java.io.File; |
63 |
import java.io.IOException; |
64 |
|
65 |
import java.sql.Types; |
66 |
|
67 |
|
68 |
/**
|
69 |
* This class is a simple FileDataSource adapter used to create a data source
|
70 |
* based on a DBF file which contains the oracle <--> epsg codes.
|
71 |
*
|
72 |
* @author jldominguez
|
73 |
*
|
74 |
*/
|
75 |
class OFileDataSourceAdapter extends AbstractFileDataSource |
76 |
implements FileDataSource {
|
77 |
private File file; |
78 |
private FileDriver driver;
|
79 |
private int sem = 0; |
80 |
private int fieldCount = -1; |
81 |
|
82 |
/**
|
83 |
* @see com.hardcode.gdbms.engine.data.DataSource#start()
|
84 |
*/
|
85 |
public void start() throws DriverException { |
86 |
try {
|
87 |
if (sem == 0) { |
88 |
driver.open(file); |
89 |
} |
90 |
|
91 |
sem++; |
92 |
} |
93 |
catch (IOException e) { |
94 |
throw new DriverException(e); |
95 |
} |
96 |
} |
97 |
|
98 |
/**
|
99 |
* @see com.hardcode.gdbms.engine.data.DataSource#stop()
|
100 |
*/
|
101 |
public void stop() throws DriverException { |
102 |
try {
|
103 |
sem--; |
104 |
|
105 |
if (sem == 0) { |
106 |
driver.close(); |
107 |
} |
108 |
else if (sem < 0) { |
109 |
throw new RuntimeException("DataSource closed too many times"); |
110 |
} |
111 |
} |
112 |
catch (IOException e) { |
113 |
throw new DriverException(e); |
114 |
} |
115 |
} |
116 |
|
117 |
/**
|
118 |
* Asigna el driver al adaptador
|
119 |
*
|
120 |
* @param driver
|
121 |
* The driver to set.
|
122 |
*/
|
123 |
public void setDriver(FileDriver driver) { |
124 |
this.driver = driver;
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Sets the source information of the DataSource
|
129 |
*
|
130 |
* @param sourceInfo
|
131 |
* The file to set.
|
132 |
*/
|
133 |
public void setSourceInfo(SourceInfo sourceInfo) { |
134 |
super.setSourceInfo(sourceInfo);
|
135 |
file = new File(((FileSourceInfo) sourceInfo).file); |
136 |
} |
137 |
|
138 |
/**
|
139 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDriver()
|
140 |
*/
|
141 |
public ReadAccess getReadDriver() {
|
142 |
return driver;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* @see com.hardcode.gdbms.engine.data.DataSource#getName()
|
147 |
*/
|
148 |
public String getName() { |
149 |
return sourceInfo.name;
|
150 |
} |
151 |
|
152 |
/**
|
153 |
* @see com.hardcode.gdbms.engine.data.DataSource#getPrimaryKeys()
|
154 |
*/
|
155 |
public int[] getPrimaryKeys() throws DriverException { |
156 |
// The last field is the pk/row in FileDataSources
|
157 |
return new int[] { getFieldCount() - 1 }; |
158 |
} |
159 |
|
160 |
/**
|
161 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldCount()
|
162 |
*/
|
163 |
public int getFieldCount() throws DriverException { |
164 |
if (fieldCount == -1) { |
165 |
fieldCount = getReadDriver().getFieldCount() + 1;
|
166 |
} |
167 |
|
168 |
return fieldCount;
|
169 |
} |
170 |
|
171 |
/**
|
172 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldName(int)
|
173 |
*/
|
174 |
public String getFieldName(int fieldId) throws DriverException { |
175 |
// last field is the virtual primary key
|
176 |
if (fieldId == (getFieldCount() - 1)) { |
177 |
return "PK"; |
178 |
} |
179 |
|
180 |
return getReadDriver().getFieldName(fieldId);
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldType(int)
|
185 |
*/
|
186 |
public int getFieldType(int i) throws DriverException { |
187 |
// last field is the virtual primary key
|
188 |
if (i == (getFieldCount() - 1)) { |
189 |
return Types.BIGINT; |
190 |
} |
191 |
|
192 |
return getReadDriver().getFieldType(i);
|
193 |
} |
194 |
|
195 |
/**
|
196 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getFieldValue(long,
|
197 |
* int)
|
198 |
*/
|
199 |
public Value getFieldValue(long rowIndex, int fieldId) |
200 |
throws DriverException {
|
201 |
// last field is the virtual primary key
|
202 |
if (fieldId == (getFieldCount() - 1)) { |
203 |
return ValueFactory.createValue(rowIndex);
|
204 |
} |
205 |
|
206 |
Value v = getReadDriver().getFieldValue(rowIndex, fieldId); |
207 |
|
208 |
return (v == null) ? ValueFactory.createNullValue() : v; |
209 |
} |
210 |
|
211 |
/**
|
212 |
* @see com.hardcode.gdbms.engine.data.file.FileDataSource#getDriver()
|
213 |
*/
|
214 |
public Driver getDriver() { |
215 |
return driver;
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* @throws DriverException
|
220 |
* @see com.hardcode.gdbms.engine.data.DataSource#getDataWare(int)
|
221 |
*/
|
222 |
public DataWare getDataWare(int mode) throws DriverException { |
223 |
try {
|
224 |
FileDataWare dw = FileDataSourceFactory.newDataWareInstance(); |
225 |
FileDriver driver; |
226 |
driver = (FileDriver) getDataSourceFactory().getDriverManager() |
227 |
.getDriver(getDriver().getName()); |
228 |
((GDBMSDriver) driver).setDataSourceFactory(getDataSourceFactory()); |
229 |
dw.setDriver(driver); |
230 |
dw.setDataSourceFactory(dsf); |
231 |
dw.setSourceInfo(getSourceInfo()); |
232 |
|
233 |
return dw;
|
234 |
} |
235 |
catch (DriverLoadException e) {
|
236 |
throw new DriverException(e); |
237 |
} |
238 |
} |
239 |
|
240 |
public int getFieldWidth(int i) throws DriverException { |
241 |
return getReadDriver().getFieldWidth(i);
|
242 |
} |
243 |
|
244 |
public boolean isVirtualField(int fieldId) throws DriverException { |
245 |
// last field is the virtual primary key
|
246 |
if (fieldId == (this.getFieldCount() - 1)) { |
247 |
return true; |
248 |
} |
249 |
|
250 |
return false; |
251 |
} |
252 |
|
253 |
public void reload() throws DriverException { |
254 |
try {
|
255 |
sem = 0;
|
256 |
driver.close(); |
257 |
} |
258 |
catch (IOException e) { |
259 |
throw new DriverException(e); |
260 |
} |
261 |
|
262 |
this.fieldCount = -1; |
263 |
|
264 |
this.start();
|
265 |
|
266 |
this.raiseEventReloaded();
|
267 |
} |
268 |
} |