svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / dbf / DBFStore.java @ 23534
History | View | Annotate | Download (9.84 KB)
1 |
package org.gvsig.fmap.data.feature.file.dbf; |
---|---|
2 |
|
3 |
import java.lang.ref.WeakReference; |
4 |
import java.security.KeyException; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Collection; |
7 |
import java.util.Iterator; |
8 |
|
9 |
import org.gvsig.fmap.data.CloseException; |
10 |
import org.gvsig.fmap.data.DataCollection; |
11 |
import org.gvsig.fmap.data.DataException; |
12 |
import org.gvsig.fmap.data.DataExplorer; |
13 |
import org.gvsig.fmap.data.DataExplorerParameters; |
14 |
import org.gvsig.fmap.data.DataManager; |
15 |
import org.gvsig.fmap.data.DataStoreParameters; |
16 |
import org.gvsig.fmap.data.InitializeException; |
17 |
import org.gvsig.fmap.data.OpenException; |
18 |
import org.gvsig.fmap.data.ReadException; |
19 |
import org.gvsig.fmap.data.ResourceManager; |
20 |
import org.gvsig.fmap.data.WriteException; |
21 |
import org.gvsig.fmap.data.feature.AttributeDescriptor; |
22 |
import org.gvsig.fmap.data.feature.DefaultFeatureType; |
23 |
import org.gvsig.fmap.data.feature.Feature; |
24 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
25 |
import org.gvsig.fmap.data.feature.FeatureCollection; |
26 |
import org.gvsig.fmap.data.feature.FeatureID; |
27 |
import org.gvsig.fmap.data.feature.FeatureType; |
28 |
import org.gvsig.fmap.data.feature.FeaturesWriter; |
29 |
import org.gvsig.fmap.data.feature.InitializeWriterException; |
30 |
import org.gvsig.fmap.data.feature.IsNotAttributeSettingException; |
31 |
import org.gvsig.fmap.data.feature.file.FileExplorerParameters; |
32 |
import org.gvsig.fmap.data.feature.file.FileStore; |
33 |
import org.gvsig.metadata.IMetadata; |
34 |
import org.gvsig.metadata.IMetadataManager; |
35 |
import org.gvsig.metadata.MetadataManager; |
36 |
import org.gvsig.tools.exception.BaseException; |
37 |
|
38 |
public class DBFStore extends FileStore { |
39 |
public static String DATASTORE_NAME = "DBFStore"; |
40 |
private DBFResource dbf= null; |
41 |
protected IMetadata metadata;
|
42 |
private DBFStoreParameters dbfParameters=null; |
43 |
|
44 |
|
45 |
public void init(DataStoreParameters parameters) throws InitializeException { |
46 |
dbfParameters=(DBFStoreParameters)parameters; |
47 |
DBFResource tmpResource = new DBFResource(dbfParameters);
|
48 |
|
49 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
50 |
|
51 |
try {
|
52 |
this.dbf = (DBFResource) resMan.addResource(tmpResource, this); |
53 |
} catch (DataException e1) {
|
54 |
throw new InitializeException(this.getName(),e1); |
55 |
} |
56 |
|
57 |
super.init(parameters,this.dbf); |
58 |
|
59 |
// this.dbf.open();
|
60 |
// this.dbf.addObserver(this);
|
61 |
this.initFeatureType();
|
62 |
} |
63 |
|
64 |
private void initFeatureType() throws InitializeException{ |
65 |
int fieldCount = -1; |
66 |
try {
|
67 |
fieldCount = dbf.getFieldCount(); |
68 |
} catch (DataException e) {
|
69 |
throw new InitializeException(this.getName(), e); |
70 |
} |
71 |
|
72 |
defaultFeatureType = new DefaultFeatureType();
|
73 |
for (int i = 0; i < fieldCount; i++) { |
74 |
FeatureAttributeDescriptor fad; |
75 |
try {
|
76 |
fad = createFeatureAttribute( |
77 |
(DefaultFeatureType) defaultFeatureType, i); |
78 |
} catch (IsNotAttributeSettingException e) {
|
79 |
throw new InitializeException(this.getName(), e); |
80 |
} catch (ReadException e) {
|
81 |
// TODO Auto-generated catch block
|
82 |
throw new InitializeException(this.getName(), e); |
83 |
} |
84 |
defaultFeatureType.add(fad); |
85 |
} |
86 |
|
87 |
this.featureTypes = new ArrayList(); |
88 |
this.featureTypes.add(defaultFeatureType);
|
89 |
|
90 |
} |
91 |
|
92 |
|
93 |
private FeatureAttributeDescriptor createFeatureAttribute(DefaultFeatureType fType, int i) throws ReadException, IsNotAttributeSettingException { |
94 |
char fieldType = dbf.getFieldType(i);
|
95 |
AttributeDescriptor dad=(AttributeDescriptor) fType.createAttributeDescriptor(); |
96 |
dad.loading(); |
97 |
dad.setName(dbf.getFieldName(i)); |
98 |
dad.setSize(dbf.getFieldLength(i)); |
99 |
if (fieldType == 'L') { |
100 |
dad.setType(FeatureAttributeDescriptor.TYPE_BOOLEAN); |
101 |
|
102 |
} else if ((fieldType == 'F') || (fieldType == 'N')) { |
103 |
int precision = dbf.getFieldDecimalLength(i);
|
104 |
if (precision > 0){ |
105 |
dad.setType(FeatureAttributeDescriptor.TYPE_DOUBLE); |
106 |
dad.setPrecision(precision); |
107 |
} else{
|
108 |
dad.setType(FeatureAttributeDescriptor.TYPE_INT); |
109 |
} |
110 |
} else if (fieldType == 'C') { |
111 |
dad.setType(FeatureAttributeDescriptor.TYPE_STRING); |
112 |
} else if (fieldType == 'D') { |
113 |
dad.setType(FeatureAttributeDescriptor.TYPE_DATE); |
114 |
} else {
|
115 |
// throw new BadFieldDriverException(getName(),null,String.valueOf(fieldType));
|
116 |
} |
117 |
dad.stopLoading(); |
118 |
return dad;
|
119 |
|
120 |
} |
121 |
|
122 |
protected void doFinishEdition() throws WriteException, ReadException { |
123 |
FeaturesWriter writer = getFeaturesWriter(); |
124 |
writer.init(this);
|
125 |
writer.updateFeatureType(this.getDefaultFeatureType());
|
126 |
this.dbf.editing();
|
127 |
writer.preProcess(); |
128 |
Collection collection=getDataCollection();
|
129 |
Iterator iterator=collection.iterator();
|
130 |
Feature feature; |
131 |
while (iterator.hasNext()) {
|
132 |
feature= (Feature) iterator.next(); |
133 |
writer.insertFeature(feature); |
134 |
} |
135 |
writer.postProcess(); |
136 |
this.dbf.stopEditing();
|
137 |
|
138 |
this.dbf.changed(this); |
139 |
} |
140 |
|
141 |
public DataCollection getDataCollection(FeatureType type, String filter, String order) throws ReadException { |
142 |
try {
|
143 |
type = this.checkFeatureTypeForCollection(type);
|
144 |
} catch (DataException e) {
|
145 |
throw new ReadException(this.getName(), e); |
146 |
} |
147 |
|
148 |
FeatureCollection coll; |
149 |
if (order == null){ |
150 |
if (featureManager==null){ |
151 |
coll=new DBFFeatureCollectionBitSet(this,type,filter); |
152 |
}else{
|
153 |
coll=new DBFFeatureCollection(featureManager,this,type,filter); |
154 |
} |
155 |
}else{
|
156 |
coll=new DBFFeatureCollectionWithFeatureID(featureManager,this,type,filter,order); |
157 |
} |
158 |
|
159 |
this.addObserver(new WeakReference(coll)); |
160 |
return coll;
|
161 |
|
162 |
} |
163 |
|
164 |
public Feature getFeatureByID(FeatureID id, FeatureType featureType)
|
165 |
throws ReadException {
|
166 |
if (featureType == null){ |
167 |
featureType=this.getDefaultFeatureType();
|
168 |
} else{
|
169 |
if (!featureType.isSubtypeOf(this.getDefaultFeatureType())){ |
170 |
throw new ReadException("invalid type",this.getName()); |
171 |
} |
172 |
} |
173 |
if (this.alterMode){ |
174 |
if (featureManager.contains(id)) {
|
175 |
return featureManager.getFeature(id,this,featureType); |
176 |
} |
177 |
} |
178 |
return this.getFeatureByIndex(((DBFFeatureID) id).getIndex(), |
179 |
featureType); |
180 |
} |
181 |
|
182 |
/**
|
183 |
*
|
184 |
* Only for persistenced features
|
185 |
*
|
186 |
* @param index
|
187 |
* @param featureType
|
188 |
* @return
|
189 |
* @throws ReadException
|
190 |
*/
|
191 |
protected Feature getFeatureByIndex(long index, FeatureType featureType) |
192 |
throws ReadException {
|
193 |
if (index >= this.getFeatureCount()) { |
194 |
throw new ReadException(this.getName(), |
195 |
"Unsupported operation for not persistented features");
|
196 |
} |
197 |
DBFFeature feature = new DBFFeature(featureType, this, index); |
198 |
return feature;
|
199 |
} |
200 |
|
201 |
/***
|
202 |
* NOT supported in Alter Mode
|
203 |
*
|
204 |
* @param index
|
205 |
* @return
|
206 |
* @throws ReadException
|
207 |
*/
|
208 |
protected Feature getFeatureByIndex(long index) throws ReadException { |
209 |
return this.getFeatureByIndex(index, this.getDefaultFeatureType()); |
210 |
} |
211 |
|
212 |
public boolean canAlterFeatureType() { |
213 |
return true; |
214 |
} |
215 |
|
216 |
public String getName() { |
217 |
return DATASTORE_NAME;
|
218 |
} |
219 |
|
220 |
protected void doOpen() throws OpenException { |
221 |
//No hace nada
|
222 |
|
223 |
} |
224 |
|
225 |
protected void doClose() throws CloseException { |
226 |
try {
|
227 |
dbf.close(); |
228 |
} catch (DataException e) {
|
229 |
throw new CloseException(this.getName(),e); |
230 |
} |
231 |
|
232 |
} |
233 |
|
234 |
protected void doDispose() throws CloseException { |
235 |
super.doDispose();
|
236 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
237 |
|
238 |
try {
|
239 |
resMan.remove(this.dbf, this); |
240 |
} catch (DataException e1) {
|
241 |
throw new CloseException(this.getName(),e1); |
242 |
} catch (KeyException e) { |
243 |
// TODO Auto-generated catch block
|
244 |
throw new CloseException(this.getName(),e); |
245 |
} |
246 |
|
247 |
this.dbf = null; |
248 |
this.metadata=null; |
249 |
} |
250 |
|
251 |
public boolean isEditable() { |
252 |
return this.dbf.isEditable(); |
253 |
} |
254 |
|
255 |
public IMetadata getMetadata() throws BaseException { |
256 |
if (metadata==null){ |
257 |
IMetadataManager manager=MetadataManager.getManager(); |
258 |
metadata=manager.create(DATASTORE_NAME); |
259 |
} |
260 |
return metadata;
|
261 |
} |
262 |
|
263 |
// protected Feature getFeatureByPosition(FeatureType ft,long position) throws ReadException {
|
264 |
//// Field Type (C or M)
|
265 |
//// char cfieldType = fieldTypes[fieldId];
|
266 |
//// int fieldType = getFieldType(fieldId);
|
267 |
// DBFFeature feature=new DBFFeature(ft,this,position);
|
268 |
// feature.load(this.dbf);
|
269 |
// return feature;
|
270 |
// }
|
271 |
|
272 |
protected FeaturesWriter getFeaturesWriter() throws InitializeWriterException{ |
273 |
FeaturesWriter writer = new DBFFeaturesWriter();
|
274 |
// writer.init(this);
|
275 |
return writer;
|
276 |
} |
277 |
|
278 |
protected long getFeatureCount() throws ReadException{ |
279 |
return dbf.getRecordCount();
|
280 |
} |
281 |
|
282 |
|
283 |
public DataStoreParameters getParameters() {
|
284 |
return parameters;
|
285 |
} |
286 |
|
287 |
|
288 |
public DataExplorer getExplorer() throws InitializeException { |
289 |
DataManager dsm=DataManager.getManager(); |
290 |
DataExplorerParameters dsp = dsm.createDataExplorerParameters( |
291 |
DBFDataExplorer.DATASOURCE_NAME); |
292 |
((FileExplorerParameters)dsp).setSource(dbfParameters.getFile().getParentFile()); |
293 |
|
294 |
DataExplorer src=null;
|
295 |
try {
|
296 |
src = dsm.createDataExplorer(dsp); |
297 |
} catch (InitializeException e1) {
|
298 |
e1.printStackTrace(); |
299 |
} |
300 |
return src;
|
301 |
} |
302 |
|
303 |
/* (non-Javadoc)
|
304 |
* @see org.gvsig.fmap.data.feature.FeatureStore#doRefresh()
|
305 |
*/
|
306 |
protected void doRefresh() throws OpenException, InitializeException { |
307 |
this.initFeatureType();
|
308 |
} |
309 |
|
310 |
/**
|
311 |
* @param i
|
312 |
* @param i2
|
313 |
* @return
|
314 |
* @throws ReadException
|
315 |
*/
|
316 |
protected String getStringFieldValue(int rowIndex, int fieldId) throws ReadException { |
317 |
return this.dbf.getStringFieldValue(rowIndex, fieldId); |
318 |
} |
319 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
320 |
try {
|
321 |
return getFeaturesWriter().canWriteGeometry(gvSIGgeometryType);
|
322 |
} catch (InitializeWriterException e) {
|
323 |
e.printStackTrace(); |
324 |
} |
325 |
return false; |
326 |
} |
327 |
|
328 |
public String getClassName() { |
329 |
// TODO Auto-generated method stub
|
330 |
return null; |
331 |
} |
332 |
} |