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