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 @ 40435
History | View | Annotate | Download (10.9 KB)
1 | 40435 | jjdelcerro | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
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 2
|
||
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 | */
|
||
22 | |||
23 | /*
|
||
24 | * AUTHORS (In addition to CIT):
|
||
25 | * 2008 IVER T.I. S.A. {{Task}}
|
||
26 | */
|
||
27 | |||
28 | /**
|
||
29 | *
|
||
30 | */
|
||
31 | package org.gvsig.fmap.dal.store.shp; |
||
32 | |||
33 | import java.io.File; |
||
34 | import java.io.IOException; |
||
35 | |||
36 | import org.gvsig.fmap.dal.DALLocator; |
||
37 | import org.gvsig.fmap.dal.DataManager; |
||
38 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
39 | import org.gvsig.fmap.dal.DataStore; |
||
40 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
41 | import org.gvsig.fmap.dal.NewDataStoreParameters; |
||
42 | import org.gvsig.fmap.dal.exception.CreateException; |
||
43 | import org.gvsig.fmap.dal.exception.DataException; |
||
44 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
45 | import org.gvsig.fmap.dal.exception.RemoveException; |
||
46 | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
||
47 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
48 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
49 | import org.gvsig.fmap.dal.resource.ResourceAction; |
||
50 | import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException; |
||
51 | import org.gvsig.fmap.dal.resource.file.FileResource; |
||
52 | import org.gvsig.fmap.dal.resource.spi.MultiResource; |
||
53 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
54 | import org.gvsig.fmap.dal.serverexplorer.filesystem.AbsolutePathRequiredException; |
||
55 | import org.gvsig.fmap.dal.store.dbf.DBFFilesystemServerProvider; |
||
56 | |||
57 | /**
|
||
58 | * @author jmvivo
|
||
59 | *
|
||
60 | */
|
||
61 | public class SHPFilesystemServerProvider extends DBFFilesystemServerProvider { |
||
62 | |||
63 | public boolean accept(File pathname) { |
||
64 | return pathname.getName().toLowerCase().endsWith(".shp"); |
||
65 | } |
||
66 | |||
67 | public int getMode() { |
||
68 | return DataServerExplorer.MODE_GEOMETRY|DataServerExplorer.MODE_FEATURE;
|
||
69 | } |
||
70 | |||
71 | public boolean canCreate() { |
||
72 | return true; |
||
73 | } |
||
74 | |||
75 | // private FileResource[] createResources(File[] files)
|
||
76 | // throws InitializeException {
|
||
77 | // FileResource[] result = new FileResource[files.length];
|
||
78 | // for (int i = 0; i < files.length; i++) {
|
||
79 | // result[i] = this.createResource(files[i]);
|
||
80 | // }
|
||
81 | //
|
||
82 | // return result;
|
||
83 | // }
|
||
84 | |||
85 | private ResourceProvider createResource(File shpFile, File[] otherFiles) |
||
86 | throws InitializeException {
|
||
87 | |||
88 | Object[] shpParams = new Object[] { shpFile.getAbsolutePath() }; |
||
89 | |||
90 | MultiResource resource = |
||
91 | (MultiResource) serverExplorer.getServerExplorerProviderServices() |
||
92 | .createResource(MultiResource.TYPE_NAME, shpParams); |
||
93 | |||
94 | resource.addResource(FileResource.NAME, shpParams, true);
|
||
95 | |||
96 | for (int i = 0; i < otherFiles.length; i++) { |
||
97 | resource.addResource(FileResource.NAME, |
||
98 | new Object[] { otherFiles[i].getAbsolutePath() }, false); |
||
99 | } |
||
100 | |||
101 | resource.addConsumer(this);
|
||
102 | return resource;
|
||
103 | } |
||
104 | |||
105 | // private FileResource createResource(File file) throws InitializeException
|
||
106 | // {
|
||
107 | // FileResource resource;
|
||
108 | // resource =
|
||
109 | // (FileResource) this.serverExplorer.getServerExplorerProviderServices()
|
||
110 | // .createResource(FileResource.NAME,
|
||
111 | // new Object[] { file.getAbsolutePath() });
|
||
112 | // resource.addConsumer(this);
|
||
113 | // return resource;
|
||
114 | // }
|
||
115 | |||
116 | public boolean canCreate(NewDataStoreParameters parameters) { |
||
117 | if (!super.canCreate(parameters)) { |
||
118 | return false; |
||
119 | } |
||
120 | SHPNewStoreParameters params = (SHPNewStoreParameters) parameters; |
||
121 | if (params.getSHPFile().getParentFile().canWrite()) {
|
||
122 | return false; |
||
123 | } |
||
124 | if (params.getSHPFile().exists()) {
|
||
125 | if (!params.getSHPFile().canWrite()) {
|
||
126 | return false; |
||
127 | } |
||
128 | } |
||
129 | if (params.getSHXFile().getParentFile().canWrite()) {
|
||
130 | return false; |
||
131 | } |
||
132 | if (params.getSHXFile().exists()) {
|
||
133 | if (!params.getSHXFile().canWrite()) {
|
||
134 | return false; |
||
135 | } |
||
136 | } |
||
137 | return true; |
||
138 | } |
||
139 | |||
140 | public boolean closeResourceRequested(ResourceProvider resource) { |
||
141 | // while it is using a resource anyone can't close it
|
||
142 | return false; |
||
143 | } |
||
144 | |||
145 | public void create(NewDataStoreParameters parameters, boolean overwrite) |
||
146 | throws CreateException {
|
||
147 | |||
148 | final SHPNewStoreParameters params = (SHPNewStoreParameters) parameters;
|
||
149 | final FeatureType fType;
|
||
150 | // TODO Comprobar que el campo de geometria
|
||
151 | final EditableFeatureType dbfFtype;
|
||
152 | if (params.getDefaultFeatureType() instanceof EditableFeatureType) { |
||
153 | fType = params.getDefaultFeatureType(); |
||
154 | dbfFtype = (EditableFeatureType) fType.getCopy(); |
||
155 | } else {
|
||
156 | fType = params.getDefaultFeatureType(); |
||
157 | dbfFtype = fType.getEditable(); |
||
158 | params.setDefaultFeatureType(fType.getEditable()); |
||
159 | } |
||
160 | |||
161 | SHPStoreProvider.removeGeometryColumn(dbfFtype); |
||
162 | EditableFeatureAttributeDescriptor efad = SHPStoreProvider.addGeometryColumn((EditableFeatureType) fType); |
||
163 | efad.setGeometryType(params.getGeometryType()); |
||
164 | |||
165 | File dbfFile = params.getDBFFile();
|
||
166 | File shpFile = params.getSHPFile();
|
||
167 | File shxFile = params.getSHXFile();
|
||
168 | |||
169 | if (!shpFile.isAbsolute()) {
|
||
170 | throw new AbsolutePathRequiredException(shpFile.getPath()); |
||
171 | } |
||
172 | |||
173 | if (!dbfFile.isAbsolute()) {
|
||
174 | throw new AbsolutePathRequiredException(dbfFile.getPath()); |
||
175 | } |
||
176 | |||
177 | if (!shxFile.isAbsolute()) {
|
||
178 | throw new AbsolutePathRequiredException(shxFile.getPath()); |
||
179 | } |
||
180 | |||
181 | // File[] files = new File[] { shpFile, shxFile, dbfFile };
|
||
182 | // TODO .prj file
|
||
183 | |||
184 | // FileResource[] resources;
|
||
185 | ResourceProvider resource; |
||
186 | try {
|
||
187 | // resources =
|
||
188 | resource = createResource(shpFile, new File[] { shxFile, dbfFile }); |
||
189 | resource.closeRequest(); |
||
190 | // closeResources(resources);
|
||
191 | } catch (DataException e1) {
|
||
192 | throw new CreateException(shpFile.getPath(), e1); |
||
193 | } |
||
194 | |||
195 | if (shpFile.exists()) {
|
||
196 | if (overwrite) {
|
||
197 | // FIXME
|
||
198 | // if (!shpFile.delete()) {
|
||
199 | // throw new CreateException(this.getDataStoreProviderName(),
|
||
200 | // new IOException(
|
||
201 | // "cannot delete file: " + shpFile.getPath()));
|
||
202 | // }
|
||
203 | // if (shxFile.exists()) {
|
||
204 | // if (!shxFile.delete()) {
|
||
205 | // throw new CreateException(this.getDataStoreProviderName(),
|
||
206 | // new IOException("cannot delete file: "
|
||
207 | // + shxFile.getPath()));
|
||
208 | // }
|
||
209 | //
|
||
210 | // }
|
||
211 | } else {
|
||
212 | throw new CreateException(this.getDataStoreProviderName(), |
||
213 | new IOException("file already exist")); |
||
214 | } |
||
215 | } |
||
216 | |||
217 | try {
|
||
218 | |||
219 | // beginResources(resources);
|
||
220 | |||
221 | resource.execute(new ResourceAction() {
|
||
222 | public Object run() throws Exception { |
||
223 | SHPFeatureWriter writer = |
||
224 | new SHPFeatureWriter(getDataStoreProviderName());
|
||
225 | |||
226 | writer.begin(params, fType, dbfFtype, 0);
|
||
227 | |||
228 | writer.end(); |
||
229 | return null; |
||
230 | } |
||
231 | }); |
||
232 | |||
233 | // notifyChangesResources(resources);
|
||
234 | resource.notifyChanges(); |
||
235 | } catch (Exception e) { |
||
236 | throw new CreateException(this.getDataStoreProviderName(), e); |
||
237 | } finally {
|
||
238 | // endResources(resources);
|
||
239 | // removeConsumer(resources);
|
||
240 | resource.removeConsumer(this);
|
||
241 | } |
||
242 | } |
||
243 | |||
244 | // private boolean closeResources(FileResource[] resources)
|
||
245 | // throws ResourceException {
|
||
246 | // for (int i = 0; i < resources.length; i++) {
|
||
247 | // // TODO
|
||
248 | // // if (!resources[i].closeRequest()){
|
||
249 | // // return false;
|
||
250 | // // }
|
||
251 | // resources[i].closeRequest();
|
||
252 | // }
|
||
253 | // return true;
|
||
254 | //
|
||
255 | // }
|
||
256 | |||
257 | private void removeConsumer(FileResource[] resources) { |
||
258 | for (int i = 0; i < resources.length; i++) { |
||
259 | resources[i].removeConsumer(this);
|
||
260 | } |
||
261 | } |
||
262 | |||
263 | // private void endResources(FileResource[] resources) {
|
||
264 | // for (int i = 0; i < resources.length; i++) {
|
||
265 | // resources[i].end();
|
||
266 | // }
|
||
267 | // }
|
||
268 | |||
269 | private void notifyChangesResources(FileResource[] resources) |
||
270 | throws ResourceNotifyChangesException {
|
||
271 | for (int i = 0; i < resources.length; i++) { |
||
272 | resources[i].notifyChanges(); |
||
273 | } |
||
274 | |||
275 | } |
||
276 | |||
277 | // private void beginResources(FileResource[] resources)
|
||
278 | // throws ResourceExecuteException {
|
||
279 | // for (int i = 0; i < resources.length; i++) {
|
||
280 | // resources[i].begin();
|
||
281 | // }
|
||
282 | //
|
||
283 | // }
|
||
284 | |||
285 | public NewDataStoreParameters getCreateParameters() {
|
||
286 | SHPNewStoreParameters params = |
||
287 | (SHPNewStoreParameters) super.getCreateParameters();
|
||
288 | |||
289 | EditableFeatureType fType = |
||
290 | (EditableFeatureType) params.getDefaultFeatureType(); |
||
291 | // SHPStoreProvider.addGeometryColumn(fType);
|
||
292 | // params.setDefaultFeatureType(fType);
|
||
293 | return params;
|
||
294 | } |
||
295 | |||
296 | protected NewDataStoreParameters createInstanceNewDataStoreParameters() {
|
||
297 | return new SHPNewStoreParameters(); |
||
298 | } |
||
299 | |||
300 | public String getDataStoreProviderName() { |
||
301 | return SHPStoreProvider.NAME;
|
||
302 | } |
||
303 | |||
304 | public String getDescription() { |
||
305 | return SHPStoreProvider.DESCRIPTION;
|
||
306 | } |
||
307 | |||
308 | public DataStoreParameters getParameters(File file) throws DataException { |
||
309 | DataManager manager = DALLocator.getDataManager(); |
||
310 | SHPStoreParameters params = |
||
311 | (SHPStoreParameters) manager.createStoreParameters(this.getDataStoreProviderName());
|
||
312 | params.setSHPFile(file.getPath()); |
||
313 | return params;
|
||
314 | |||
315 | } |
||
316 | |||
317 | public void remove(DataStoreParameters parameters) throws RemoveException { |
||
318 | SHPStoreParameters params = (SHPStoreParameters) parameters; |
||
319 | |||
320 | final File dbfFile = params.getDBFFile(); |
||
321 | final File shpFile = params.getSHPFile(); |
||
322 | final File shxFile = params.getSHXFile(); |
||
323 | // TODO .prj file
|
||
324 | |||
325 | // File[] files = new File[] { shpFile, shxFile, dbfFile };
|
||
326 | //
|
||
327 | // FileResource[] resources;
|
||
328 | |||
329 | File[] otherFiles = new File[] { shxFile, dbfFile }; |
||
330 | final ResourceProvider resource;
|
||
331 | try {
|
||
332 | // resources = this.createResources(files);
|
||
333 | resource = createResource(shpFile, otherFiles); |
||
334 | resource.closeRequest(); |
||
335 | resource.closeRequest(); |
||
336 | // closeResources(resources);
|
||
337 | } catch (DataException e1) {
|
||
338 | throw new RemoveException(shpFile.getPath(), e1); |
||
339 | } |
||
340 | |||
341 | try {
|
||
342 | // beginResources(resources);
|
||
343 | resource.execute(new ResourceAction() {
|
||
344 | public Object run() throws Exception { |
||
345 | deleteFile(shpFile); |
||
346 | deleteFile(dbfFile); |
||
347 | deleteFile(shxFile); |
||
348 | resource.notifyChanges(); |
||
349 | return null; |
||
350 | } |
||
351 | }); |
||
352 | // for (int i = 0; i < files.length; i++) {
|
||
353 | // if (!files[i].exists()) {
|
||
354 | // continue;
|
||
355 | // }
|
||
356 | // if (!files[i].delete()) {
|
||
357 | // throw new RemoveException(this.getDataStoreProviderName(),
|
||
358 | // new IOException()); // FIXME Exception
|
||
359 | // }
|
||
360 | // }
|
||
361 | |||
362 | // notifyChangesResources(resources);
|
||
363 | } catch (Exception e) { |
||
364 | throw new RemoveException(this.getDataStoreProviderName(), e); |
||
365 | } finally {
|
||
366 | // endResources(resources);
|
||
367 | // removeConsumer(resources);
|
||
368 | resource.removeConsumer(this);
|
||
369 | } |
||
370 | } |
||
371 | |||
372 | private void deleteFile(File file) throws RemoveException { |
||
373 | if (file.exists() && !file.delete()) {
|
||
374 | throw new RemoveException(this.getDataStoreProviderName(), |
||
375 | new IOException()); // FIXME Exception |
||
376 | } |
||
377 | } |
||
378 | |||
379 | public String getResourceRootPathName(DataStore dataStore) { |
||
380 | SHPStoreParameters shpParams = (SHPStoreParameters) dataStore.getParameters(); |
||
381 | return removeFileExtension(shpParams.getSHPFile());
|
||
382 | } |
||
383 | |||
384 | } |