root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / driver / vect / dbf / DbfMemoryDataSource.java @ 21606
History | View | Annotate | Download (12.1 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 |
* http://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 |
* or
|
44 |
*
|
45 |
* Instituto de Rob?tica
|
46 |
* Apartado de correos 2085
|
47 |
* 46071 Valencia
|
48 |
* (Spain)
|
49 |
*
|
50 |
* +34 963 543 577
|
51 |
* jjordan@robotica.uv.es
|
52 |
* http://robotica.uv.es
|
53 |
*
|
54 |
*/
|
55 |
|
56 |
package es.prodevelop.gvsig.mobile.fmap.driver.vect.dbf; |
57 |
|
58 |
import java.io.File; |
59 |
import java.io.IOException; |
60 |
import java.sql.Types; |
61 |
|
62 |
import org.apache.log4j.Logger; |
63 |
|
64 |
import com.hardcode.driverManager.Driver; |
65 |
import com.hardcode.gdbms.engine.data.DataSource; |
66 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
67 |
import com.hardcode.gdbms.engine.data.IDataSourceListener; |
68 |
import com.hardcode.gdbms.engine.data.SourceInfo; |
69 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
70 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
71 |
import com.hardcode.gdbms.engine.data.persistence.Memento; |
72 |
import com.hardcode.gdbms.engine.data.persistence.MementoException; |
73 |
import com.hardcode.gdbms.engine.values.Value; |
74 |
import com.hardcode.gdbms.engine.values.ValueCollection; |
75 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
76 |
|
77 |
/**
|
78 |
* This class implements a DataSource which is based on
|
79 |
* a DBF file fully loaded into memory.
|
80 |
*
|
81 |
* @see es.prodevelop.gvsig.mobile.fmap.driver.vect.dbf.DbaseFileNIO
|
82 |
*
|
83 |
* @author jldominguez
|
84 |
*
|
85 |
*/
|
86 |
public class DbfMemoryDataSource implements DataSource { |
87 |
|
88 |
private static Logger logger = Logger.getLogger(DbfMemoryDataSource.class); |
89 |
private File dbFile = null; |
90 |
private DbaseFileNIO dbFileAccess = null; |
91 |
|
92 |
private int[] fieldTypes = null; |
93 |
|
94 |
/**
|
95 |
* Constructor
|
96 |
* @param _dbFile the DBF file to read
|
97 |
*/
|
98 |
public DbfMemoryDataSource(File _dbFile) { |
99 |
dbFile = _dbFile; |
100 |
dbFileAccess = new DbaseFileNIO();
|
101 |
} |
102 |
|
103 |
/**
|
104 |
* Initializes the dfata source (reads the file into memory)
|
105 |
*/
|
106 |
public void start() throws DriverException { |
107 |
|
108 |
try {
|
109 |
|
110 |
dbFileAccess.open(dbFile); |
111 |
|
112 |
int fcnt = dbFileAccess.getFieldCount();
|
113 |
fieldTypes = new int[fcnt]; |
114 |
for (int i=0; i<fcnt; i++) { |
115 |
fieldTypes[i] = dbFileAccess.getFieldIntType(i); |
116 |
} |
117 |
|
118 |
} catch (IOException e) { |
119 |
logger.error("Error while opening db file " + dbFile.getAbsolutePath() + ": " + e.getMessage()); |
120 |
throw new DriverException(e.getMessage()); |
121 |
} |
122 |
} |
123 |
|
124 |
/**
|
125 |
* Stops the datasource. Tries to close the DBF file.
|
126 |
*/
|
127 |
public void stop() throws DriverException { |
128 |
try {
|
129 |
dbFileAccess.close(); |
130 |
} catch (IOException e) { |
131 |
logger.error("Error while closing db file " + dbFile.getAbsolutePath() + ": " + e.getMessage()); |
132 |
throw new DriverException(e.getMessage()); |
133 |
} |
134 |
} |
135 |
|
136 |
/**
|
137 |
* @return the data source generic name
|
138 |
*/
|
139 |
public String getName() { |
140 |
return "DBF Data Source"; |
141 |
} |
142 |
|
143 |
/**
|
144 |
* @return an array representing the WHERE filter. Currently unused.
|
145 |
*/
|
146 |
public long[] getWhereFilter() throws IOException { |
147 |
logger.warn("Method getWhereFilter() was called, returned new long[0]");
|
148 |
return new long[0]; |
149 |
} |
150 |
|
151 |
/**
|
152 |
* @return the associated datasource factory. Currently unused.
|
153 |
*/
|
154 |
public DataSourceFactory getDataSourceFactory() {
|
155 |
logger.warn("Method getDataSourceFactory() was called, returned NULL");
|
156 |
return null; |
157 |
} |
158 |
|
159 |
/**
|
160 |
* @return the memento. Currently unused.
|
161 |
*/
|
162 |
public Memento getMemento() throws MementoException { |
163 |
logger.warn("Method getMemento() was called, returned NULL");
|
164 |
return null; |
165 |
} |
166 |
|
167 |
/**
|
168 |
* Sets the datasource factory. Currently unused.
|
169 |
*
|
170 |
* @param arg0 the new data source factory
|
171 |
*/
|
172 |
public void setDataSourceFactory(DataSourceFactory arg0) { |
173 |
logger.warn("Method setDataSourceFactory(...) was called, nothing done.");
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* Sets the source info. Currently unused.
|
178 |
* @param arg0 the new source info
|
179 |
*/
|
180 |
public void setSourceInfo(SourceInfo arg0) { |
181 |
logger.warn("Method setSourceInfo(...) was called, nothing done.");
|
182 |
} |
183 |
|
184 |
/**
|
185 |
* @return the source info. Currently unused.
|
186 |
*/
|
187 |
public SourceInfo getSourceInfo() {
|
188 |
logger.warn("Method getSourceInfo() was called, returned NULL");
|
189 |
return null; |
190 |
} |
191 |
|
192 |
/**
|
193 |
* @return a string representing the datasource. Unused. Simply redirects to
|
194 |
* <code>getName()</code>
|
195 |
*/
|
196 |
public String getAsString() throws DriverException { |
197 |
logger.warn("Method getAsString() was called, returned getName()");
|
198 |
return getName();
|
199 |
} |
200 |
|
201 |
/**
|
202 |
* Called when the layer is removed. Tries to prevent memory leaks.
|
203 |
*
|
204 |
*/
|
205 |
public void remove() throws DriverException { |
206 |
logger.warn("Method remove() was called, nothing done.");
|
207 |
} |
208 |
|
209 |
/**
|
210 |
* @return an array with the indices of the primary keys.
|
211 |
*/
|
212 |
public int[] getPrimaryKeys() throws DriverException { |
213 |
logger.warn("Method getPrimaryKeys() was called, returned all indices.");
|
214 |
int size = dbFileAccess.getFieldCount();
|
215 |
int[] resp = new int[size]; |
216 |
for (int i=0; i<size; i++) resp[i] = i; |
217 |
return resp;
|
218 |
} |
219 |
|
220 |
/**
|
221 |
* Gets the primary key value for a row
|
222 |
* @param arg0 the row of interest
|
223 |
* @return a collection representing the primary key
|
224 |
*/
|
225 |
public ValueCollection getPKValue(long arg0) throws DriverException { |
226 |
logger.warn("Method getPKValue(" + arg0 + ") was called, returned NULL"); |
227 |
return null; |
228 |
} |
229 |
|
230 |
/**
|
231 |
* Gets the name of one of the fields that compose the PK.
|
232 |
* @param arg0 the index of the PK field of interest
|
233 |
* @return the name of the field of interest
|
234 |
*/
|
235 |
public String getPKName(int arg0) throws DriverException { |
236 |
logger.warn("Method getPKName(" + arg0 + ") was called, returned NULL"); |
237 |
return null; |
238 |
} |
239 |
|
240 |
/**
|
241 |
* @return the names of the fields that compose the PK.
|
242 |
*
|
243 |
*/
|
244 |
public String[] getPKNames() throws DriverException { |
245 |
logger.warn("Method getPKNames() was called, returned NULL");
|
246 |
return null; |
247 |
} |
248 |
|
249 |
/**
|
250 |
* Gets the type of one of the fields that compose the PK.
|
251 |
*
|
252 |
* @param arg0 the index of the field of interest
|
253 |
* @return the type of the field of interest
|
254 |
*/
|
255 |
public int getPKType(int arg0) throws DriverException { |
256 |
logger.warn("Method getPKType(" + arg0 + ") was called, returned getFieldType(...)"); |
257 |
return dbFileAccess.getFieldType(arg0);
|
258 |
} |
259 |
|
260 |
/**
|
261 |
* @return the cumber of fields that compose the PK.
|
262 |
*/
|
263 |
public int getPKCardinality() throws DriverException { |
264 |
return dbFileAccess.getFieldCount();
|
265 |
} |
266 |
|
267 |
private Value getValueOfItsType(int fieldindex, int row) { |
268 |
|
269 |
Value resp = ValueFactory.createNullValue(); |
270 |
|
271 |
int type = dbFileAccess.getFieldIntType(fieldindex);
|
272 |
switch (type) {
|
273 |
|
274 |
case Types.DOUBLE: |
275 |
if (getFieldDecimalPrecision(fieldindex) > 0) { |
276 |
resp = ValueFactory.createValue( |
277 |
dbFileAccess.getNumberFieldValue( |
278 |
row, fieldindex |
279 |
).doubleValue()); |
280 |
} else {
|
281 |
resp = ValueFactory.createValue( |
282 |
dbFileAccess.getNumberFieldValue( |
283 |
row, fieldindex |
284 |
).intValue()); |
285 |
} |
286 |
break;
|
287 |
case Types.VARCHAR: |
288 |
resp = ValueFactory.createValue( |
289 |
dbFileAccess.getStringFieldValue( |
290 |
row, fieldindex |
291 |
)); |
292 |
break;
|
293 |
case Types.DATE: |
294 |
resp = ValueFactory.createValue( |
295 |
dbFileAccess.getStringFieldValue( |
296 |
row, fieldindex |
297 |
)); |
298 |
break;
|
299 |
case Types.BOOLEAN: |
300 |
resp = ValueFactory.createValue( |
301 |
dbFileAccess.getBooleanFieldValue( |
302 |
row, fieldindex |
303 |
)); |
304 |
break;
|
305 |
} |
306 |
return resp;
|
307 |
} |
308 |
|
309 |
/**
|
310 |
* Gets a row of this data source.
|
311 |
* @param rowind the index of the row of interest
|
312 |
* @return the row as an array of values
|
313 |
*/
|
314 |
public Value[] getRow(long rowind) throws DriverException { |
315 |
|
316 |
int size = dbFileAccess.getFieldCount();
|
317 |
Value[] resp = new Value[size]; |
318 |
for (int i=0; i<size; i++) { |
319 |
resp[i] = getValueOfItsType(i, (int) rowind);
|
320 |
} |
321 |
return resp;
|
322 |
} |
323 |
|
324 |
/**
|
325 |
* @return an array with all the field names
|
326 |
*/
|
327 |
public String[] getFieldNames() throws DriverException { |
328 |
|
329 |
int size = dbFileAccess.getFieldCount();
|
330 |
String[] fnames = new String[size]; |
331 |
for (int i=0; i<size; i++) { |
332 |
fnames[i] = dbFileAccess.getFieldName(i); |
333 |
} |
334 |
return fnames;
|
335 |
} |
336 |
|
337 |
/**
|
338 |
* Gets the idnex of a field, provided its name.
|
339 |
* @param arg0 the name of the field of interest
|
340 |
* @return the index of the field of interest
|
341 |
*/
|
342 |
public int getFieldIndexByName(String arg0) throws DriverException { |
343 |
int size = dbFileAccess.getFieldCount();
|
344 |
for (int i=0; i<size; i++) { |
345 |
if (arg0.compareToIgnoreCase(dbFileAccess.getFieldName(i)) == 0) return i; |
346 |
} |
347 |
logger.error("Field name not found: " + arg0);
|
348 |
return -1; |
349 |
} |
350 |
|
351 |
/**
|
352 |
* Gets the data ware associated with the given index.
|
353 |
* Currently unused.
|
354 |
*
|
355 |
* @param arg0 the index of interest
|
356 |
* @return the associated data ware
|
357 |
*/
|
358 |
public DataWare getDataWare(int arg0) throws DriverException { |
359 |
logger.warn("Method getDataWare(" + arg0 + ") returned NULL."); |
360 |
return null; |
361 |
} |
362 |
|
363 |
/**
|
364 |
* Gets whether the referenced field is virtual or not.
|
365 |
*
|
366 |
* @param arg0 the index of the field of interest
|
367 |
* @return whether it is a virtual field.
|
368 |
*
|
369 |
*/
|
370 |
public boolean isVirtualField(int arg0) throws DriverException { |
371 |
logger.warn("Method isVirtualField(" + arg0 + ") returned FALSE."); |
372 |
return false; |
373 |
} |
374 |
|
375 |
/**
|
376 |
* @return the associated driver, currently unused.
|
377 |
*/
|
378 |
public Driver getDriver() { |
379 |
logger.warn("Method getDriver() returned NULL.");
|
380 |
return null; |
381 |
} |
382 |
|
383 |
/**
|
384 |
* Reloads the data source (stops and starts)
|
385 |
*/
|
386 |
public void reload() throws DriverException, IOException { |
387 |
stop(); |
388 |
dbFileAccess = new DbaseFileNIO();
|
389 |
start(); |
390 |
} |
391 |
|
392 |
/**
|
393 |
* Adds a datasource listener. Currently unused.
|
394 |
* @param arg0 the listener to be added.
|
395 |
*/
|
396 |
public void addDataSourceListener(IDataSourceListener arg0) { |
397 |
logger.warn("Method addDataSourceListener(), nothing done.");
|
398 |
} |
399 |
|
400 |
/**
|
401 |
* Removes a datasource listener. Currently unused.
|
402 |
* @param arg0 the listener to be removed.
|
403 |
*/
|
404 |
public void removeDataSourceListener(IDataSourceListener arg0) { |
405 |
logger.warn("Method removeDataSourceListener(), nothing done.");
|
406 |
} |
407 |
|
408 |
/**
|
409 |
* Gets a value from thsi data source.
|
410 |
* @param arg0 the row of interest
|
411 |
* @param arg1 the index of the field of interest
|
412 |
*
|
413 |
* @return the value of the given row and field
|
414 |
*/
|
415 |
public Value getFieldValue(long arg0, int arg1) throws DriverException { |
416 |
return getValueOfItsType(arg1, (int) arg0); |
417 |
} |
418 |
|
419 |
/**
|
420 |
* @return the number of fields in this datasource
|
421 |
*/
|
422 |
public int getFieldCount() throws DriverException { |
423 |
return dbFileAccess.getFieldCount();
|
424 |
} |
425 |
|
426 |
/**
|
427 |
* Gets the name of the field with the given index.
|
428 |
* @param arg0 the index of the field of interest
|
429 |
* @return the name of the field
|
430 |
*/
|
431 |
public String getFieldName(int arg0) throws DriverException { |
432 |
return dbFileAccess.getFieldName(arg0);
|
433 |
} |
434 |
|
435 |
/**
|
436 |
* @return the number of rows in this data source
|
437 |
*/
|
438 |
public long getRowCount() throws DriverException { |
439 |
return dbFileAccess.getRecordCount();
|
440 |
} |
441 |
|
442 |
/**
|
443 |
* Gets the type of the field with the given index.
|
444 |
* @param arg0 the index of the field of interest
|
445 |
* @return teh field type
|
446 |
*/
|
447 |
public int getFieldType(int arg0) throws DriverException { |
448 |
return dbFileAccess.getFieldIntType(arg0);
|
449 |
} |
450 |
|
451 |
/**
|
452 |
* Gets the width of the field with the given index.
|
453 |
* @param arg0 the index of the field of interest
|
454 |
* @return teh field width
|
455 |
*/
|
456 |
public int getFieldWidth(int arg0) throws DriverException { |
457 |
return dbFileAccess.getFieldLength(arg0);
|
458 |
} |
459 |
|
460 |
public int getFieldDecimalPrecision(int i) { |
461 |
return dbFileAccess.getFieldDecimalLength(i);
|
462 |
} |
463 |
|
464 |
} |