Statistics
| Revision:

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 / impl / expressionevaluator / DefaultFeatureSymbolTable.java @ 44533

History | View | Annotate | Download (14.1 KB)

1
package org.gvsig.fmap.dal.impl.expressionevaluator;
2

    
3
import java.util.Arrays;
4
import java.util.List;
5
import org.apache.commons.lang3.Range;
6
import org.apache.commons.lang3.StringUtils;
7
import org.gvsig.expressionevaluator.ExpressionBuilder;
8
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
9
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
10
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
11
import org.gvsig.expressionevaluator.ExpressionUtils;
12
import org.gvsig.expressionevaluator.Interpreter;
13
import org.gvsig.expressionevaluator.MutableSymbolTable;
14
import org.gvsig.expressionevaluator.spi.AbstractFunction;
15
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
16
import org.gvsig.fmap.dal.DALLocator;
17
import org.gvsig.fmap.dal.DataManager;
18
import static org.gvsig.fmap.dal.DataManager.FUNCTION_FOREING_VALUE;
19
import org.gvsig.fmap.dal.DataStore;
20
import org.gvsig.fmap.dal.StoresRepository;
21
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
22
import org.gvsig.fmap.dal.feature.Feature;
23
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
24
import org.gvsig.fmap.dal.feature.FeatureSelection;
25
import org.gvsig.fmap.dal.feature.FeatureStore;
26
import org.gvsig.fmap.dal.feature.FeatureType;
27
import org.gvsig.fmap.dal.feature.ForeingKey;
28
import org.gvsig.fmap.dal.impl.expressionevaluator.DALSymbolTable.FeaturesFunction;
29
import org.gvsig.tools.dynobject.DynObjectAdapter;
30
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
31
import org.gvsig.tools.script.Script;
32
import org.gvsig.tools.util.UnmodifiableBasicList;
33
import org.gvsig.tools.util.UnmodifiableBasicListAdapter;
34

    
35
/**
36
 *
37
 * @author jjdelcerro
38
 */
39
    @SuppressWarnings("UseSpecificCatch")
