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