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