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