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