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