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