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