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