Revision 44669 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.dbf/src/main/java/org/gvsig/fmap/dal/store/dbf/DBFStoreProvider.java
DBFStoreProvider.java | ||
---|---|---|
24 | 24 |
|
25 | 25 |
import java.io.File; |
26 | 26 |
import java.io.IOException; |
27 |
import java.math.BigDecimal; |
|
28 |
import java.math.MathContext; |
|
29 |
import java.math.RoundingMode; |
|
27 | 30 |
import java.nio.charset.Charset; |
28 |
import java.text.DateFormat; |
|
29 |
import java.text.MessageFormat; |
|
30 |
import java.text.ParseException; |
|
31 | 31 |
import java.text.SimpleDateFormat; |
32 | 32 |
import java.util.ArrayList; |
33 | 33 |
import java.util.Date; |
34 | 34 |
import java.util.Iterator; |
35 | 35 |
import java.util.List; |
36 |
import java.util.Locale; |
|
37 |
import java.util.logging.Level; |
|
38 | 36 |
|
39 | 37 |
import org.apache.commons.io.FileUtils; |
40 |
import org.apache.commons.lang3.StringUtils; |
|
41 | 38 |
|
42 | 39 |
import org.gvsig.fmap.dal.DALLocator; |
43 | 40 |
import org.gvsig.fmap.dal.DataManager; |
44 | 41 |
import org.gvsig.fmap.dal.DataServerExplorer; |
42 |
import org.gvsig.fmap.dal.DataStore; |
|
45 | 43 |
import org.gvsig.fmap.dal.DataStoreNotification; |
46 | 44 |
import org.gvsig.fmap.dal.DataTypes; |
47 | 45 |
import org.gvsig.fmap.dal.FileHelper; |
... | ... | |
53 | 51 |
import org.gvsig.fmap.dal.exception.ReadException; |
54 | 52 |
import org.gvsig.fmap.dal.exception.UnsupportedVersionException; |
55 | 53 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
56 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
57 | 54 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
58 | 55 |
import org.gvsig.fmap.dal.feature.Feature; |
59 | 56 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
... | ... | |
64 | 61 |
import org.gvsig.fmap.dal.feature.exception.AttributeNameException; |
65 | 62 |
import org.gvsig.fmap.dal.feature.exception.AttributeNameTooLongException; |
66 | 63 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
67 |
import org.gvsig.fmap.dal.feature.exception.UnknownDataTypeException; |
|
68 | 64 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
69 | 65 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
70 | 66 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
71 | 67 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
72 | 68 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
73 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
|
74 | 69 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
75 | 70 |
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException; |
76 | 71 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException; |
... | ... | |
83 | 78 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
84 | 79 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
85 | 80 |
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFile; |
81 |
import org.gvsig.fmap.dal.store.dbf.utils.FieldFormatter; |
|
86 | 82 |
import org.gvsig.metadata.MetadataLocator; |
87 | 83 |
import org.gvsig.metadata.MetadataManager; |
88 | 84 |
import org.gvsig.metadata.exceptions.MetadataException; |
... | ... | |
91 | 87 |
import org.gvsig.tools.dynobject.DynObject; |
92 | 88 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
93 | 89 |
import org.gvsig.tools.exception.BaseException; |
90 |
import org.gvsig.tools.logger.FilteredLogger; |
|
94 | 91 |
|
95 | 92 |
import org.slf4j.Logger; |
96 | 93 |
import org.slf4j.LoggerFactory; |
94 |
import org.gvsig.tools.dataTypes.CoercionContextDecimal; |
|
97 | 95 |
|
98 | 96 |
public class DBFStoreProvider extends AbstractFeatureStoreProvider implements |
99 | 97 |
ResourceConsumer { |
... | ... | |
102 | 100 |
|
103 | 101 |
public static final int MAX_FIELD_NAME_LENGTH = DbaseFile.MAX_FIELD_NAME_LENGTH; |
104 | 102 |
|
105 |
public static String NAME = "DBF"; |
|
106 |
public static String DESCRIPTION = "DBF file"; |
|
107 |
private static final Locale ukLocale = new Locale("en", "UK"); |
|
103 |
public static final String NAME = DataStore.DBASE_PROVIDER_NAME; |
|
104 |
public static final String DESCRIPTION = "DBF file"; |
|
108 | 105 |
|
109 | 106 |
public static final String METADATA_DEFINITION_NAME = NAME; |
110 | 107 |
private static final String METADATA_ENCODING = "Encoding"; |
... | ... | |
115 | 112 |
private long counterNewsOIDs = -1; |
116 | 113 |
private DBFFeatureWriter writer; |
117 | 114 |
|
118 |
private static long lastLogTime = 0; |
|
119 |
|
|
120 | 115 |
private boolean loTengoEnUso; |
121 | 116 |
|
122 | 117 |
private boolean allowDuplicatedFieldNames; |
118 |
private FilteredLogger logger; |
|
123 | 119 |
|
124 | 120 |
protected static void registerMetadataDefinition() throws MetadataException { |
125 | 121 |
MetadataManager manager = MetadataLocator.getMetadataManager(); |
... | ... | |
140 | 136 |
storeServices, |
141 | 137 |
FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME) |
142 | 138 |
); |
139 |
this.logger = new FilteredLogger(LOG, "DBFStoreProvider", -1); |
|
140 |
this.logger.setInterval(2000); |
|
143 | 141 |
this.init(params, storeServices); |
144 | 142 |
} |
145 | 143 |
|
... | ... | |
181 | 179 |
if (METADATA_ENCODING.equalsIgnoreCase(name)) { |
182 | 180 |
return this.dbfFile.getOriginalCharset(); |
183 | 181 |
} else if (METADATA_CODEPAGE.equalsIgnoreCase(name)) { |
184 |
return new Integer(this.dbfFile.getCodePageInt());
|
|
182 |
return this.dbfFile.getCodePageInt();
|
|
185 | 183 |
} |
186 | 184 |
return super.getDynValue(name); |
187 | 185 |
} |
... | ... | |
218 | 216 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
219 | 217 |
params.setRoot(this.getDBFParameters().getDBFFile().getParent()); |
220 | 218 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME, params); |
221 |
} catch (DataException e) { |
|
219 |
} catch (DataException | ValidateDataParametersException e) {
|
|
222 | 220 |
throw new ReadException(this.getName(), e); |
223 |
} catch (ValidateDataParametersException e) { |
|
224 |
// TODO Auto-generated catch block |
|
225 |
throw new ReadException(this.getName(), e); |
|
226 | 221 |
} |
227 | 222 |
} |
228 | 223 |
|
... | ... | |
246 | 241 |
try { |
247 | 242 |
// TODO repasar el concepto de enUso de un recurso. |
248 | 243 |
loTengoEnUso = true; |
249 |
final FeatureStore store |
|
250 |
= this.getStoreServices().getFeatureStore(); |
|
244 |
final FeatureStore theStore = this.getStoreServices().getFeatureStore(); |
|
251 | 245 |
resourceCloseRequest(); |
252 | 246 |
getResource().execute(new ResourceAction() { |
253 | 247 |
|
... | ... | |
255 | 249 |
FeatureSet set = null; |
256 | 250 |
DisposableIterator iter = null; |
257 | 251 |
try { |
258 |
set = store.getFeatureSet();
|
|
252 |
set = theStore.getFeatureSet();
|
|
259 | 253 |
DBFStoreParameters tmpParams |
260 | 254 |
= (DBFStoreParameters) getDBFParameters().getCopy(); |
261 | 255 |
|
... | ... | |
265 | 259 |
|
266 | 260 |
tmpParams.setDBFFile(tmp_file); |
267 | 261 |
|
268 |
writer.begin(tmpParams, store.getDefaultFeatureType(),
|
|
262 |
writer.begin(tmpParams, theStore.getDefaultFeatureType(),
|
|
269 | 263 |
set.getSize()); |
270 | 264 |
|
271 | 265 |
iter = set.fastIterator(); |
... | ... | |
314 | 308 |
} |
315 | 309 |
}); |
316 | 310 |
} catch (ResourceExecuteException | ResourceException e) { |
317 |
throw new PerformEditingException(this.getProviderName(), e);
|
|
311 |
throw new PerformEditingException(NAME, e);
|
|
318 | 312 |
} |
319 | 313 |
|
320 | 314 |
this.counterNewsOIDs = -1; |
... | ... | |
341 | 335 |
protected String getNewFieldsWithNameTooLong(Iterator ft_updated) { |
342 | 336 |
|
343 | 337 |
String resp = ""; |
344 |
FeatureTypeChanged item = null;
|
|
345 |
FeatureType ft = null;
|
|
346 |
FeatureAttributeDescriptor[] atts = null;
|
|
338 |
FeatureTypeChanged item; |
|
339 |
FeatureType ft; |
|
340 |
FeatureAttributeDescriptor[] atts; |
|
347 | 341 |
while (ft_updated.hasNext()) { |
348 | 342 |
item = (FeatureTypeChanged) ft_updated.next(); |
349 | 343 |
ft = item.getTarget(); |
350 | 344 |
atts = ft.getAttributeDescriptors(); |
351 |
for (int i = 0; i < atts.length; i++) {
|
|
352 |
if (atts[i].getName().length() > MAX_FIELD_NAME_LENGTH) {
|
|
345 |
for (FeatureAttributeDescriptor att : atts) {
|
|
346 |
if (att.getName().length() > MAX_FIELD_NAME_LENGTH) { |
|
353 | 347 |
if (resp.length() == 0) { |
354 |
resp = atts[i].getName();
|
|
348 |
resp = att.getName(); |
|
355 | 349 |
} else { |
356 |
resp = resp + ", " + atts[i].getName();
|
|
350 |
resp = resp + ", " + att.getName(); |
|
357 | 351 |
} |
358 | 352 |
} |
359 | 353 |
} |
... | ... | |
365 | 359 |
return "(" + resp + ")"; |
366 | 360 |
} |
367 | 361 |
} |
368 |
/* |
|
369 |
* ================================================== |
|
370 |
*/ |
|
371 | 362 |
|
372 | 363 |
public FeatureProvider createFeatureProvider(FeatureType type) throws DataException { |
373 | 364 |
return new DBFFeatureProvider(this, type); |
374 | 365 |
} |
375 | 366 |
|
376 |
|
|
377 |
/* |
|
378 |
* =================================================== |
|
379 |
*/ |
|
380 | 367 |
protected void initFeatureType() throws InitializeException { |
381 |
try { |
|
382 |
FeatureType defaultType |
|
383 |
= this.getTheFeatureType().getNotEditableCopy(); |
|
384 |
List types = new ArrayList(1); |
|
385 |
types.add(defaultType); |
|
386 |
this.getStoreServices().setFeatureTypes(types, defaultType); |
|
387 |
} catch (OpenException e) { |
|
388 |
throw new InitializeException(getResource().toString(), e); |
|
389 |
} |
|
368 |
FeatureType defaultType = this.getTheFeatureType().getNotEditableCopy(); |
|
369 |
List types = new ArrayList(1); |
|
370 |
types.add(defaultType); |
|
371 |
getStoreServices().setFeatureTypes(types, defaultType); |
|
390 | 372 |
} |
391 | 373 |
|
392 |
public class DuplicatedFieldNameException extends ReadException { |
|
393 |
|
|
394 |
/** |
|
395 |
* |
|
396 |
*/ |
|
397 |
private static final long serialVersionUID = -1671651605329756160L; |
|
398 |
private final static String MESSAGE_FORMAT = "Duplicated field name '%(fieldName)'.\nCheck 'Allow duplicated field names' in layer properties in add layer dialog. The layer will become read only."; |
|
399 |
private final static String MESSAGE_KEY = "_DuplicatedFieldNameException"; |
|
400 |
|
|
401 |
public DuplicatedFieldNameException(String fieldName) { |
|
402 |
super(MESSAGE_FORMAT, null, MESSAGE_KEY, serialVersionUID); |
|
403 |
setValue("fieldName", fieldName); |
|
404 |
} |
|
405 |
} |
|
406 |
|
|
407 |
// |
|
408 |
// private String getUniqueFieldName(String fieldName, EditableFeatureType fType) { |
|
409 |
// |
|
410 |
// int index = 1; |
|
411 |
// String tempFieldName = fieldName; |
|
412 |
// while(fType.get(tempFieldName)!=null && index<1000){ |
|
413 |
// index++; |
|
414 |
// String sufix = String.valueOf(index); |
|
415 |
// tempFieldName = tempFieldName.substring(0, DbaseFile.MAX_FIELD_NAME_LENGTH-sufix.length())+sufix; |
|
416 |
// } |
|
417 |
// if(index>=1000){ |
|
418 |
// throw new RuntimeException("Can't fix duplicated name for field '"+fieldName+"'."); |
|
419 |
// } |
|
420 |
// return tempFieldName; |
|
421 |
// } |
|
422 |
|
|
423 |
protected EditableFeatureType getTheFeatureType() |
|
424 |
throws InitializeException, OpenException { |
|
374 |
protected EditableFeatureType getTheFeatureType() throws InitializeException { |
|
425 | 375 |
try { |
426 | 376 |
this.open(); |
427 | 377 |
} catch (DataException e) { |
428 | 378 |
throw new InitializeException(this.getProviderName(), e); |
429 | 379 |
} |
430 |
return (EditableFeatureType) getResource().execute( |
|
431 |
new ResourceAction() { |
|
432 |
|
|
433 |
public Object run() throws Exception { |
|
434 |
int fieldCount = -1; |
|
435 |
fieldCount = dbfFile.getFieldCount(); |
|
436 |
|
|
437 |
EditableFeatureType fType = getStoreServices().createFeatureType(getName()); |
|
438 |
|
|
439 |
fType.setHasOID(true); |
|
440 |
int precision; |
|
441 |
for (int i = 0; i < fieldCount; i++) { |
|
442 |
char fieldType = dbfFile.getFieldType(i); |
|
443 |
EditableFeatureAttributeDescriptor attr; |
|
444 |
|
|
445 |
String fieldName = dbfFile.getFieldName(i); |
|
446 |
if(fType.get(fieldName)!=null){ |
|
447 |
throw new DuplicatedFieldNameException(fieldName); |
|
448 |
} |
|
449 |
|
|
450 |
if (fieldType == 'L') { |
|
451 |
attr = fType.add(fieldName, DataTypes.BOOLEAN); |
|
452 |
attr.setDefaultValue(new Boolean(false)); |
|
453 |
attr.setAllowNull(false); |
|
454 |
|
|
455 |
} else if ((fieldType == 'F') || (fieldType == 'N')) { |
|
456 |
precision = dbfFile.getFieldDecimalLength(i); |
|
457 |
if (precision > 0) { |
|
458 |
attr = fType.add(fieldName, |
|
459 |
DataTypes.DOUBLE, |
|
460 |
dbfFile.getFieldLength(i)); |
|
461 |
attr.setPrecision(precision); |
|
462 |
attr.setDefaultValue(new Double(0)); |
|
463 |
|
|
464 |
} else { |
|
465 |
int length = dbfFile.getFieldLength(i); |
|
466 |
int type = DataTypes.INT; |
|
467 |
if (length > 9) { |
|
468 |
type = DataTypes.LONG; |
|
469 |
} |
|
470 |
attr = fType.add(fieldName, |
|
471 |
type, |
|
472 |
length); |
|
473 |
attr.setDefaultValue(new Integer(0)); |
|
474 |
} |
|
475 |
attr.setAllowNull(false); |
|
476 |
|
|
477 |
} else if (fieldType == 'C' || getDBFParameters().handleDatesAsStrings()) { |
|
478 |
attr = fType.add(fieldName, DataTypes.STRING); |
|
479 |
attr.setSize(dbfFile.getFieldLength(i)); |
|
480 |
attr.setDefaultValue(""); |
|
481 |
attr.setAllowNull(false); |
|
482 |
|
|
483 |
} else if (fieldType == 'D') { |
|
484 |
attr = fType.add(fieldName, DataTypes.DATE); |
|
485 |
attr.setDefaultValue(new Date(0)); // def value 1-1-1970 |
|
486 |
attr.setAllowNull(true); |
|
487 |
String sfmt = getDBFParameters().getDateFormat(); |
|
488 |
if (!StringUtils.isBlank(sfmt)) { |
|
489 |
try { |
|
490 |
SimpleDateFormat datefmt = new SimpleDateFormat(sfmt, getDBFParameters().getLocale()); |
|
491 |
attr.setDateFormat(datefmt); |
|
492 |
} catch (Exception ex) { |
|
493 |
LOG.warn("Invalid date format ("+sfmt+") specified in DBF parameters.",ex); |
|
494 |
} |
|
495 |
} |
|
496 |
} else { |
|
497 |
throw new InitializeException(getProviderName(), |
|
498 |
new UnknownDataTypeException( |
|
499 |
fieldName, "" |
|
500 |
+ fieldType, getProviderName())); |
|
501 |
} |
|
502 |
} |
|
503 |
|
|
504 |
// FeatureRules rules = fType.getRules(); |
|
505 |
// rules.add(new DBFRowValidator()); |
|
506 |
return fType; |
|
507 |
} |
|
508 |
|
|
380 |
EditableFeatureType featureType = (EditableFeatureType) getResource().execute( |
|
381 |
new ResourceAction() { |
|
382 |
public Object run() throws Exception { |
|
383 |
EditableFeatureType featureType = getStoreServices().createFeatureType(getName()); |
|
384 |
featureType.setHasOID(true); |
|
385 |
dbfFile.getHeader().toFeatureType( |
|
386 |
featureType, |
|
387 |
getDBFParameters().handleDatesAsStrings() |
|
388 |
); |
|
389 |
return featureType; |
|
509 | 390 |
} |
391 |
} |
|
510 | 392 |
); |
393 |
return featureType; |
|
511 | 394 |
} |
512 |
|
|
395 |
|
|
396 |
|
|
513 | 397 |
protected void loadValue(FeatureProvider featureProvider, long rowIndex, |
514 | 398 |
FeatureAttributeDescriptor descriptor) throws ReadException { |
515 | 399 |
|
... | ... | |
518 | 402 |
return; |
519 | 403 |
} |
520 | 404 |
|
521 |
int dbfIndex = this.dbfFile.getFieldIndex(descriptor.getName()); |
|
405 |
int dbfFieldIndex = this.dbfFile.getFieldIndex(descriptor.getName());
|
|
522 | 406 |
|
523 |
if (dbfIndex < 0) { |
|
407 |
if (dbfFieldIndex < 0) {
|
|
524 | 408 |
// Someone asked to load a field |
525 | 409 |
// which does not exist in the DBF file. This can happen |
526 | 410 |
// in editing process when a field has been added |
527 | 411 |
// in the current editing session, so we simply do nothing. |
528 | 412 |
// The expansion manager is expected to manage those new fields |
529 | 413 |
// and their values. |
530 |
long curr_time = System.currentTimeMillis(); |
|
531 |
// This ensures not more than one log every 2 seconds |
|
532 |
if (curr_time - lastLogTime > 2000) { |
|
533 |
LOG.info("Warning: The requested field does not exist in the DBF file. Assumed it's a new field in editing mode."); |
|
534 |
lastLogTime = curr_time; |
|
535 |
} |
|
414 |
this.logger.warn("The requested field ("+descriptor.getName()+") does not exist in the DBF file. Assumed it's a new field in editing mode."); |
|
536 | 415 |
return; |
537 | 416 |
} |
538 | 417 |
|
539 | 418 |
String value = null; |
540 | 419 |
try { |
541 |
value = this.dbfFile.getStringFieldValue(rowIndex, dbfIndex); |
|
420 |
value = this.dbfFile.getStringFieldValue(rowIndex, dbfFieldIndex);
|
|
542 | 421 |
} catch (DataException e) { |
543 | 422 |
throw new ReadException(this.getName(), e); |
544 | 423 |
} |
545 | 424 |
value = value.trim(); |
546 |
int fieldType = descriptor.getType(); |
|
547 |
switch (fieldType) { |
|
425 |
FieldFormatter formatter = new FieldFormatter(); |
|
426 |
|
|
427 |
int index = descriptor.getIndex(); |
|
428 |
Object defaultValue = descriptor.getDefaultValueCoerced(); |
|
429 |
|
|
430 |
switch (descriptor.getType()) { |
|
548 | 431 |
case DataTypes.STRING: |
549 |
featureProvider.set(descriptor.getIndex(), value);
|
|
432 |
featureProvider.set(index, formatter.parseString(value, (String) defaultValue));
|
|
550 | 433 |
break; |
551 | 434 |
|
552 |
case DataTypes.DOUBLE: |
|
553 |
try { |
|
554 |
featureProvider.set(descriptor.getIndex(), new Double(value)); |
|
555 |
} catch (NumberFormatException e) { |
|
556 |
featureProvider.set(descriptor.getIndex(), null); |
|
557 |
} |
|
435 |
case DataTypes.DECIMAL: |
|
436 |
featureProvider.set(index, |
|
437 |
formatter.parseDecimal( |
|
438 |
value, |
|
439 |
descriptor.getMathContext(), |
|
440 |
descriptor.getScale(), |
|
441 |
(BigDecimal) defaultValue |
|
442 |
) |
|
443 |
); |
|
558 | 444 |
break; |
559 | 445 |
|
560 |
case DataTypes.INT: |
|
561 |
try { |
|
562 |
featureProvider.set(descriptor.getIndex(), new Integer(value)); |
|
563 |
} catch (NumberFormatException e) { |
|
564 |
featureProvider.set(descriptor.getIndex(), null); |
|
565 |
} |
|
446 |
case DataTypes.DOUBLE: |
|
447 |
featureProvider.set(index, formatter.parseDouble(value, (Double) defaultValue)); |
|
566 | 448 |
break; |
567 | 449 |
|
568 | 450 |
case DataTypes.FLOAT: |
569 |
try { |
|
570 |
featureProvider.set(descriptor.getIndex(), new Float(value)); |
|
571 |
} catch (NumberFormatException e) { |
|
572 |
featureProvider.set(descriptor.getIndex(), null); |
|
573 |
} |
|
451 |
featureProvider.set(index, formatter.parseFloat(value, (Float) defaultValue)); |
|
574 | 452 |
break; |
575 | 453 |
|
576 | 454 |
case DataTypes.LONG: |
577 |
try { |
|
578 |
featureProvider.set(descriptor.getIndex(), new Long(value)); |
|
579 |
} catch (NumberFormatException e) { |
|
580 |
featureProvider.set(descriptor.getIndex(), null); |
|
581 |
} |
|
455 |
featureProvider.set(index, formatter.parseLong(value, (Long) defaultValue)); |
|
582 | 456 |
break; |
583 | 457 |
|
584 |
case DataTypes.BOOLEAN: |
|
585 |
if (value.equalsIgnoreCase("T")) { |
|
586 |
featureProvider.set(descriptor.getIndex(), Boolean.TRUE); |
|
587 |
} else { |
|
588 |
featureProvider.set(descriptor.getIndex(), Boolean.FALSE); |
|
589 |
|
|
590 |
} |
|
458 |
case DataTypes.INT: |
|
459 |
featureProvider.set(index, formatter.parseInt(value, (Integer) defaultValue)); |
|
591 | 460 |
break; |
592 | 461 |
|
593 | 462 |
case DataTypes.BYTE: |
594 |
try { |
|
595 |
featureProvider.set(descriptor.getIndex(), new Byte(value)); |
|
596 |
} catch (NumberFormatException e) { |
|
597 |
featureProvider.set(descriptor.getIndex(), null); |
|
598 |
} |
|
463 |
featureProvider.set(index, formatter.parseByte(value, (Byte) defaultValue)); |
|
599 | 464 |
break; |
600 | 465 |
|
466 |
case DataTypes.BOOLEAN: |
|
467 |
featureProvider.set(index, formatter.parseBoolean(value, (Boolean) defaultValue)); |
|
468 |
break; |
|
469 |
|
|
470 |
case DataTypes.TIMESTAMP: |
|
471 |
featureProvider.set(index, formatter.parseTimestamp(value, (Date) defaultValue)); |
|
472 |
break; |
|
473 |
|
|
474 |
case DataTypes.TIME: |
|
475 |
featureProvider.set(index, formatter.parseTime(value, (Date) defaultValue)); |
|
476 |
break; |
|
477 |
|
|
601 | 478 |
case DataTypes.DATE: |
602 |
if (value.equals("")) { |
|
603 |
value = null; |
|
604 |
return; |
|
605 |
} |
|
606 |
Date dat = null; |
|
607 |
DateFormat df = new SimpleDateFormat("yyyyMMdd"); |
|
608 |
try { |
|
609 |
dat = df.parse(value); |
|
610 |
} catch (ParseException e) { |
|
611 |
throw new InvalidDateException(df.toString(), value, this.getProviderName(), e); |
|
612 |
} |
|
613 |
featureProvider.set(descriptor.getIndex(), dat); |
|
479 |
featureProvider.set(index, formatter.parseDate(value, (Date) defaultValue)); |
|
614 | 480 |
break; |
615 | 481 |
|
616 | 482 |
default: { |
... | ... | |
618 | 484 |
try { |
619 | 485 |
v = descriptor.getDataType().coerce(value); |
620 | 486 |
} catch (CoercionException ex) { |
621 |
v = descriptor.getDefaultValue();
|
|
487 |
v = defaultValue;
|
|
622 | 488 |
} |
623 |
featureProvider.set(descriptor.getIndex(), v);
|
|
489 |
featureProvider.set(index, v);
|
|
624 | 490 |
} |
625 | 491 |
break; |
626 | 492 |
} |
627 | 493 |
} |
628 | 494 |
|
629 |
private static class InvalidDateException extends ReadException { |
|
630 |
public InvalidDateException(String dateFormat, String value, String store, Throwable cause) { |
|
631 |
super( |
|
632 |
"Can't parse date value '%(value)' with format '%(dateFormat)' in dbf '%(store)'.", |
|
633 |
cause, |
|
634 |
"Cant_parse_date_value_XvalueX_with_format_XdateFormatX_in_dbf_XstoreX", |
|
635 |
0 |
|
636 |
); |
|
637 |
setValue("dateFormat",dateFormat); |
|
638 |
setValue("value", value); |
|
639 |
setValue("store",store); |
|
640 |
} |
|
641 |
} |
|
642 |
|
|
643 |
/** |
|
644 |
* * |
|
645 |
* NOT supported in Alter Mode |
|
646 |
* |
|
647 |
* @param index |
|
648 |
* @return |
|
649 |
* @throws ReadException |
|
650 |
*/ |
|
651 | 495 |
protected FeatureProvider getFeatureProviderByIndex(long index) throws DataException { |
652 | 496 |
return this |
653 | 497 |
.getFeatureProviderByIndex(index, this.getStoreServices() |
... | ... | |
657 | 501 |
public long getFeatureCount() throws ReadException, OpenException, |
658 | 502 |
ResourceNotifyChangesException { |
659 | 503 |
this.open(); |
660 |
return ((Long) getResource().execute(new ResourceAction() {
|
|
504 |
return (long) getResource().execute(new ResourceAction() {
|
|
661 | 505 |
public Object run() throws Exception { |
662 |
return Long.valueOf(dbfFile.getRecordCount());
|
|
506 |
return (long) dbfFile.getRecordCount();
|
|
663 | 507 |
} |
664 |
})).longValue();
|
|
508 |
}); |
|
665 | 509 |
} |
666 | 510 |
|
667 | 511 |
public FeatureSetProvider createSet(FeatureQuery query, FeatureType featureType) |
... | ... | |
782 | 626 |
protected FeatureProvider getFeatureProviderByIndex(long index, |
783 | 627 |
FeatureType featureType) throws DataException { |
784 | 628 |
FeatureProvider featureProvider = this.createFeatureProvider(featureType); |
785 |
featureProvider.setOID(new Long(index));
|
|
629 |
featureProvider.setOID(index);
|
|
786 | 630 |
return featureProvider; |
787 | 631 |
} |
788 | 632 |
|
789 | 633 |
protected void initFeatureProviderByIndex(FeatureProvider featureProvider, |
790 | 634 |
long index, FeatureType featureType) throws DataException { |
791 |
featureProvider.setOID(new Long(index));
|
|
635 |
featureProvider.setOID(index);
|
|
792 | 636 |
} |
793 | 637 |
|
794 | 638 |
/** |
... | ... | |
813 | 657 |
throw rex; |
814 | 658 |
} |
815 | 659 |
|
816 |
Iterator iterator = featureProvider.getType().iterator(); |
|
817 |
while (iterator.hasNext()) { |
|
818 |
FeatureAttributeDescriptor descriptor |
|
819 |
= (FeatureAttributeDescriptor) iterator.next(); |
|
820 |
this.loadValue(featureProvider, index, descriptor); |
|
660 |
for (FeatureAttributeDescriptor desc : featureProvider.getType()) { |
|
661 |
this.loadValue(featureProvider, index, desc); |
|
821 | 662 |
} |
822 | 663 |
} |
823 | 664 |
|
... | ... | |
830 | 671 |
try { |
831 | 672 |
this.counterNewsOIDs = this.getFeatureCount(); |
832 | 673 |
} catch (DataException e) { |
833 |
e.printStackTrace();
|
|
674 |
LOG.warn("Can't initialice counter for news OIDs.",e);
|
|
834 | 675 |
} |
835 | 676 |
|
836 | 677 |
} else { |
837 | 678 |
this.counterNewsOIDs++; |
838 | 679 |
} |
839 |
return new Long(counterNewsOIDs);
|
|
680 |
return counterNewsOIDs;
|
|
840 | 681 |
} |
841 | 682 |
|
842 | 683 |
public boolean supportsAppendMode() { |
... | ... | |
876 | 717 |
}); |
877 | 718 |
} |
878 | 719 |
|
879 |
/* |
|
880 |
* (non-Javadoc) |
|
881 |
* |
|
882 |
* @see |
|
883 |
* org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org. |
|
884 |
* gvsig.fmap.dal.resource.spi.ResourceProvider) |
|
885 |
*/ |
|
886 | 720 |
public void resourceChanged(ResourceProvider resource) { |
887 | 721 |
if (this.getStoreServices()!=null){ |
888 | 722 |
this.getStoreServices().notifyChange( |
Also available in: Unified diff