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