svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / store / dbf / DBFStoreProvider.java @ 28676
History | View | Annotate | Download (20.7 KB)
1 |
package org.gvsig.fmap.dal.store.dbf; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.nio.charset.Charset; |
6 |
import java.text.DateFormat; |
7 |
import java.text.ParseException; |
8 |
import java.util.ArrayList; |
9 |
import java.util.Arrays; |
10 |
import java.util.Date; |
11 |
import java.util.Iterator; |
12 |
import java.util.List; |
13 |
import java.util.Locale; |
14 |
|
15 |
import org.gvsig.fmap.dal.DALLocator; |
16 |
import org.gvsig.fmap.dal.DataManager; |
17 |
import org.gvsig.fmap.dal.DataServerExplorer; |
18 |
import org.gvsig.fmap.dal.DataStoreNotification; |
19 |
import org.gvsig.fmap.dal.DataTypes; |
20 |
import org.gvsig.fmap.dal.exception.CloseException; |
21 |
import org.gvsig.fmap.dal.exception.DataException; |
22 |
import org.gvsig.fmap.dal.exception.FileNotFoundException; |
23 |
import org.gvsig.fmap.dal.exception.InitializeException; |
24 |
import org.gvsig.fmap.dal.exception.OpenException; |
25 |
import org.gvsig.fmap.dal.exception.ReadException; |
26 |
import org.gvsig.fmap.dal.exception.UnsupportedVersionException; |
27 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
28 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
29 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
30 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
31 |
import org.gvsig.fmap.dal.feature.Feature; |
32 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
33 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
34 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
36 |
import org.gvsig.fmap.dal.feature.FeatureType; |
37 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
38 |
import org.gvsig.fmap.dal.feature.exception.UnknownDataTypeException; |
39 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
40 |
import org.gvsig.fmap.dal.feature.spi.FeatureData; |
41 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
42 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
43 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
44 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
45 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
46 |
import org.gvsig.fmap.dal.resource.exception.ResourceBeginException; |
47 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
48 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException; |
49 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException; |
50 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException; |
51 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
52 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
53 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
54 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
55 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
56 |
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFile; |
57 |
import org.gvsig.tools.ToolsLocator; |
58 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
59 |
import org.gvsig.tools.dynobject.DynClass; |
60 |
import org.gvsig.tools.dynobject.DynField; |
61 |
import org.gvsig.tools.dynobject.DynObject; |
62 |
import org.gvsig.tools.dynobject.DynObjectManager; |
63 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
64 |
import org.gvsig.tools.exception.BaseException; |
65 |
import org.gvsig.tools.persistence.PersistenceException; |
66 |
import org.gvsig.tools.persistence.PersistentState; |
67 |
|
68 |
public class DBFStoreProvider extends AbstractFeatureStoreProvider implements |
69 |
ResourceConsumer { |
70 |
|
71 |
public static String NAME = "DBFStore"; |
72 |
public static String DESCRIPTION = "DBF file"; |
73 |
// private DBFResource dbf = null;
|
74 |
private static final Locale ukLocale = new Locale("en", "UK"); |
75 |
private static final String DYNCLASS_NAME = "DBFStore"; |
76 |
protected static DynClass DYNCLASS = null; |
77 |
|
78 |
public static final String DYNFIELD_CURRENT_ENCODING = "CurrentEncoding"; |
79 |
public static final String DYNFIELD_ORIGINAL_ENCODING = "OriginalEncoding"; |
80 |
|
81 |
private DbaseFile dbfFile = null; |
82 |
private ResourceProvider dbfResource;
|
83 |
private DBFStoreParameters dbfParams;
|
84 |
private long counterNewsOIDs = -1; |
85 |
private DBFFeatureWriter writer;
|
86 |
|
87 |
public DBFStoreProvider() throws InitializeException { |
88 |
super();
|
89 |
writer = new DBFFeatureWriter(this.getName()); |
90 |
} |
91 |
|
92 |
public DBFStoreProvider(DBFStoreParameters params)
|
93 |
throws InitializeException {
|
94 |
super();
|
95 |
this.init(params);
|
96 |
writer = new DBFFeatureWriter(this.getName()); |
97 |
} |
98 |
|
99 |
|
100 |
protected void init(DBFStoreParameters params) throws InitializeException { |
101 |
this.dbfParams = params;
|
102 |
this.dynObject = (DelegatedDynObject) ToolsLocator
|
103 |
.getDynObjectManager().createDynObject( |
104 |
DYNCLASS); |
105 |
|
106 |
this.dynObject.setDynValue("DefaultSRS", null); |
107 |
this.dynObject.setDynValue("Envelope", null); |
108 |
|
109 |
File theFile = getDBFParameters().getDBFFile();
|
110 |
dbfResource = this.createResource(FileResource.NAME,
|
111 |
new Object[] { theFile.getAbsolutePath() }); |
112 |
dbfResource.addConsumer(this);
|
113 |
|
114 |
Charset charset = dbfParams.getEncoding();
|
115 |
this.dbfFile = new DbaseFile(theFile, charset); |
116 |
|
117 |
|
118 |
} |
119 |
|
120 |
public FeatureStoreProvider initialize(FeatureStoreProviderServices store)
|
121 |
throws InitializeException {
|
122 |
super.initialize(store);
|
123 |
this.initFeatureType();
|
124 |
return this; |
125 |
} |
126 |
|
127 |
public String getName() { |
128 |
return NAME;
|
129 |
} |
130 |
|
131 |
protected DBFStoreParameters getDBFParameters() {
|
132 |
return dbfParams;
|
133 |
} |
134 |
|
135 |
|
136 |
public DataServerExplorer getExplorer() throws ReadException { |
137 |
DataManager manager = DALLocator.getDataManager(); |
138 |
FilesystemServerExplorerParameters params; |
139 |
try {
|
140 |
params = (FilesystemServerExplorerParameters) manager |
141 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
142 |
params.setRoot(this.getDBFParameters().getDBFFile().getParent());
|
143 |
return manager.createServerExplorer(params);
|
144 |
} catch (DataException e) {
|
145 |
throw new ReadException(this.getName(), e); |
146 |
} catch (ValidateDataParametersException e) {
|
147 |
// TODO Auto-generated catch block
|
148 |
throw new ReadException(this.getName(), e); |
149 |
} |
150 |
} |
151 |
|
152 |
public FeatureData getFeatureDataByReference(
|
153 |
FeatureReferenceProviderServices reference, FeatureType featureType) |
154 |
throws DataException {
|
155 |
|
156 |
return this.getFeatureDataByIndex(((Long) reference.getOID()) |
157 |
.longValue(), |
158 |
featureType); |
159 |
} |
160 |
|
161 |
|
162 |
public FeatureData getFeatureDataByReference(
|
163 |
FeatureReferenceProviderServices reference) throws DataException {
|
164 |
return this.getFeatureDataByReference(reference, this.store |
165 |
.getDefaultFeatureType()); |
166 |
} |
167 |
|
168 |
public void performEditing(Iterator deleteds, Iterator inserteds, |
169 |
Iterator updateds, Iterator originalFeatureTypesUpdated) |
170 |
throws PerformEditingException {
|
171 |
|
172 |
try {
|
173 |
this.resourcesBegin();
|
174 |
FeatureSet set = this.store.getFeatureSet();
|
175 |
DBFStoreParameters tmpParams = (DBFStoreParameters) this
|
176 |
.getDBFParameters().getCopy(); |
177 |
|
178 |
tmpParams.setDBFFileName(tmpParams.getDBFFileName() + ".tmp");
|
179 |
|
180 |
writer.begin(tmpParams, this.store
|
181 |
.getDefaultFeatureType(), set.getSize()); |
182 |
|
183 |
DisposableIterator iter = set.fastIterator(); |
184 |
while (iter.hasNext()) {
|
185 |
Feature feature=(Feature) iter.next(); |
186 |
writer.append(feature); |
187 |
} |
188 |
iter.dispose(); |
189 |
|
190 |
|
191 |
writer.end(); |
192 |
|
193 |
try {
|
194 |
this.close();
|
195 |
} catch (CloseException e1) {
|
196 |
throw new PerformEditingException(this.getName(), e1); |
197 |
} |
198 |
this.getDBFParameters().getDBFFile().delete();
|
199 |
tmpParams.getDBFFile().renameTo( |
200 |
this.getDBFParameters().getDBFFile());
|
201 |
|
202 |
this.resourcesNotifyChanges();
|
203 |
this.initFeatureType();
|
204 |
} catch (Exception e) { |
205 |
throw new PerformEditingException(this.getName(), e); |
206 |
} finally {
|
207 |
this.resourcesEnd();
|
208 |
} |
209 |
|
210 |
this.counterNewsOIDs = -1; |
211 |
} |
212 |
|
213 |
/*
|
214 |
* ==================================================
|
215 |
*/
|
216 |
|
217 |
public FeatureData createFeatureData(FeatureType type) throws DataException { |
218 |
return new DBFFeatureData(this, type); |
219 |
} |
220 |
|
221 |
|
222 |
/*
|
223 |
* ===================================================
|
224 |
*/
|
225 |
|
226 |
FeatureStoreProviderServices getProviderServices() { |
227 |
return this.store; |
228 |
} |
229 |
|
230 |
|
231 |
protected void initFeatureType() throws InitializeException { |
232 |
FeatureType defaultType = this.getTheFeatureType().getNotEditableCopy();
|
233 |
List types = new ArrayList(1); |
234 |
types.add(defaultType); |
235 |
this.store.setFeatureTypes(types, defaultType);
|
236 |
} |
237 |
|
238 |
protected EditableFeatureType getTheFeatureType() throws InitializeException { |
239 |
try {
|
240 |
this.open();
|
241 |
this.resourcesBegin();
|
242 |
} catch (DataException e) {
|
243 |
throw new InitializeException(this.getName(), e); |
244 |
} |
245 |
try {
|
246 |
int fieldCount = -1; |
247 |
fieldCount = dbfFile.getFieldCount(); |
248 |
|
249 |
EditableFeatureType fType = this.store.createFeatureType();
|
250 |
|
251 |
fType.setHasOID(true);
|
252 |
int precision;
|
253 |
for (int i = 0; i < fieldCount; i++) { |
254 |
char fieldType = dbfFile.getFieldType(i);
|
255 |
EditableFeatureAttributeDescriptor attr; |
256 |
|
257 |
if (fieldType == 'L') { |
258 |
attr = fType |
259 |
.add(dbfFile.getFieldName(i), DataTypes.BOOLEAN); |
260 |
attr.setDefaultValue(new Boolean(false)); |
261 |
attr.setAllowNull(false);
|
262 |
|
263 |
} else if ((fieldType == 'F') || (fieldType == 'N')) { |
264 |
precision = dbfFile.getFieldDecimalLength(i); |
265 |
if (precision > 0) { |
266 |
attr = fType.add(dbfFile.getFieldName(i), |
267 |
DataTypes.DOUBLE, dbfFile.getFieldLength(i)); |
268 |
attr.setPrecision(precision); |
269 |
attr.setDefaultValue(new Double(0)); |
270 |
|
271 |
} else {
|
272 |
attr = fType |
273 |
.add(dbfFile.getFieldName(i), DataTypes.INT); |
274 |
attr.setDefaultValue(new Integer(0)); |
275 |
} |
276 |
attr.setAllowNull(false);
|
277 |
|
278 |
} else if (fieldType == 'C') { |
279 |
attr = fType.add(dbfFile.getFieldName(i), DataTypes.STRING); |
280 |
attr.setSize(dbfFile.getFieldLength(i)); |
281 |
attr.setDefaultValue("");
|
282 |
attr.setAllowNull(false);
|
283 |
|
284 |
} else if (fieldType == 'D') { |
285 |
attr = fType.add(dbfFile.getFieldName(i), DataTypes.DATE); |
286 |
attr.setDefaultValue(null);
|
287 |
attr.setAllowNull(true);
|
288 |
} else {
|
289 |
throw new InitializeException(this.getName(), |
290 |
new UnknownDataTypeException(
|
291 |
dbfFile.getFieldName(i), "" + fieldType,
|
292 |
this.getName()));
|
293 |
} |
294 |
} |
295 |
return fType;
|
296 |
} finally {
|
297 |
this.resourcesEnd();
|
298 |
} |
299 |
} |
300 |
|
301 |
|
302 |
protected void loadValue(FeatureData featureData, int rowIndex, |
303 |
FeatureAttributeDescriptor descriptor) throws ReadException {
|
304 |
if (descriptor.getEvaluator() != null) { |
305 |
// Nothing to do
|
306 |
return;
|
307 |
} |
308 |
|
309 |
|
310 |
int dbfIndex = this.dbfFile.getFieldIndex(descriptor.getName()); |
311 |
String value = null; |
312 |
try {
|
313 |
value = this.dbfFile.getStringFieldValue(rowIndex, dbfIndex);
|
314 |
} catch (DataException e) {
|
315 |
throw new ReadException(this.store.getName(), e); |
316 |
} |
317 |
value = value.trim(); |
318 |
int fieldType = descriptor.getDataType();
|
319 |
switch (fieldType) {
|
320 |
case DataTypes.STRING:
|
321 |
featureData.set(descriptor.getIndex(), value); |
322 |
break;
|
323 |
|
324 |
case DataTypes.DOUBLE:
|
325 |
try {
|
326 |
featureData.set(descriptor.getIndex(), new Double(value)); |
327 |
} catch (NumberFormatException e) { |
328 |
featureData.set(descriptor.getIndex(), null);
|
329 |
} |
330 |
break;
|
331 |
|
332 |
case DataTypes.INT:
|
333 |
try {
|
334 |
featureData.set(descriptor.getIndex(), new Integer(value)); |
335 |
} catch (NumberFormatException e) { |
336 |
featureData.set(descriptor.getIndex(), null);
|
337 |
} |
338 |
break;
|
339 |
|
340 |
case DataTypes.FLOAT:
|
341 |
try {
|
342 |
featureData.set(descriptor.getIndex(), new Float(value)); |
343 |
} catch (NumberFormatException e) { |
344 |
featureData.set(descriptor.getIndex(), null);
|
345 |
} |
346 |
break;
|
347 |
|
348 |
case DataTypes.LONG:
|
349 |
try {
|
350 |
featureData.set(descriptor.getIndex(), new Long(value)); |
351 |
} catch (NumberFormatException e) { |
352 |
featureData.set(descriptor.getIndex(), null);
|
353 |
} |
354 |
break;
|
355 |
|
356 |
case DataTypes.BOOLEAN:
|
357 |
featureData.set(descriptor.getIndex(), new Boolean(value)); |
358 |
break;
|
359 |
|
360 |
case DataTypes.BYTE:
|
361 |
try {
|
362 |
featureData.set(descriptor.getIndex(), new Byte(value)); |
363 |
} catch (NumberFormatException e) { |
364 |
featureData.set(descriptor.getIndex(), null);
|
365 |
} |
366 |
break;
|
367 |
|
368 |
case DataTypes.DATE:
|
369 |
if (value.equals("")){ |
370 |
value=null;
|
371 |
return;
|
372 |
} |
373 |
String year = value.substring(0, 4); |
374 |
String month = value.substring(4, 6); |
375 |
String day = value.substring(6, 8); |
376 |
DateFormat df;
|
377 |
if (descriptor.getDateFormat() == null){ |
378 |
df = DateFormat.getDateInstance(DateFormat.SHORT, |
379 |
ukLocale); |
380 |
} else{
|
381 |
df = descriptor.getDateFormat(); |
382 |
} |
383 |
/*
|
384 |
* Calendar c = Calendar.getInstance(); c.clear();
|
385 |
* c.set(Integer.parseInt(year), Integer.parseInt(month),
|
386 |
* Integer.parseInt(day)); c.set(Calendar.MILLISECOND, 0);
|
387 |
*/
|
388 |
String strAux = month + "/" + day + "/" + year; |
389 |
Date dat = null; |
390 |
try {
|
391 |
dat = df.parse(strAux); |
392 |
} catch (ParseException e) { |
393 |
throw new ReadException(this.store.getName(), e); |
394 |
} |
395 |
featureData.set(descriptor.getIndex(), dat); |
396 |
break;
|
397 |
|
398 |
|
399 |
default:
|
400 |
featureData |
401 |
.set(descriptor.getIndex(), descriptor.getDefaultValue()); |
402 |
break;
|
403 |
} |
404 |
} |
405 |
|
406 |
|
407 |
/***
|
408 |
* NOT supported in Alter Mode
|
409 |
*
|
410 |
* @param index
|
411 |
* @return
|
412 |
* @throws ReadException
|
413 |
*/
|
414 |
protected FeatureData getFeatureDataByIndex(long index) throws DataException { |
415 |
return this |
416 |
.getFeatureDataByIndex(index, this.store
|
417 |
.getDefaultFeatureType()); |
418 |
} |
419 |
|
420 |
public long getFeatureCount() throws ReadException, OpenException, |
421 |
ResourceNotifyChangesException { |
422 |
this.open();
|
423 |
try {
|
424 |
this.resourcesBegin();
|
425 |
} catch (ResourceBeginException e) {
|
426 |
throw new ReadException(this.getName(), e); |
427 |
} |
428 |
try {
|
429 |
return this.dbfFile.getRecordCount(); |
430 |
} finally {
|
431 |
this.resourcesEnd();
|
432 |
} |
433 |
} |
434 |
|
435 |
public FeatureSetProvider createSet(FeatureQuery query, FeatureType featureType)
|
436 |
throws DataException {
|
437 |
return new DBFSetProvider(this, query, featureType); |
438 |
} |
439 |
|
440 |
public boolean canCreate() { |
441 |
return true; |
442 |
} |
443 |
|
444 |
public boolean canWriteGeometry(int geometryType) throws DataException { |
445 |
return false; |
446 |
} |
447 |
|
448 |
public void open() throws OpenException { |
449 |
if (this.dbfFile.isOpen()) { |
450 |
return;
|
451 |
} |
452 |
try {
|
453 |
this.resourcesBegin();
|
454 |
} catch (ResourceBeginException e) {
|
455 |
throw new OpenException(this.getName(), e); |
456 |
} |
457 |
try {
|
458 |
this.openFile();
|
459 |
this.resourcesOpen();
|
460 |
|
461 |
// Load metadata values
|
462 |
this.loadMetadataValues();
|
463 |
|
464 |
} catch (UnsupportedVersionException e) {
|
465 |
throw new OpenException(this.getName(), e); |
466 |
} catch (ResourceNotifyOpenException e) {
|
467 |
throw new OpenException(this.getName(), e); |
468 |
} catch (FileNotFoundException e) { |
469 |
throw new OpenException(this.getName(), e); |
470 |
} catch (IOException e) { |
471 |
throw new OpenException(this.getName(), e); |
472 |
} catch (BaseException e) {
|
473 |
throw new OpenException(this.getName(), e); |
474 |
} finally {
|
475 |
this.resourcesEnd();
|
476 |
} |
477 |
} |
478 |
|
479 |
protected void openFile() throws FileNotFoundException, |
480 |
UnsupportedVersionException, IOException, DataException {
|
481 |
this.dbfFile.open();
|
482 |
} |
483 |
|
484 |
protected void loadMetadataValues() throws DynFieldNotFoundException, |
485 |
ReadException { |
486 |
this.dynObject.setDynValue(DBFLibrary.DYNFIELD_CODEPAGE_NAME, new Byte( |
487 |
this.dbfFile.getCodePage()));
|
488 |
this.dynObject.setDynValue(DYNFIELD_CURRENT_ENCODING, dbfFile
|
489 |
.getCurrenCharset().name()); |
490 |
this.dynObject.setDynValue(DYNFIELD_ORIGINAL_ENCODING, dbfFile
|
491 |
.getOriginalCharset().name()); |
492 |
} |
493 |
|
494 |
public void close() throws CloseException { |
495 |
if (!this.dbfFile.isOpen()) { |
496 |
return;
|
497 |
} |
498 |
super.close();
|
499 |
//Cerrar recurso
|
500 |
try {
|
501 |
this.resourcesBegin();
|
502 |
} catch (ResourceBeginException e) {
|
503 |
throw new CloseException(this.getName(), e); |
504 |
} |
505 |
try {
|
506 |
this.closeFile();
|
507 |
this.resourcesNotifyClose();
|
508 |
|
509 |
} catch (ResourceNotifyCloseException e) {
|
510 |
throw new CloseException(this.getName(), e); |
511 |
} finally {
|
512 |
this.resourcesEnd();
|
513 |
} |
514 |
} |
515 |
|
516 |
protected void closeFile() throws CloseException { |
517 |
this.dbfFile.close();
|
518 |
} |
519 |
|
520 |
public void dispose() throws CloseException { |
521 |
this.close();
|
522 |
dbfFile = null;
|
523 |
this.dbfResource.removeConsumer(this); |
524 |
dbfResource = null;
|
525 |
super.dispose();
|
526 |
} |
527 |
|
528 |
public boolean closeResourceRequested(ResourceProvider resource) { |
529 |
try {
|
530 |
this.close();
|
531 |
} catch (CloseException e) {
|
532 |
return false; |
533 |
} |
534 |
return true; |
535 |
} |
536 |
|
537 |
public boolean allowWrite() { |
538 |
File file;
|
539 |
try {
|
540 |
file = new File((String) this.dbfResource.get()); |
541 |
} catch (AccessResourceException e) {
|
542 |
return false; |
543 |
} |
544 |
return file.canWrite();
|
545 |
} |
546 |
|
547 |
public void refresh() throws OpenException { |
548 |
try {
|
549 |
this.close();
|
550 |
} catch (CloseException e) {
|
551 |
throw new OpenException(this.getName(), e); |
552 |
} |
553 |
this.open();
|
554 |
try {
|
555 |
this.initFeatureType();
|
556 |
} catch (InitializeException e) {
|
557 |
throw new OpenException(this.getName(), e); |
558 |
} |
559 |
} |
560 |
|
561 |
/**
|
562 |
*
|
563 |
* @param index
|
564 |
* @param featureType
|
565 |
* @return
|
566 |
* @throws ReadException
|
567 |
*/
|
568 |
protected FeatureData getFeatureDataByIndex(long index, |
569 |
FeatureType featureType) throws DataException {
|
570 |
FeatureData featureData = this.createFeatureData(featureType);
|
571 |
featureData.setOID(new Long(index)); |
572 |
return featureData;
|
573 |
} |
574 |
|
575 |
protected void initFeatureDataByIndex(FeatureData featureData, |
576 |
long index, FeatureType featureType) throws DataException { |
577 |
featureData.setOID(new Long(index)); |
578 |
} |
579 |
|
580 |
/**
|
581 |
*
|
582 |
* @param featureData
|
583 |
* @throws DataException
|
584 |
*/
|
585 |
protected void loadFeatureDataByIndex(FeatureData featureData) |
586 |
throws DataException {
|
587 |
this.open();
|
588 |
this.resourcesBegin();
|
589 |
long index = ((Long) featureData.getOID()).longValue(); |
590 |
try {
|
591 |
if (index >= this.dbfFile.getRecordCount()) { |
592 |
// FIXME
|
593 |
throw new ArrayIndexOutOfBoundsException("" + index); |
594 |
} |
595 |
Iterator iterator = featureData.getType().iterator();
|
596 |
while (iterator.hasNext()) {
|
597 |
FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator |
598 |
.next(); |
599 |
this.loadValue(featureData, (int) index, descriptor); |
600 |
} |
601 |
|
602 |
|
603 |
} finally {
|
604 |
this.resourcesEnd();
|
605 |
} |
606 |
} |
607 |
|
608 |
public int getFeatureReferenceOIDType() { |
609 |
return DataTypes.LONG;
|
610 |
} |
611 |
|
612 |
public Object createNewOID() { |
613 |
if (this.counterNewsOIDs < 0) { |
614 |
try {
|
615 |
this.counterNewsOIDs = this.getFeatureCount(); |
616 |
} catch (DataException e) {
|
617 |
e.printStackTrace(); |
618 |
} |
619 |
|
620 |
}else{
|
621 |
this.counterNewsOIDs++;
|
622 |
} |
623 |
return new Long(counterNewsOIDs); |
624 |
} |
625 |
|
626 |
public boolean supportsAppendMode() { |
627 |
return true; |
628 |
} |
629 |
|
630 |
|
631 |
public void append(FeatureData featureData) throws DataException { |
632 |
this.resourcesBegin();
|
633 |
try {
|
634 |
writer.append(store.createFeature(featureData)); |
635 |
} finally {
|
636 |
this.resourcesEnd();
|
637 |
} |
638 |
} |
639 |
|
640 |
public void beginAppend() throws DataException { |
641 |
this.close();
|
642 |
this.resourcesBegin();
|
643 |
try {
|
644 |
FeatureSet set = this.store.getFeatureSet();
|
645 |
writer.begin(this.getDBFParameters(), this.store |
646 |
.getDefaultFeatureType(), set.getSize()); |
647 |
} finally {
|
648 |
this.resourcesEnd();
|
649 |
} |
650 |
} |
651 |
|
652 |
public void endAppend() throws DataException { |
653 |
this.resourcesBegin();
|
654 |
try {
|
655 |
writer.end(); |
656 |
|
657 |
this.resourcesNotifyChanges();
|
658 |
this.counterNewsOIDs = -1; |
659 |
} finally {
|
660 |
this.resourcesEnd();
|
661 |
} |
662 |
} |
663 |
|
664 |
public PersistentState getState() throws PersistenceException { |
665 |
// Nothing to do
|
666 |
return null; |
667 |
} |
668 |
|
669 |
public void saveToState(PersistentState state) throws PersistenceException { |
670 |
try {
|
671 |
this.init((DBFStoreParameters) this.store.getParameters()); |
672 |
} catch (InitializeException e) {
|
673 |
throw new PersistenceException(e); |
674 |
} |
675 |
} |
676 |
|
677 |
public void loadFromState(PersistentState state) throws PersistenceException { |
678 |
try {
|
679 |
this.init((DBFStoreParameters) this.store.getParameters()); |
680 |
} catch (InitializeException e) {
|
681 |
throw new PersistenceException(e); |
682 |
} |
683 |
} |
684 |
|
685 |
public Iterator getChilds() { |
686 |
return Arrays.asList(new DynObject[] { this.dbfParams }).iterator(); |
687 |
|
688 |
} |
689 |
|
690 |
protected static void registerDynClass() { |
691 |
DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
692 |
DynClass dynClass; |
693 |
DynField field; |
694 |
if (DYNCLASS == null) { |
695 |
dynClass = dynman.add(DYNCLASS_NAME, |
696 |
"DBF File Store");
|
697 |
field = DBFLibrary.addCodePageField(dynClass); |
698 |
|
699 |
field = dynClass.addDynField(DYNFIELD_CURRENT_ENCODING); |
700 |
field.setType(org.gvsig.tools.dataTypes.DataTypes.STRING); |
701 |
|
702 |
field = dynClass.addDynField(DYNFIELD_ORIGINAL_ENCODING); |
703 |
field.setType(org.gvsig.tools.dataTypes.DataTypes.STRING); |
704 |
|
705 |
dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME)); |
706 |
|
707 |
|
708 |
|
709 |
|
710 |
DYNCLASS = dynClass; |
711 |
} |
712 |
|
713 |
} |
714 |
|
715 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
716 |
try {
|
717 |
this.open();
|
718 |
} catch (OpenException e) {
|
719 |
// FIXME
|
720 |
throw new RuntimeException(e); |
721 |
} |
722 |
return super.getDynValue(name); |
723 |
} |
724 |
|
725 |
|
726 |
/*
|
727 |
* (non-Javadoc)
|
728 |
*
|
729 |
* @see
|
730 |
* org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.
|
731 |
* gvsig.fmap.dal.resource.spi.ResourceProvider)
|
732 |
*/
|
733 |
public void resourceChanged(ResourceProvider resource) { |
734 |
this.store.notifyChange(DataStoreNotification.RESOURCE_CHANGED,
|
735 |
resource); |
736 |
} |
737 |
|
738 |
protected void resourcesBegin() throws ResourceBeginException { |
739 |
this.dbfResource.begin();
|
740 |
} |
741 |
|
742 |
protected void resourcesEnd() { |
743 |
this.dbfResource.end();
|
744 |
} |
745 |
|
746 |
/**
|
747 |
*
|
748 |
* @throws ResourceNotifyChangesException
|
749 |
*/
|
750 |
protected void resourcesNotifyChanges() |
751 |
throws ResourceNotifyChangesException {
|
752 |
this.dbfResource.notifyChanges();
|
753 |
} |
754 |
|
755 |
/**
|
756 |
* @throws ResourceNotifyCloseException
|
757 |
*
|
758 |
*/
|
759 |
protected void resourcesNotifyClose() throws ResourceNotifyCloseException { |
760 |
this.dbfResource.notifyClose();
|
761 |
} |
762 |
|
763 |
/**
|
764 |
* @throws ResourceNotifyOpenException
|
765 |
*
|
766 |
*/
|
767 |
protected void resourcesOpen() throws ResourceNotifyOpenException { |
768 |
this.dbfResource.notifyOpen();
|
769 |
} |
770 |
|
771 |
public Object getSourceId() { |
772 |
return this.getDBFParameters().getFile(); |
773 |
} |
774 |
|
775 |
protected void resourceCloseRequest() throws ResourceException { |
776 |
this.dbfResource.closeRequest();
|
777 |
} |
778 |
} |