root / trunk / libraries / libDataSource / src / org / gvsig / data / vectorial / Feature.java @ 20599
History | View | Annotate | Download (11.8 KB)
1 |
package org.gvsig.data.vectorial; |
---|---|
2 |
|
3 |
import java.text.ParseException; |
4 |
import java.util.Date; |
5 |
import java.util.Iterator; |
6 |
|
7 |
import org.gvsig.data.exception.DataException; |
8 |
import org.gvsig.data.exception.InitializeException; |
9 |
import org.gvsig.data.exception.ReadException; |
10 |
|
11 |
public abstract class Feature implements IFeature{ |
12 |
protected IFeatureType featureType;
|
13 |
protected Object defaultGeometry; |
14 |
protected Object object; |
15 |
private boolean editing = false; |
16 |
private boolean loading = false; |
17 |
private IFeature newFeature;
|
18 |
protected Object[] values; |
19 |
|
20 |
protected abstract IFeature cloneFeature() throws DataException; |
21 |
|
22 |
public Feature(IFeatureType featureType) throws ReadException{ |
23 |
this.init(featureType, false); |
24 |
} |
25 |
|
26 |
public Feature(IFeatureType featureType,boolean loadDefaultValues) throws ReadException{ |
27 |
this.init(featureType, loadDefaultValues);
|
28 |
} |
29 |
|
30 |
protected void init(IFeatureType featureType,boolean loadDefaultValues)throws InitializeException{ |
31 |
this.values = new Object[featureType.getCount()]; |
32 |
this.featureType=featureType;
|
33 |
if (loadDefaultValues){
|
34 |
try {
|
35 |
this.loadDefaultValues();
|
36 |
} catch (Exception e) { |
37 |
throw new InitializeException("Feature",e); |
38 |
} |
39 |
} |
40 |
|
41 |
} |
42 |
|
43 |
public int size() { |
44 |
return this.getType().size(); |
45 |
} |
46 |
|
47 |
public IFeatureType getType() {
|
48 |
return featureType;
|
49 |
} |
50 |
|
51 |
public Object getDefaultGeometry() { |
52 |
return defaultGeometry;
|
53 |
} |
54 |
|
55 |
public Object get(String name) { |
56 |
int i=featureType.getFieldIndex(name);
|
57 |
return this.get(i); |
58 |
} |
59 |
|
60 |
public int getInt(String name) { |
61 |
int i=featureType.getFieldIndex(name);
|
62 |
Integer value=(Integer)this.get(i); |
63 |
if (value==null) |
64 |
return 0; |
65 |
return value.intValue();
|
66 |
} |
67 |
|
68 |
public boolean getBoolean(String name) { |
69 |
int i=featureType.getFieldIndex(name);
|
70 |
Boolean value=(Boolean)this.get(i); |
71 |
if (value==null) |
72 |
return false; |
73 |
return value.booleanValue();
|
74 |
} |
75 |
|
76 |
public long getLong(String name) { |
77 |
int i=featureType.getFieldIndex(name);
|
78 |
Long value=(Long)this.get(i); |
79 |
if (value==null) |
80 |
return 0L; |
81 |
return value.longValue();
|
82 |
} |
83 |
|
84 |
public float getFloat(String name) { |
85 |
int i=featureType.getFieldIndex(name);
|
86 |
Float value=(Float)this.get(i); |
87 |
if (value==null) |
88 |
return 0F; |
89 |
return value.floatValue();
|
90 |
} |
91 |
|
92 |
public double getDouble(String name) { |
93 |
int i=featureType.getFieldIndex(name);
|
94 |
Double value=(Double)this.get(i); |
95 |
if (value==null) |
96 |
return 0D; |
97 |
return value.doubleValue();
|
98 |
} |
99 |
public String getString(String name) { |
100 |
int i=featureType.getFieldIndex(name);
|
101 |
String value=(String)this.get(i); |
102 |
return value;
|
103 |
} |
104 |
public String getString(int index) { |
105 |
String value=(String)this.get(index); |
106 |
return value;
|
107 |
} |
108 |
public Object get(int index) { |
109 |
if (this.isEditing() && this.newFeature != null) |
110 |
this.newFeature.get(index);
|
111 |
return this.values[index]; |
112 |
} |
113 |
|
114 |
public IFeature getFeature(int index) { |
115 |
IFeature value=(IFeature)this.get(index);
|
116 |
return value;
|
117 |
} |
118 |
|
119 |
public Object[] getArray(int index) { |
120 |
// TODO Auto-generated method stub
|
121 |
return null; |
122 |
} |
123 |
|
124 |
public int getInt(int index) { |
125 |
Integer value=(Integer)this.get(index); |
126 |
if (value==null) |
127 |
return 0; |
128 |
return value.intValue();
|
129 |
} |
130 |
|
131 |
public boolean getBoolean(int index) { |
132 |
Boolean value=(Boolean)this.get(index); |
133 |
if (value==null) |
134 |
return false; |
135 |
return value.booleanValue();
|
136 |
} |
137 |
|
138 |
public long getLong(int index) { |
139 |
Long value=(Long)this.get(index); |
140 |
if (value==null) |
141 |
return 0L; |
142 |
return value.longValue();
|
143 |
} |
144 |
|
145 |
public float getFloat(int index) { |
146 |
Float value=(Float)this.get(index); |
147 |
if (value==null) |
148 |
return 0F; |
149 |
return value.floatValue();
|
150 |
} |
151 |
|
152 |
public double getDouble(int index) { |
153 |
Double value=(Double)this.get(index); |
154 |
if (value==null) |
155 |
return 0D; |
156 |
return value.doubleValue();
|
157 |
} |
158 |
|
159 |
public Date getDate(int index) { |
160 |
return (Date)this.get(index); |
161 |
} |
162 |
|
163 |
public Date getDate(String name) { |
164 |
int i=featureType.getFieldIndex(name);
|
165 |
return (Date)this.get(i); |
166 |
} |
167 |
|
168 |
public void set(String name, Object value) throws IsNotFeatureSettingException { |
169 |
int i=featureType.getFieldIndex(name);
|
170 |
this.set(i, value);
|
171 |
} |
172 |
|
173 |
public void set(String name, int value) throws IsNotFeatureSettingException { |
174 |
int i=featureType.getFieldIndex(name);
|
175 |
this.set(i, new Integer(value)); |
176 |
} |
177 |
|
178 |
public void set(String name, boolean value) throws IsNotFeatureSettingException { |
179 |
int i=featureType.getFieldIndex(name);
|
180 |
this.set(i,new Boolean(value)); |
181 |
} |
182 |
|
183 |
public void set(String name, long value) throws IsNotFeatureSettingException { |
184 |
int i=featureType.getFieldIndex(name);
|
185 |
this.set(i,new Long(value)); |
186 |
} |
187 |
|
188 |
public void set(String name, float value) throws IsNotFeatureSettingException { |
189 |
int i=featureType.getFieldIndex(name);
|
190 |
this.set(i,new Float(value)); |
191 |
} |
192 |
|
193 |
public void set(String name, double value) throws IsNotFeatureSettingException { |
194 |
int i=featureType.getFieldIndex(name);
|
195 |
this.set(i,new Double(value)); |
196 |
} |
197 |
|
198 |
public void set(int index, Object value) throws IsNotFeatureSettingException { |
199 |
canSetValue(index); |
200 |
if ( this.isEditing() && newFeature!=null){ |
201 |
((Feature)newFeature).loading(); |
202 |
newFeature.set(index,value); |
203 |
((Feature)newFeature).stopLoading(); |
204 |
}else
|
205 |
this.values[index] =value;
|
206 |
|
207 |
} |
208 |
|
209 |
public void set(int index, int value) throws IsNotFeatureSettingException { |
210 |
this.set(index,new Integer(value)); |
211 |
} |
212 |
|
213 |
public void set(int index, boolean value) throws IsNotFeatureSettingException { |
214 |
this.set(index,new Boolean(value)); |
215 |
} |
216 |
|
217 |
public void set(int index, long value) throws IsNotFeatureSettingException { |
218 |
this.set(index,new Long(value)); |
219 |
} |
220 |
|
221 |
public void set(int index, float value) throws IsNotFeatureSettingException { |
222 |
this.set(index,new Float(value)); |
223 |
} |
224 |
|
225 |
public void set(int index, double value) throws IsNotFeatureSettingException { |
226 |
this.set(index,new Double(value)); |
227 |
} |
228 |
|
229 |
public void validateEnd(IFeatureStore featureStore) { |
230 |
getType().validateFeatureEnd(this,featureStore);
|
231 |
|
232 |
} |
233 |
|
234 |
public void validateModification(IFeatureStore featureStore) { |
235 |
getType().validateFeatureModifiction(this,featureStore);
|
236 |
} |
237 |
|
238 |
public void validate(IFeatureStore featureStore) { |
239 |
getType().validateFeature(this,featureStore);
|
240 |
} |
241 |
public IFeature getFeature(String name) { |
242 |
int i=featureType.getFieldIndex(name);
|
243 |
return (IFeature)this.values[i]; |
244 |
} |
245 |
public Object getGeometry(int index) { |
246 |
return this.get(index); |
247 |
} |
248 |
public Object getGeometry(String name) { |
249 |
int i=featureType.getFieldIndex(name);
|
250 |
return this.get(i); |
251 |
} |
252 |
public byte getByte(int index) { |
253 |
Byte value=(Byte)this.get(index); |
254 |
if (value==null) |
255 |
return 0; |
256 |
return value.byteValue();
|
257 |
} |
258 |
public byte getByte(String name) { |
259 |
int i=featureType.getFieldIndex(name);
|
260 |
Byte value=(Byte)this.get(i); |
261 |
if (value==null) |
262 |
return 0; |
263 |
return value.byteValue();
|
264 |
} |
265 |
public void set(int index, byte value) throws IsNotFeatureSettingException { |
266 |
this.set(index,new Byte(value)); |
267 |
} |
268 |
public void set(int index, String value) throws IsNotFeatureSettingException { |
269 |
this.set(index,(Object)value); |
270 |
} |
271 |
public void set(String name, byte value) throws IsNotFeatureSettingException { |
272 |
int i=featureType.getFieldIndex(name);
|
273 |
this.set(i,new Byte(value)); |
274 |
} |
275 |
public void setGeometry(String name,Object geometry) throws IsNotFeatureSettingException { |
276 |
int i=featureType.getFieldIndex(name);
|
277 |
canSetValue(i); |
278 |
if (this.isEditing()){ |
279 |
((Feature)newFeature).loading(); |
280 |
newFeature.setGeometry(name,geometry); |
281 |
((Feature)newFeature).stopLoading(); |
282 |
}else{
|
283 |
this.values[i]=geometry;
|
284 |
if (featureType.getDefaultGeometry().equals(name)){
|
285 |
defaultGeometry=geometry; |
286 |
} |
287 |
} |
288 |
} |
289 |
public void setGeometry(int index,Object geometry) throws IsNotFeatureSettingException { |
290 |
canSetValue(index); |
291 |
if (newFeature!=null){ |
292 |
((Feature)newFeature).loading(); |
293 |
newFeature.setGeometry(index,geometry); |
294 |
((Feature)newFeature).stopLoading(); |
295 |
}else{
|
296 |
this.values[index] =geometry;
|
297 |
Iterator iterator=featureType.iterator();
|
298 |
while (iterator.hasNext()) {
|
299 |
IFeatureAttributeDescriptor fad = (IFeatureAttributeDescriptor) iterator.next(); |
300 |
if (fad.ordinal()==index && fad.getName().equals(featureType.getDefaultGeometry())){
|
301 |
defaultGeometry=geometry; |
302 |
} |
303 |
} |
304 |
} |
305 |
} |
306 |
protected void canSetValue(int i) throws IsNotFeatureSettingException { |
307 |
if (!editing && !loading){
|
308 |
throw new IsNotFeatureSettingException("set"); |
309 |
} |
310 |
IFeatureAttributeDescriptor fad=(IFeatureAttributeDescriptor)featureType.getByOrder(i); |
311 |
if (!loading && fad.isReadOnly()){
|
312 |
//TODO Construir la clase adecuada para esta excepci?n
|
313 |
throw new IsNotFeatureSettingException(" Read only field: " +fad.getName()); |
314 |
} |
315 |
} |
316 |
public void cancelEditing() { |
317 |
editing=false;
|
318 |
newFeature=null;
|
319 |
} |
320 |
|
321 |
public void editing() throws DataException { |
322 |
newFeature=this.cloneFeature();
|
323 |
editing=true;
|
324 |
} |
325 |
|
326 |
protected boolean isEditing(){ |
327 |
return editing;
|
328 |
} |
329 |
protected IFeature getNewFeature() {
|
330 |
return newFeature;
|
331 |
} |
332 |
protected void loading(){ |
333 |
loading=true;
|
334 |
} |
335 |
protected void stopLoading(){ |
336 |
loading=false;
|
337 |
} |
338 |
protected void stopEditing(){ |
339 |
editing=false;
|
340 |
newFeature=null;
|
341 |
} |
342 |
protected void refreshEvaluatedFields(IFeatureStore store){ |
343 |
Iterator iter = this.featureType.iterator(); |
344 |
while (iter.hasNext()){
|
345 |
IFeatureAttributeDescriptor desc = (IFeatureAttributeDescriptor)iter.next(); |
346 |
if (desc.isEvaluated()){
|
347 |
Object value = desc.getEvaluator().evalue(store, this, desc); |
348 |
this.values[desc.ordinal()] = value;
|
349 |
if (desc.getDataType().equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)){
|
350 |
if (featureType.getDefaultGeometry().equals(desc.getName())){
|
351 |
this.defaultGeometry=value;
|
352 |
} |
353 |
} |
354 |
} |
355 |
} |
356 |
} |
357 |
|
358 |
protected void loadDefaultValues() throws IsNotFeatureSettingException, NumberFormatException, ParseException{ |
359 |
IFeatureType type = this.getType();
|
360 |
Iterator iterator=type.iterator();
|
361 |
|
362 |
this.loading();
|
363 |
while(iterator.hasNext()){
|
364 |
IFeatureAttributeDescriptor featureAttribute=(IFeatureAttributeDescriptor)iterator.next(); |
365 |
int i=((DefaultAttributeDescriptor)featureAttribute).originalPosition();
|
366 |
String attributeType=featureAttribute.getDataType();
|
367 |
Object defaultValue=featureAttribute.getDefaultValue();
|
368 |
if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_BYTE)){
|
369 |
if (defaultValue instanceof String){ |
370 |
set(i,Byte.parseByte((String)defaultValue)); |
371 |
}else{
|
372 |
set(i,(Byte)defaultValue);
|
373 |
} |
374 |
}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_DATE)){ |
375 |
if (defaultValue instanceof String){ |
376 |
set(i,this.getType().getDateFormat().parse((String)defaultValue)); |
377 |
} else{
|
378 |
|
379 |
} |
380 |
}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)){ |
381 |
if (defaultValue instanceof String){ |
382 |
set(i,Double.parseDouble((String)defaultValue)); |
383 |
}else{
|
384 |
set(i,(Double)defaultValue);
|
385 |
} |
386 |
}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)){ |
387 |
if (defaultValue instanceof String){ |
388 |
set(i,Float.parseFloat((String)defaultValue)); |
389 |
}else{
|
390 |
set(i,(Float)defaultValue);
|
391 |
} |
392 |
}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_GEOMETRY)){ |
393 |
setGeometry(i,defaultValue); |
394 |
}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_INT)){ |
395 |
if (defaultValue instanceof String){ |
396 |
set(i,Integer.parseInt((String)defaultValue)); |
397 |
}else{
|
398 |
set(i,(Integer)defaultValue);
|
399 |
} |
400 |
}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_LONG)){ |
401 |
if (defaultValue instanceof String){ |
402 |
set(i,Long.parseLong((String)defaultValue)); |
403 |
}else{
|
404 |
set(i,(Long)defaultValue);
|
405 |
} |
406 |
}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_OBJECT)){ |
407 |
set(i,defaultValue); |
408 |
}else if (attributeType.equals(IFeatureAttributeDescriptor.TYPE_STRING)){ |
409 |
set(i,(String)defaultValue);
|
410 |
} |
411 |
} |
412 |
this.stopLoading();
|
413 |
|
414 |
} |
415 |
|
416 |
} |