root / branches / Mobile_Compatible_Hito_1 / libFMap / src-file / org / gvsig / data / datastores / vectorial / file / dbf / DBFStore.java @ 21606
History | View | Annotate | Download (7.78 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.IDataCollection; |
15 |
import org.gvsig.data.IDataExplorer; |
16 |
import org.gvsig.data.IDataStoreParameters; |
17 |
import org.gvsig.data.datastores.vectorial.IFeaturesWriter; |
18 |
import org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile; |
19 |
import org.gvsig.data.exception.CloseException; |
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.AbstractFeatureStore; |
26 |
import org.gvsig.data.vectorial.DefaultAttributeDescriptor; |
27 |
import org.gvsig.data.vectorial.DefaultFeatureType; |
28 |
import org.gvsig.data.vectorial.FeatureStoreNotification; |
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.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 AbstractFeatureStore{ |
40 |
public static String DATASTORE_NAME = "DBFStore"; |
41 |
|
42 |
static {
|
43 |
Register.selfRegister(); |
44 |
} |
45 |
|
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 |
featureType.add(createFeatureAttribute(i)); |
62 |
} |
63 |
} |
64 |
|
65 |
|
66 |
private IFeatureAttributeDescriptor createFeatureAttribute(int i) { |
67 |
char fieldType = dbf.getFieldType(i);
|
68 |
DefaultAttributeDescriptor dad=new DefaultAttributeDescriptor();
|
69 |
dad.setOrdinal(i); |
70 |
dad.setName(dbf.getFieldName(i)); |
71 |
dad.setSize(dbf.getFieldLength(i)); |
72 |
if (fieldType == 'L') { |
73 |
dad.setType(IFeatureAttributeDescriptor.TYPE_BOOLEAN); |
74 |
|
75 |
} else if ((fieldType == 'F') || (fieldType == 'N')) { |
76 |
int precision = dbf.getFieldDecimalLength(i);
|
77 |
if (precision > 0){ |
78 |
dad.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE); |
79 |
dad.setPrecision(precision); |
80 |
} else{
|
81 |
dad.setType(IFeatureAttributeDescriptor.TYPE_INT); |
82 |
} |
83 |
} else if (fieldType == 'C') { |
84 |
dad.setType(IFeatureAttributeDescriptor.TYPE_STRING); |
85 |
} else if (fieldType == 'D') { |
86 |
dad.setType(IFeatureAttributeDescriptor.TYPE_DATE); |
87 |
} else {
|
88 |
// throw new BadFieldDriverException(getName(),null,String.valueOf(fieldType));
|
89 |
} |
90 |
return dad;
|
91 |
|
92 |
} |
93 |
|
94 |
protected void doFinishEdition() throws WriteException, ReadException { |
95 |
IFeaturesWriter writer = getFeaturesWriter(); |
96 |
writer.init(this);
|
97 |
writer.updateFeatureType(featureType); |
98 |
writer.preProcess(); |
99 |
Collection collection=getDataCollection();
|
100 |
Iterator iterator=collection.iterator();
|
101 |
IFeature feature; |
102 |
while (iterator.hasNext()) {
|
103 |
feature= (IFeature) iterator.next(); |
104 |
writer.insertFeature(feature); |
105 |
} |
106 |
writer.postProcess(); |
107 |
|
108 |
} |
109 |
|
110 |
public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException { |
111 |
if (type==null){ |
112 |
type=getDefaultFeatureType(); |
113 |
} |
114 |
IFeatureCollection coll; |
115 |
if (order == null){ |
116 |
coll=new DBFFeatureCollection(featureManager,this,type,filter); |
117 |
}else{
|
118 |
coll=new DBFFeatureCollectionWithFeatureID(featureManager,this,type,filter,order); |
119 |
} |
120 |
|
121 |
this.addObserver(new WeakReference(coll)); |
122 |
return coll;
|
123 |
|
124 |
} |
125 |
|
126 |
public IFeature getFeatureByID(IFeatureID id) throws ReadException { |
127 |
if (this.alterMode){ |
128 |
if (featureManager.contains(id)) {
|
129 |
return featureManager.getFeature(id);
|
130 |
} |
131 |
} |
132 |
return getFeatureByPosition(featureType,((DBFFeatureID)id).getIndex());
|
133 |
} |
134 |
|
135 |
public List getFeatureTypes() { |
136 |
featureTypes.set(0,getDefaultFeatureType());
|
137 |
return featureTypes;
|
138 |
} |
139 |
|
140 |
public IFeatureType getDefaultFeatureType() {
|
141 |
IFeatureType ft = featureType; |
142 |
if (isEditing()){
|
143 |
// Aqu? hay que construir un FeatureType con los cambios que se hayan hecho en la edici?n.
|
144 |
return attributeManager.getFeatureType(ft);
|
145 |
} |
146 |
return ft;
|
147 |
} |
148 |
|
149 |
public boolean isWithDefaultLegend() { |
150 |
return false; |
151 |
} |
152 |
|
153 |
public Object getDefaultLegend() { |
154 |
return null; |
155 |
} |
156 |
|
157 |
public Object getDefaultLabelingStrategy() { |
158 |
return null; |
159 |
} |
160 |
|
161 |
public boolean canAlterFeatureType() { |
162 |
return true; |
163 |
} |
164 |
|
165 |
public String getName() { |
166 |
return DATASTORE_NAME;
|
167 |
} |
168 |
|
169 |
public void open() throws OpenException { |
170 |
this.observable.notifyObservers(
|
171 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_OPEN) |
172 |
); |
173 |
//No hace nada
|
174 |
this.observable.notifyObservers(
|
175 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_OPEN) |
176 |
); |
177 |
|
178 |
} |
179 |
|
180 |
public void close() throws CloseException { |
181 |
this.observable.notifyObservers(
|
182 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_CLOSE) |
183 |
); |
184 |
dbf.close(); |
185 |
this.observable.notifyObservers(
|
186 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_CLOSE) |
187 |
); |
188 |
|
189 |
} |
190 |
|
191 |
public void dispose() { |
192 |
this.observable.notifyObservers(
|
193 |
new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_DISPOSE) |
194 |
); |
195 |
//No hace nada
|
196 |
this.observable.notifyObservers(
|
197 |
new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_DISPOSE) |
198 |
); |
199 |
} |
200 |
|
201 |
public boolean isEditable() { |
202 |
if (file.canWrite()) return true; |
203 |
return false; |
204 |
} |
205 |
|
206 |
public IMetadata getMetadata() throws BaseException { |
207 |
if (metadata==null){ |
208 |
IMetadataManager manager=MetadataManager.getManager(); |
209 |
metadata=manager.create(DATASTORE_NAME); |
210 |
} |
211 |
if (this.alterMode){ |
212 |
IExtent extent=(IExtent)metadata.get("extent");
|
213 |
IFeatureCollection featureCollection=(IFeatureCollection)getDataCollection(); |
214 |
if (spatialManager.isFullExtentDirty()){
|
215 |
if (!featureCollection.isEmpty()){
|
216 |
Iterator featureIterator=featureCollection.iterator();
|
217 |
extent = ((IFeature)featureIterator.next()).getExtent(); |
218 |
while(featureIterator.hasNext()){
|
219 |
IFeature feature=(IFeature)featureIterator.next(); |
220 |
IExtent boundExtent=feature.getExtent(); |
221 |
if (boundExtent!=null) |
222 |
extent.add(boundExtent); |
223 |
} |
224 |
} |
225 |
} |
226 |
metadata.set("extent",extent);
|
227 |
} |
228 |
return metadata;
|
229 |
} |
230 |
|
231 |
protected IFeature getFeatureByPosition(IFeatureType ft,long position) throws ReadException { |
232 |
// Field Type (C or M)
|
233 |
// char cfieldType = fieldTypes[fieldId];
|
234 |
// int fieldType = getFieldType(fieldId);
|
235 |
DBFFeature feature=new DBFFeature(ft,this,position); |
236 |
feature.load(this.dbf);
|
237 |
return feature;
|
238 |
} |
239 |
|
240 |
protected IFeaturesWriter getFeaturesWriter() {
|
241 |
IFeaturesWriter writer = new DBFFeaturesWriter();
|
242 |
// writer.init(this);
|
243 |
return writer;
|
244 |
} |
245 |
|
246 |
protected long getFeatureCount(){ |
247 |
return dbf.getRecordCount();
|
248 |
} |
249 |
|
250 |
|
251 |
public IDataStoreParameters getParameters() {
|
252 |
return parameters;
|
253 |
} |
254 |
|
255 |
|
256 |
public IDataExplorer getExplorer() {
|
257 |
// TODO Auto-generated method stub
|
258 |
return null; |
259 |
} |
260 |
} |