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