root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / impl / DefaultFeature.java @ 30187
History | View | Annotate | Download (12.1 KB)
1 |
package org.gvsig.fmap.dal.feature.impl; |
---|---|
2 |
|
3 |
import java.lang.ref.WeakReference; |
4 |
import java.text.ParseException; |
5 |
import java.util.Date; |
6 |
import java.util.Iterator; |
7 |
import java.util.List; |
8 |
|
9 |
import org.cresques.cts.IProjection; |
10 |
import org.gvsig.fmap.dal.DataTypes; |
11 |
import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException; |
12 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
13 |
import org.gvsig.fmap.dal.feature.Feature; |
14 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
15 |
import org.gvsig.fmap.dal.feature.FeatureReference; |
16 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
17 |
import org.gvsig.fmap.dal.feature.FeatureType; |
18 |
import org.gvsig.fmap.dal.feature.exception.IllegalValueException; |
19 |
import org.gvsig.fmap.dal.feature.exception.SetReadOnlyAttributeException; |
20 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
21 |
import org.gvsig.fmap.geom.Geometry; |
22 |
import org.gvsig.fmap.geom.primitive.Envelope; |
23 |
import org.gvsig.tools.dynobject.DynClass; |
24 |
import org.gvsig.tools.dynobject.DynObject; |
25 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
26 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
27 |
import org.gvsig.tools.evaluator.Evaluator; |
28 |
import org.gvsig.tools.evaluator.EvaluatorData; |
29 |
import org.gvsig.tools.evaluator.EvaluatorException; |
30 |
|
31 |
public class DefaultFeature implements Feature, EvaluatorData { |
32 |
|
33 |
protected FeatureProvider data;
|
34 |
protected FeatureReference reference;
|
35 |
private WeakReference storeRef; |
36 |
|
37 |
/*
|
38 |
* Usar con mucha precaucion o mejor no usar. Lo precisa el
|
39 |
* DefaultFeatureSet en la ordenacion.
|
40 |
*/
|
41 |
public DefaultFeature(FeatureStore store) {
|
42 |
this.storeRef = new WeakReference(store); |
43 |
this.reference = null; |
44 |
} |
45 |
|
46 |
public DefaultFeature(FeatureStore store, FeatureProvider data) {
|
47 |
this.data = data;
|
48 |
this.storeRef = new WeakReference(store); |
49 |
this.reference = null; |
50 |
} |
51 |
|
52 |
DefaultFeature(DefaultFeature feature) { |
53 |
this.data = feature.data.getCopy();
|
54 |
this.storeRef = feature.storeRef;
|
55 |
this.reference = feature.reference;
|
56 |
} |
57 |
|
58 |
public void setData(FeatureProvider data) { |
59 |
this.data = data;
|
60 |
this.reference = null; |
61 |
} |
62 |
|
63 |
public FeatureProvider getData() {
|
64 |
return this.data; |
65 |
} |
66 |
|
67 |
void set(FeatureAttributeDescriptor attribute, Object value) { |
68 |
int i = attribute.getIndex();
|
69 |
|
70 |
if (attribute.isReadOnly()) {
|
71 |
throw new SetReadOnlyAttributeException(); |
72 |
} |
73 |
|
74 |
if (attribute.getEvaluator() != null) { |
75 |
throw new SetReadOnlyAttributeException(); |
76 |
} |
77 |
|
78 |
if (value == null) { |
79 |
if (!attribute.allowNull()) {
|
80 |
if (!attribute.isAutomatic()) {
|
81 |
throw new IllegalValueException(attribute, value); |
82 |
} |
83 |
} |
84 |
this.data.set(i, null); |
85 |
return;
|
86 |
|
87 |
} |
88 |
|
89 |
if (attribute.getObjectClass().isInstance(value)) {
|
90 |
this.data.set(i, value);
|
91 |
return;
|
92 |
} |
93 |
|
94 |
if ((Number.class.isAssignableFrom(attribute.getObjectClass()) && Number.class.isAssignableFrom(value.getClass())) |
95 |
|| (attribute.getDataType()==DataTypes.STRING && Number.class.isAssignableFrom(value.getClass()))) {
|
96 |
Object number=getNumberByType((Number)value, attribute.getDataType()); |
97 |
this.data.set(i, number);
|
98 |
return;
|
99 |
} |
100 |
|
101 |
if (!(value instanceof String)) { |
102 |
throw new IllegalValueException(attribute, value); |
103 |
} |
104 |
int dataType = attribute.getDataType();
|
105 |
|
106 |
switch (dataType) {
|
107 |
case DataTypes.BYTE:
|
108 |
this.data.set(i, Byte.valueOf((String) value)); |
109 |
break;
|
110 |
|
111 |
case DataTypes.DATE:
|
112 |
try {
|
113 |
this.data.set(i, attribute.getDateFormat()
|
114 |
.parse((String) value));
|
115 |
} catch (ParseException e) { |
116 |
throw new IllegalValueException(attribute, value, e); |
117 |
} |
118 |
break;
|
119 |
|
120 |
case DataTypes.DOUBLE:
|
121 |
this.data.set(i, Double.valueOf((String) value)); |
122 |
break;
|
123 |
|
124 |
case DataTypes.FLOAT:
|
125 |
this.data.set(i, Float.valueOf((String) value)); |
126 |
break;
|
127 |
|
128 |
case DataTypes.INT:
|
129 |
this.data.set(i, Integer.valueOf((String) value)); |
130 |
break;
|
131 |
|
132 |
case DataTypes.LONG:
|
133 |
this.data.set(i, Long.valueOf((String) value)); |
134 |
break;
|
135 |
|
136 |
default:
|
137 |
throw new IllegalValueException(attribute, value); |
138 |
} |
139 |
} |
140 |
|
141 |
private Object getNumberByType(Number value, int type) { |
142 |
if (type==DataTypes.DOUBLE){
|
143 |
return new Double(value.doubleValue()); |
144 |
}else if (type==DataTypes.FLOAT){ |
145 |
return new Float(value.floatValue()); |
146 |
}else if (type==DataTypes.LONG){ |
147 |
return new Long(value.longValue()); |
148 |
}else if (type==DataTypes.INT){ |
149 |
return new Integer(value.intValue()); |
150 |
}else if (type==DataTypes.STRING){ |
151 |
return value.toString();
|
152 |
} |
153 |
return value;
|
154 |
} |
155 |
|
156 |
public void initializeValues() { |
157 |
FeatureType type = this.getType();
|
158 |
Iterator iterator = type.iterator();
|
159 |
|
160 |
while (iterator.hasNext()) {
|
161 |
FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator |
162 |
.next(); |
163 |
if (attribute.isAutomatic() || attribute.isReadOnly()
|
164 |
|| attribute.getEvaluator() != null) {
|
165 |
continue;
|
166 |
} |
167 |
if (attribute.getDefaultValue() == null && !attribute.allowNull()) { |
168 |
continue;
|
169 |
} |
170 |
this.set(attribute, attribute.getDefaultValue());
|
171 |
} |
172 |
} |
173 |
|
174 |
public void initializeValues(Feature feature) { |
175 |
FeatureType myType=this.getType();
|
176 |
FeatureType type =feature.getType(); |
177 |
Iterator iterator = type.iterator();
|
178 |
|
179 |
while (iterator.hasNext()) {
|
180 |
FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator |
181 |
.next(); |
182 |
FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName()); |
183 |
if (myAttribute != null) { |
184 |
this.set(myAttribute, feature.get(attribute.getIndex()));
|
185 |
} |
186 |
} |
187 |
} |
188 |
|
189 |
public FeatureStore getStore() {
|
190 |
return (FeatureStore) this.storeRef.get(); |
191 |
} |
192 |
|
193 |
public FeatureType getType() {
|
194 |
return this.data.getType(); |
195 |
} |
196 |
|
197 |
public EditableFeature getEditable() {
|
198 |
return new DefaultEditableFeature(this); |
199 |
} |
200 |
|
201 |
public Feature getCopy() {
|
202 |
return new DefaultFeature(this); |
203 |
} |
204 |
|
205 |
public FeatureReference getReference() {
|
206 |
if (this.reference == null) { |
207 |
this.reference = new DefaultFeatureReference(this); |
208 |
} |
209 |
return this.reference; |
210 |
} |
211 |
|
212 |
public void validate(int mode) { |
213 |
((DefaultFeatureType) this.data.getType()).validateFeature(this, mode); |
214 |
} |
215 |
|
216 |
public List getSRSs() { |
217 |
// TODO Auto-generated method stub
|
218 |
return null; |
219 |
} |
220 |
|
221 |
public Envelope getDefaultEnvelope() {
|
222 |
return this.data.getDefaultEnvelope(); |
223 |
} |
224 |
|
225 |
public Geometry getDefaultGeometry() {
|
226 |
return this.data.getDefaultGeometry(); |
227 |
} |
228 |
|
229 |
public IProjection getDefaultSRS() {
|
230 |
return this.data.getType().getDefaultSRS(); |
231 |
} |
232 |
|
233 |
public List getGeometries() { |
234 |
// TODO Auto-generated method stub
|
235 |
return null; |
236 |
} |
237 |
|
238 |
public Object get(String name) { |
239 |
return this.get(this.data.getType().getIndex(name)); |
240 |
} |
241 |
|
242 |
public Object get(int index) { |
243 |
if (!this.data.getType().hasEvaluators()) { |
244 |
return this.data.get(index); |
245 |
} |
246 |
FeatureAttributeDescriptor attribute = this.data.getType()
|
247 |
.getAttributeDescriptor(index); |
248 |
Evaluator eval = attribute.getEvaluator(); |
249 |
if (eval == null) { |
250 |
return this.data.get(index); |
251 |
} else {
|
252 |
Object value = this.data.get(index); |
253 |
if (value != null) { // FIXME: para comprobar si esta calculado usar |
254 |
// un array
|
255 |
// especifico.
|
256 |
return this.data.get(index); |
257 |
} |
258 |
try {
|
259 |
value = eval.evaluate(this);
|
260 |
} catch (EvaluatorException e) {
|
261 |
throw new DataEvaluatorRuntimeException(e); |
262 |
} |
263 |
this.data.set(index, value);
|
264 |
return value;
|
265 |
} |
266 |
} |
267 |
|
268 |
public Object[] getArray(String name) { |
269 |
return this.getArray(this.data.getType().getIndex(name)); |
270 |
} |
271 |
|
272 |
public Object[] getArray(int index) { |
273 |
return (Object[]) this.get(index); |
274 |
} |
275 |
|
276 |
public boolean getBoolean(String name) { |
277 |
return this.getBoolean(this.data.getType().getIndex(name)); |
278 |
} |
279 |
|
280 |
public boolean getBoolean(int index) { |
281 |
Boolean value = (Boolean) this.get(index); |
282 |
if (value == null) { |
283 |
return false; |
284 |
} |
285 |
return value.booleanValue();
|
286 |
} |
287 |
|
288 |
public byte getByte(String name) { |
289 |
return this.getByte(this.data.getType().getIndex(name)); |
290 |
} |
291 |
|
292 |
public byte getByte(int index) { |
293 |
Byte value = (Byte) this.get(index); |
294 |
if (value == null) { |
295 |
return 0; |
296 |
} |
297 |
return value.byteValue();
|
298 |
} |
299 |
|
300 |
public Date getDate(String name) { |
301 |
return this.getDate(this.data.getType().getIndex(name)); |
302 |
} |
303 |
|
304 |
public Date getDate(int index) { |
305 |
return (Date) this.get(index); |
306 |
} |
307 |
|
308 |
public double getDouble(String name) { |
309 |
return this.getDouble(this.data.getType().getIndex(name)); |
310 |
} |
311 |
|
312 |
public double getDouble(int index) { |
313 |
Double value = (Double) this.get(index); |
314 |
if (value == null) { |
315 |
return 0; |
316 |
} |
317 |
return value.doubleValue();
|
318 |
} |
319 |
|
320 |
public Feature getFeature(String name) { |
321 |
return this.getFeature(this.data.getType().getIndex(name)); |
322 |
} |
323 |
|
324 |
public Feature getFeature(int index) { |
325 |
return (Feature) this.get(index); |
326 |
} |
327 |
|
328 |
public float getFloat(String name) { |
329 |
return this.getFloat(this.data.getType().getIndex(name)); |
330 |
} |
331 |
|
332 |
public float getFloat(int index) { |
333 |
Float value = (Float) this.get(index); |
334 |
if (value == null) { |
335 |
return 0; |
336 |
} |
337 |
return value.floatValue();
|
338 |
} |
339 |
|
340 |
public Geometry getGeometry(String name) { |
341 |
return this.getGeometry(this.data.getType().getIndex(name)); |
342 |
} |
343 |
|
344 |
public Geometry getGeometry(int index) { |
345 |
return (Geometry) this.get(index); |
346 |
} |
347 |
|
348 |
public int getInt(String name) { |
349 |
return this.getInt(this.data.getType().getIndex(name)); |
350 |
} |
351 |
|
352 |
public int getInt(int index) { |
353 |
Integer value = (Integer) this.get(index); |
354 |
if (value == null) { |
355 |
return 0; |
356 |
} |
357 |
return value.intValue();
|
358 |
} |
359 |
|
360 |
public long getLong(String name) { |
361 |
return this.getLong(this.data.getType().getIndex(name)); |
362 |
} |
363 |
|
364 |
public long getLong(int index) { |
365 |
Long value = (Long) this.get(index); |
366 |
if (value == null) { |
367 |
return 0; |
368 |
} |
369 |
return value.longValue();
|
370 |
} |
371 |
|
372 |
public String getString(String name) { |
373 |
return this.getString(this.data.getType().getIndex(name)); |
374 |
} |
375 |
|
376 |
public String getString(int index) { |
377 |
return (String) this.get(index); |
378 |
} |
379 |
|
380 |
public Object getContextValue(String name) { |
381 |
name = name.toLowerCase(); |
382 |
if (name.equals("store")) { |
383 |
return this.getStore(); |
384 |
} |
385 |
|
386 |
if (name.equals("featuretype")) { |
387 |
return this.data.getType(); |
388 |
} |
389 |
|
390 |
if (name.equals("feature")) { |
391 |
return this; |
392 |
} |
393 |
|
394 |
throw new IllegalArgumentException(name); |
395 |
} |
396 |
|
397 |
public Iterator getDataNames() { |
398 |
class DataNamesIterator implements Iterator { |
399 |
Iterator attributeIteraror;
|
400 |
|
401 |
DataNamesIterator(DefaultFeature feature) { |
402 |
this.attributeIteraror = feature.getType().iterator();
|
403 |
} |
404 |
|
405 |
public boolean hasNext() { |
406 |
return this.attributeIteraror.hasNext(); |
407 |
} |
408 |
|
409 |
public Object next() { |
410 |
return ((FeatureAttributeDescriptor) this.attributeIteraror |
411 |
.next()).getName(); |
412 |
} |
413 |
|
414 |
public void remove() { |
415 |
throw new UnsupportedOperationException(); |
416 |
} |
417 |
|
418 |
} |
419 |
return new DataNamesIterator(this); |
420 |
} |
421 |
|
422 |
public Object getDataValue(String name) { |
423 |
name = name.toLowerCase(); |
424 |
return get(name);
|
425 |
} |
426 |
|
427 |
public Iterator getDataValues() { |
428 |
class DataValuesIterator implements Iterator { |
429 |
DefaultFeature feature; |
430 |
int current = 0; |
431 |
|
432 |
DataValuesIterator(DefaultFeature feature) { |
433 |
this.feature = feature;
|
434 |
} |
435 |
|
436 |
public boolean hasNext() { |
437 |
return current < feature.getType().size() - 1; |
438 |
} |
439 |
|
440 |
public Object next() { |
441 |
return feature.get(current++);
|
442 |
} |
443 |
|
444 |
public void remove() { |
445 |
throw new UnsupportedOperationException(); |
446 |
} |
447 |
|
448 |
} |
449 |
return new DataValuesIterator(this); |
450 |
} |
451 |
|
452 |
public boolean hasContextValue(String name) { |
453 |
name = name.toLowerCase(); |
454 |
if (name.equals("store")) { |
455 |
return true; |
456 |
} |
457 |
|
458 |
if (name.equals("featuretype")) { |
459 |
return true; |
460 |
} |
461 |
|
462 |
if (name.equals("feature")) { |
463 |
return true; |
464 |
} |
465 |
return false; |
466 |
} |
467 |
|
468 |
public boolean hasDataValue(String name) { |
469 |
name = name.toLowerCase(); |
470 |
return this.data.getType().getIndex(name) >= 0; |
471 |
} |
472 |
|
473 |
/**
|
474 |
* Start of DynField interface Implementation
|
475 |
* READONLY
|
476 |
*/
|
477 |
|
478 |
public void delegate(DynObject dynObject) { |
479 |
throw new UnsupportedOperationException(); |
480 |
} |
481 |
|
482 |
public DynClass getDynClass() {
|
483 |
return getType();
|
484 |
} |
485 |
|
486 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
487 |
return get(name);
|
488 |
} |
489 |
|
490 |
public boolean hasDynValue(String name) { |
491 |
if (data.getType().getIndex(name)<0) |
492 |
return false; |
493 |
return true; |
494 |
} |
495 |
|
496 |
public void implement(DynClass dynClass) { |
497 |
throw new UnsupportedOperationException(); |
498 |
|
499 |
} |
500 |
|
501 |
public Object invokeDynMethod(String name, DynObject context) |
502 |
throws DynMethodException {
|
503 |
throw new UnsupportedOperationException(); |
504 |
} |
505 |
|
506 |
public Object invokeDynMethod(int code, DynObject context) |
507 |
throws DynMethodException {
|
508 |
throw new UnsupportedOperationException(); |
509 |
} |
510 |
|
511 |
public void setDynValue(String name, Object value) |
512 |
throws DynFieldNotFoundException {
|
513 |
throw new UnsupportedOperationException(); |
514 |
|
515 |
} |
516 |
|
517 |
} |