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 @ 44495
History | View | Annotate | Download (8.29 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | 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 | 41637 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
40 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
41 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
42 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
43 | import org.gvsig.fmap.dal.resource.ResourceAction; |
||
44 | import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException; |
||
45 | import org.gvsig.fmap.dal.resource.file.FileResource; |
||
46 | import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
||
47 | import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
||
48 | import org.gvsig.fmap.dal.serverexplorer.filesystem.AbsolutePathRequiredException; |
||
49 | import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider; |
||
50 | import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider; |
||
51 | import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices; |
||
52 | 41637 | jjdelcerro | import org.gvsig.tools.dataTypes.DataTypes; |
53 | 40435 | jjdelcerro | |
54 | public class DBFFilesystemServerProvider extends AbstractFilesystemServerExplorerProvider |
||
55 | implements FilesystemServerExplorerProvider, ResourceConsumer {
|
||
56 | |||
57 | protected FilesystemServerExplorerProviderServices serverExplorer;
|
||
58 | |||
59 | public String getDataStoreProviderName() { |
||
60 | return DBFStoreProvider.NAME;
|
||
61 | } |
||
62 | |||
63 | public int getMode() { |
||
64 | return DataServerExplorer.MODE_FEATURE;
|
||
65 | } |
||
66 | |||
67 | public boolean accept(File pathname) { |
||
68 | return (pathname.getName().toLowerCase().endsWith(".dbf")); |
||
69 | } |
||
70 | |||
71 | public String getDescription() { |
||
72 | return DBFStoreProvider.DESCRIPTION;
|
||
73 | } |
||
74 | |||
75 | public DataStoreParameters getParameters(File file) throws DataException { |
||
76 | DataManager manager = DALLocator.getDataManager(); |
||
77 | DBFStoreParameters params = (DBFStoreParameters) manager |
||
78 | .createStoreParameters(this.getDataStoreProviderName());
|
||
79 | params.setDBFFile(file.getPath()); |
||
80 | return params;
|
||
81 | } |
||
82 | |||
83 | public boolean canCreate() { |
||
84 | return true; |
||
85 | } |
||
86 | |||
87 | public boolean canCreate(NewDataStoreParameters parameters) { |
||
88 | DBFNewStoreParameters params = (DBFNewStoreParameters) parameters; |
||
89 | if (params.getDBFFile().getParentFile().canWrite()) {
|
||
90 | return false; |
||
91 | } |
||
92 | if (params.getDBFFile().exists()) {
|
||
93 | if (!params.getDBFFile().canWrite()) {
|
||
94 | return false; |
||
95 | } |
||
96 | } |
||
97 | if (params.getDefaultFeatureType() == null) { |
||
98 | return false; |
||
99 | } |
||
100 | return true; |
||
101 | } |
||
102 | |||
103 | public void create(NewDataStoreParameters parameters, boolean overwrite) |
||
104 | throws CreateException {
|
||
105 | |||
106 | final DBFNewStoreParameters params = (DBFNewStoreParameters) parameters;
|
||
107 | File file = params.getDBFFile();
|
||
108 | |||
109 | if (!file.isAbsolute()) {
|
||
110 | throw new AbsolutePathRequiredException(file.getPath()); |
||
111 | } |
||
112 | |||
113 | |||
114 | if (file.exists()) {
|
||
115 | if (overwrite) {
|
||
116 | // if (!file.delete()) {
|
||
117 | // throw new CreateException(this.getDataStoreProviderName(),
|
||
118 | // new IOException(
|
||
119 | // "cannot delete file"));
|
||
120 | // }
|
||
121 | } else {
|
||
122 | throw new CreateException(this.getDataStoreProviderName(), |
||
123 | new IOException( |
||
124 | "file already exist"));
|
||
125 | } |
||
126 | } |
||
127 | |||
128 | final FileResource resource;
|
||
129 | try {
|
||
130 | |||
131 | resource = (FileResource) this.serverExplorer
|
||
132 | .getServerExplorerProviderServices().createResource( |
||
133 | FileResource.NAME, |
||
134 | new Object[] { file.getAbsolutePath() }); |
||
135 | } catch (InitializeException e1) {
|
||
136 | throw new CreateException(params.getDBFFileName(), e1); |
||
137 | } |
||
138 | resource.addConsumer(this);
|
||
139 | |||
140 | try {
|
||
141 | resource.execute(new ResourceAction() {
|
||
142 | public Object run() throws Exception { |
||
143 | DBFFeatureWriter writer = |
||
144 | new DBFFeatureWriter(getDataStoreProviderName());
|
||
145 | |||
146 | 41637 | jjdelcerro | writer.begin( |
147 | params, |
||
148 | fixFeatureType(params.getDefaultFeatureType()), |
||
149 | 0
|
||
150 | ); |
||
151 | 40435 | jjdelcerro | writer.end(); |
152 | writer.dispose(); |
||
153 | resource.notifyChanges(); |
||
154 | return null; |
||
155 | } |
||
156 | }); |
||
157 | } catch (ResourceExecuteException e) {
|
||
158 | throw new CreateException(this.getDataStoreProviderName(), e); |
||
159 | } finally {
|
||
160 | resource.removeConsumer(this);
|
||
161 | } |
||
162 | |||
163 | |||
164 | } |
||
165 | |||
166 | |||
167 | protected NewDataStoreParameters createInstanceNewDataStoreParameters() {
|
||
168 | return new DBFNewStoreParameters(); |
||
169 | } |
||
170 | |||
171 | |||
172 | public NewDataStoreParameters getCreateParameters() {
|
||
173 | NewFeatureStoreParameters params = (NewFeatureStoreParameters) this
|
||
174 | .createInstanceNewDataStoreParameters(); |
||
175 | 44495 | omartinez | EditableFeatureType featureType = this.serverExplorer
|
176 | .getServerExplorerProviderServices().createNewFeatureType(); |
||
177 | featureType.setHasOID(true);
|
||
178 | params.setDefaultFeatureType(featureType); |
||
179 | 40435 | jjdelcerro | return params;
|
180 | } |
||
181 | |||
182 | public void initialize(FilesystemServerExplorerProviderServices serverExplorer) { |
||
183 | this.serverExplorer = serverExplorer;
|
||
184 | |||
185 | } |
||
186 | |||
187 | public void remove(DataStoreParameters parameters) throws RemoveException { |
||
188 | DBFStoreParameters params = (DBFStoreParameters) parameters; |
||
189 | File file = params.getDBFFile();
|
||
190 | if (!file.exists()) {
|
||
191 | throw new RemoveException(this.getDataStoreProviderName(), |
||
192 | new FileNotFoundException(params.getDBFFile())); |
||
193 | } |
||
194 | if (!file.delete()) {
|
||
195 | throw new RemoveException(this.getDataStoreProviderName(), |
||
196 | new IOException()); // FIXME Exception |
||
197 | } |
||
198 | } |
||
199 | |||
200 | public boolean closeResourceRequested(ResourceProvider resource) { |
||
201 | // while it is using a resource anyone can't close it
|
||
202 | return !(this.equals(resource)); |
||
203 | } |
||
204 | |||
205 | /*
|
||
206 | * (non-Javadoc)
|
||
207 | *
|
||
208 | * @see
|
||
209 | * org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.
|
||
210 | * gvsig.fmap.dal.resource.spi.ResourceProvider)
|
||
211 | */
|
||
212 | public void resourceChanged(ResourceProvider resource) { |
||
213 | // Do nothing
|
||
214 | } |
||
215 | |||
216 | 41637 | jjdelcerro | protected EditableFeatureType fixFeatureType(FeatureType featureType) {
|
217 | EditableFeatureType newFeatureType; |
||
218 | |||
219 | if( featureType instanceof EditableFeatureType ) { |
||
220 | newFeatureType = (EditableFeatureType) featureType.getCopy(); |
||
221 | } else {
|
||
222 | newFeatureType = featureType.getEditable(); |
||
223 | } |
||
224 | |||
225 | for( int i=0; i<newFeatureType.size(); i++ ) { |
||
226 | EditableFeatureAttributeDescriptor attr = (EditableFeatureAttributeDescriptor) newFeatureType.getAttributeDescriptor(i); |
||
227 | String s;
|
||
228 | switch(attr.getType()) {
|
||
229 | case DataTypes.INT:
|
||
230 | s = String.valueOf(Integer.MAX_VALUE); |
||
231 | attr.setSize(18);
|
||
232 | attr.setPrecision(0);
|
||
233 | break;
|
||
234 | case DataTypes.LONG:
|
||
235 | s = String.valueOf(Long.MAX_VALUE); |
||
236 | attr.setSize(18);
|
||
237 | attr.setPrecision(0);
|
||
238 | break;
|
||
239 | case DataTypes.FLOAT:
|
||
240 | attr.setSize(18);
|
||
241 | attr.setPrecision(6);
|
||
242 | break;
|
||
243 | case DataTypes.DOUBLE:
|
||
244 | attr.setSize(18);
|
||
245 | attr.setPrecision(6);
|
||
246 | break;
|
||
247 | } |
||
248 | } |
||
249 | return newFeatureType;
|
||
250 | } |
||
251 | |||
252 | 40435 | jjdelcerro | } |