svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dalfile / src / org / gvsig / fmap / dal / store / dbf / DBFStoreProvider.java @ 27875
History | View | Annotate | Download (19.9 KB)
1 | 24498 | jmvivo | package org.gvsig.fmap.dal.store.dbf; |
---|---|---|---|
2 | 23912 | jmvivo | |
3 | import java.io.File; |
||
4 | 24199 | jmvivo | import java.io.IOException; |
5 | 23912 | jmvivo | import java.text.DateFormat; |
6 | import java.text.ParseException; |
||
7 | import java.util.ArrayList; |
||
8 | 25789 | jmvivo | import java.util.Arrays; |
9 | 23912 | jmvivo | import java.util.Date; |
10 | import java.util.Iterator; |
||
11 | import java.util.List; |
||
12 | import java.util.Locale; |
||
13 | |||
14 | 24710 | jmvivo | import org.gvsig.fmap.dal.DALLocator; |
15 | import org.gvsig.fmap.dal.DataManager; |
||
16 | 24898 | jmvivo | import org.gvsig.fmap.dal.DataServerExplorer; |
17 | 25789 | jmvivo | import org.gvsig.fmap.dal.DataStoreNotification; |
18 | 24498 | jmvivo | import org.gvsig.fmap.dal.DataTypes; |
19 | 24502 | jmvivo | import org.gvsig.fmap.dal.exception.CloseException; |
20 | import org.gvsig.fmap.dal.exception.DataException; |
||
21 | import org.gvsig.fmap.dal.exception.FileNotFoundException; |
||
22 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
23 | import org.gvsig.fmap.dal.exception.OpenException; |
||
24 | import org.gvsig.fmap.dal.exception.ReadException; |
||
25 | import org.gvsig.fmap.dal.exception.UnsupportedVersionException; |
||
26 | 27723 | jmvivo | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
27 | 27525 | jmvivo | import org.gvsig.fmap.dal.feature.DisposableIterator; |
28 | 25667 | jmvivo | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
29 | 24498 | jmvivo | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
30 | import org.gvsig.fmap.dal.feature.Feature; |
||
31 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
32 | import org.gvsig.fmap.dal.feature.FeatureQuery; |
||
33 | 24710 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureSet; |
34 | 25789 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureStore; |
35 | 24498 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureType; |
36 | 24502 | jmvivo | import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
37 | 26790 | jmvivo | import org.gvsig.fmap.dal.feature.exception.UnknownDataTypeException; |
38 | 24498 | jmvivo | import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
39 | import org.gvsig.fmap.dal.feature.spi.FeatureData; |
||
40 | import org.gvsig.fmap.dal.feature.spi.FeatureReferenceProviderServices; |
||
41 | import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
||
42 | import org.gvsig.fmap.dal.feature.spi.FeatureStoreProvider; |
||
43 | import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
||
44 | import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
||
45 | import org.gvsig.fmap.dal.resource.exception.ResourceBeginException; |
||
46 | 27875 | vcaballero | import org.gvsig.fmap.dal.resource.exception.ResourceException; |
47 | 24498 | jmvivo | 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 | 24893 | jmvivo | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
54 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
||
55 | 24498 | jmvivo | import org.gvsig.fmap.dal.store.dbf.utils.DbaseFile; |
56 | 25789 | jmvivo | import org.gvsig.tools.ToolsLocator; |
57 | import org.gvsig.tools.dynobject.DelegatedDynObject; |
||
58 | import org.gvsig.tools.dynobject.DynClass; |
||
59 | import org.gvsig.tools.dynobject.DynField; |
||
60 | import org.gvsig.tools.dynobject.DynObject; |
||
61 | import org.gvsig.tools.dynobject.DynObjectManager; |
||
62 | import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
||
63 | 26252 | jmvivo | import org.gvsig.tools.exception.BaseException; |
64 | 24177 | jmvivo | import org.gvsig.tools.persistence.PersistenceException; |
65 | import org.gvsig.tools.persistence.PersistentState; |
||
66 | 23912 | jmvivo | |
67 | public class DBFStoreProvider extends AbstractFeatureStoreProvider implements |
||
68 | ResourceConsumer { |
||
69 | 24205 | jmvivo | |
70 | 24193 | jmvivo | public static String NAME = "DBFStore"; |
71 | public static String DESCRIPTION = "DBF file"; |
||
72 | 23912 | jmvivo | // private DBFResource dbf = null;
|
73 | private DbaseFile dbfFile = null; |
||
74 | 24447 | jmvivo | private ResourceProvider dbfResource;
|
75 | 23912 | jmvivo | private static final Locale ukLocale = new Locale("en", "UK"); |
76 | 25789 | jmvivo | private static final String DYNCLASS_NAME = "DBFStore"; |
77 | protected static DynClass DYNCLASS = null; |
||
78 | 24447 | jmvivo | private DBFStoreParameters dbfParams;
|
79 | 24432 | jmvivo | private long counterNewsOIDs = -1; |
80 | 25752 | vcaballero | private DBFFeatureWriter writer;
|
81 | 25977 | jmvivo | |
82 | 24710 | jmvivo | public DBFStoreProvider() throws InitializeException { |
83 | super();
|
||
84 | 25752 | vcaballero | writer = new DBFFeatureWriter(this.getName()); |
85 | 24710 | jmvivo | } |
86 | |||
87 | 24205 | jmvivo | public DBFStoreProvider(DBFStoreParameters params)
|
88 | 23912 | jmvivo | throws InitializeException {
|
89 | 24205 | jmvivo | super();
|
90 | 24959 | jmvivo | this.init(params);
|
91 | 25752 | vcaballero | writer = new DBFFeatureWriter(this.getName()); |
92 | 24959 | jmvivo | } |
93 | |||
94 | 25977 | jmvivo | |
95 | 24959 | jmvivo | protected void init(DBFStoreParameters params) throws InitializeException { |
96 | 24447 | jmvivo | this.dbfParams = params;
|
97 | 25789 | jmvivo | this.dynObject = (DelegatedDynObject) ToolsLocator
|
98 | .getDynObjectManager().createDynObject( |
||
99 | DYNCLASS); |
||
100 | 23912 | jmvivo | |
101 | 25924 | jmvivo | this.dynObject.setDynValue("DefaultSRS", null); |
102 | this.dynObject.setDynValue("Envelope", null); |
||
103 | |||
104 | 24710 | jmvivo | File theFile = getDBFParameters().getDBFFile();
|
105 | 24447 | jmvivo | dbfResource = this.createResource(FileResource.NAME,
|
106 | 24710 | jmvivo | new Object[] { theFile.getAbsolutePath() }); |
107 | 24447 | jmvivo | dbfResource.addConsumer(this);
|
108 | 23912 | jmvivo | |
109 | 25789 | jmvivo | this.dbfFile = new DbaseFile(theFile); |
110 | 23912 | jmvivo | |
111 | 24205 | jmvivo | } |
112 | 23996 | jmvivo | |
113 | 24205 | jmvivo | public FeatureStoreProvider initialize(FeatureStoreProviderServices store)
|
114 | throws InitializeException {
|
||
115 | super.initialize(store);
|
||
116 | 23912 | jmvivo | this.initFeatureType();
|
117 | return this; |
||
118 | } |
||
119 | |||
120 | 24205 | jmvivo | public String getName() { |
121 | return NAME;
|
||
122 | } |
||
123 | 23912 | jmvivo | |
124 | 24447 | jmvivo | protected DBFStoreParameters getDBFParameters() {
|
125 | return dbfParams;
|
||
126 | 24205 | jmvivo | } |
127 | |||
128 | |||
129 | 24876 | jmvivo | public DataServerExplorer getExplorer() throws ReadException { |
130 | 24710 | jmvivo | DataManager manager = DALLocator.getDataManager(); |
131 | 24893 | jmvivo | FilesystemServerExplorerParameters params; |
132 | 24710 | jmvivo | try {
|
133 | 24893 | jmvivo | params = (FilesystemServerExplorerParameters) manager |
134 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
135 | 24710 | jmvivo | params.setRoot(this.getDBFParameters().getDBFFile().getParent());
|
136 | 24876 | jmvivo | return manager.createServerExplorer(params);
|
137 | 24710 | jmvivo | } catch (DataException e) {
|
138 | throw new ReadException(this.getName(), e); |
||
139 | 27723 | jmvivo | } catch (ValidateDataParametersException e) {
|
140 | // TODO Auto-generated catch block
|
||
141 | throw new ReadException(this.getName(), e); |
||
142 | 24710 | jmvivo | } |
143 | 23912 | jmvivo | } |
144 | |||
145 | 24248 | jjdelcerro | public FeatureData getFeatureDataByReference(
|
146 | FeatureReferenceProviderServices reference, FeatureType featureType) |
||
147 | 23912 | jmvivo | throws DataException {
|
148 | |||
149 | 25566 | vcaballero | return this.getFeatureDataByIndex(((Long) reference.getOID()) |
150 | .longValue(), |
||
151 | 23912 | jmvivo | featureType); |
152 | } |
||
153 | |||
154 | 24248 | jjdelcerro | |
155 | public FeatureData getFeatureDataByReference(
|
||
156 | FeatureReferenceProviderServices reference) throws DataException {
|
||
157 | return this.getFeatureDataByReference(reference, this.store |
||
158 | .getDefaultFeatureType()); |
||
159 | } |
||
160 | |||
161 | 24791 | jmvivo | public void performEditing(Iterator deleteds, Iterator inserteds, |
162 | Iterator updateds)
|
||
163 | throws PerformEditingException {
|
||
164 | 23912 | jmvivo | |
165 | 24710 | jmvivo | try {
|
166 | 26252 | jmvivo | this.resourcesBegin();
|
167 | 24710 | jmvivo | FeatureSet set = this.store.getFeatureSet();
|
168 | 25997 | jmvivo | DBFStoreParameters tmpParams = (DBFStoreParameters) this
|
169 | .getDBFParameters().getCopy(); |
||
170 | |||
171 | tmpParams.setDBFFileName(tmpParams.getDBFFileName() + ".tmp");
|
||
172 | |||
173 | writer.begin(tmpParams, this.store
|
||
174 | 24791 | jmvivo | .getDefaultFeatureType(), set.getSize()); |
175 | 24710 | jmvivo | |
176 | 27525 | jmvivo | DisposableIterator iter = set.fastIterator(); |
177 | 24710 | jmvivo | while (iter.hasNext()) {
|
178 | 25782 | vcaballero | Feature feature=(Feature) iter.next(); |
179 | 25993 | jmvivo | writer.append(feature); |
180 | 24710 | jmvivo | } |
181 | 27525 | jmvivo | iter.dispose(); |
182 | 24710 | jmvivo | |
183 | 27525 | jmvivo | |
184 | 24791 | jmvivo | writer.end(); |
185 | |||
186 | 25997 | jmvivo | try {
|
187 | this.close();
|
||
188 | } catch (CloseException e1) {
|
||
189 | throw new PerformEditingException(this.getName(), e1); |
||
190 | } |
||
191 | this.getDBFParameters().getDBFFile().delete();
|
||
192 | tmpParams.getDBFFile().renameTo( |
||
193 | this.getDBFParameters().getDBFFile());
|
||
194 | |||
195 | 26252 | jmvivo | this.resourcesNotifyChanges();
|
196 | this.initFeatureType();
|
||
197 | 24710 | jmvivo | } catch (Exception e) { |
198 | throw new PerformEditingException(this.getName(), e); |
||
199 | } finally {
|
||
200 | 26252 | jmvivo | this.resourcesEnd();
|
201 | 24710 | jmvivo | } |
202 | |||
203 | 24432 | jmvivo | this.counterNewsOIDs = -1; |
204 | 23912 | jmvivo | } |
205 | |||
206 | /*
|
||
207 | * ==================================================
|
||
208 | */
|
||
209 | |||
210 | 24248 | jjdelcerro | public FeatureData createFeatureData(FeatureType type) throws DataException { |
211 | 27757 | cordinyana | return new DBFFeatureData(this, type); |
212 | 23912 | jmvivo | } |
213 | |||
214 | |||
215 | /*
|
||
216 | * ===================================================
|
||
217 | */
|
||
218 | |||
219 | FeatureStoreProviderServices getProviderServices() { |
||
220 | return this.store; |
||
221 | } |
||
222 | |||
223 | |||
224 | 24447 | jmvivo | protected void initFeatureType() throws InitializeException { |
225 | FeatureType defaultType = this.getTheFeatureType().getNotEditableCopy();
|
||
226 | List types = new ArrayList(1); |
||
227 | types.add(defaultType); |
||
228 | this.store.setFeatureTypes(types, defaultType);
|
||
229 | } |
||
230 | |||
231 | protected EditableFeatureType getTheFeatureType() throws InitializeException { |
||
232 | 23912 | jmvivo | try {
|
233 | this.open();
|
||
234 | 26252 | jmvivo | this.resourcesBegin();
|
235 | 23912 | jmvivo | } catch (DataException e) {
|
236 | throw new InitializeException(this.getName(), e); |
||
237 | } |
||
238 | try {
|
||
239 | int fieldCount = -1; |
||
240 | fieldCount = dbfFile.getFieldCount(); |
||
241 | |||
242 | EditableFeatureType fType = this.store.createFeatureType();
|
||
243 | |||
244 | 25229 | jmvivo | fType.setHasOID(true);
|
245 | 25667 | jmvivo | int precision;
|
246 | 23912 | jmvivo | for (int i = 0; i < fieldCount; i++) { |
247 | char fieldType = dbfFile.getFieldType(i);
|
||
248 | 25667 | jmvivo | EditableFeatureAttributeDescriptor attr; |
249 | 23912 | jmvivo | |
250 | if (fieldType == 'L') { |
||
251 | 25667 | jmvivo | attr = fType |
252 | .add(dbfFile.getFieldName(i), DataTypes.BOOLEAN); |
||
253 | 26233 | vcaballero | attr.setDefaultValue(new Boolean(false)); |
254 | 25667 | jmvivo | attr.setAllowNull(false);
|
255 | 23912 | jmvivo | |
256 | } else if ((fieldType == 'F') || (fieldType == 'N')) { |
||
257 | precision = dbfFile.getFieldDecimalLength(i); |
||
258 | if (precision > 0) { |
||
259 | 25667 | jmvivo | attr = fType.add(dbfFile.getFieldName(i), |
260 | 25981 | jmvivo | DataTypes.DOUBLE, dbfFile.getFieldLength(i)); |
261 | attr.setPrecision(precision); |
||
262 | 26233 | vcaballero | attr.setDefaultValue(new Double(0)); |
263 | 25981 | jmvivo | |
264 | 23912 | jmvivo | } else {
|
265 | 25667 | jmvivo | attr = fType |
266 | .add(dbfFile.getFieldName(i), DataTypes.INT); |
||
267 | 26233 | vcaballero | attr.setDefaultValue(new Integer(0)); |
268 | 23912 | jmvivo | } |
269 | 25667 | jmvivo | attr.setAllowNull(false);
|
270 | |||
271 | 23912 | jmvivo | } else if (fieldType == 'C') { |
272 | 25667 | jmvivo | attr = fType.add(dbfFile.getFieldName(i), DataTypes.STRING); |
273 | attr.setSize(dbfFile.getFieldLength(i)); |
||
274 | attr.setDefaultValue("");
|
||
275 | attr.setAllowNull(false);
|
||
276 | |||
277 | 23912 | jmvivo | } else if (fieldType == 'D') { |
278 | 25667 | jmvivo | attr = fType.add(dbfFile.getFieldName(i), DataTypes.DATE); |
279 | attr.setDefaultValue(null);
|
||
280 | attr.setAllowNull(true);
|
||
281 | 23912 | jmvivo | } else {
|
282 | throw new InitializeException(this.getName(), |
||
283 | 26790 | jmvivo | new UnknownDataTypeException(
|
284 | 23912 | jmvivo | dbfFile.getFieldName(i), "" + fieldType,
|
285 | this.getName()));
|
||
286 | } |
||
287 | } |
||
288 | 24447 | jmvivo | return fType;
|
289 | 23912 | jmvivo | } finally {
|
290 | 26252 | jmvivo | this.resourcesEnd();
|
291 | 23912 | jmvivo | } |
292 | } |
||
293 | |||
294 | |||
295 | protected void loadValue(FeatureData featureData, int rowIndex, |
||
296 | FeatureAttributeDescriptor descriptor) throws ReadException {
|
||
297 | if (descriptor.getEvaluator() != null) { |
||
298 | // Nothing to do
|
||
299 | return;
|
||
300 | } |
||
301 | |||
302 | 25245 | jmvivo | |
303 | 26359 | jmvivo | int dbfIndex = this.dbfFile.getFieldIndex(descriptor.getName()); |
304 | 23912 | jmvivo | String value = null; |
305 | try {
|
||
306 | 26359 | jmvivo | value = this.dbfFile.getStringFieldValue(rowIndex, dbfIndex);
|
307 | 23912 | jmvivo | } catch (DataException e) {
|
308 | throw new ReadException(this.store.getName(), e); |
||
309 | } |
||
310 | value = value.trim(); |
||
311 | int fieldType = descriptor.getDataType();
|
||
312 | switch (fieldType) {
|
||
313 | case DataTypes.STRING:
|
||
314 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), value); |
315 | 23912 | jmvivo | break;
|
316 | |||
317 | case DataTypes.DOUBLE:
|
||
318 | try {
|
||
319 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), new Double(value)); |
320 | 23912 | jmvivo | } catch (NumberFormatException e) { |
321 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), null);
|
322 | 23912 | jmvivo | } |
323 | break;
|
||
324 | |||
325 | case DataTypes.INT:
|
||
326 | try {
|
||
327 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), new Integer(value)); |
328 | 23912 | jmvivo | } catch (NumberFormatException e) { |
329 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), null);
|
330 | 23912 | jmvivo | } |
331 | break;
|
||
332 | |||
333 | case DataTypes.FLOAT:
|
||
334 | try {
|
||
335 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), new Float(value)); |
336 | 23912 | jmvivo | } catch (NumberFormatException e) { |
337 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), null);
|
338 | 23912 | jmvivo | } |
339 | break;
|
||
340 | |||
341 | case DataTypes.LONG:
|
||
342 | try {
|
||
343 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), new Long(value)); |
344 | 23912 | jmvivo | } catch (NumberFormatException e) { |
345 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), null);
|
346 | 23912 | jmvivo | } |
347 | break;
|
||
348 | |||
349 | case DataTypes.BOOLEAN:
|
||
350 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), new Boolean(value)); |
351 | 23912 | jmvivo | break;
|
352 | |||
353 | case DataTypes.BYTE:
|
||
354 | try {
|
||
355 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), new Byte(value)); |
356 | 23912 | jmvivo | } catch (NumberFormatException e) { |
357 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), null);
|
358 | 23912 | jmvivo | } |
359 | break;
|
||
360 | |||
361 | case DataTypes.DATE:
|
||
362 | String year = value.substring(0, 4); |
||
363 | String month = value.substring(4, 6); |
||
364 | String day = value.substring(6, 8); |
||
365 | DateFormat df;
|
||
366 | if (descriptor.getDateFormat() == null){ |
||
367 | df = DateFormat.getDateInstance(DateFormat.SHORT, |
||
368 | ukLocale); |
||
369 | } else{
|
||
370 | df = descriptor.getDateFormat(); |
||
371 | } |
||
372 | /*
|
||
373 | * Calendar c = Calendar.getInstance(); c.clear();
|
||
374 | * c.set(Integer.parseInt(year), Integer.parseInt(month),
|
||
375 | * Integer.parseInt(day)); c.set(Calendar.MILLISECOND, 0);
|
||
376 | */
|
||
377 | String strAux = month + "/" + day + "/" + year; |
||
378 | Date dat = null; |
||
379 | try {
|
||
380 | dat = df.parse(strAux); |
||
381 | } catch (ParseException e) { |
||
382 | throw new ReadException(this.store.getName(), e); |
||
383 | } |
||
384 | 26359 | jmvivo | featureData.set(descriptor.getIndex(), dat); |
385 | 23912 | jmvivo | break;
|
386 | |||
387 | |||
388 | default:
|
||
389 | 26359 | jmvivo | featureData |
390 | .set(descriptor.getIndex(), descriptor.getDefaultValue()); |
||
391 | 23912 | jmvivo | break;
|
392 | } |
||
393 | } |
||
394 | |||
395 | |||
396 | /***
|
||
397 | * NOT supported in Alter Mode
|
||
398 | *
|
||
399 | * @param index
|
||
400 | * @return
|
||
401 | * @throws ReadException
|
||
402 | */
|
||
403 | protected FeatureData getFeatureDataByIndex(long index) throws DataException { |
||
404 | return this |
||
405 | .getFeatureDataByIndex(index, this.store
|
||
406 | .getDefaultFeatureType()); |
||
407 | } |
||
408 | |||
409 | protected int getFeatureCount() throws ReadException, OpenException, |
||
410 | ResourceNotifyChangesException { |
||
411 | this.open();
|
||
412 | try {
|
||
413 | 26252 | jmvivo | this.resourcesBegin();
|
414 | 23912 | jmvivo | } catch (ResourceBeginException e) {
|
415 | throw new ReadException(this.getName(), e); |
||
416 | } |
||
417 | try {
|
||
418 | return this.dbfFile.getRecordCount(); |
||
419 | } finally {
|
||
420 | 26252 | jmvivo | this.resourcesEnd();
|
421 | 23912 | jmvivo | } |
422 | } |
||
423 | |||
424 | 27274 | jmvivo | public FeatureSetProvider createSet(FeatureQuery query, FeatureType featureType)
|
425 | 23912 | jmvivo | throws DataException {
|
426 | 27274 | jmvivo | return new DBFSetProvider(this, query, featureType); |
427 | 23912 | jmvivo | } |
428 | |||
429 | public boolean canCreate() { |
||
430 | return true; |
||
431 | } |
||
432 | |||
433 | 24447 | jmvivo | public boolean canWriteGeometry(int geometryType) throws DataException { |
434 | 23912 | jmvivo | return false; |
435 | } |
||
436 | |||
437 | public void open() throws OpenException { |
||
438 | if (this.dbfFile.isOpen()) { |
||
439 | return;
|
||
440 | } |
||
441 | 23996 | jmvivo | try {
|
442 | 26252 | jmvivo | this.resourcesBegin();
|
443 | 23996 | jmvivo | } catch (ResourceBeginException e) {
|
444 | throw new OpenException(this.getName(), e); |
||
445 | } |
||
446 | try {
|
||
447 | 26252 | jmvivo | this.openFile();
|
448 | this.resourcesOpen();
|
||
449 | 23912 | jmvivo | |
450 | 25789 | jmvivo | // Load metadata values
|
451 | 26252 | jmvivo | this.loadMetadataValues();
|
452 | 25789 | jmvivo | |
453 | 23996 | jmvivo | } catch (UnsupportedVersionException e) {
|
454 | throw new OpenException(this.getName(), e); |
||
455 | } catch (ResourceNotifyOpenException e) {
|
||
456 | throw new OpenException(this.getName(), e); |
||
457 | 24199 | jmvivo | } catch (FileNotFoundException e) { |
458 | throw new OpenException(this.getName(), e); |
||
459 | } catch (IOException e) { |
||
460 | throw new OpenException(this.getName(), e); |
||
461 | 26252 | jmvivo | } catch (BaseException e) {
|
462 | throw new OpenException(this.getName(), e); |
||
463 | 23996 | jmvivo | } finally {
|
464 | 26252 | jmvivo | this.resourcesEnd();
|
465 | 23996 | jmvivo | } |
466 | 23912 | jmvivo | } |
467 | |||
468 | 26252 | jmvivo | protected void openFile() throws FileNotFoundException, |
469 | UnsupportedVersionException, IOException, DataException {
|
||
470 | this.dbfFile.open();
|
||
471 | } |
||
472 | |||
473 | protected void loadMetadataValues() throws DynFieldNotFoundException, |
||
474 | ReadException { |
||
475 | this.dynObject.setDynValue(DBFLibrary.DYNFIELD_CODEPAGE_NAME, new Byte( |
||
476 | this.dbfFile.getCodePage()));
|
||
477 | |||
478 | } |
||
479 | |||
480 | 23912 | jmvivo | public void close() throws CloseException { |
481 | if (!this.dbfFile.isOpen()) { |
||
482 | return;
|
||
483 | } |
||
484 | 25997 | jmvivo | super.close();
|
485 | 23912 | jmvivo | //Cerrar recurso
|
486 | try {
|
||
487 | 26252 | jmvivo | this.resourcesBegin();
|
488 | 23912 | jmvivo | } catch (ResourceBeginException e) {
|
489 | throw new CloseException(this.getName(), e); |
||
490 | } |
||
491 | try {
|
||
492 | 26252 | jmvivo | this.closeFile();
|
493 | this.resourcesNotifyClose();
|
||
494 | 23912 | jmvivo | |
495 | } catch (ResourceNotifyCloseException e) {
|
||
496 | throw new CloseException(this.getName(), e); |
||
497 | } finally {
|
||
498 | 26252 | jmvivo | this.resourcesEnd();
|
499 | 23912 | jmvivo | } |
500 | } |
||
501 | |||
502 | 26252 | jmvivo | protected void closeFile() throws CloseException { |
503 | this.dbfFile.close();
|
||
504 | 25997 | jmvivo | } |
505 | |||
506 | 23912 | jmvivo | public void dispose() throws CloseException { |
507 | this.close();
|
||
508 | dbfFile = null;
|
||
509 | 24447 | jmvivo | this.dbfResource.removeConsumer(this); |
510 | dbfResource = null;
|
||
511 | 23912 | jmvivo | super.dispose();
|
512 | } |
||
513 | |||
514 | public boolean closeResourceRequested(ResourceProvider resource) { |
||
515 | try {
|
||
516 | this.close();
|
||
517 | } catch (CloseException e) {
|
||
518 | return false; |
||
519 | } |
||
520 | return true; |
||
521 | } |
||
522 | |||
523 | public boolean allowWrite() { |
||
524 | File file;
|
||
525 | try {
|
||
526 | 25213 | jmvivo | file = new File((String) this.dbfResource.get()); |
527 | 23912 | jmvivo | } catch (AccessResourceException e) {
|
528 | return false; |
||
529 | } |
||
530 | 25579 | vcaballero | return file.canWrite();
|
531 | 23912 | jmvivo | } |
532 | |||
533 | public void refresh() throws OpenException { |
||
534 | try {
|
||
535 | this.close();
|
||
536 | } catch (CloseException e) {
|
||
537 | throw new OpenException(this.getName(), e); |
||
538 | } |
||
539 | this.open();
|
||
540 | try {
|
||
541 | 26289 | jmvivo | this.initFeatureType();
|
542 | 23912 | jmvivo | } catch (InitializeException e) {
|
543 | throw new OpenException(this.getName(), e); |
||
544 | } |
||
545 | } |
||
546 | |||
547 | 24432 | jmvivo | /**
|
548 | 24447 | jmvivo | *
|
549 | 24432 | jmvivo | * @param index
|
550 | * @param featureType
|
||
551 | * @return
|
||
552 | * @throws ReadException
|
||
553 | */
|
||
554 | protected FeatureData getFeatureDataByIndex(long index, |
||
555 | FeatureType featureType) throws DataException {
|
||
556 | FeatureData featureData = this.createFeatureData(featureType);
|
||
557 | featureData.setOID(new Long(index)); |
||
558 | return featureData;
|
||
559 | 24123 | jmvivo | } |
560 | 23912 | jmvivo | |
561 | 24447 | jmvivo | protected void initFeatureDataByIndex(FeatureData featureData, |
562 | long index, FeatureType featureType) throws DataException { |
||
563 | featureData.setOID(new Long(index)); |
||
564 | } |
||
565 | |||
566 | 24432 | jmvivo | /**
|
567 | 24447 | jmvivo | *
|
568 | * @param featureData
|
||
569 | * @throws DataException
|
||
570 | 24432 | jmvivo | */
|
571 | protected void loadFeatureDataByIndex(FeatureData featureData) |
||
572 | throws DataException {
|
||
573 | this.open();
|
||
574 | 26252 | jmvivo | this.resourcesBegin();
|
575 | 24432 | jmvivo | long index = ((Long) featureData.getOID()).longValue(); |
576 | try {
|
||
577 | if (index >= this.dbfFile.getRecordCount()) { |
||
578 | // FIXME
|
||
579 | throw new ArrayIndexOutOfBoundsException("" + index); |
||
580 | } |
||
581 | Iterator iterator = featureData.getType().iterator();
|
||
582 | while (iterator.hasNext()) {
|
||
583 | FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator |
||
584 | .next(); |
||
585 | this.loadValue(featureData, (int) index, descriptor); |
||
586 | } |
||
587 | |||
588 | |||
589 | } finally {
|
||
590 | 26252 | jmvivo | this.resourcesEnd();
|
591 | 24432 | jmvivo | } |
592 | } |
||
593 | |||
594 | 24248 | jjdelcerro | public int getFeatureReferenceOIDType() { |
595 | 24123 | jmvivo | return DataTypes.LONG;
|
596 | } |
||
597 | |||
598 | 24432 | jmvivo | public Object createNewOID() { |
599 | if (this.counterNewsOIDs < 0) { |
||
600 | try {
|
||
601 | this.counterNewsOIDs = this.getFeatureCount(); |
||
602 | } catch (DataException e) {
|
||
603 | e.printStackTrace(); |
||
604 | } |
||
605 | |||
606 | 25752 | vcaballero | }else{
|
607 | this.counterNewsOIDs++;
|
||
608 | 24432 | jmvivo | } |
609 | 25610 | vcaballero | return new Long(counterNewsOIDs); |
610 | 24432 | jmvivo | } |
611 | |||
612 | 24710 | jmvivo | public boolean supportsAppendMode() { |
613 | 25752 | vcaballero | return true; |
614 | 24177 | jmvivo | } |
615 | |||
616 | 24710 | jmvivo | |
617 | 25955 | jmvivo | public void append(Feature feature) throws DataException { |
618 | 26252 | jmvivo | this.resourcesBegin();
|
619 | 25752 | vcaballero | try {
|
620 | 25993 | jmvivo | writer.append(feature); |
621 | 25955 | jmvivo | } finally {
|
622 | 26252 | jmvivo | this.resourcesEnd();
|
623 | 25752 | vcaballero | } |
624 | 24177 | jmvivo | } |
625 | |||
626 | 25955 | jmvivo | public void beginAppend() throws DataException { |
627 | this.close();
|
||
628 | 26252 | jmvivo | this.resourcesBegin();
|
629 | 25752 | vcaballero | try {
|
630 | FeatureSet set = this.store.getFeatureSet();
|
||
631 | writer.begin(this.getDBFParameters(), this.store |
||
632 | .getDefaultFeatureType(), set.getSize()); |
||
633 | 25955 | jmvivo | } finally {
|
634 | 26252 | jmvivo | this.resourcesEnd();
|
635 | 25752 | vcaballero | } |
636 | 24177 | jmvivo | } |
637 | |||
638 | 25955 | jmvivo | public void endAppend() throws DataException { |
639 | 26252 | jmvivo | this.resourcesBegin();
|
640 | 25752 | vcaballero | try {
|
641 | writer.end(); |
||
642 | 25955 | jmvivo | |
643 | 26252 | jmvivo | this.resourcesNotifyChanges();
|
644 | 25752 | vcaballero | this.counterNewsOIDs = -1; |
645 | 25955 | jmvivo | } finally {
|
646 | 26252 | jmvivo | this.resourcesEnd();
|
647 | 25752 | vcaballero | } |
648 | 24177 | jmvivo | } |
649 | |||
650 | 24710 | jmvivo | public PersistentState getState() throws PersistenceException { |
651 | 24914 | jmvivo | // Nothing to do
|
652 | 24806 | jmvivo | return null; |
653 | 24177 | jmvivo | } |
654 | |||
655 | 24710 | jmvivo | public void loadState(PersistentState state) throws PersistenceException { |
656 | 24959 | jmvivo | try {
|
657 | this.init((DBFStoreParameters) this.store.getParameters()); |
||
658 | } catch (InitializeException e) {
|
||
659 | throw new PersistenceException(e); |
||
660 | } |
||
661 | 24569 | jmvivo | } |
662 | |||
663 | 24710 | jmvivo | public void setState(PersistentState state) throws PersistenceException { |
664 | 24959 | jmvivo | try {
|
665 | this.init((DBFStoreParameters) this.store.getParameters()); |
||
666 | } catch (InitializeException e) {
|
||
667 | throw new PersistenceException(e); |
||
668 | } |
||
669 | 24569 | jmvivo | } |
670 | |||
671 | 25789 | jmvivo | public Iterator getChilds() { |
672 | return Arrays.asList(new DynObject[] { this.dbfParams }).iterator(); |
||
673 | |||
674 | } |
||
675 | |||
676 | protected static void registerDynClass() { |
||
677 | DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
||
678 | DynClass dynClass; |
||
679 | DynField field; |
||
680 | if (DYNCLASS == null) { |
||
681 | dynClass = dynman.add(DYNCLASS_NAME, |
||
682 | "DBF File Store");
|
||
683 | field = DBFLibrary.addCodePageField(dynClass); |
||
684 | |||
685 | dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME)); |
||
686 | |||
687 | DYNCLASS = dynClass; |
||
688 | } |
||
689 | |||
690 | } |
||
691 | |||
692 | public Object getDynValue(String name) throws DynFieldNotFoundException { |
||
693 | try {
|
||
694 | this.open();
|
||
695 | } catch (OpenException e) {
|
||
696 | // FIXME
|
||
697 | throw new RuntimeException(e); |
||
698 | } |
||
699 | return super.getDynValue(name); |
||
700 | } |
||
701 | |||
702 | |||
703 | /*
|
||
704 | * (non-Javadoc)
|
||
705 | 25977 | jmvivo | *
|
706 | 25789 | jmvivo | * @see
|
707 | * org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.
|
||
708 | * gvsig.fmap.dal.resource.spi.ResourceProvider)
|
||
709 | */
|
||
710 | public void resourceChanged(ResourceProvider resource) { |
||
711 | this.store.notifyChange(DataStoreNotification.RESOURCE_CHANGED,
|
||
712 | resource); |
||
713 | } |
||
714 | 26252 | jmvivo | |
715 | protected void resourcesBegin() throws ResourceBeginException { |
||
716 | this.dbfResource.begin();
|
||
717 | } |
||
718 | |||
719 | protected void resourcesEnd() { |
||
720 | this.dbfResource.end();
|
||
721 | } |
||
722 | |||
723 | /**
|
||
724 | *
|
||
725 | * @throws ResourceNotifyChangesException
|
||
726 | */
|
||
727 | protected void resourcesNotifyChanges() |
||
728 | throws ResourceNotifyChangesException {
|
||
729 | this.dbfResource.notifyChanges();
|
||
730 | } |
||
731 | |||
732 | /**
|
||
733 | * @throws ResourceNotifyCloseException
|
||
734 | *
|
||
735 | */
|
||
736 | protected void resourcesNotifyClose() throws ResourceNotifyCloseException { |
||
737 | this.dbfResource.notifyClose();
|
||
738 | } |
||
739 | |||
740 | /**
|
||
741 | * @throws ResourceNotifyOpenException
|
||
742 | *
|
||
743 | */
|
||
744 | protected void resourcesOpen() throws ResourceNotifyOpenException { |
||
745 | this.dbfResource.notifyOpen();
|
||
746 | } |
||
747 | 26837 | jmvivo | |
748 | public Object getSourceId() { |
||
749 | return this.getDBFParameters().getFile(); |
||
750 | } |
||
751 | 27875 | vcaballero | |
752 | protected void resourceCloseRequest() throws ResourceException { |
||
753 | this.dbfResource.closeRequest();
|
||
754 | } |
||
755 | 23912 | jmvivo | } |