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