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