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