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