Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.shp / src / main / java / org / gvsig / fmap / dal / store / shp / SHPFilesystemServerProvider.java @ 40596

History | View | Annotate | Download (10.9 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.store.shp;
25

    
26
import java.io.File;
27
import java.io.IOException;
28

    
29
import org.gvsig.fmap.dal.DALLocator;
30
import org.gvsig.fmap.dal.DataManager;
31
import org.gvsig.fmap.dal.DataServerExplorer;
32
import org.gvsig.fmap.dal.DataStore;
33
import org.gvsig.fmap.dal.DataStoreParameters;
34
import org.gvsig.fmap.dal.NewDataStoreParameters;
35
import org.gvsig.fmap.dal.exception.CreateException;
36
import org.gvsig.fmap.dal.exception.DataException;
37
import org.gvsig.fmap.dal.exception.InitializeException;
38
import org.gvsig.fmap.dal.exception.RemoveException;
39
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
40
import org.gvsig.fmap.dal.feature.EditableFeatureType;
41
import org.gvsig.fmap.dal.feature.FeatureType;
42
import org.gvsig.fmap.dal.resource.ResourceAction;
43
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException;
44
import org.gvsig.fmap.dal.resource.file.FileResource;
45
import org.gvsig.fmap.dal.resource.spi.MultiResource;
46
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
47
import org.gvsig.fmap.dal.serverexplorer.filesystem.AbsolutePathRequiredException;
48
import org.gvsig.fmap.dal.store.dbf.DBFFilesystemServerProvider;
49

    
50
/**
51
 * @author jmvivo
52
 * 
53
 */
54
public class SHPFilesystemServerProvider extends DBFFilesystemServerProvider {
55

    
56
        public boolean accept(File pathname) {
57
                return pathname.getName().toLowerCase().endsWith(".shp");
58
        }
59
        
60
        public int getMode() {
61
                return DataServerExplorer.MODE_GEOMETRY|DataServerExplorer.MODE_FEATURE;
62
        }
63
        
64
        public boolean canCreate() {
65
                return true;
66
        }
67

    
68
        // private FileResource[] createResources(File[] files)
69
        // throws InitializeException {
70
        // FileResource[] result = new FileResource[files.length];
71
        // for (int i = 0; i < files.length; i++) {
72
        // result[i] = this.createResource(files[i]);
73
        // }
74
        //
75
        // return result;
76
        // }
77

    
78
        private ResourceProvider createResource(File shpFile, File[] otherFiles)
79
                        throws InitializeException {
80

    
81
                Object[] shpParams = new Object[] { shpFile.getAbsolutePath() };
82

    
83
                MultiResource resource =
84
                                (MultiResource) serverExplorer.getServerExplorerProviderServices()
85
                                                .createResource(MultiResource.TYPE_NAME, shpParams);
86

    
87
                resource.addResource(FileResource.NAME, shpParams, true);
88

    
89
                for (int i = 0; i < otherFiles.length; i++) {
90
                        resource.addResource(FileResource.NAME,
91
                                        new Object[] { otherFiles[i].getAbsolutePath() }, false);
92
                }
93

    
94
                resource.addConsumer(this);
95
                return resource;
96
        }
97

    
98
        // private FileResource createResource(File file) throws InitializeException
99
        // {
100
        // FileResource resource;
101
        // resource =
102
        // (FileResource) this.serverExplorer.getServerExplorerProviderServices()
103
        // .createResource(FileResource.NAME,
104
        // new Object[] { file.getAbsolutePath() });
105
        // resource.addConsumer(this);
106
        // return resource;
107
        // }
108

    
109
        public boolean canCreate(NewDataStoreParameters parameters) {
110
                if (!super.canCreate(parameters)) {
111
                        return false;
112
                }
113
                SHPNewStoreParameters params = (SHPNewStoreParameters) parameters;
114
                if (params.getSHPFile().getParentFile().canWrite()) {
115
                        return false;
116
                }
117
                if (params.getSHPFile().exists()) {
118
                        if (!params.getSHPFile().canWrite()) {
119
                                return false;
120
                        }
121
                }
122
                if (params.getSHXFile().getParentFile().canWrite()) {
123
                        return false;
124
                }
125
                if (params.getSHXFile().exists()) {
126
                        if (!params.getSHXFile().canWrite()) {
127
                                return false;
128
                        }
129
                }
130
                return true;
131
        }
132

    
133
        public boolean closeResourceRequested(ResourceProvider resource) {
134
                // while it is using a resource anyone can't close it
135
                return false;
136
        }
137

    
138
        public void create(NewDataStoreParameters parameters, boolean overwrite)
139
                        throws CreateException {
140

    
141
                final SHPNewStoreParameters params = (SHPNewStoreParameters) parameters;
142
                final FeatureType fType;
143
                // TODO Comprobar que el campo de geometria
144
                final EditableFeatureType dbfFtype;
145
                if (params.getDefaultFeatureType() instanceof EditableFeatureType) {
146
                        fType = params.getDefaultFeatureType();
147
                        dbfFtype = (EditableFeatureType) fType.getCopy();
148
                } else {
149
                        fType = params.getDefaultFeatureType();
150
                        dbfFtype = fType.getEditable();
151
                        params.setDefaultFeatureType(fType.getEditable());
152
                }
153

    
154
                SHPStoreProvider.removeGeometryColumn(dbfFtype);
155
                EditableFeatureAttributeDescriptor efad = SHPStoreProvider.addGeometryColumn((EditableFeatureType) fType);
156
                efad.setGeometryType(params.getGeometryType());
157
                
158
                File dbfFile = params.getDBFFile();
159
                File shpFile = params.getSHPFile();
160
                File shxFile = params.getSHXFile();
161
                
162
                if (!shpFile.isAbsolute()) {
163
                        throw new AbsolutePathRequiredException(shpFile.getPath());
164
                }
165

    
166
                if (!dbfFile.isAbsolute()) {
167
                        throw new AbsolutePathRequiredException(dbfFile.getPath());
168
                }
169
                
170
                if (!shxFile.isAbsolute()) {
171
                        throw new AbsolutePathRequiredException(shxFile.getPath());
172
                }
173

    
174
                // File[] files = new File[] { shpFile, shxFile, dbfFile };
175
                // TODO .prj file
176

    
177
                // FileResource[] resources;
178
                ResourceProvider resource;
179
                try {
180
                        // resources =
181
                        resource = createResource(shpFile, new File[] { shxFile, dbfFile });
182
                        resource.closeRequest();
183
                        // closeResources(resources);
184
                } catch (DataException e1) {
185
                        throw new CreateException(shpFile.getPath(), e1);
186
                }
187

    
188
                if (shpFile.exists()) {
189
                        if (overwrite) {
190
                                // FIXME
191
                                // if (!shpFile.delete()) {
192
                                // throw new CreateException(this.getDataStoreProviderName(),
193
                                // new IOException(
194
                                // "cannot delete file: " + shpFile.getPath()));
195
                                // }
196
                                // if (shxFile.exists()) {
197
                                // if (!shxFile.delete()) {
198
                                // throw new CreateException(this.getDataStoreProviderName(),
199
                                // new IOException("cannot delete file: "
200
                                // + shxFile.getPath()));
201
                                // }
202
                                //
203
                                // }
204
                        } else {
205
                                throw new CreateException(this.getDataStoreProviderName(),
206
                                                new IOException("file already exist"));
207
                        }
208
                }
209

    
210
                try {
211
                        
212
//                        beginResources(resources);
213

    
214
                        resource.execute(new ResourceAction() {
215
                                public Object run() throws Exception {
216
                                        SHPFeatureWriter writer =
217
                                                        new SHPFeatureWriter(getDataStoreProviderName());
218
                                        
219
                                        writer.begin(params, fType, dbfFtype, 0);
220
                                        
221
                                        writer.end();
222
                                        return null;
223
                                }
224
                        });
225

    
226
//                        notifyChangesResources(resources);
227
                        resource.notifyChanges();
228
                } catch (Exception e) {
229
                        throw new CreateException(this.getDataStoreProviderName(), e);
230
                } finally {
231
                        // endResources(resources);
232
                        // removeConsumer(resources);
233
                        resource.removeConsumer(this);
234
                }
235
        }
236

    
237
        // private boolean closeResources(FileResource[] resources)
238
        // throws ResourceException {
239
        // for (int i = 0; i < resources.length; i++) {
240
        // // TODO
241
        // // if (!resources[i].closeRequest()){
242
        // // return false;
243
        // // }
244
        // resources[i].closeRequest();
245
        // }
246
        // return true;
247
        //
248
        // }
249

    
250
        private void removeConsumer(FileResource[] resources) {
251
                for (int i = 0; i < resources.length; i++) {
252
                        resources[i].removeConsumer(this);
253
                }
254
        }
255

    
256
        // private void endResources(FileResource[] resources) {
257
        // for (int i = 0; i < resources.length; i++) {
258
        // resources[i].end();
259
        // }
260
        // }
261

    
262
        private void notifyChangesResources(FileResource[] resources)
263
                        throws ResourceNotifyChangesException {
264
                for (int i = 0; i < resources.length; i++) {
265
                        resources[i].notifyChanges();
266
                }
267

    
268
        }
269

    
270
        // private void beginResources(FileResource[] resources)
271
        // throws ResourceExecuteException {
272
        // for (int i = 0; i < resources.length; i++) {
273
        // resources[i].begin();
274
        // }
275
        //
276
        // }
277

    
278
        public NewDataStoreParameters getCreateParameters() {
279
                SHPNewStoreParameters params =
280
                                (SHPNewStoreParameters) super.getCreateParameters();
281

    
282
                EditableFeatureType fType =
283
                                (EditableFeatureType) params.getDefaultFeatureType();
284
                // SHPStoreProvider.addGeometryColumn(fType);
285
                // params.setDefaultFeatureType(fType);
286
                return params;
287
        }
288

    
289
        protected NewDataStoreParameters createInstanceNewDataStoreParameters() {
290
                return new SHPNewStoreParameters();
291
        }
292

    
293
        public String getDataStoreProviderName() {
294
                return SHPStoreProvider.NAME;
295
        }
296

    
297
        public String getDescription() {
298
                return SHPStoreProvider.DESCRIPTION;
299
        }
300

    
301
        public DataStoreParameters getParameters(File file) throws DataException {
302
                DataManager manager = DALLocator.getDataManager();
303
                SHPStoreParameters params =
304
                                (SHPStoreParameters) manager.createStoreParameters(this.getDataStoreProviderName());
305
                params.setSHPFile(file.getPath());
306
                return params;
307

    
308
        }
309

    
310
        public void remove(DataStoreParameters parameters) throws RemoveException {
311
                SHPStoreParameters params = (SHPStoreParameters) parameters;
312

    
313
                final File dbfFile = params.getDBFFile();
314
                final File shpFile = params.getSHPFile();
315
                final File shxFile = params.getSHXFile();
316
                // TODO .prj file
317

    
318
                // File[] files = new File[] { shpFile, shxFile, dbfFile };
319
                //
320
                // FileResource[] resources;
321

    
322
                File[] otherFiles = new File[] { shxFile, dbfFile };
323
                final ResourceProvider resource;
324
                try {
325
                        // resources = this.createResources(files);
326
                        resource = createResource(shpFile, otherFiles);
327
                        resource.closeRequest();
328
                        resource.closeRequest();
329
                        // closeResources(resources);
330
                } catch (DataException e1) {
331
                        throw new RemoveException(shpFile.getPath(), e1);
332
                }
333

    
334
                try {
335
//                        beginResources(resources);
336
                        resource.execute(new ResourceAction() {
337
                                public Object run() throws Exception {
338
                                        deleteFile(shpFile);
339
                                        deleteFile(dbfFile);
340
                                        deleteFile(shxFile);
341
                                        resource.notifyChanges();
342
                                        return null;
343
                                }
344
                        });
345
                        // for (int i = 0; i < files.length; i++) {
346
                        // if (!files[i].exists()) {
347
                        // continue;
348
                        // }
349
                        // if (!files[i].delete()) {
350
                        // throw new RemoveException(this.getDataStoreProviderName(),
351
                        // new IOException()); // FIXME Exception
352
                        // }
353
                        // }
354

    
355
                        // notifyChangesResources(resources);
356
                } catch (Exception e) {
357
                        throw new RemoveException(this.getDataStoreProviderName(), e);
358
                } finally {
359
//                        endResources(resources);
360
//                        removeConsumer(resources);
361
                        resource.removeConsumer(this);
362
                }
363
        }
364

    
365
        private void deleteFile(File file) throws RemoveException {
366
                if (file.exists() && !file.delete()) {
367
                        throw new RemoveException(this.getDataStoreProviderName(),
368
                                        new IOException()); // FIXME Exception
369
                }
370
        }
371
        
372
        public String getResourceRootPathName(DataStore dataStore) {
373
                SHPStoreParameters shpParams = (SHPStoreParameters) dataStore.getParameters();
374
                return removeFileExtension(shpParams.getSHPFile());                        
375
        }
376

    
377
}