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 @ 44297
History | View | Annotate | Download (26.4 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.feature.impl; |
25 | |||
26 | import java.lang.ref.WeakReference; |
||
27 | 43550 | jjdelcerro | import java.util.ArrayList; |
28 | 40435 | jjdelcerro | import java.util.Date; |
29 | import java.util.Iterator; |
||
30 | import java.util.List; |
||
31 | 43550 | jjdelcerro | import java.util.Map; |
32 | 44128 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
33 | 40435 | jjdelcerro | import org.cresques.cts.IProjection; |
34 | 44128 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
35 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.DataTypes; |
36 | import org.gvsig.fmap.dal.exception.DataEvaluatorRuntimeException; |
||
37 | import org.gvsig.fmap.dal.exception.DataException; |
||
38 | 44128 | jjdelcerro | import org.gvsig.fmap.dal.feature.DataProfile; |
39 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeature; |
40 | import org.gvsig.fmap.dal.feature.Feature; |
||
41 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
42 | 41335 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
43 | import org.gvsig.fmap.dal.feature.FeatureAttributeGetter; |
||
44 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureReference; |
45 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
46 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
47 | import org.gvsig.fmap.dal.feature.exception.IllegalValueException; |
||
48 | import org.gvsig.fmap.dal.feature.exception.SetReadOnlyAttributeException; |
||
49 | 42775 | jjdelcerro | import org.gvsig.fmap.dal.feature.impl.dynobjectutils.DynObjectFeatureFacade; |
50 | 43729 | fdiaz | import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider; |
51 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
52 | import org.gvsig.fmap.geom.Geometry; |
||
53 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
54 | import org.gvsig.timesupport.Instant; |
||
55 | import org.gvsig.timesupport.Interval; |
||
56 | 44086 | jjdelcerro | import org.gvsig.timesupport.Time; |
57 | 40435 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
58 | import org.gvsig.tools.dataTypes.CoercionException; |
||
59 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
60 | import org.gvsig.tools.dynobject.DynObject; |
||
61 | import org.gvsig.tools.evaluator.Evaluator; |
||
62 | import org.gvsig.tools.evaluator.EvaluatorData; |
||
63 | import org.gvsig.tools.evaluator.EvaluatorException; |
||
64 | import org.gvsig.tools.exception.BaseException; |
||
65 | import org.gvsig.tools.exception.BaseRuntimeException; |
||
66 | 40876 | jjdelcerro | import org.gvsig.tools.lang.Cloneable; |
67 | 40435 | jjdelcerro | |
68 | 40876 | jjdelcerro | public class DefaultFeature implements Feature, EvaluatorData, Cloneable { |
69 | 40435 | jjdelcerro | |
70 | 43729 | fdiaz | private static DataTypesManager dataTypesManager = null; |
71 | 40435 | jjdelcerro | protected FeatureProvider data;
|
72 | protected FeatureReference reference;
|
||
73 | private WeakReference storeRef; |
||
74 | 43729 | fdiaz | |
75 | 40435 | jjdelcerro | private boolean inserted = false; |
76 | |||
77 | /*
|
||
78 | * Usar con mucha precaucion o mejor no usar. Lo precisa el
|
||
79 | * DefaultFeatureSet en la ordenacion.
|
||
80 | */
|
||
81 | public DefaultFeature(FeatureStore store) {
|
||
82 | this.storeRef = new WeakReference(store); |
||
83 | this.reference = null; |
||
84 | } |
||
85 | |||
86 | public DefaultFeature(FeatureStore store, FeatureProvider data) {
|
||
87 | this.data = data;
|
||
88 | this.storeRef = new WeakReference(store); |
||
89 | this.reference = null; |
||
90 | this.inserted = !data.isNew();
|
||
91 | } |
||
92 | |||
93 | DefaultFeature(DefaultFeature feature) { |
||
94 | this.data = feature.data.getCopy();
|
||
95 | this.storeRef = feature.storeRef;
|
||
96 | this.reference = feature.reference;
|
||
97 | this.inserted = feature.isInserted();
|
||
98 | } |
||
99 | |||
100 | 43729 | fdiaz | public DefaultFeature(FeatureType targetType, Feature sourceFeature) {
|
101 | DefaultFeature defaultFeature = (DefaultFeature)sourceFeature; |
||
102 | this.data = new DefaultFeatureProvider(targetType, (DefaultFeatureProvider)defaultFeature.getData()); |
||
103 | this.storeRef = defaultFeature.storeRef;
|
||
104 | this.reference = defaultFeature.reference;
|
||
105 | this.inserted = defaultFeature.isInserted();
|
||
106 | |||
107 | FeatureType sourceType = sourceFeature.getType(); |
||
108 | |||
109 | for (FeatureAttributeDescriptor targetAttrDescriptor : targetType) {
|
||
110 | if ( targetAttrDescriptor.isAutomatic() ||
|
||
111 | targetAttrDescriptor.isReadOnly() || |
||
112 | targetAttrDescriptor.getEvaluator() != null) {
|
||
113 | continue;
|
||
114 | } |
||
115 | int sourceIndex = sourceType.getIndex(targetAttrDescriptor.getName());
|
||
116 | if (sourceIndex<0){ |
||
117 | continue;
|
||
118 | } |
||
119 | Object value = sourceFeature.get(sourceIndex);
|
||
120 | if (value == null && !targetAttrDescriptor.allowNull()) { |
||
121 | continue;
|
||
122 | } |
||
123 | this.set(targetAttrDescriptor,value);
|
||
124 | } |
||
125 | } |
||
126 | |||
127 | 40435 | jjdelcerro | public void setData(FeatureProvider data) { |
128 | this.data = data;
|
||
129 | this.reference = null; |
||
130 | 43729 | fdiaz | this.inserted = true; |
131 | 40435 | jjdelcerro | } |
132 | |||
133 | public FeatureProvider getData() {
|
||
134 | return this.data; |
||
135 | } |
||
136 | |||
137 | protected DataTypesManager getDataTypesManager() {
|
||
138 | if( dataTypesManager==null ) { |
||
139 | dataTypesManager = ToolsLocator.getDataTypesManager(); |
||
140 | } |
||
141 | return dataTypesManager;
|
||
142 | } |
||
143 | |||
144 | 41335 | jjdelcerro | void set(FeatureAttributeDescriptor attribute, Object value) { |
145 | int i = attribute.getIndex();
|
||
146 | 40435 | jjdelcerro | |
147 | 41335 | jjdelcerro | if ( attribute.isReadOnly() ) {
|
148 | 41823 | jjdelcerro | throw new SetReadOnlyAttributeException(attribute.getName(), this.getType()); |
149 | 41335 | jjdelcerro | } |
150 | FeatureAttributeEmulator emulator = attribute.getFeatureAttributeEmulator(); |
||
151 | if( emulator!= null ) { |
||
152 | emulator.set((EditableFeature) this,value);
|
||
153 | return;
|
||
154 | } |
||
155 | 43729 | fdiaz | |
156 | 41335 | jjdelcerro | if ( value == null ) { |
157 | if ( !attribute.allowNull() ) {
|
||
158 | if ( !attribute.isAutomatic() ) {
|
||
159 | throw new IllegalValueException(attribute, value); |
||
160 | } |
||
161 | } |
||
162 | this.data.set(i, null); |
||
163 | return;
|
||
164 | 40435 | jjdelcerro | |
165 | 41335 | jjdelcerro | } |
166 | 40435 | jjdelcerro | |
167 | 41335 | jjdelcerro | if ( attribute.getFeatureAttributeGetter() != null ) { |
168 | value = attribute.getFeatureAttributeGetter().setter(value); |
||
169 | 40435 | jjdelcerro | } |
170 | 41335 | jjdelcerro | |
171 | 42218 | jjdelcerro | Class objectClass = attribute.getObjectClass();
|
172 | if( objectClass!=null ) { |
||
173 | if ( objectClass.isInstance(value) ) {
|
||
174 | this.data.set(i, value);
|
||
175 | return;
|
||
176 | } |
||
177 | 40435 | jjdelcerro | |
178 | 42218 | jjdelcerro | if ( !objectClass.isInstance(value) ) {
|
179 | try {
|
||
180 | value |
||
181 | = this.getDataTypesManager().coerce(attribute.getType(),
|
||
182 | value); |
||
183 | } catch (CoercionException e) {
|
||
184 | throw new IllegalArgumentException("Can't convert to " |
||
185 | + this.getDataTypesManager().getTypeName(
|
||
186 | attribute.getType()) + " from '"
|
||
187 | + value.getClass().getName() + "' with value '"
|
||
188 | + value.toString() + "'.");
|
||
189 | } |
||
190 | 40435 | jjdelcerro | } |
191 | 41335 | jjdelcerro | } |
192 | 40435 | jjdelcerro | this.data.set(i, value);
|
193 | } |
||
194 | 43729 | fdiaz | |
195 | 40435 | jjdelcerro | private Object get(int index,Class theClass, int type) { |
196 | Object value = this.get(index); |
||
197 | if( theClass.isInstance(value) ) {
|
||
198 | return value;
|
||
199 | } |
||
200 | try {
|
||
201 | return this.getDataTypesManager().coerce(type, value); |
||
202 | } catch (CoercionException e) {
|
||
203 | 43729 | fdiaz | |
204 | 40435 | jjdelcerro | if (value == null) { |
205 | return null; |
||
206 | } |
||
207 | throw new IllegalArgumentException( |
||
208 | "Can't convert to "+theClass.getName()+
|
||
209 | " from '"+value.getClass().getName()+
|
||
210 | "' with value '"+value.toString()+"'."); |
||
211 | } |
||
212 | } |
||
213 | 43729 | fdiaz | |
214 | 40435 | jjdelcerro | public void initializeValues() { |
215 | FeatureType type = this.getType();
|
||
216 | 44297 | jjdelcerro | for (FeatureAttributeDescriptor attribute : type) {
|
217 | 40435 | jjdelcerro | if (attribute.isAutomatic() || attribute.isReadOnly()
|
218 | 44203 | jjdelcerro | || attribute.isComputed() ) { |
219 | 40435 | jjdelcerro | continue;
|
220 | } |
||
221 | if (attribute.getDefaultValue() == null && !attribute.allowNull()) { |
||
222 | continue;
|
||
223 | } |
||
224 | this.set(attribute, attribute.getDefaultValue());
|
||
225 | } |
||
226 | } |
||
227 | |||
228 | public void clear() { |
||
229 | initializeValues(); |
||
230 | } |
||
231 | |||
232 | public void initializeValues(Feature feature) { |
||
233 | FeatureType myType=this.getType();
|
||
234 | FeatureType type =feature.getType(); |
||
235 | 44297 | jjdelcerro | for (FeatureAttributeDescriptor attribute : type) {
|
236 | 40435 | jjdelcerro | FeatureAttributeDescriptor myAttribute=myType.getAttributeDescriptor(attribute.getName()); |
237 | if (myAttribute != null) { |
||
238 | this.set(myAttribute, feature.get(attribute.getIndex()));
|
||
239 | } |
||
240 | } |
||
241 | } |
||
242 | |||
243 | |||
244 | 44297 | jjdelcerro | @Override
|
245 | 40435 | jjdelcerro | public FeatureStore getStore() {
|
246 | return (FeatureStore) this.storeRef.get(); |
||
247 | } |
||
248 | |||
249 | 44297 | jjdelcerro | @Override
|
250 | 40435 | jjdelcerro | public FeatureType getType() {
|
251 | return this.data.getType(); |
||
252 | } |
||
253 | |||
254 | 44297 | jjdelcerro | @Override
|
255 | 40435 | jjdelcerro | public EditableFeature getEditable() {
|
256 | return new DefaultEditableFeature(this); |
||
257 | } |
||
258 | |||
259 | 44297 | jjdelcerro | @Override
|
260 | 40435 | jjdelcerro | public Feature getCopy() {
|
261 | return new DefaultFeature(this); |
||
262 | } |
||
263 | 43729 | fdiaz | |
264 | 44297 | jjdelcerro | @Override
|
265 | @SuppressWarnings("CloneDoesntCallSuperClone") |
||
266 | 40876 | jjdelcerro | public Object clone() throws CloneNotSupportedException { |
267 | return new DefaultFeature(this); |
||
268 | } |
||
269 | 40435 | jjdelcerro | |
270 | 44297 | jjdelcerro | @Override
|
271 | 40435 | jjdelcerro | public FeatureReference getReference() {
|
272 | if (this.reference == null) { |
||
273 | if (!isInserted()) {
|
||
274 | return null; |
||
275 | } |
||
276 | reference = new DefaultFeatureReference(this); |
||
277 | } |
||
278 | return this.reference; |
||
279 | } |
||
280 | |||
281 | class UnableToGetReferenceException extends BaseRuntimeException { |
||
282 | |||
283 | /**
|
||
284 | 43729 | fdiaz | *
|
285 | 40435 | jjdelcerro | */
|
286 | private static final long serialVersionUID = 1812805035204824163L; |
||
287 | |||
288 | /**
|
||
289 | * @param exception
|
||
290 | */
|
||
291 | public UnableToGetReferenceException(BaseException exception) {
|
||
292 | super("Unable to get reference", "_UnableToGetReferenceException", |
||
293 | serialVersionUID); |
||
294 | this.initCause(exception);
|
||
295 | 43729 | fdiaz | |
296 | 40435 | jjdelcerro | } |
297 | 43729 | fdiaz | |
298 | 40435 | jjdelcerro | } |
299 | 43729 | fdiaz | |
300 | 44297 | jjdelcerro | @Override
|
301 | 41251 | jjdelcerro | public void validate(int mode) throws DataException { |
302 | 40435 | jjdelcerro | ((DefaultFeatureType) this.data.getType()).validateFeature(this, mode); |
303 | } |
||
304 | |||
305 | 44297 | jjdelcerro | @Override
|
306 | 40435 | jjdelcerro | public List getSRSs() { |
307 | // TODO Auto-generated method stub
|
||
308 | return null; |
||
309 | } |
||
310 | |||
311 | 44297 | jjdelcerro | @Override
|
312 | 40435 | jjdelcerro | public Envelope getDefaultEnvelope() {
|
313 | 41342 | jjdelcerro | Envelope envelope = this.data.getDefaultEnvelope();
|
314 | if( envelope == null ) { |
||
315 | Geometry geom = this.getDefaultGeometry();
|
||
316 | 41348 | jjdelcerro | if( geom!=null ) { |
317 | envelope = geom.getEnvelope(); |
||
318 | } |
||
319 | 41342 | jjdelcerro | } |
320 | return envelope;
|
||
321 | 40435 | jjdelcerro | } |
322 | |||
323 | 44022 | jjdelcerro | @Override
|
324 | 40435 | jjdelcerro | public Geometry getDefaultGeometry() {
|
325 | 41058 | jjdelcerro | Geometry geom = this.data.getDefaultGeometry();
|
326 | 44022 | jjdelcerro | if( geom == null ) { |
327 | int i = this.data.getType().getDefaultGeometryAttributeIndex(); |
||
328 | Object x = this.get(i); |
||
329 | if( x instanceof Geometry ) { |
||
330 | geom = (Geometry) x; |
||
331 | } else {
|
||
332 | geom = this.getGeometry(i);
|
||
333 | } |
||
334 | 43420 | jjdelcerro | } |
335 | 44022 | jjdelcerro | if( geom != null ) { |
336 | if( geom.getProjection()==null ) { |
||
337 | FeatureType type = this.getType();
|
||
338 | DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) type.get(type.getDefaultGeometryAttributeIndex()); |
||
339 | IProjection proj = attrdesc.getSRS(this.storeRef);
|
||
340 | geom.setProjection(proj); |
||
341 | } |
||
342 | } |
||
343 | return geom;
|
||
344 | 40435 | jjdelcerro | } |
345 | |||
346 | 44086 | jjdelcerro | @Override
|
347 | public Time getDefaultTime() { |
||
348 | Time time = this.data.getDefaultTime(); |
||
349 | if( time == null ) { |
||
350 | int i = this.data.getType().getDefaultTimeAttributeIndex(); |
||
351 | Object x = this.get(i); |
||
352 | if( x instanceof Time ) { |
||
353 | time = (Time) x;
|
||
354 | } else {
|
||
355 | time = this.getTime(i);
|
||
356 | } |
||
357 | } |
||
358 | return time;
|
||
359 | } |
||
360 | |||
361 | 44297 | jjdelcerro | @Override
|
362 | 40435 | jjdelcerro | public IProjection getDefaultSRS() {
|
363 | 44022 | jjdelcerro | IProjection srs = this.data.getType().getDefaultSRS();
|
364 | if( srs == null ) { |
||
365 | FeatureType type = this.getType();
|
||
366 | DefaultFeatureAttributeDescriptor attrdesc = (DefaultFeatureAttributeDescriptor) type.get(type.getDefaultGeometryAttributeIndex()); |
||
367 | srs = attrdesc.getSRS(this.storeRef);
|
||
368 | } |
||
369 | return srs;
|
||
370 | 40435 | jjdelcerro | } |
371 | |||
372 | 44297 | jjdelcerro | @Override
|
373 | 40435 | jjdelcerro | public List getGeometries() { |
374 | // TODO Auto-generated method stub
|
||
375 | return null; |
||
376 | } |
||
377 | |||
378 | 44128 | jjdelcerro | @Override
|
379 | public Object getFromProfile(int index) { |
||
380 | FeatureAttributeDescriptor descriptor = this.data.getType().getAttributeDescriptor(index);
|
||
381 | Object value = this.get(index); |
||
382 | String profileName = descriptor.getDataProfileName();
|
||
383 | if( StringUtils.isBlank(profileName) ) {
|
||
384 | return value;
|
||
385 | } |
||
386 | DataProfile profile = DALLocator.getDataManager().getDataProfile(profileName); |
||
387 | if( profile==null ) { |
||
388 | return value;
|
||
389 | } |
||
390 | 44253 | jjdelcerro | return profile.createData(value, descriptor.getTags());
|
391 | 44128 | jjdelcerro | } |
392 | |||
393 | @Override
|
||
394 | public Object getFromProfile(String name) { |
||
395 | FeatureAttributeDescriptor descriptor = this.data.getType().getAttributeDescriptor(name);
|
||
396 | return this.getFromProfile(descriptor.getIndex()); |
||
397 | } |
||
398 | |||
399 | 44297 | jjdelcerro | @Override
|
400 | 40435 | jjdelcerro | public Object get(String name) { |
401 | int index = this.data.getType().getIndex(name); |
||
402 | if( index < 0 ) { |
||
403 | throw new IllegalArgumentException("Attribute name '"+name+"' not found in the feature."); |
||
404 | } |
||
405 | return this.get(index); |
||
406 | } |
||
407 | 43729 | fdiaz | |
408 | 43550 | jjdelcerro | public boolean has_key(String key) { |
409 | Object x = this.getType().get(key); |
||
410 | return x != null; |
||
411 | } |
||
412 | 43729 | fdiaz | |
413 | 43550 | jjdelcerro | public List<String> keys() { |
414 | List<String> ks = new ArrayList<>(); |
||
415 | for( FeatureAttributeDescriptor attr : this.getType()) { |
||
416 | ks.add(attr.getName()); |
||
417 | } |
||
418 | return ks;
|
||
419 | } |
||
420 | 43729 | fdiaz | |
421 | 43550 | jjdelcerro | public Iterator<String> iterkeys() { |
422 | final Iterator it = this.getType().iterator(); |
||
423 | return new Iterator<String>() { |
||
424 | @Override
|
||
425 | public boolean hasNext() { |
||
426 | return it.hasNext();
|
||
427 | } |
||
428 | 40435 | jjdelcerro | |
429 | 43550 | jjdelcerro | @Override
|
430 | public String next() { |
||
431 | return ((FeatureAttributeDescriptor)it.next()).getName();
|
||
432 | } |
||
433 | 43561 | jjdelcerro | |
434 | @Override
|
||
435 | public void remove() { |
||
436 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
437 | } |
||
438 | 43550 | jjdelcerro | }; |
439 | } |
||
440 | 40435 | jjdelcerro | |
441 | 43550 | jjdelcerro | public Iterator iteritems() { |
442 | final Iterator it = this.getType().iterator(); |
||
443 | return new Iterator<Map.Entry>() { |
||
444 | @Override
|
||
445 | public boolean hasNext() { |
||
446 | return it.hasNext();
|
||
447 | } |
||
448 | |||
449 | @Override
|
||
450 | public Map.Entry next() { |
||
451 | final String name = ((FeatureAttributeDescriptor)it.next()).getName(); |
||
452 | return new Map.Entry<String, Object>() { |
||
453 | @Override
|
||
454 | public String getKey() { |
||
455 | return name;
|
||
456 | } |
||
457 | |||
458 | @Override
|
||
459 | public Object getValue() { |
||
460 | return get(name);
|
||
461 | } |
||
462 | |||
463 | @Override
|
||
464 | public Object setValue(Object value) { |
||
465 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
466 | } |
||
467 | 43729 | fdiaz | |
468 | 43550 | jjdelcerro | }; |
469 | } |
||
470 | 43561 | jjdelcerro | |
471 | @Override
|
||
472 | public void remove() { |
||
473 | throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
||
474 | } |
||
475 | 43729 | fdiaz | }; |
476 | 43550 | jjdelcerro | } |
477 | |||
478 | @Override
|
||
479 | 40435 | jjdelcerro | public Object get(int index) { |
480 | FeatureType type = this.data.getType();
|
||
481 | if( index <0 || index >= type.size() ) { |
||
482 | throw new IllegalArgumentException("Attribute index '"+index+"' out of range (0 to "+this.data.getType().size()+"."); |
||
483 | } |
||
484 | FeatureAttributeDescriptor attribute = type.getAttributeDescriptor(index); |
||
485 | if (!this.data.getType().hasEvaluators()) { |
||
486 | 43729 | fdiaz | return get(attribute, this.data.get(index)); |
487 | } |
||
488 | 40435 | jjdelcerro | Evaluator eval = attribute.getEvaluator(); |
489 | if (eval == null) { |
||
490 | return this.data.get(index); |
||
491 | } else {
|
||
492 | Object value = this.data.get(index); |
||
493 | if (value != null) { // FIXME: para comprobar si esta calculado usar |
||
494 | // un array
|
||
495 | // especifico.
|
||
496 | return get(attribute, this.data.get(index)); |
||
497 | } |
||
498 | try {
|
||
499 | value = eval.evaluate(this);
|
||
500 | } catch (EvaluatorException e) {
|
||
501 | throw new DataEvaluatorRuntimeException(e); |
||
502 | } |
||
503 | this.data.set(index, value);
|
||
504 | return get(attribute, value);
|
||
505 | 43729 | fdiaz | } |
506 | 40435 | jjdelcerro | } |
507 | |||
508 | private Object get(FeatureAttributeDescriptor featureAttributeDescriptor, Object value){ |
||
509 | 41335 | jjdelcerro | FeatureAttributeEmulator emulator = featureAttributeDescriptor.getFeatureAttributeEmulator(); |
510 | if( emulator != null ) { |
||
511 | 44226 | jjdelcerro | // int index = featureAttributeDescriptor.getIndex();
|
512 | // value = this.data.get(index);
|
||
513 | // if( value==null ) {
|
||
514 | 44203 | jjdelcerro | value = emulator.get(this);
|
515 | 44226 | jjdelcerro | // this.data.set(index,value);
|
516 | // }
|
||
517 | 44022 | jjdelcerro | } else {
|
518 | FeatureAttributeGetter getter = featureAttributeDescriptor.getFeatureAttributeGetter(); |
||
519 | if( getter != null ) { |
||
520 | value = getter.getter(value); |
||
521 | } |
||
522 | 40435 | jjdelcerro | } |
523 | 44022 | jjdelcerro | if( featureAttributeDescriptor.getType()==DataTypes.GEOMETRY ) {
|
524 | if( value != null ) { |
||
525 | Geometry geom = (Geometry)value; |
||
526 | if( geom.getProjection()==null ) { |
||
527 | IProjection proj = ((DefaultFeatureAttributeDescriptor)featureAttributeDescriptor).getSRS(this.storeRef);
|
||
528 | geom.setProjection(proj); |
||
529 | } |
||
530 | } |
||
531 | 41335 | jjdelcerro | } |
532 | return value;
|
||
533 | 40435 | jjdelcerro | } |
534 | |||
535 | 44297 | jjdelcerro | @Override
|
536 | public byte[] getByteArray(String name) { |
||
537 | return this.getByteArray(this.data.getType().getIndex(name)); |
||
538 | } |
||
539 | |||
540 | @Override
|
||
541 | public byte[] getByteArray(int index) { |
||
542 | return (byte[]) this.get(index); |
||
543 | } |
||
544 | |||
545 | @Override
|
||
546 | 40435 | jjdelcerro | public Object[] getArray(String name) { |
547 | return this.getArray(this.data.getType().getIndex(name)); |
||
548 | } |
||
549 | |||
550 | 44297 | jjdelcerro | @Override
|
551 | 40435 | jjdelcerro | public Object[] getArray(int index) { |
552 | return (Object[]) this.get(index); |
||
553 | } |
||
554 | |||
555 | 44297 | jjdelcerro | @Override
|
556 | 40435 | jjdelcerro | public boolean getBoolean(String name) { |
557 | return this.getBoolean(this.data.getType().getIndex(name)); |
||
558 | } |
||
559 | |||
560 | 44297 | jjdelcerro | @Override
|
561 | 40435 | jjdelcerro | public boolean getBoolean(int index) { |
562 | Boolean value = ((Boolean) this.get(index,Boolean.class,DataTypes.BOOLEAN)); |
||
563 | if (value == null) { |
||
564 | return false; |
||
565 | } |
||
566 | 44297 | jjdelcerro | return value;
|
567 | 40435 | jjdelcerro | } |
568 | |||
569 | 44297 | jjdelcerro | @Override
|
570 | 40435 | jjdelcerro | public byte getByte(String name) { |
571 | return this.getByte(this.data.getType().getIndex(name)); |
||
572 | } |
||
573 | |||
574 | 44297 | jjdelcerro | @Override
|
575 | 40435 | jjdelcerro | public byte getByte(int index) { |
576 | Byte value = ((Byte) this.get(index,Byte.class,DataTypes.BYTE)); |
||
577 | if (value == null) { |
||
578 | return 0; |
||
579 | } |
||
580 | 44297 | jjdelcerro | return value;
|
581 | 40435 | jjdelcerro | } |
582 | |||
583 | 44297 | jjdelcerro | @Override
|
584 | 40435 | jjdelcerro | public Date getDate(String name) { |
585 | return this.getDate(this.data.getType().getIndex(name)); |
||
586 | } |
||
587 | |||
588 | 44297 | jjdelcerro | @Override
|
589 | 40435 | jjdelcerro | public Date getDate(int index) { |
590 | Date value = ((Date) this.get(index,Date.class,DataTypes.DATE)); |
||
591 | |||
592 | return value;
|
||
593 | } |
||
594 | |||
595 | 44297 | jjdelcerro | @Override
|
596 | 40435 | jjdelcerro | public double getDouble(String name) { |
597 | return this.getDouble(this.data.getType().getIndex(name)); |
||
598 | } |
||
599 | |||
600 | 44297 | jjdelcerro | @Override
|
601 | 40435 | jjdelcerro | public double getDouble(int index) { |
602 | 43729 | fdiaz | |
603 | 40435 | jjdelcerro | Double value = ((Double) this.get(index,Double.class,DataTypes.DOUBLE)); |
604 | if (value == null) { |
||
605 | return 0; |
||
606 | } |
||
607 | 44297 | jjdelcerro | return value;
|
608 | 40435 | jjdelcerro | } |
609 | |||
610 | 44297 | jjdelcerro | @Override
|
611 | 40435 | jjdelcerro | public Feature getFeature(String name) { |
612 | return this.getFeature(this.data.getType().getIndex(name)); |
||
613 | } |
||
614 | |||
615 | 44297 | jjdelcerro | @Override
|
616 | 40435 | jjdelcerro | public Feature getFeature(int index) { |
617 | return (Feature) this.get(index); |
||
618 | } |
||
619 | |||
620 | 44297 | jjdelcerro | @Override
|
621 | 40435 | jjdelcerro | public float getFloat(String name) { |
622 | return this.getFloat(this.data.getType().getIndex(name)); |
||
623 | } |
||
624 | |||
625 | 44297 | jjdelcerro | @Override
|
626 | 40435 | jjdelcerro | public float getFloat(int index) { |
627 | Float value = ((Float) this.get(index,Float.class,DataTypes.FLOAT)); |
||
628 | if (value == null) { |
||
629 | return 0; |
||
630 | } |
||
631 | 44297 | jjdelcerro | return value;
|
632 | 40435 | jjdelcerro | } |
633 | |||
634 | 44297 | jjdelcerro | @Override
|
635 | 40435 | jjdelcerro | public Geometry getGeometry(String name) { |
636 | return this.getGeometry(this.data.getType().getIndex(name)); |
||
637 | } |
||
638 | |||
639 | 44297 | jjdelcerro | @Override
|
640 | 40435 | jjdelcerro | public Geometry getGeometry(int index) { |
641 | return (Geometry) this.get(index,Geometry.class,DataTypes.GEOMETRY); |
||
642 | } |
||
643 | |||
644 | 44297 | jjdelcerro | @Override
|
645 | 40435 | jjdelcerro | public int getInt(String name) { |
646 | return this.getInt(this.data.getType().getIndex(name)); |
||
647 | } |
||
648 | |||
649 | 44297 | jjdelcerro | @Override
|
650 | 40435 | jjdelcerro | public int getInt(int index) { |
651 | Integer value = ((Integer) this.get(index,Integer.class,DataTypes.INT)); |
||
652 | if (value == null) { |
||
653 | return 0; |
||
654 | } |
||
655 | 44297 | jjdelcerro | return value;
|
656 | 40435 | jjdelcerro | } |
657 | |||
658 | 44297 | jjdelcerro | @Override
|
659 | 40435 | jjdelcerro | public long getLong(String name) { |
660 | return this.getLong(this.data.getType().getIndex(name)); |
||
661 | } |
||
662 | |||
663 | 44297 | jjdelcerro | @Override
|
664 | 40435 | jjdelcerro | public long getLong(int index) { |
665 | Long value = ((Long) this.get(index,Long.class,DataTypes.LONG)); |
||
666 | if (value == null) { |
||
667 | return 0; |
||
668 | } |
||
669 | 44297 | jjdelcerro | return value;
|
670 | 40435 | jjdelcerro | } |
671 | |||
672 | 44297 | jjdelcerro | @Override
|
673 | 40435 | jjdelcerro | public String getString(String name) { |
674 | return this.getString(this.data.getType().getIndex(name)); |
||
675 | } |
||
676 | |||
677 | 44297 | jjdelcerro | @Override
|
678 | 40435 | jjdelcerro | public String getString(int index) { |
679 | return (String) this.get(index,String.class,DataTypes.STRING); |
||
680 | } |
||
681 | |||
682 | 44297 | jjdelcerro | @Override
|
683 | 40435 | jjdelcerro | public Object getContextValue(String name) { |
684 | name = name.toLowerCase(); |
||
685 | if (name.equals("store")) { |
||
686 | return this.getStore(); |
||
687 | } |
||
688 | |||
689 | if (name.equals("featuretype")) { |
||
690 | return this.data.getType(); |
||
691 | } |
||
692 | |||
693 | if (name.equals("feature")) { |
||
694 | return this; |
||
695 | } |
||
696 | |||
697 | throw new IllegalArgumentException(name); |
||
698 | } |
||
699 | |||
700 | 44297 | jjdelcerro | @Override
|
701 | 40435 | jjdelcerro | public Iterator getDataNames() { |
702 | class DataNamesIterator implements Iterator { |
||
703 | Iterator attributeIteraror;
|
||
704 | |||
705 | DataNamesIterator(DefaultFeature feature) { |
||
706 | this.attributeIteraror = feature.getType().iterator();
|
||
707 | } |
||
708 | |||
709 | 44297 | jjdelcerro | @Override
|
710 | 40435 | jjdelcerro | public boolean hasNext() { |
711 | return this.attributeIteraror.hasNext(); |
||
712 | } |
||
713 | |||
714 | 44297 | jjdelcerro | @Override
|
715 | 40435 | jjdelcerro | public Object next() { |
716 | return ((FeatureAttributeDescriptor) this.attributeIteraror |
||
717 | .next()).getName(); |
||
718 | } |
||
719 | |||
720 | 44297 | jjdelcerro | @Override
|
721 | 40435 | jjdelcerro | public void remove() { |
722 | throw new UnsupportedOperationException(); |
||
723 | } |
||
724 | |||
725 | } |
||
726 | return new DataNamesIterator(this); |
||
727 | } |
||
728 | |||
729 | 44297 | jjdelcerro | @Override
|
730 | 40435 | jjdelcerro | public Object getDataValue(String name) { |
731 | name = name.toLowerCase(); |
||
732 | 41542 | jjdelcerro | try {
|
733 | return get(name);
|
||
734 | } catch (IllegalArgumentException ex) { |
||
735 | if( "defaultgeometry".equalsIgnoreCase(name )) { |
||
736 | return this.getDefaultGeometry(); |
||
737 | } |
||
738 | throw ex;
|
||
739 | } |
||
740 | 40435 | jjdelcerro | } |
741 | |||
742 | 44297 | jjdelcerro | @Override
|
743 | 40435 | jjdelcerro | public Iterator getDataValues() { |
744 | class DataValuesIterator implements Iterator { |
||
745 | DefaultFeature feature; |
||
746 | int current = 0; |
||
747 | |||
748 | DataValuesIterator(DefaultFeature feature) { |
||
749 | this.feature = feature;
|
||
750 | } |
||
751 | |||
752 | 44297 | jjdelcerro | @Override
|
753 | 40435 | jjdelcerro | public boolean hasNext() { |
754 | return current < feature.getType().size() - 1; |
||
755 | } |
||
756 | |||
757 | 44297 | jjdelcerro | @Override
|
758 | 40435 | jjdelcerro | public Object next() { |
759 | return feature.get(current++);
|
||
760 | } |
||
761 | |||
762 | 44297 | jjdelcerro | @Override
|
763 | 40435 | jjdelcerro | public void remove() { |
764 | throw new UnsupportedOperationException(); |
||
765 | } |
||
766 | |||
767 | } |
||
768 | return new DataValuesIterator(this); |
||
769 | } |
||
770 | |||
771 | 44297 | jjdelcerro | @Override
|
772 | 40435 | jjdelcerro | public boolean hasContextValue(String name) { |
773 | name = name.toLowerCase(); |
||
774 | if (name.equals("store")) { |
||
775 | return true; |
||
776 | } |
||
777 | |||
778 | if (name.equals("featuretype")) { |
||
779 | return true; |
||
780 | } |
||
781 | |||
782 | 44297 | jjdelcerro | return name.equals("feature"); |
783 | 40435 | jjdelcerro | } |
784 | |||
785 | 44297 | jjdelcerro | @Override
|
786 | 40435 | jjdelcerro | public boolean hasDataValue(String name) { |
787 | name = name.toLowerCase(); |
||
788 | return this.data.getType().getIndex(name) >= 0; |
||
789 | } |
||
790 | 43729 | fdiaz | |
791 | 44297 | jjdelcerro | @Override
|
792 | 44086 | jjdelcerro | public Time getTime(int index) { |
793 | return ((Time) this.get(index,Time.class,DataTypes.INSTANT)); |
||
794 | } |
||
795 | |||
796 | 44297 | jjdelcerro | @Override
|
797 | 44086 | jjdelcerro | public Time getTime(String name) { |
798 | return this.getInstant(this.data.getType().getIndex(name)); |
||
799 | } |
||
800 | |||
801 | 44297 | jjdelcerro | @Override
|
802 | 40435 | jjdelcerro | public Instant getInstant(int index) { |
803 | 44086 | jjdelcerro | return ((Instant) this.get(index,Instant.class,DataTypes.INSTANT)); |
804 | 40435 | jjdelcerro | } |
805 | |||
806 | 44297 | jjdelcerro | @Override
|
807 | 40435 | jjdelcerro | public Instant getInstant(String name) { |
808 | return this.getInstant(this.data.getType().getIndex(name)); |
||
809 | } |
||
810 | |||
811 | 44297 | jjdelcerro | @Override
|
812 | 40435 | jjdelcerro | public Interval getInterval(int index) { |
813 | 44086 | jjdelcerro | return ((Interval) this.get(index,Interval.class,DataTypes.INTERVAL)); |
814 | 40435 | jjdelcerro | } |
815 | |||
816 | 44297 | jjdelcerro | @Override
|
817 | 40435 | jjdelcerro | public Interval getInterval(String name) { |
818 | return this.getInterval(this.data.getType().getIndex(name)); |
||
819 | } |
||
820 | |||
821 | 42775 | jjdelcerro | @Override
|
822 | 40435 | jjdelcerro | public DynObject getAsDynObject() {
|
823 | 42775 | jjdelcerro | DynObjectFeatureFacade facade = new DynObjectFeatureFacade(this); |
824 | 40435 | jjdelcerro | return facade;
|
825 | } |
||
826 | |||
827 | 44297 | jjdelcerro | @Override
|
828 | 40435 | jjdelcerro | public String toString() { |
829 | 44297 | jjdelcerro | StringBuilder builder = new StringBuilder(); |
830 | 40435 | jjdelcerro | FeatureAttributeDescriptor[] attributeDescriptors =
|
831 | getType().getAttributeDescriptors(); |
||
832 | for (int i = 0; i < attributeDescriptors.length; i++) { |
||
833 | String name = attributeDescriptors[i].getName();
|
||
834 | 44297 | jjdelcerro | builder.append(get(name)); |
835 | 40435 | jjdelcerro | if (i < attributeDescriptors.length - 1) { |
836 | 44297 | jjdelcerro | builder.append(", ");
|
837 | 40435 | jjdelcerro | } |
838 | } |
||
839 | 44297 | jjdelcerro | return builder.toString();
|
840 | 40435 | jjdelcerro | } |
841 | 40867 | jbadia | |
842 | |||
843 | 43729 | fdiaz | |
844 | |||
845 | 40867 | jbadia | /**
|
846 | 40435 | jjdelcerro | * @return the inserted
|
847 | */
|
||
848 | public boolean isInserted() { |
||
849 | return inserted;
|
||
850 | } |
||
851 | |||
852 | 43729 | fdiaz | |
853 | 40435 | jjdelcerro | /**
|
854 | * @param inserted the inserted to set
|
||
855 | */
|
||
856 | public void setInserted(boolean inserted) { |
||
857 | this.inserted = inserted;
|
||
858 | } |
||
859 | |||
860 | 43983 | jjdelcerro | @Override
|
861 | 40435 | jjdelcerro | public EvaluatorData getEvaluatorData() {
|
862 | return this; |
||
863 | } |
||
864 | 43983 | jjdelcerro | |
865 | public int size() { |
||
866 | return this.data.getType().size(); |
||
867 | } |
||
868 | |||
869 | public boolean isEmpty() { |
||
870 | return false; |
||
871 | } |
||
872 | |||
873 | public Iterator<String> iterator() { |
||
874 | final Iterator<FeatureAttributeDescriptor> x = this.data.getType().iterator(); |
||
875 | return new Iterator<String>() { |
||
876 | @Override
|
||
877 | public boolean hasNext() { |
||
878 | return x.hasNext();
|
||
879 | } |
||
880 | |||
881 | @Override
|
||
882 | public String next() { |
||
883 | return x.next().getName();
|
||
884 | } |
||
885 | }; |
||
886 | } |
||
887 | |||
888 | public boolean containsKey(String key) { |
||
889 | return this.data.getType().get(key)!=null; |
||
890 | } |
||
891 | 40435 | jjdelcerro | } |