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