svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / dbf / deprecated / DBFStore.java @ 24081
History | View | Annotate | Download (10 KB)
1 |
package org.gvsig.fmap.data.feature.file.dbf.deprecated; |
---|---|
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 |
|
9 |
import org.gvsig.fmap.data.DataSet; |
10 |
import org.gvsig.fmap.data.DataExplorer; |
11 |
import org.gvsig.fmap.data.DataExplorerParameters; |
12 |
import org.gvsig.fmap.data.DataManager; |
13 |
import org.gvsig.fmap.data.DataStoreParameters; |
14 |
import org.gvsig.fmap.data.exceptions.CloseException; |
15 |
import org.gvsig.fmap.data.exceptions.DataException; |
16 |
import org.gvsig.fmap.data.exceptions.InitializeException; |
17 |
import org.gvsig.fmap.data.exceptions.OpenException; |
18 |
import org.gvsig.fmap.data.exceptions.ReadException; |
19 |
import org.gvsig.fmap.data.exceptions.WriteException; |
20 |
import org.gvsig.fmap.data.feature.Feature; |
21 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
22 |
import org.gvsig.fmap.data.feature.FeatureSet; |
23 |
import org.gvsig.fmap.data.feature.FeatureReference; |
24 |
import org.gvsig.fmap.data.feature.FeatureType; |
25 |
import org.gvsig.fmap.data.feature.exceptions.InitializeWriterException; |
26 |
import org.gvsig.fmap.data.feature.file.FileExplorerParameters; |
27 |
import org.gvsig.fmap.data.feature.file.FileStore; |
28 |
import org.gvsig.fmap.data.feature.impl.DefaultFeatureType; |
29 |
import org.gvsig.fmap.data.feature.impl.FeaturesWriter; |
30 |
import org.gvsig.fmap.data.feature.spi.FeatureData; |
31 |
import org.gvsig.fmap.data.impl.DefaultDataManager; |
32 |
import org.gvsig.fmap.data.resource.ResourceManager; |
33 |
import org.gvsig.fmap.data.resource.impl.DefaultResourceManager; |
34 |
import org.gvsig.metadata.DefaultMetadataManager; |
35 |
import org.gvsig.metadata.Metadata; |
36 |
import org.gvsig.metadata.MetadataManager; |
37 |
import org.gvsig.tools.exception.BaseException; |
38 |
|
39 |
/**
|
40 |
* @deprecated
|
41 |
*
|
42 |
* @author jjdelcerro
|
43 |
*
|
44 |
*/
|
45 |
public class DBFStore extends FileStore { |
46 |
public static String DATASTORE_NAME = "DBFStore"; |
47 |
private DBFResource dbf= null; |
48 |
protected Metadata metadata;
|
49 |
private DBFStoreParameters dbfParameters=null; |
50 |
|
51 |
|
52 |
public void init(DataStoreParameters parameters) throws InitializeException { |
53 |
dbfParameters=(DBFStoreParameters)parameters; |
54 |
DBFResource tmpResource = new DBFResource(dbfParameters);
|
55 |
|
56 |
DefaultResourceManager resMan = DefaultResourceManager.getResourceManager(); |
57 |
|
58 |
try {
|
59 |
this.dbf = (DBFResource) resMan.addResource(tmpResource, this); |
60 |
} catch (DataException e1) {
|
61 |
throw new InitializeException(this.getName(),e1); |
62 |
} |
63 |
|
64 |
super.init(parameters,this.dbf); |
65 |
|
66 |
// this.dbf.open();
|
67 |
// this.dbf.addObserver(this);
|
68 |
this.initFeatureType();
|
69 |
} |
70 |
|
71 |
private void initFeatureType() throws InitializeException{ |
72 |
int fieldCount = -1; |
73 |
try {
|
74 |
fieldCount = dbf.getFieldCount(); |
75 |
} catch (DataException e) {
|
76 |
throw new InitializeException(this.getName(), e); |
77 |
} |
78 |
|
79 |
defaultFeatureType = new DefaultFeatureType();
|
80 |
for (int i = 0; i < fieldCount; i++) { |
81 |
FeatureAttributeDescriptor fad; |
82 |
try {
|
83 |
fad = createFeatureAttribute( |
84 |
(DefaultFeatureType) defaultFeatureType, i); |
85 |
} catch (IsNotAttributeSettingException e) {
|
86 |
throw new InitializeException(this.getName(), e); |
87 |
} catch (ReadException e) {
|
88 |
// TODO Auto-generated catch block
|
89 |
throw new InitializeException(this.getName(), e); |
90 |
} |
91 |
defaultFeatureType.add(fad); |
92 |
} |
93 |
|
94 |
this.featureTypes = new ArrayList(); |
95 |
this.featureTypes.add(defaultFeatureType);
|
96 |
|
97 |
} |
98 |
|
99 |
|
100 |
private FeatureAttributeDescriptor createFeatureAttribute(DefaultFeatureType fType, int i) throws ReadException, IsNotAttributeSettingException { |
101 |
char fieldType = dbf.getFieldType(i);
|
102 |
AttributeDescriptor dad=(AttributeDescriptor) fType.createAttributeDescriptor(); |
103 |
dad.loading(); |
104 |
dad.setName(dbf.getFieldName(i)); |
105 |
dad.setSize(dbf.getFieldLength(i)); |
106 |
if (fieldType == 'L') { |
107 |
dad.setType(FeatureAttributeDescriptor.BOOLEAN); |
108 |
|
109 |
} else if ((fieldType == 'F') || (fieldType == 'N')) { |
110 |
int precision = dbf.getFieldDecimalLength(i);
|
111 |
if (precision > 0){ |
112 |
dad.setType(FeatureAttributeDescriptor.DOUBLE); |
113 |
dad.setPrecision(precision); |
114 |
} else{
|
115 |
dad.setType(FeatureAttributeDescriptor.INT); |
116 |
} |
117 |
} else if (fieldType == 'C') { |
118 |
dad.setType(FeatureAttributeDescriptor.STRING); |
119 |
} else if (fieldType == 'D') { |
120 |
dad.setType(FeatureAttributeDescriptor.DATE); |
121 |
} else {
|
122 |
// throw new BadFieldDriverException(getName(),null,String.valueOf(fieldType));
|
123 |
} |
124 |
dad.stopLoading(); |
125 |
return dad;
|
126 |
|
127 |
} |
128 |
|
129 |
protected void doFinishEdition() throws WriteException, ReadException { |
130 |
FeaturesWriter writer = getFeaturesWriter(); |
131 |
writer.init(this);
|
132 |
writer.updateFeatureType(this.getDefaultFeatureType());
|
133 |
this.dbf.editing();
|
134 |
writer.preProcess(); |
135 |
Collection collection=getDataSet();
|
136 |
Iterator iterator=collection.iterator();
|
137 |
Feature feature; |
138 |
while (iterator.hasNext()) {
|
139 |
feature= (Feature) iterator.next(); |
140 |
writer.insertFeature(feature); |
141 |
} |
142 |
writer.postProcess(); |
143 |
this.dbf.stopEditing();
|
144 |
|
145 |
this.dbf.changed(this); |
146 |
} |
147 |
|
148 |
public DataSet getDataCollection(FeatureType type, String filter, String order) throws ReadException { |
149 |
try {
|
150 |
type = this.checkFeatureTypeForCollection(type);
|
151 |
} catch (DataException e) {
|
152 |
throw new ReadException(this.getName(), e); |
153 |
} |
154 |
|
155 |
FeatureSet coll; |
156 |
if (order == null){ |
157 |
//if (featureManager==null){
|
158 |
// coll=new DBFFeatureCollectionBitSet(this,type,filter);
|
159 |
//}else{
|
160 |
coll=new DBFFeatureCollection(featureManager,this,type,filter); |
161 |
//}
|
162 |
}else{
|
163 |
coll=new DBFFeatureCollectionWithFeatureID(featureManager,this,type,filter,order); |
164 |
} |
165 |
|
166 |
this.addObserver(new WeakReference(coll)); |
167 |
return coll;
|
168 |
|
169 |
} |
170 |
|
171 |
public FeatureData getFeatureDataByID(FeatureReference id, FeatureType featureType)
|
172 |
throws DataException {
|
173 |
if (featureType == null){ |
174 |
featureType=this.getDefaultFeatureType();
|
175 |
} else{
|
176 |
if (!featureType.isSubtypeOf(this.getDefaultFeatureType())){ |
177 |
throw new ReadException("invalid type",this.getName()); |
178 |
} |
179 |
} |
180 |
if (this.alterMode){ |
181 |
if (featureManager.contains(id)) {
|
182 |
return featureManager.getFeature(id,this,featureType); |
183 |
} |
184 |
} |
185 |
return this.getFeatureByIndex(((DBFFeatureID) id).getIndex(), |
186 |
featureType); |
187 |
} |
188 |
|
189 |
/**
|
190 |
*
|
191 |
* Only for persistenced features
|
192 |
*
|
193 |
* @param index
|
194 |
* @param featureType
|
195 |
* @return
|
196 |
* @throws ReadException
|
197 |
*/
|
198 |
protected Feature getFeatureByIndex(long index, FeatureType featureType) |
199 |
throws ReadException {
|
200 |
if (index >= this.getFeatureCount()) { |
201 |
throw new ReadException(this.getName(), |
202 |
"Unsupported operation for not persistented features");
|
203 |
} |
204 |
DBFFeature feature = new DBFFeature(featureType, this, index); |
205 |
return feature;
|
206 |
} |
207 |
|
208 |
/***
|
209 |
* NOT supported in Alter Mode
|
210 |
*
|
211 |
* @param index
|
212 |
* @return
|
213 |
* @throws ReadException
|
214 |
*/
|
215 |
protected Feature getFeatureDataByIndex(long index) throws ReadException { |
216 |
return this.getFeatureByIndex(index, this.getDefaultFeatureType()); |
217 |
} |
218 |
|
219 |
public boolean canAlterFeatureType() { |
220 |
return true; |
221 |
} |
222 |
|
223 |
public String getName() { |
224 |
return DATASTORE_NAME;
|
225 |
} |
226 |
|
227 |
protected void doOpen() throws OpenException { |
228 |
//No hace nada
|
229 |
|
230 |
} |
231 |
|
232 |
protected void doClose() throws CloseException { |
233 |
try {
|
234 |
dbf.close(); |
235 |
} catch (DataException e) {
|
236 |
throw new CloseException(this.getName(),e); |
237 |
} |
238 |
|
239 |
} |
240 |
|
241 |
protected void doDispose() throws CloseException { |
242 |
super.doDispose();
|
243 |
ResourceManager resMan = DefaultResourceManager.getResourceManager(); |
244 |
|
245 |
try {
|
246 |
resMan.remove(this.dbf, this); |
247 |
} catch (DataException e1) {
|
248 |
throw new CloseException(this.getName(),e1); |
249 |
} catch (KeyException e) { |
250 |
// TODO Auto-generated catch block
|
251 |
throw new CloseException(this.getName(),e); |
252 |
} |
253 |
|
254 |
this.dbf = null; |
255 |
this.metadata=null; |
256 |
} |
257 |
|
258 |
public boolean allowWrite() { |
259 |
return this.dbf.isEditable(); |
260 |
} |
261 |
|
262 |
public Metadata getMetadata() throws BaseException { |
263 |
if (metadata==null){ |
264 |
MetadataManager manager=DefaultMetadataManager.getManager(); |
265 |
metadata=manager.create(DATASTORE_NAME); |
266 |
} |
267 |
return metadata;
|
268 |
} |
269 |
|
270 |
// protected Feature getFeatureByPosition(FeatureType ft,long position) throws ReadException {
|
271 |
//// Field Type (C or M)
|
272 |
//// char cfieldType = fieldTypes[fieldId];
|
273 |
//// int fieldType = getFieldType(fieldId);
|
274 |
// DBFFeature feature=new DBFFeature(ft,this,position);
|
275 |
// feature.load(this.dbf);
|
276 |
// return feature;
|
277 |
// }
|
278 |
|
279 |
protected FeaturesWriter getFeaturesWriter() throws InitializeWriterException{ |
280 |
FeaturesWriter writer = new DBFFeaturesWriter();
|
281 |
// writer.init(this);
|
282 |
return writer;
|
283 |
} |
284 |
|
285 |
protected long getFeatureCount() throws ReadException{ |
286 |
return dbf.getRecordCount();
|
287 |
} |
288 |
|
289 |
|
290 |
public DataStoreParameters getParameters() {
|
291 |
return parameters;
|
292 |
} |
293 |
|
294 |
|
295 |
public DataExplorer getExplorer() throws InitializeException { |
296 |
DataManager dsm=DefaultDataManager.getManager(); |
297 |
DataExplorerParameters dsp = dsm.createExplorerParameters( |
298 |
DBFDataExplorer.DATASOURCE_NAME); |
299 |
((FileExplorerParameters)dsp).setSource(dbfParameters.getFile().getParentFile()); |
300 |
|
301 |
DataExplorer src=null;
|
302 |
try {
|
303 |
src = dsm.createExplorer(dsp); |
304 |
} catch (InitializeException e1) {
|
305 |
e1.printStackTrace(); |
306 |
} |
307 |
return src;
|
308 |
} |
309 |
|
310 |
/* (non-Javadoc)
|
311 |
* @see org.gvsig.fmap.data.feature.FeatureStore#doRefresh()
|
312 |
*/
|
313 |
protected void doRefresh() throws OpenException, InitializeException { |
314 |
this.initFeatureType();
|
315 |
} |
316 |
|
317 |
/**
|
318 |
* @param i
|
319 |
* @param i2
|
320 |
* @return
|
321 |
* @throws ReadException
|
322 |
*/
|
323 |
protected String getStringFieldValue(int rowIndex, int fieldId) throws ReadException { |
324 |
return this.dbf.getStringFieldValue(rowIndex, fieldId); |
325 |
} |
326 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
327 |
try {
|
328 |
return getFeaturesWriter().canWriteGeometry(gvSIGgeometryType);
|
329 |
} catch (InitializeWriterException e) {
|
330 |
e.printStackTrace(); |
331 |
} |
332 |
return false; |
333 |
} |
334 |
|
335 |
public String getClassName() { |
336 |
// TODO Auto-generated method stub
|
337 |
return null; |
338 |
} |
339 |
} |