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