svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / impl / DefaultFeature.java @ 40435
History | View | Annotate | Download (16.9 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() + "'.");
|
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.getName()+
|
150 |
" from '"+value.getClass().getName()+
|
151 |
"' with value '"+value.toString()+"'."); |
152 |
} |
153 |
} |
154 |
|
155 |
// private Object getNumberByType(Number value, int type) {
|
156 |
// if (type==DataTypes.DOUBLE){
|
157 |
// return new Double(value.doubleValue());
|
158 |
// }else if (type==DataTypes.FLOAT){
|
159 |
// return new Float(value.floatValue());
|
160 |
// }else if (type==DataTypes.LONG){
|
161 |
// return new Long(value.longValue());
|
162 |
// }else if (type==DataTypes.INT){
|
163 |
// return new Integer(value.intValue());
|
164 |
// }else if (type==DataTypes.STRING){
|
165 |
// return value.toString();
|
166 |
// }
|
167 |
// return value;
|
168 |
// }
|
169 |
|
170 |
public void initializeValues() { |
171 |
FeatureType type = this.getType();
|
172 |
Iterator iterator = type.iterator();
|
173 |
|
174 |
while (iterator.hasNext()) {
|
175 |
FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator |
176 |
.next(); |
177 |
if (attribute.isAutomatic() || attribute.isReadOnly()
|
178 |
|| attribute.getEvaluator() != null) {
|
179 |
continue;
|
180 |
} |
181 |
if (attribute.getDefaultValue() == null && !attribute.allowNull()) { |
182 |
continue;
|
183 |
} |
184 |
this.set(attribute, attribute.getDefaultValue());
|
185 |
} |
186 |
} |
187 |
|
188 |
public void clear() { |
189 |
initializeValues(); |
190 |
} |
191 |
|
192 |
public void initializeValues(Feature feature) { |
193 |
FeatureType myType=this.getType();
|
194 |
FeatureType type =feature.getType(); |
195 |
Iterator iterator = type.iterator();
|
196 |
|
197 |
while (iterator.hasNext()) {
|
198 |
FeatureAttributeDescriptor attribute = (FeatureAttributeDescriptor) iterator |
199 |
.next(); |
200 |
FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName()); |
201 |
if (myAttribute != null) { |
202 |
this.set(myAttribute, feature.get(attribute.getIndex()));
|
203 |
} |
204 |
} |
205 |
} |
206 |
|
207 |
|
208 |
public FeatureStore getStore() {
|
209 |
return (FeatureStore) this.storeRef.get(); |
210 |
} |
211 |
|
212 |
public FeatureType getType() {
|
213 |
return this.data.getType(); |
214 |
} |
215 |
|
216 |
public EditableFeature getEditable() {
|
217 |
return new DefaultEditableFeature(this); |
218 |
} |
219 |
|
220 |
public Feature getCopy() {
|
221 |
return new DefaultFeature(this); |
222 |
} |
223 |
|
224 |
public FeatureReference getReference() {
|
225 |
if (this.reference == null) { |
226 |
if (!isInserted()) {
|
227 |
return null; |
228 |
} |
229 |
reference = new DefaultFeatureReference(this); |
230 |
} |
231 |
return this.reference; |
232 |
} |
233 |
|
234 |
class UnableToGetReferenceException extends BaseRuntimeException { |
235 |
|
236 |
/**
|
237 |
*
|
238 |
*/
|
239 |
private static final long serialVersionUID = 1812805035204824163L; |
240 |
|
241 |
/**
|
242 |
* @param exception
|
243 |
*/
|
244 |
public UnableToGetReferenceException(BaseException exception) {
|
245 |
super("Unable to get reference", "_UnableToGetReferenceException", |
246 |
serialVersionUID); |
247 |
this.initCause(exception);
|
248 |
|
249 |
} |
250 |
|
251 |
} |
252 |
|
253 |
public void validate(int mode) { |
254 |
((DefaultFeatureType) this.data.getType()).validateFeature(this, mode); |
255 |
} |
256 |
|
257 |
public List getSRSs() { |
258 |
// TODO Auto-generated method stub
|
259 |
return null; |
260 |
} |
261 |
|
262 |
public Envelope getDefaultEnvelope() {
|
263 |
return this.data.getDefaultEnvelope(); |
264 |
} |
265 |
|
266 |
public Geometry getDefaultGeometry() {
|
267 |
return this.data.getDefaultGeometry(); |
268 |
} |
269 |
|
270 |
public IProjection getDefaultSRS() {
|
271 |
return this.data.getType().getDefaultSRS(); |
272 |
} |
273 |
|
274 |
public List getGeometries() { |
275 |
// TODO Auto-generated method stub
|
276 |
return null; |
277 |
} |
278 |
|
279 |
public Object get(String name) { |
280 |
int index = this.data.getType().getIndex(name); |
281 |
if( index < 0 ) { |
282 |
throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature."); |
283 |
} |
284 |
return this.get(index); |
285 |
} |
286 |
|
287 |
|
288 |
public Object get(int index) { |
289 |
FeatureType type = this.data.getType();
|
290 |
if( index <0 || index >= type.size() ) { |
291 |
throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+"."); |
292 |
} |
293 |
FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index); |
294 |
if (!this.data.getType().hasEvaluators()) { |
295 |
return get(attribute, this.data.get(index)); |
296 |
} |
297 |
Evaluator eval = attribute.getEvaluator(); |
298 |
if (eval == null) { |
299 |
return this.data.get(index); |
300 |
} else {
|
301 |
Object value = this.data.get(index); |
302 |
if (value != null) { // FIXME: para comprobar si esta calculado usar |
303 |
// un array
|
304 |
// especifico.
|
305 |
return get(attribute, this.data.get(index)); |
306 |
} |
307 |
try {
|
308 |
value = eval.evaluate(this);
|
309 |
} catch (EvaluatorException e) {
|
310 |
throw new DataEvaluatorRuntimeException(e); |
311 |
} |
312 |
this.data.set(index, value);
|
313 |
return get(attribute, value);
|
314 |
} |
315 |
} |
316 |
|
317 |
private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){ |
318 |
if (featureAttributeDescriptor.getFeatureAttributeGetter() == null){ |
319 |
return value;
|
320 |
}else{
|
321 |
return featureAttributeDescriptor.getFeatureAttributeGetter().getter(value);
|
322 |
} |
323 |
} |
324 |
|
325 |
public Object[] getArray(String name) { |
326 |
return this.getArray(this.data.getType().getIndex(name)); |
327 |
} |
328 |
|
329 |
public Object[] getArray(int index) { |
330 |
return (Object[]) this.get(index); |
331 |
} |
332 |
|
333 |
public boolean getBoolean(String name) { |
334 |
return this.getBoolean(this.data.getType().getIndex(name)); |
335 |
} |
336 |
|
337 |
public boolean getBoolean(int index) { |
338 |
Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN)); |
339 |
if (value == null) { |
340 |
return false; |
341 |
} |
342 |
return value.booleanValue();
|
343 |
} |
344 |
|
345 |
public byte getByte(String name) { |
346 |
return this.getByte(this.data.getType().getIndex(name)); |
347 |
} |
348 |
|
349 |
public byte getByte(int index) { |
350 |
Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE)); |
351 |
if (value == null) { |
352 |
return 0; |
353 |
} |
354 |
return value.byteValue();
|
355 |
} |
356 |
|
357 |
public Date getDate(String name) { |
358 |
return this.getDate(this.data.getType().getIndex(name)); |
359 |
} |
360 |
|
361 |
public Date getDate(int index) { |
362 |
Date value = ((Date) this.get(index,Date.class,DataTypes.DATE)); |
363 |
|
364 |
return value;
|
365 |
} |
366 |
|
367 |
public double getDouble(String name) { |
368 |
return this.getDouble(this.data.getType().getIndex(name)); |
369 |
} |
370 |
|
371 |
public double getDouble(int index) { |
372 |
|
373 |
Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE)); |
374 |
if (value == null) { |
375 |
return 0; |
376 |
} |
377 |
return value.doubleValue();
|
378 |
} |
379 |
|
380 |
public Feature getFeature(String name) { |
381 |
return this.getFeature(this.data.getType().getIndex(name)); |
382 |
} |
383 |
|
384 |
public Feature getFeature(int index) { |
385 |
return (Feature) this.get(index); |
386 |
} |
387 |
|
388 |
public float getFloat(String name) { |
389 |
return this.getFloat(this.data.getType().getIndex(name)); |
390 |
} |
391 |
|
392 |
public float getFloat(int index) { |
393 |
Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT)); |
394 |
if (value == null) { |
395 |
return 0; |
396 |
} |
397 |
return value.floatValue();
|
398 |
} |
399 |
|
400 |
public Geometry getGeometry(String name) { |
401 |
return this.getGeometry(this.data.getType().getIndex(name)); |
402 |
} |
403 |
|
404 |
public Geometry getGeometry(int index) { |
405 |
return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY); |
406 |
} |
407 |
|
408 |
public int getInt(String name) { |
409 |
return this.getInt(this.data.getType().getIndex(name)); |
410 |
} |
411 |
|
412 |
public int getInt(int index) { |
413 |
Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT)); |
414 |
if (value == null) { |
415 |
return 0; |
416 |
} |
417 |
return ((Integer)value).intValue(); |
418 |
} |
419 |
|
420 |
public long getLong(String name) { |
421 |
return this.getLong(this.data.getType().getIndex(name)); |
422 |
} |
423 |
|
424 |
public long getLong(int index) { |
425 |
Long value = ((Long) this.get(index,Long.class,DataTypes.LONG)); |
426 |
if (value == null) { |
427 |
return 0; |
428 |
} |
429 |
return value.longValue();
|
430 |
} |
431 |
|
432 |
public String getString(String name) { |
433 |
return this.getString(this.data.getType().getIndex(name)); |
434 |
} |
435 |
|
436 |
public String getString(int index) { |
437 |
return (String) this.get(index,String.class,DataTypes.STRING); |
438 |
} |
439 |
|
440 |
public Object getContextValue(String name) { |
441 |
name = name.toLowerCase(); |
442 |
if (name.equals("store")) { |
443 |
return this.getStore(); |
444 |
} |
445 |
|
446 |
if (name.equals("featuretype")) { |
447 |
return this.data.getType(); |
448 |
} |
449 |
|
450 |
if (name.equals("feature")) { |
451 |
return this; |
452 |
} |
453 |
|
454 |
throw new IllegalArgumentException(name); |
455 |
} |
456 |
|
457 |
public Iterator getDataNames() { |
458 |
class DataNamesIterator implements Iterator { |
459 |
Iterator attributeIteraror;
|
460 |
|
461 |
DataNamesIterator(DefaultFeature feature) { |
462 |
this.attributeIteraror = feature.getType().iterator();
|
463 |
} |
464 |
|
465 |
public boolean hasNext() { |
466 |
return this.attributeIteraror.hasNext(); |
467 |
} |
468 |
|
469 |
public Object next() { |
470 |
return ((FeatureAttributeDescriptor) this.attributeIteraror |
471 |
.next()).getName(); |
472 |
} |
473 |
|
474 |
public void remove() { |
475 |
throw new UnsupportedOperationException(); |
476 |
} |
477 |
|
478 |
} |
479 |
return new DataNamesIterator(this); |
480 |
} |
481 |
|
482 |
public Object getDataValue(String name) { |
483 |
name = name.toLowerCase(); |
484 |
return get(name);
|
485 |
} |
486 |
|
487 |
public Iterator getDataValues() { |
488 |
class DataValuesIterator implements Iterator { |
489 |
DefaultFeature feature; |
490 |
int current = 0; |
491 |
|
492 |
DataValuesIterator(DefaultFeature feature) { |
493 |
this.feature = feature;
|
494 |
} |
495 |
|
496 |
public boolean hasNext() { |
497 |
return current < feature.getType().size() - 1; |
498 |
} |
499 |
|
500 |
public Object next() { |
501 |
return feature.get(current++);
|
502 |
} |
503 |
|
504 |
public void remove() { |
505 |
throw new UnsupportedOperationException(); |
506 |
} |
507 |
|
508 |
} |
509 |
return new DataValuesIterator(this); |
510 |
} |
511 |
|
512 |
public boolean hasContextValue(String name) { |
513 |
name = name.toLowerCase(); |
514 |
if (name.equals("store")) { |
515 |
return true; |
516 |
} |
517 |
|
518 |
if (name.equals("featuretype")) { |
519 |
return true; |
520 |
} |
521 |
|
522 |
if (name.equals("feature")) { |
523 |
return true; |
524 |
} |
525 |
return false; |
526 |
} |
527 |
|
528 |
public boolean hasDataValue(String name) { |
529 |
name = name.toLowerCase(); |
530 |
return this.data.getType().getIndex(name) >= 0; |
531 |
} |
532 |
|
533 |
public Instant getInstant(int index) { |
534 |
return ((Instant) this.get(index,Date.class,DataTypes.INSTANT)); |
535 |
} |
536 |
|
537 |
public Instant getInstant(String name) { |
538 |
return this.getInstant(this.data.getType().getIndex(name)); |
539 |
} |
540 |
|
541 |
public Interval getInterval(int index) { |
542 |
return ((Interval) this.get(index,Date.class,DataTypes.INTERVAL)); |
543 |
} |
544 |
|
545 |
public Interval getInterval(String name) { |
546 |
return this.getInterval(this.data.getType().getIndex(name)); |
547 |
} |
548 |
|
549 |
public DynObject getAsDynObject() {
|
550 |
DynObjectFeatureFacade facade = new DynObjectFeatureFacade();
|
551 |
facade.setFeature(this);
|
552 |
return facade;
|
553 |
} |
554 |
|
555 |
public String toString() { |
556 |
StringBuffer buffer = new StringBuffer("Feature with values: "); |
557 |
FeatureAttributeDescriptor[] attributeDescriptors =
|
558 |
getType().getAttributeDescriptors(); |
559 |
for (int i = 0; i < attributeDescriptors.length; i++) { |
560 |
String name = attributeDescriptors[i].getName();
|
561 |
buffer.append(name).append("=").append(get(name));
|
562 |
if (i < attributeDescriptors.length - 1) { |
563 |
buffer.append(", ");
|
564 |
} |
565 |
} |
566 |
return super.toString(); |
567 |
} |
568 |
|
569 |
|
570 |
/**
|
571 |
* @return the inserted
|
572 |
*/
|
573 |
public boolean isInserted() { |
574 |
return inserted;
|
575 |
} |
576 |
|
577 |
|
578 |
/**
|
579 |
* @param inserted the inserted to set
|
580 |
*/
|
581 |
public void setInserted(boolean inserted) { |
582 |
this.inserted = inserted;
|
583 |
} |
584 |
|
585 |
public EvaluatorData getEvaluatorData() {
|
586 |
return this; |
587 |
} |
588 |
} |