root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / store / dbf / DBFStoreProvider.java @ 24248
History | View | Annotate | Download (13.4 KB)
1 |
package org.gvsig.fmap.data.store.dbf; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.text.DateFormat; |
6 |
import java.text.ParseException; |
7 |
import java.util.ArrayList; |
8 |
import java.util.Date; |
9 |
import java.util.Iterator; |
10 |
import java.util.List; |
11 |
import java.util.Locale; |
12 |
|
13 |
import org.gvsig.fmap.data.DataExplorer; |
14 |
import org.gvsig.fmap.data.DataStoreParameters; |
15 |
import org.gvsig.fmap.data.DataTypes; |
16 |
import org.gvsig.fmap.data.exceptions.CloseException; |
17 |
import org.gvsig.fmap.data.exceptions.CreateException; |
18 |
import org.gvsig.fmap.data.exceptions.DataException; |
19 |
import org.gvsig.fmap.data.exceptions.FileNotFoundException; |
20 |
import org.gvsig.fmap.data.exceptions.InitializeException; |
21 |
import org.gvsig.fmap.data.exceptions.OpenException; |
22 |
import org.gvsig.fmap.data.exceptions.ReadException; |
23 |
import org.gvsig.fmap.data.exceptions.UnsupportedVersionException; |
24 |
import org.gvsig.fmap.data.feature.EditableFeatureType; |
25 |
import org.gvsig.fmap.data.feature.Feature; |
26 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
27 |
import org.gvsig.fmap.data.feature.FeatureQuery; |
28 |
import org.gvsig.fmap.data.feature.FeatureType; |
29 |
import org.gvsig.fmap.data.feature.exceptions.PerformEditingException; |
30 |
import org.gvsig.fmap.data.feature.exceptions.UnknowDataTypeException; |
31 |
import org.gvsig.fmap.data.feature.impl.DefaultFeatureType; |
32 |
import org.gvsig.fmap.data.feature.spi.AbstractFeatureStoreProvider; |
33 |
import org.gvsig.fmap.data.feature.spi.FeatureData; |
34 |
import org.gvsig.fmap.data.feature.spi.FeatureReferenceProviderServices; |
35 |
import org.gvsig.fmap.data.feature.spi.FeatureSetProvider; |
36 |
import org.gvsig.fmap.data.feature.spi.FeatureStoreProvider; |
37 |
import org.gvsig.fmap.data.feature.spi.FeatureStoreProviderServices; |
38 |
import org.gvsig.fmap.data.resource.exception.AccessResourceException; |
39 |
import org.gvsig.fmap.data.resource.exception.ResourceBeginException; |
40 |
import org.gvsig.fmap.data.resource.exception.ResourceNotifyChangesException; |
41 |
import org.gvsig.fmap.data.resource.exception.ResourceNotifyCloseException; |
42 |
import org.gvsig.fmap.data.resource.exception.ResourceNotifyDisposeException; |
43 |
import org.gvsig.fmap.data.resource.exception.ResourceNotifyOpenException; |
44 |
import org.gvsig.fmap.data.resource.file.FileResource; |
45 |
import org.gvsig.fmap.data.resource.spi.ResourceConsumer; |
46 |
import org.gvsig.fmap.data.resource.spi.ResourceProvider; |
47 |
import org.gvsig.fmap.data.store.dbf.utils.DbaseFile; |
48 |
import org.gvsig.metadata.Metadata; |
49 |
import org.gvsig.tools.exception.NotYetImplemented; |
50 |
import org.gvsig.tools.persistence.PersistenceException; |
51 |
import org.gvsig.tools.persistence.PersistentState; |
52 |
|
53 |
public class DBFStoreProvider extends AbstractFeatureStoreProvider implements |
54 |
ResourceConsumer { |
55 |
|
56 |
public static String NAME = "DBFStore"; |
57 |
public static String DESCRIPTION = "DBF file"; |
58 |
// private DBFResource dbf = null;
|
59 |
private DbaseFile dbfFile = null; |
60 |
private ResourceProvider resource;
|
61 |
protected Metadata metadata;
|
62 |
private static final Locale ukLocale = new Locale("en", "UK"); |
63 |
private DBFStoreParameters params;
|
64 |
|
65 |
public DBFStoreProvider(DBFStoreParameters params)
|
66 |
throws InitializeException {
|
67 |
super();
|
68 |
this.params = params;
|
69 |
|
70 |
resource = this.createResource(FileResource.NAME,
|
71 |
new Object[] { this.params |
72 |
.getFile() }); |
73 |
resource.addConsumer(this);
|
74 |
|
75 |
//DBFResource tmpResource = new DBFResource(dbfParameters);
|
76 |
//
|
77 |
//try {
|
78 |
// this.dbf = (DBFResource) this.store.addResource(tmpResource);
|
79 |
//} catch (DataException e1) {
|
80 |
// throw new InitializeException(this.getName(), e1);
|
81 |
//}
|
82 |
File theFile = new File(getParameters().getFile()); |
83 |
|
84 |
this.dbfFile = new DbaseFile(theFile); |
85 |
} |
86 |
|
87 |
public FeatureStoreProvider initialize(FeatureStoreProviderServices store)
|
88 |
throws InitializeException {
|
89 |
super.initialize(store);
|
90 |
this.initFeatureType();
|
91 |
return this; |
92 |
} |
93 |
|
94 |
public String getName() { |
95 |
return NAME;
|
96 |
} |
97 |
|
98 |
protected DBFStoreParameters getParameters() {
|
99 |
return params;
|
100 |
} |
101 |
|
102 |
|
103 |
public DataExplorer getExplorer() throws ReadException { |
104 |
// TODO Auto-generated method stub
|
105 |
return null; |
106 |
} |
107 |
|
108 |
public FeatureData getFeatureDataByReference(
|
109 |
FeatureReferenceProviderServices reference, FeatureType featureType) |
110 |
throws DataException {
|
111 |
|
112 |
return this.getFeatureDataByIndex(((Integer) reference.getOID()) |
113 |
.intValue(), |
114 |
featureType); |
115 |
} |
116 |
|
117 |
|
118 |
public FeatureData getFeatureDataByReference(
|
119 |
FeatureReferenceProviderServices reference) throws DataException {
|
120 |
return this.getFeatureDataByReference(reference, this.store |
121 |
.getDefaultFeatureType()); |
122 |
} |
123 |
|
124 |
public void performEditing(Iterator deleteds, Iterator inserteds, Iterator updateds) throws PerformEditingException { |
125 |
// TODO Auto-generated method stub
|
126 |
|
127 |
} |
128 |
|
129 |
/**
|
130 |
*
|
131 |
* Only for persistenced features
|
132 |
*
|
133 |
* @param index
|
134 |
* @param featureType
|
135 |
* @return
|
136 |
* @throws ReadException
|
137 |
*/
|
138 |
protected FeatureData getFeatureDataByIndex(long index, |
139 |
FeatureType featureType) throws DataException {
|
140 |
this.open();
|
141 |
this.resource.begin();
|
142 |
try {
|
143 |
if (index >= this.getFeatureCount()) { |
144 |
// FIXME
|
145 |
throw new ArrayIndexOutOfBoundsException("" + index); |
146 |
} |
147 |
FeatureData featureData = this.createFeatureData(featureType);
|
148 |
Iterator iterator = featureType.iterator();
|
149 |
while (iterator.hasNext()) {
|
150 |
FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator |
151 |
.next(); |
152 |
this.loadValue(featureData, (int) index, descriptor); |
153 |
|
154 |
} |
155 |
|
156 |
featureData.setOID(new Integer((int) index)); |
157 |
|
158 |
return featureData;
|
159 |
} finally {
|
160 |
this.resource.end();
|
161 |
} |
162 |
} |
163 |
|
164 |
/*
|
165 |
* ==================================================
|
166 |
*/
|
167 |
|
168 |
public FeatureData createFeatureData(FeatureType type) throws DataException { |
169 |
return new DBFFeatureData((DefaultFeatureType) type); |
170 |
} |
171 |
|
172 |
|
173 |
/*
|
174 |
* ===================================================
|
175 |
*/
|
176 |
|
177 |
FeatureStoreProviderServices getProviderServices() { |
178 |
return this.store; |
179 |
} |
180 |
|
181 |
|
182 |
private void initFeatureType() throws InitializeException { |
183 |
try {
|
184 |
this.open();
|
185 |
this.resource.begin();
|
186 |
} catch (DataException e) {
|
187 |
throw new InitializeException(this.getName(), e); |
188 |
} |
189 |
try {
|
190 |
int fieldCount = -1; |
191 |
fieldCount = dbfFile.getFieldCount(); |
192 |
|
193 |
EditableFeatureType fType = this.store.createFeatureType();
|
194 |
|
195 |
for (int i = 0; i < fieldCount; i++) { |
196 |
char fieldType = dbfFile.getFieldType(i);
|
197 |
int attrType;
|
198 |
int precision = 0; |
199 |
|
200 |
if (fieldType == 'L') { |
201 |
attrType = DataTypes.BOOLEAN; |
202 |
|
203 |
} else if ((fieldType == 'F') || (fieldType == 'N')) { |
204 |
precision = dbfFile.getFieldDecimalLength(i); |
205 |
if (precision > 0) { |
206 |
attrType = DataTypes.DOUBLE; |
207 |
} else {
|
208 |
attrType = DataTypes.INT; |
209 |
} |
210 |
} else if (fieldType == 'C') { |
211 |
attrType = DataTypes.STRING; |
212 |
} else if (fieldType == 'D') { |
213 |
attrType = DataTypes.DATE; |
214 |
} else {
|
215 |
throw new InitializeException(this.getName(), |
216 |
new UnknowDataTypeException(
|
217 |
dbfFile.getFieldName(i), "" + fieldType,
|
218 |
this.getName()));
|
219 |
} |
220 |
fType.add(dbfFile.getFieldName(i), attrType, |
221 |
dbfFile.getFieldLength(i)).setPrecision(precision); |
222 |
} |
223 |
|
224 |
|
225 |
FeatureType defaultType = fType.getNotEditableCopy(); |
226 |
List types = new ArrayList(1); |
227 |
types.add(defaultType); |
228 |
this.store.setFeatureTypes(types, defaultType);
|
229 |
} finally {
|
230 |
this.resource.end();
|
231 |
} |
232 |
} |
233 |
|
234 |
|
235 |
protected void loadValue(FeatureData featureData, int rowIndex, |
236 |
FeatureAttributeDescriptor descriptor) throws ReadException {
|
237 |
if (descriptor.getEvaluator() != null) { |
238 |
// Nothing to do
|
239 |
return;
|
240 |
} |
241 |
|
242 |
int fieldIndex = descriptor.getIndex(); // TODO check it's correct |
243 |
String value = null; |
244 |
try {
|
245 |
value = this.dbfFile.getStringFieldValue(rowIndex, fieldIndex);
|
246 |
} catch (DataException e) {
|
247 |
throw new ReadException(this.store.getName(), e); |
248 |
} |
249 |
value = value.trim(); |
250 |
int fieldType = descriptor.getDataType();
|
251 |
switch (fieldType) {
|
252 |
case DataTypes.STRING:
|
253 |
featureData.set(fieldIndex, value); |
254 |
break;
|
255 |
|
256 |
case DataTypes.DOUBLE:
|
257 |
try {
|
258 |
featureData.set(fieldIndex, new Double(value)); |
259 |
} catch (NumberFormatException e) { |
260 |
featureData.set(fieldIndex, null);
|
261 |
} |
262 |
break;
|
263 |
|
264 |
case DataTypes.INT:
|
265 |
try {
|
266 |
featureData.set(fieldIndex, new Integer(value)); |
267 |
} catch (NumberFormatException e) { |
268 |
featureData.set(fieldIndex, null);
|
269 |
} |
270 |
break;
|
271 |
|
272 |
case DataTypes.FLOAT:
|
273 |
try {
|
274 |
featureData.set(fieldIndex, new Float(value)); |
275 |
} catch (NumberFormatException e) { |
276 |
featureData.set(fieldIndex, null);
|
277 |
} |
278 |
break;
|
279 |
|
280 |
case DataTypes.LONG:
|
281 |
try {
|
282 |
featureData.set(fieldIndex, new Long(value)); |
283 |
} catch (NumberFormatException e) { |
284 |
featureData.set(fieldIndex, null);
|
285 |
} |
286 |
break;
|
287 |
|
288 |
case DataTypes.BOOLEAN:
|
289 |
featureData.set(fieldIndex, new Boolean(value)); |
290 |
break;
|
291 |
|
292 |
case DataTypes.BYTE:
|
293 |
try {
|
294 |
featureData.set(fieldIndex, new Byte(value)); |
295 |
} catch (NumberFormatException e) { |
296 |
featureData.set(fieldIndex, null);
|
297 |
} |
298 |
break;
|
299 |
|
300 |
case DataTypes.DATE:
|
301 |
String year = value.substring(0, 4); |
302 |
String month = value.substring(4, 6); |
303 |
String day = value.substring(6, 8); |
304 |
DateFormat df;
|
305 |
if (descriptor.getDateFormat() == null){ |
306 |
df = DateFormat.getDateInstance(DateFormat.SHORT, |
307 |
ukLocale); |
308 |
} else{
|
309 |
df = descriptor.getDateFormat(); |
310 |
} |
311 |
/*
|
312 |
* Calendar c = Calendar.getInstance(); c.clear();
|
313 |
* c.set(Integer.parseInt(year), Integer.parseInt(month),
|
314 |
* Integer.parseInt(day)); c.set(Calendar.MILLISECOND, 0);
|
315 |
*/
|
316 |
String strAux = month + "/" + day + "/" + year; |
317 |
Date dat = null; |
318 |
try {
|
319 |
dat = df.parse(strAux); |
320 |
} catch (ParseException e) { |
321 |
throw new ReadException(this.store.getName(), e); |
322 |
} |
323 |
featureData.set(fieldIndex, dat); |
324 |
break;
|
325 |
|
326 |
|
327 |
default:
|
328 |
featureData.set(fieldIndex, descriptor.getDefaultValue()); |
329 |
break;
|
330 |
} |
331 |
} |
332 |
|
333 |
|
334 |
/***
|
335 |
* NOT supported in Alter Mode
|
336 |
*
|
337 |
* @param index
|
338 |
* @return
|
339 |
* @throws ReadException
|
340 |
*/
|
341 |
protected FeatureData getFeatureDataByIndex(long index) throws DataException { |
342 |
return this |
343 |
.getFeatureDataByIndex(index, this.store
|
344 |
.getDefaultFeatureType()); |
345 |
} |
346 |
|
347 |
protected int getFeatureCount() throws ReadException, OpenException, |
348 |
ResourceNotifyChangesException { |
349 |
this.open();
|
350 |
try {
|
351 |
this.resource.begin();
|
352 |
} catch (ResourceBeginException e) {
|
353 |
throw new ReadException(this.getName(), e); |
354 |
} |
355 |
try {
|
356 |
return this.dbfFile.getRecordCount(); |
357 |
} finally {
|
358 |
this.resource.end();
|
359 |
} |
360 |
} |
361 |
|
362 |
public FeatureSetProvider createSet(FeatureQuery query)
|
363 |
throws DataException {
|
364 |
return new DBFSetProvider(this, query); |
365 |
} |
366 |
|
367 |
public boolean canCreate() { |
368 |
return true; |
369 |
} |
370 |
|
371 |
public boolean canWriteGeometry(int geometryType) { |
372 |
return false; |
373 |
} |
374 |
|
375 |
public void create(DataStoreParameters parameters, FeatureType featureType, |
376 |
boolean overwrite) throws CreateException { |
377 |
//TODO
|
378 |
throw new NotYetImplemented(); |
379 |
} |
380 |
|
381 |
public void open() throws OpenException { |
382 |
if (this.dbfFile.isOpen()) { |
383 |
return;
|
384 |
} |
385 |
try {
|
386 |
this.resource.begin();
|
387 |
} catch (ResourceBeginException e) {
|
388 |
throw new OpenException(this.getName(), e); |
389 |
} |
390 |
try {
|
391 |
this.dbfFile.open();
|
392 |
this.resource.notifyOpen();
|
393 |
|
394 |
} catch (UnsupportedVersionException e) {
|
395 |
throw new OpenException(this.getName(), e); |
396 |
} catch (ResourceNotifyOpenException e) {
|
397 |
throw new OpenException(this.getName(), e); |
398 |
} catch (FileNotFoundException e) { |
399 |
throw new OpenException(this.getName(), e); |
400 |
} catch (IOException e) { |
401 |
throw new OpenException(this.getName(), e); |
402 |
} finally {
|
403 |
this.resource.end();
|
404 |
} |
405 |
} |
406 |
|
407 |
public void close() throws CloseException { |
408 |
super.close();
|
409 |
if (!this.dbfFile.isOpen()) { |
410 |
return;
|
411 |
} |
412 |
//Cerrar recurso
|
413 |
try {
|
414 |
this.resource.begin();
|
415 |
} catch (ResourceBeginException e) {
|
416 |
throw new CloseException(this.getName(), e); |
417 |
} |
418 |
try {
|
419 |
this.dbfFile.close();
|
420 |
this.resource.notifyClose();
|
421 |
|
422 |
} catch (ResourceNotifyCloseException e) {
|
423 |
throw new CloseException(this.getName(), e); |
424 |
} finally {
|
425 |
this.resource.end();
|
426 |
} |
427 |
} |
428 |
|
429 |
public void dispose() throws CloseException { |
430 |
this.close();
|
431 |
try {
|
432 |
this.resource.notifyDispose();
|
433 |
} catch (ResourceNotifyDisposeException e) {
|
434 |
throw new CloseException(this.getName(), e); |
435 |
} |
436 |
dbfFile = null;
|
437 |
this.resource.removeConsumer(this); |
438 |
resource = null;
|
439 |
metadata = null;
|
440 |
super.dispose();
|
441 |
} |
442 |
|
443 |
public boolean closeResourceRequested(ResourceProvider resource) { |
444 |
try {
|
445 |
this.close();
|
446 |
} catch (CloseException e) {
|
447 |
return false; |
448 |
} |
449 |
return true; |
450 |
} |
451 |
|
452 |
public boolean allowWrite() { |
453 |
File file;
|
454 |
try {
|
455 |
file = (File) this.resource.get(); |
456 |
} catch (AccessResourceException e) {
|
457 |
return false; |
458 |
} |
459 |
return super.allowWrite() && file.canWrite(); |
460 |
} |
461 |
|
462 |
public void refresh() throws OpenException { |
463 |
try {
|
464 |
this.close();
|
465 |
} catch (CloseException e) {
|
466 |
throw new OpenException(this.getName(), e); |
467 |
} |
468 |
this.open();
|
469 |
try {
|
470 |
this.initFeatureType();
|
471 |
} catch (InitializeException e) {
|
472 |
throw new OpenException(this.getName(), e); |
473 |
} |
474 |
} |
475 |
|
476 |
public FeatureData getFeatureData(Object refId) throws DataException { |
477 |
return this.getFeatureDataByIndex(((Long) refId).longValue()); |
478 |
} |
479 |
|
480 |
public int getFeatureReferenceOIDType() { |
481 |
return DataTypes.LONG;
|
482 |
} |
483 |
|
484 |
public void append(Feature feature) { |
485 |
// TODO Auto-generated method stub
|
486 |
|
487 |
} |
488 |
|
489 |
public void beginAppend() { |
490 |
// TODO Auto-generated method stub
|
491 |
|
492 |
} |
493 |
|
494 |
public void endAppend() { |
495 |
// TODO Auto-generated method stub
|
496 |
|
497 |
} |
498 |
|
499 |
public PersistentState getState() throws PersistenceException { |
500 |
// TODO Auto-generated method stub
|
501 |
return null; |
502 |
} |
503 |
|
504 |
public void loadState(PersistentState state) throws PersistenceException { |
505 |
// TODO Auto-generated method stub
|
506 |
|
507 |
} |
508 |
|
509 |
public void setState(PersistentState state) throws PersistenceException { |
510 |
// TODO Auto-generated method stub
|
511 |
|
512 |
} |
513 |
|
514 |
public boolean supportsAppendMode() { |
515 |
return false; |
516 |
} |
517 |
|
518 |
public Object createNewOID() { |
519 |
// TODO Auto-generated method stub
|
520 |
return null; |
521 |
} |
522 |
|
523 |
|
524 |
|
525 |
|
526 |
} |