40
public class DefaultFeatureSymbolTable extends AbstractSymbolTable implements FeatureSymbolTable {
41

    
42
//    public static final String FUNCTION_FEATURES = "FEATURES";
43
//
44
//    public class FeaturesFunction extends AbstractFunction {
45
//
46
//        public FeaturesFunction() {
47
//            super(
48
//                    "Data access",
49
//                    FUNCTION_FEATURES,
50
//                    Range.between(1, 3),
51
//                    "Return a list of the features selecteds with filter in the store.",
52
//                    FUNCTION_FEATURES + "({{store_name}}, filter, order)",
53
//                    new String[]{
54
//                        "store - data store name to be used",
55
//                        "where - Optional. String value with a filter expression",
56
//                        "order - Optional. String value with the order. must be a string with the names of separate fields with commas"
57
//                    },
58
//                    "LIST"
59
//            );
60
//        }
61
//
62
//        @Override
63
//        public Object call(Interpreter interpreter, Object[] args) throws Exception {
64
//            String storeName = getStr(args, 0);
65
//            String where = null;
66
//            String order = null;
67
//            switch (args.length) {
68
//                case 2:
69
//                    where = getStr(args, 1);
70
//                    break;
71
//                case 3:
72
//                    where = getStr(args, 1);
73
//                    order = getStr(args, 2);
74
//                    break;
75
//            }
76
//            try {
77
//                DataStore store = getStore(storeName);
78
//                if (store == null || !(store instanceof FeatureStore)) {
79
//                    throw new ExpressionRuntimeException("Cant locate the store '" + storeName + "' in function '" + FUNCTION_FEATURES + "'.");
80
//                }
81
//                if (!(store instanceof FeatureStore)) {
82
//                    throw new ExpressionRuntimeException("The store'" + storeName + "' is not valid for function '" + FUNCTION_FEATURES + "', a FeatureStore is required.");
83
//                }
84
//                FeatureStore featureStore = (FeatureStore) store;
85
//                List<Feature> features;
86
//                if (where == null && order == null) {
87
//                    features = featureStore.getFeatures();
88
//                } else {
89
//                    FeatureQuery query = featureStore.createFeatureQuery();
90
//                    if (where != null) {
91
//                        query.addFilter(where);
92
//                    }
93
//                    if (order != null) {
94
//                        query.getOrder().add(order);
95
//                    }
96
//                    query.retrievesAllAttributes();
97
//                    features = featureStore.getFeatures(query);
98
//                }
99
//                return features;
100
//            } catch (ExpressionRuntimeException ex) {
101
//                throw ex;
102
//            } catch (Exception ex) {
103
//                throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_FEATURES + "' function", ex);
104
//            }
105
//        }
106
//
107
//        protected DataStore getStore(String storeName) {
108
//            DataStore store = feature.getStore();
109
////            DataManager dataManager = DALLocator.getDataManager();
110
////            DataStore store = dataManager.getStoresRepository().getStore(storeName);
111
//            return store;
112
//        }
113
//    }
114

    
115
    public class ForeingValueFunction extends AbstractFunction {
116

    
117
        public ForeingValueFunction() {
118
            super(
119
                    "Data access",
120
                    FUNCTION_FOREING_VALUE,
121
                    Range.is(1),
122
                    "Return the value of a field throw a relation from other tables.",
123
                    FUNCTION_FOREING_VALUE + "({{field_name}})",
124
                    new String[]{
125
                        "fieldName - dot separated field path or list of strings with field names."
126
                    },
127
                    "OBJECT"
128
            );
129
        }
130

    
131
        @Override
132
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
133
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
134
            UnmodifiableBasicList<String> fieldNames;
135
            Object fields_o = getObject(args, 0);
136
            if( fields_o instanceof List ) {
137
                fieldNames = new UnmodifiableBasicListAdapter<>((List<String>) fields_o);
138
            } else if( fields_o instanceof String ) {
139
                fieldNames =  new UnmodifiableBasicListAdapter<>(Arrays.asList(((String)fields_o).split("[.]")));
140
            } else if( fields_o instanceof UnmodifiableBasicList ) {
141
                fieldNames = (UnmodifiableBasicList<String>) fields_o;
142
            } else {
143
                throw new ExpressionRuntimeException(
144
                    "Problems calling '" + FUNCTION_FOREING_VALUE + 
145
                    "' function, type of parameter '"+fields_o.getClass().getSimpleName()+
146
                    "' not supported, use string or list.");
147
            }
148
            
149
            Feature currentFeature = feature;
150
            try {
151
                String fieldName;
152
                Object value;
153
                
154
                value = feature.getExtraValue(StringUtils.join(fieldNames, "."));
155
                if( value != null ) {
156
                    return value;
157
                }
158
                for (int i = 0; i < fieldNames.size()-1; i++) {
159
                    fieldName = fieldNames.get(i);
160
                    value = currentFeature.get(fieldName);
161
                    
162
                    FeatureAttributeDescriptor attrdesc = currentFeature.getType().getAttributeDescriptor(fieldName);
163
                    ForeingKey foreingKey = attrdesc.getForeingKey();
164
//                    ContextForeingKey context = foreingKey.createContext();
165
                    currentFeature = foreingKey.getFeature(null, value);
166
//                    DisposeUtils.disposeQuietly(context);
167
                    if( currentFeature==null ) {
168
                        return null;
169
                    }
170
                }
171
                fieldName = fieldNames.get(fieldNames.size()-1);
172
                value = currentFeature.get(fieldName);
173
                return value;
174
            } catch (ExpressionRuntimeException ex) {
175
                throw ex;
176
            } catch (Exception ex) {
177
                throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_FOREING_VALUE + "' function", ex);
178
            }
179
        }
180
        
181
    }
182

    
183
    private class LocalFeaturesFunction extends FeaturesFunction {
184

    
185
        public LocalFeaturesFunction() {
186
            super();
187
        }
188

    
189
        @Override
190
        protected DataStore getStore(String storeName) {
191
            DataManager dataManager = DALLocator.getDataManager();
192
            DataStore store = null;
193
            if (feature != null) {
194
                store = feature.getStore();
195
                store = store.getStoresRepository().getStore(storeName);
196
            }
197
            if (store == null) {
198
                StoresRepository repository = dataManager.getStoresRepository();
199
                if (repository.containsKey(storeName)) {
200
                    store = repository.getStore(storeName);
201
                }
202
            }
203
            return store;
204
        }
205
    }
206

    
207
    private class FeatureFunction extends AbstractFunction {
208

    
209
        public FeatureFunction() {
210
            super(
211
                    "Data access",
212
                    "feature",
213
                    Range.is(0),
214
                    "Access to the current feature object when used in a filter.\n"
215
                    + "Return null if used outer a filter.",
216
                    "feature()",
217
                    null,
218
                    "Feature"
219
            );
220
        }
221

    
222
        @Override
223
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
224
            return feature;
225
        }
226

    
227
    }
