Revision 29326 branches/v2_0_0_prep/libraries/libFMap_dalfile/src/org/gvsig/fmap/dal/store/dbf/DBFStoreProvider.java
DBFStoreProvider.java | ||
---|---|---|
6 | 6 |
import java.text.DateFormat; |
7 | 7 |
import java.text.ParseException; |
8 | 8 |
import java.util.ArrayList; |
9 |
import java.util.Arrays; |
|
10 | 9 |
import java.util.Date; |
11 | 10 |
import java.util.Iterator; |
12 | 11 |
import java.util.List; |
... | ... | |
40 | 39 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
41 | 40 |
import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
42 | 41 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
43 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
|
44 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
|
45 | 42 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
46 | 43 |
import org.gvsig.fmap.dal.resource.exception.ResourceBeginException; |
47 | 44 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
... | ... | |
53 | 50 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
54 | 51 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
55 | 52 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
53 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
|
56 | 54 |
import org.gvsig.fmap.dal.store.dbf.utils.DbaseFile; |
57 | 55 |
import org.gvsig.tools.ToolsLocator; |
58 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
|
59 | 56 |
import org.gvsig.tools.dynobject.DynClass; |
60 | 57 |
import org.gvsig.tools.dynobject.DynField; |
61 | 58 |
import org.gvsig.tools.dynobject.DynObject; |
62 | 59 |
import org.gvsig.tools.dynobject.DynObjectManager; |
63 | 60 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
64 | 61 |
import org.gvsig.tools.exception.BaseException; |
65 |
import org.gvsig.tools.persistence.PersistenceException; |
|
66 |
import org.gvsig.tools.persistence.PersistentState; |
|
67 | 62 |
|
68 | 63 |
public class DBFStoreProvider extends AbstractFeatureStoreProvider implements |
69 | 64 |
ResourceConsumer { |
... | ... | |
80 | 75 |
|
81 | 76 |
private DbaseFile dbfFile = null; |
82 | 77 |
private ResourceProvider dbfResource; |
83 |
private DBFStoreParameters dbfParams; |
|
84 | 78 |
private long counterNewsOIDs = -1; |
85 | 79 |
private DBFFeatureWriter writer; |
86 | 80 |
|
87 |
public DBFStoreProvider() throws InitializeException { |
|
88 |
super(); |
|
89 |
writer = new DBFFeatureWriter(this.getName()); |
|
81 |
public DBFStoreProvider(DBFStoreParameters params, |
|
82 |
DataStoreProviderServices storeServices) |
|
83 |
throws InitializeException { |
|
84 |
this(params, storeServices, ToolsLocator |
|
85 |
.getDynObjectManager().createDynObject( |
|
86 |
DYNCLASS)); |
|
90 | 87 |
} |
91 | 88 |
|
92 |
public DBFStoreProvider(DBFStoreParameters params) |
|
89 |
protected DBFStoreProvider(DBFStoreParameters params, |
|
90 |
DataStoreProviderServices storeServices, DynObject metadata) |
|
93 | 91 |
throws InitializeException { |
94 |
super(); |
|
95 |
this.init(params); |
|
96 |
writer = new DBFFeatureWriter(this.getName()); |
|
92 |
super(params, storeServices, metadata); |
|
93 |
this.init(params, storeServices); |
|
97 | 94 |
} |
98 | 95 |
|
96 |
protected void init(DBFStoreParameters params, |
|
97 |
DataStoreProviderServices storeServices) throws InitializeException { |
|
98 |
this.setDynValue("DefaultSRS", null); |
|
99 |
this.setDynValue("Envelope", null); |
|
99 | 100 |
|
100 |
protected void init(DBFStoreParameters params) throws InitializeException { |
|
101 |
this.dbfParams = params; |
|
102 |
this.metadata = (DelegatedDynObject) ToolsLocator |
|
103 |
.getDynObjectManager().createDynObject( |
|
104 |
DYNCLASS); |
|
105 |
|
|
106 |
this.metadata.setDynValue("DefaultSRS", null); |
|
107 |
this.metadata.setDynValue("Envelope", null); |
|
108 |
|
|
109 | 101 |
File theFile = getDBFParameters().getDBFFile(); |
110 | 102 |
dbfResource = this.createResource(FileResource.NAME, |
111 | 103 |
new Object[] { theFile.getAbsolutePath() }); |
112 | 104 |
dbfResource.addConsumer(this); |
113 | 105 |
|
114 |
Charset charset = dbfParams.getEncoding();
|
|
106 |
Charset charset = params.getEncoding();
|
|
115 | 107 |
this.dbfFile = new DbaseFile(theFile, charset); |
116 | 108 |
|
109 |
writer = new DBFFeatureWriter(this.getName()); |
|
117 | 110 |
|
118 |
} |
|
119 |
|
|
120 |
public FeatureStoreProvider initialize(FeatureStoreProviderServices store) |
|
121 |
throws InitializeException { |
|
122 |
super.initialize(store); |
|
123 | 111 |
this.initFeatureType(); |
124 |
return this; |
|
112 |
|
|
125 | 113 |
} |
126 | 114 |
|
115 |
|
|
127 | 116 |
public String getName() { |
128 | 117 |
return NAME; |
129 | 118 |
} |
130 | 119 |
|
131 | 120 |
protected DBFStoreParameters getDBFParameters() { |
132 |
return dbfParams;
|
|
121 |
return (DBFStoreParameters) super.getParameters();
|
|
133 | 122 |
} |
134 | 123 |
|
135 | 124 |
|
... | ... | |
161 | 150 |
|
162 | 151 |
public FeatureProvider getFeatureProviderByReference( |
163 | 152 |
FeatureReferenceProviderServices reference) throws DataException { |
164 |
return this.getFeatureProviderByReference(reference, this.store |
|
153 |
return this.getFeatureProviderByReference(reference, this |
|
154 |
.getStoreServices() |
|
165 | 155 |
.getDefaultFeatureType()); |
166 | 156 |
} |
167 | 157 |
|
... | ... | |
170 | 160 |
throws PerformEditingException { |
171 | 161 |
|
172 | 162 |
try { |
163 |
FeatureStore store = this.getStoreServices().getFeatureStore(); |
|
173 | 164 |
this.resourcesBegin(); |
174 |
FeatureSet set = this.store.getFeatureSet();
|
|
165 |
FeatureSet set = store.getFeatureSet(); |
|
175 | 166 |
DBFStoreParameters tmpParams = (DBFStoreParameters) this |
176 | 167 |
.getDBFParameters().getCopy(); |
177 | 168 |
|
178 | 169 |
tmpParams.setDBFFileName(tmpParams.getDBFFileName() + ".tmp"); |
179 | 170 |
|
180 |
writer.begin(tmpParams, this.store
|
|
171 |
writer.begin(tmpParams, store |
|
181 | 172 |
.getDefaultFeatureType(), set.getSize()); |
182 | 173 |
|
183 | 174 |
DisposableIterator iter = set.fastIterator(); |
... | ... | |
223 | 214 |
* =================================================== |
224 | 215 |
*/ |
225 | 216 |
|
226 |
FeatureStoreProviderServices getProviderServices() { |
|
227 |
return this.store; |
|
228 |
} |
|
229 | 217 |
|
230 | 218 |
|
231 | 219 |
protected void initFeatureType() throws InitializeException { |
232 | 220 |
FeatureType defaultType = this.getTheFeatureType().getNotEditableCopy(); |
233 | 221 |
List types = new ArrayList(1); |
234 | 222 |
types.add(defaultType); |
235 |
this.store.setFeatureTypes(types, defaultType);
|
|
223 |
this.getStoreServices().setFeatureTypes(types, defaultType);
|
|
236 | 224 |
} |
237 | 225 |
|
238 | 226 |
protected EditableFeatureType getTheFeatureType() throws InitializeException { |
... | ... | |
246 | 234 |
int fieldCount = -1; |
247 | 235 |
fieldCount = dbfFile.getFieldCount(); |
248 | 236 |
|
249 |
EditableFeatureType fType = this.store.createFeatureType(); |
|
237 |
EditableFeatureType fType = this.getStoreServices() |
|
238 |
.createFeatureType(); |
|
250 | 239 |
|
251 | 240 |
fType.setHasOID(true); |
252 | 241 |
int precision; |
... | ... | |
312 | 301 |
try { |
313 | 302 |
value = this.dbfFile.getStringFieldValue(rowIndex, dbfIndex); |
314 | 303 |
} catch (DataException e) { |
315 |
throw new ReadException(this.store.getName(), e);
|
|
304 |
throw new ReadException(this.getName(), e); |
|
316 | 305 |
} |
317 | 306 |
value = value.trim(); |
318 | 307 |
int fieldType = descriptor.getDataType(); |
... | ... | |
390 | 379 |
try { |
391 | 380 |
dat = df.parse(strAux); |
392 | 381 |
} catch (ParseException e) { |
393 |
throw new ReadException(this.store.getName(), e);
|
|
382 |
throw new ReadException(this.getName(), e); |
|
394 | 383 |
} |
395 | 384 |
featureProvider.set(descriptor.getIndex(), dat); |
396 | 385 |
break; |
... | ... | |
413 | 402 |
*/ |
414 | 403 |
protected FeatureProvider getFeatureProviderByIndex(long index) throws DataException { |
415 | 404 |
return this |
416 |
.getFeatureProviderByIndex(index, this.store
|
|
405 |
.getFeatureProviderByIndex(index, this.getStoreServices()
|
|
417 | 406 |
.getDefaultFeatureType()); |
418 | 407 |
} |
419 | 408 |
|
... | ... | |
483 | 472 |
|
484 | 473 |
protected void loadMetadataValues() throws DynFieldNotFoundException, |
485 | 474 |
ReadException { |
486 |
this.metadata.setDynValue(DBFLibrary.DYNFIELD_CODEPAGE_NAME, new Byte(
|
|
475 |
this.setDynValue(DBFLibrary.DYNFIELD_CODEPAGE_NAME, new Byte( |
|
487 | 476 |
this.dbfFile.getCodePage())); |
488 |
this.metadata.setDynValue(DYNFIELD_CURRENT_ENCODING, dbfFile
|
|
477 |
this.setDynValue(DYNFIELD_CURRENT_ENCODING, dbfFile |
|
489 | 478 |
.getCurrenCharset().name()); |
490 |
this.metadata.setDynValue(DYNFIELD_ORIGINAL_ENCODING, dbfFile
|
|
479 |
this.setDynValue(DYNFIELD_ORIGINAL_ENCODING, dbfFile |
|
491 | 480 |
.getOriginalCharset().name()); |
492 | 481 |
} |
493 | 482 |
|
... | ... | |
631 | 620 |
public void append(FeatureProvider featureProvider) throws DataException { |
632 | 621 |
this.resourcesBegin(); |
633 | 622 |
try { |
634 |
writer.append(store.createFeature(featureProvider));
|
|
623 |
writer.append(getStoreServices().createFeature(featureProvider));
|
|
635 | 624 |
} finally { |
636 | 625 |
this.resourcesEnd(); |
637 | 626 |
} |
... | ... | |
641 | 630 |
this.close(); |
642 | 631 |
this.resourcesBegin(); |
643 | 632 |
try { |
644 |
FeatureSet set = this.store.getFeatureSet(); |
|
645 |
writer.begin(this.getDBFParameters(), this.store |
|
633 |
FeatureSet set = this.getStoreServices().getFeatureStore() |
|
634 |
.getFeatureSet(); |
|
635 |
writer.begin(this.getDBFParameters(), this.getStoreServices() |
|
646 | 636 |
.getDefaultFeatureType(), set.getSize()); |
647 | 637 |
} finally { |
648 | 638 |
this.resourcesEnd(); |
... | ... | |
661 | 651 |
} |
662 | 652 |
} |
663 | 653 |
|
664 |
public PersistentState getState() throws PersistenceException { |
|
665 |
// Nothing to do |
|
666 |
return null; |
|
667 |
} |
|
668 |
|
|
669 |
public void saveToState(PersistentState state) throws PersistenceException { |
|
670 |
try { |
|
671 |
this.init((DBFStoreParameters) this.store.getParameters()); |
|
672 |
} catch (InitializeException e) { |
|
673 |
throw new PersistenceException(e); |
|
674 |
} |
|
675 |
} |
|
676 |
|
|
677 |
public void loadFromState(PersistentState state) throws PersistenceException { |
|
678 |
try { |
|
679 |
this.init((DBFStoreParameters) this.store.getParameters()); |
|
680 |
} catch (InitializeException e) { |
|
681 |
throw new PersistenceException(e); |
|
682 |
} |
|
683 |
} |
|
684 |
|
|
685 |
public Iterator getChilds() { |
|
686 |
return Arrays.asList(new DynObject[] { this.dbfParams }).iterator(); |
|
687 |
|
|
688 |
} |
|
689 |
|
|
690 | 654 |
protected static void registerDynClass() { |
691 | 655 |
DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
692 | 656 |
DynClass dynClass; |
... | ... | |
731 | 695 |
* gvsig.fmap.dal.resource.spi.ResourceProvider) |
732 | 696 |
*/ |
733 | 697 |
public void resourceChanged(ResourceProvider resource) { |
734 |
this.store.notifyChange(DataStoreNotification.RESOURCE_CHANGED, |
|
698 |
this.getStoreServices().notifyChange( |
|
699 |
DataStoreNotification.RESOURCE_CHANGED, |
|
735 | 700 |
resource); |
736 | 701 |
} |
737 | 702 |
|
Also available in: Unified diff