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.dbf / src / main / java / org / gvsig / fmap / dal / store / dbf / DBFFilesystemServerProvider.java @ 40559
History | View | Annotate | Download (6.37 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.dbf; |
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.DataStoreParameters; |
33 |
import org.gvsig.fmap.dal.NewDataStoreParameters; |
34 |
import org.gvsig.fmap.dal.exception.CreateException; |
35 |
import org.gvsig.fmap.dal.exception.DataException; |
36 |
import org.gvsig.fmap.dal.exception.FileNotFoundException; |
37 |
import org.gvsig.fmap.dal.exception.InitializeException; |
38 |
import org.gvsig.fmap.dal.exception.RemoveException; |
39 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
40 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
41 |
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException; |
42 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
43 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
44 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
45 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.AbsolutePathRequiredException; |
46 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider; |
47 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider; |
48 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices; |
49 |
|
50 |
public class DBFFilesystemServerProvider extends AbstractFilesystemServerExplorerProvider |
51 |
implements FilesystemServerExplorerProvider, ResourceConsumer {
|
52 |
|
53 |
protected FilesystemServerExplorerProviderServices serverExplorer;
|
54 |
|
55 |
public String getDataStoreProviderName() { |
56 |
return DBFStoreProvider.NAME;
|
57 |
} |
58 |
|
59 |
public int getMode() { |
60 |
return DataServerExplorer.MODE_FEATURE;
|
61 |
} |
62 |
|
63 |
public boolean accept(File pathname) { |
64 |
return (pathname.getName().toLowerCase().endsWith(".dbf")); |
65 |
} |
66 |
|
67 |
public String getDescription() { |
68 |
return DBFStoreProvider.DESCRIPTION;
|
69 |
} |
70 |
|
71 |
public DataStoreParameters getParameters(File file) throws DataException { |
72 |
DataManager manager = DALLocator.getDataManager(); |
73 |
DBFStoreParameters params = (DBFStoreParameters) manager |
74 |
.createStoreParameters(this.getDataStoreProviderName());
|
75 |
params.setDBFFile(file.getPath()); |
76 |
return params;
|
77 |
} |
78 |
|
79 |
public boolean canCreate() { |
80 |
return true; |
81 |
} |
82 |
|
83 |
public boolean canCreate(NewDataStoreParameters parameters) { |
84 |
DBFNewStoreParameters params = (DBFNewStoreParameters) parameters; |
85 |
if (params.getDBFFile().getParentFile().canWrite()) {
|
86 |
return false; |
87 |
} |
88 |
if (params.getDBFFile().exists()) {
|
89 |
if (!params.getDBFFile().canWrite()) {
|
90 |
return false; |
91 |
} |
92 |
} |
93 |
if (params.getDefaultFeatureType() == null) { |
94 |
return false; |
95 |
} |
96 |
return true; |
97 |
} |
98 |
|
99 |
public void create(NewDataStoreParameters parameters, boolean overwrite) |
100 |
throws CreateException {
|
101 |
|
102 |
final DBFNewStoreParameters params = (DBFNewStoreParameters) parameters;
|
103 |
File file = params.getDBFFile();
|
104 |
|
105 |
if (!file.isAbsolute()) {
|
106 |
throw new AbsolutePathRequiredException(file.getPath()); |
107 |
} |
108 |
|
109 |
|
110 |
if (file.exists()) {
|
111 |
if (overwrite) {
|
112 |
// if (!file.delete()) {
|
113 |
// throw new CreateException(this.getDataStoreProviderName(),
|
114 |
// new IOException(
|
115 |
// "cannot delete file"));
|
116 |
// }
|
117 |
} else {
|
118 |
throw new CreateException(this.getDataStoreProviderName(), |
119 |
new IOException( |
120 |
"file already exist"));
|
121 |
} |
122 |
} |
123 |
|
124 |
final FileResource resource;
|
125 |
try {
|
126 |
|
127 |
resource = (FileResource) this.serverExplorer
|
128 |
.getServerExplorerProviderServices().createResource( |
129 |
FileResource.NAME, |
130 |
new Object[] { file.getAbsolutePath() }); |
131 |
} catch (InitializeException e1) {
|
132 |
throw new CreateException(params.getDBFFileName(), e1); |
133 |
} |
134 |
resource.addConsumer(this);
|
135 |
|
136 |
try {
|
137 |
resource.execute(new ResourceAction() {
|
138 |
public Object run() throws Exception { |
139 |
DBFFeatureWriter writer = |
140 |
new DBFFeatureWriter(getDataStoreProviderName());
|
141 |
|
142 |
writer.begin(params, params.getDefaultFeatureType(), 0);
|
143 |
writer.end(); |
144 |
writer.dispose(); |
145 |
resource.notifyChanges(); |
146 |
return null; |
147 |
} |
148 |
}); |
149 |
} catch (ResourceExecuteException e) {
|
150 |
throw new CreateException(this.getDataStoreProviderName(), e); |
151 |
} finally {
|
152 |
resource.removeConsumer(this);
|
153 |
} |
154 |
|
155 |
|
156 |
} |
157 |
|
158 |
|
159 |
protected NewDataStoreParameters createInstanceNewDataStoreParameters() {
|
160 |
return new DBFNewStoreParameters(); |
161 |
} |
162 |
|
163 |
|
164 |
public NewDataStoreParameters getCreateParameters() {
|
165 |
NewFeatureStoreParameters params = (NewFeatureStoreParameters) this
|
166 |
.createInstanceNewDataStoreParameters(); |
167 |
params.setDefaultFeatureType(this.serverExplorer
|
168 |
.getServerExplorerProviderServices().createNewFeatureType()); |
169 |
return params;
|
170 |
} |
171 |
|
172 |
public void initialize(FilesystemServerExplorerProviderServices serverExplorer) { |
173 |
this.serverExplorer = serverExplorer;
|
174 |
|
175 |
} |
176 |
|
177 |
public void remove(DataStoreParameters parameters) throws RemoveException { |
178 |
DBFStoreParameters params = (DBFStoreParameters) parameters; |
179 |
File file = params.getDBFFile();
|
180 |
if (!file.exists()) {
|
181 |
throw new RemoveException(this.getDataStoreProviderName(), |
182 |
new FileNotFoundException(params.getDBFFile())); |
183 |
} |
184 |
if (!file.delete()) {
|
185 |
throw new RemoveException(this.getDataStoreProviderName(), |
186 |
new IOException()); // FIXME Exception |
187 |
} |
188 |
} |
189 |
|
190 |
public boolean closeResourceRequested(ResourceProvider resource) { |
191 |
// while it is using a resource anyone can't close it
|
192 |
return !(this.equals(resource)); |
193 |
} |
194 |
|
195 |
/*
|
196 |
* (non-Javadoc)
|
197 |
*
|
198 |
* @see
|
199 |
* org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.
|
200 |
* gvsig.fmap.dal.resource.spi.ResourceProvider)
|
201 |
*/
|
202 |
public void resourceChanged(ResourceProvider resource) { |
203 |
// Do nothing
|
204 |
} |
205 |
|
206 |
} |