228

    
229
    public class FeatureStoreFunction extends AbstractFunction {
230

    
231
        public FeatureStoreFunction() {
232
            super(
233
                    "Data access",
234
                    "store",
235
                    Range.is(0),
236
                    "Access to the current store object when used in a filter.\n"
237
                    + "Return null if used outer a filter.",
238
                    "store()",
239
                    null,
240
                    "FeatureStore"
241
            );
242
        }
243

    
244
        @Override
245
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
246
            if (feature == null) {
247
                return null;
248
            }
249
            return feature.getStore();
250
        }
251

    
252
    }
253

    
254
    public class IsSelectedFunction extends AbstractFunction {
255

    
256
        public IsSelectedFunction() {
257
            super(
258
                    "Data access",
259
                    "isSelected",
260
                    Range.is(0),
261
                    "Return if the current feature is selected in the store when used in a filter.\n"
262
                    + "Return false if used outer a filter.",
263
                    "isSelected()",
264
                    null,
265
                    "Boolean"
266
            );
267
        }
268

    
269
        @Override
270
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
271
            if (feature == null) {
272
                return false;
273
            }
274
            FeatureStore store = feature.getStore();
275
            FeatureSelection selection = store.getFeatureSelection();
276
            return selection.isSelected(feature);
277
        }
278

    
279
    }
280

    
281
    private Feature feature;
282
    private FeatureType type;
283
    private String storeName;
284

    
285
    @SuppressWarnings("OverridableMethodCallInConstructor")
286
    public DefaultFeatureSymbolTable() {
287
        super(DataManager.DAL_SYMBOL_TABLE_FEATURE);
288

    
289

    
290
//        this.addFunction(new FeaturesFunction());
291
        this.addFunction(new FeatureFunction());
292
        this.addFunction(new FeatureStoreFunction());
293
        this.addFunction(new IsSelectedFunction());
294
        this.addFunction(new LocalFeaturesFunction());
295
        this.addFunction(new ForeingValueFunction());
296
    }
297

    
298
//    @SuppressWarnings("OverridableMethodCallInConstructor")
299
//    public DefaultFeatureSymbolTable(Script userScript, UnmodifiableBasicList<Script> scripts) {
300
//        this();
301
//        if (userScript != null) {
302
//            this.getScripts().add(userScript);
303
//        }
304
//        if (scripts != null && !scripts.isEmpty()) {
305
//            for (Script script : scripts) {
306
//                this.getScripts().add(script);
307
//            }
308
//        }
309
//    }
310

    
311
    @Override
312
    public FeatureSymbolTable clone() throws CloneNotSupportedException {
313
        DefaultFeatureSymbolTable other = (DefaultFeatureSymbolTable) super.clone();
314
        return other;
315
    }
316

    
317
    @Override
318
    public boolean exists(String name) {
319
        if (type != null && type.get(name) != null) {
320
            return true;
321
        }
322
        return false;
323
    }
324

    
325
    @Override
326
    public Object value(String name) {
327
        if (feature == null) {
328
            return null;
329
        }
330
        if( StringUtils.equalsIgnoreCase(name, this.storeName) ) {
331
            return new DynObjectAdapter() {
332
                @Override
333
                public Object getDynValue(String string) throws DynFieldNotFoundException {
334
                    return feature.get(string);
335
                }
336
            };
337
        }
338
        return this.feature.get(name);
339
    }
340

    
341
    @Override
342
    public boolean isSQLCompatible(String name) {
343
        if (this.type == null) {
344
            return super.isSQLCompatible(name);
345
        }
346
        FeatureAttributeDescriptor attrdesc = this.type.getAttributeDescriptor(name);
347
        if (attrdesc == null) {
348
            return true;
349
        }
350
        if (attrdesc.isComputed()) {
351
            return false;
352
        }
353
        return true;
354
    }
355

    
356
    @Override
357
    public void setFeature(Feature feature) {
358
        this.feature = feature;
359
        if( this.type == null ) {
360
            this.type = feature.getType();
361
            FeatureStore store = feature.getStore();
362
            if( store!=null ) {
363
                this.storeName = store.getName();
364
            }
365
        }
366
    }
367

    
368
    @Override
369
    public MutableSymbolTable createParent() {
370
        ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
371

    
372
        MutableSymbolTable symbolTable = expManager.createSymbolTable();
373
        symbolTable.addSymbolTable(this);
374
        return symbolTable;
375
    }
376

    
377
    public static void selfRegister() {
378
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
379
        manager.registerSymbolTable(new DefaultfeatureSymbolTableFactory());
380
    }
381
}