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 @ 44262

History | View | Annotate | Download (10.2 KB)

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

    
3
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.List;
6
import org.apache.commons.lang3.Range;
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.feature.ForeingKey.ContextForeingKey;
29
import static org.gvsig.fmap.dal.impl.expressionevaluator.DALSymbolTable.FUNCTION_FEATURES;
30
import org.gvsig.fmap.dal.impl.expressionevaluator.DALSymbolTable.FeaturesFunction;
31
import org.gvsig.tools.ToolsLocator;
32
import org.gvsig.tools.dispose.DisposeUtils;
33
import org.gvsig.tools.dynobject.Tagged;
34
import org.gvsig.tools.dynobject.Tags;
35
import org.gvsig.tools.script.Script;
36
import org.gvsig.tools.util.UnmodifiableBasicList;
37
import org.gvsig.tools.util.UnmodifiableBasicListAdapter;
38

    
39
/**
40
 *
41
 * @author jjdelcerro
42
 */
43
    @SuppressWarnings("UseSpecificCatch")
44
public class DefaultFeatureSymbolTable extends AbstractSymbolTable implements FeatureSymbolTable {
45

    
46
    public class ForeingValueFunction extends AbstractFunction {
47

    
48
        public ForeingValueFunction() {
49
            super(
50
                    "Data access",
51
                    FUNCTION_FOREING_VALUE,
52
                    Range.is(1),
53
                    "Return the value of a field throw a relation from other tables.",
54
                    FUNCTION_FOREING_VALUE + "({{field_name}})",
55
                    new String[]{
56
                        "fieldName - dot separated field path or list of strings with field names."
57
                    },
58
                    "OBJECT"
59
            );
60
        }
61

    
62
        @Override
63
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
64
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
65
            UnmodifiableBasicList<String> fieldNames;
66
            Object fields_o = getObject(args, 0);
67
            if( fields_o instanceof List ) {
68
                fieldNames = new UnmodifiableBasicListAdapter<>((List<String>) fields_o);
69
            } else if( fields_o instanceof String ) {
70
                fieldNames =  new UnmodifiableBasicListAdapter<>(Arrays.asList(((String)fields_o).split("[.]")));
71
            } else if( fields_o instanceof UnmodifiableBasicList ) {
72
                fieldNames = (UnmodifiableBasicList<String>) fields_o;
73
            } else {
74
                throw new ExpressionRuntimeException(
75
                    "Problems calling '" + FUNCTION_FEATURES + 
76
                    "' function, type of parameter '"+fields_o.getClass().getSimpleName()+
77
                    "' not supported, use string or list.");
78
            }
79
            Feature currentFeature = feature;
80
            try {
81
                String fieldName;
82
                Object value;
83
                for (int i = 0; i < fieldNames.size()-1; i++) {
84
                    fieldName = fieldNames.get(i);
85
                    value = currentFeature.get(fieldName);
86
                    
87
                    FeatureAttributeDescriptor attrdesc = currentFeature.getType().getAttributeDescriptor(fieldName);
88
                    ForeingKey foreingKey = attrdesc.getForeingKey();
89
//                    ContextForeingKey context = foreingKey.createContext();
90
                    currentFeature = foreingKey.getFeature(null, value);
91
//                    DisposeUtils.disposeQuietly(context);
92
                    if( currentFeature==null ) {
93
                        return null;
94
                    }
95
                }
96
                fieldName = fieldNames.get(fieldNames.size()-1);
97
                value = currentFeature.get(fieldName);
98
                return value;
99
            } catch (ExpressionRuntimeException ex) {
100
                throw ex;
101
            } catch (Exception ex) {
102
                throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_FEATURES + "' function", ex);
103
            }
104
        }
105
        
106
    }
107

    
108
    private class LocalFeaturesFunction extends FeaturesFunction {
109

    
110
        public LocalFeaturesFunction() {
111
            super();
112
        }
113

    
114
        @Override
115
        protected DataStore getStore(String storeName) {
116
            DataManager dataManager = DALLocator.getDataManager();
117
            DataStore store = null;
118
            if (feature != null) {
119
                store = feature.getStore();
120
                store = store.getChildren().get(storeName);
121
            }
122
            if (store == null) {
123
                StoresRepository repository = dataManager.getStoresRepository();
124
                if (repository.containsKey(storeName)) {
125
                    store = repository.get(storeName);
126
                }
127
            }
128
            return store;
129
        }
130
    }
131

    
132
    private class FeatureFunction extends AbstractFunction {
133

    
134
        public FeatureFunction() {
135
            super(
136
                    "Data access",
137
                    "feature",
138
                    Range.is(0),
139
                    "Access to the current feature object when used in a filter.\n"
140
                    + "Return null if used outer a filter.",
141
                    "feature()",
142
                    null,
143
                    "Feature"
144
            );
145
        }
146

    
147
        @Override
148
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
149
            return feature;
150
        }
151

    
152
    }
153

    
154
    public class FeatureStoreFunction extends AbstractFunction {
155

    
156
        public FeatureStoreFunction() {
157
            super(
158
                    "Data access",
159
                    "store",
160
                    Range.is(0),
161
                    "Access to the current store object when used in a filter.\n"
162
                    + "Return null if used outer a filter.",
163
                    "store()",
164
                    null,
165
                    "FeatureStore"
166
            );
167
        }
168

    
169
        @Override
170
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
171
            if (feature == null) {
172
                return null;
173
            }
174
            return feature.getStore();
175
        }
176

    
177
    }
178

    
179
    public class IsSelectedFunction extends AbstractFunction {
180

    
181
        public IsSelectedFunction() {
182
            super(
183
                    "Data access",
184
                    "isSelected",
185
                    Range.is(0),
186
                    "Return if the current feature is selected in the store when used in a filter.\n"
187
                    + "Return false if used outer a filter.",
188
                    "isSelected()",
189
                    null,
190
                    "Boolean"
191
            );
192
        }
193

    
194
        @Override
195
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
196
            if (feature == null) {
197
                return false;
198
            }
199
            FeatureStore store = feature.getStore();
200
            FeatureSelection selection = store.getFeatureSelection();
201
            return selection.isSelected(feature);
202
        }
203

    
204
    }
205

    
206
    private Feature feature;
207
    private FeatureType type;
208

    
209
    @SuppressWarnings("OverridableMethodCallInConstructor")
210
    public DefaultFeatureSymbolTable() {
211
        super(DataManager.DAL_SYMBOL_TABLE_FEATURE);
212

    
213
        this.addFunction(new FeatureFunction());
214
        this.addFunction(new FeatureStoreFunction());
215
        this.addFunction(new IsSelectedFunction());
216
        this.addFunction(new LocalFeaturesFunction());
217
        this.addFunction(new ForeingValueFunction());
218
    }
219

    
220
    @SuppressWarnings("OverridableMethodCallInConstructor")
221
    public DefaultFeatureSymbolTable(Script userScript, UnmodifiableBasicList<Script> scripts) {
222
        this();
223
        if (userScript != null) {
224
            this.getScripts().add(userScript);
225
        }
226
        if (scripts != null && !scripts.isEmpty()) {
227
            for (Script script : scripts) {
228
                this.getScripts().add(script);
229
            }
230
        }
231
    }
232

    
233
    @Override
234
    public FeatureSymbolTable clone() throws CloneNotSupportedException {
235
        DefaultFeatureSymbolTable other = (DefaultFeatureSymbolTable) super.clone();
236
        return other;
237
    }
238

    
239
    @Override
240
    public boolean exists(String name) {
241
        if (type != null && type.get(name) != null) {
242
            return true;
243
        }
244
        return false;
245
    }
246

    
247
    @Override
248
    public Object value(String name) {
249
        if (feature == null) {
250
            return null;
251
        }
252
        return this.feature.get(name);
253
    }
254

    
255
    @Override
256
    public boolean isSQLCompatible(String name) {
257
        if (this.type == null) {
258
            return super.isSQLCompatible(name);
259
        }
260
        FeatureAttributeDescriptor attrdesc = this.type.getAttributeDescriptor(name);
261
        if (attrdesc == null) {
262
            return true;
263
        }
264
        if (attrdesc.isComputed()) {
265
            return false;
266
        }
267
        return true;
268
    }
269

    
270
    @Override
271
    public void setFeature(Feature feature) {
272
        this.feature = feature;
273
        this.type = feature.getType();
274
    }
275

    
276
    @Override
277
    public MutableSymbolTable createParent() {
278
        ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
279

    
280
        MutableSymbolTable symbolTable = expManager.createSymbolTable();
281
        symbolTable.addSymbolTable(this);
282
        return symbolTable;
283
    }
284

    
285
    public static void selfRegister() {
286
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
287
        manager.registerSymbolTable(new DefaultfeatureSymbolTableFactory());
288
    }
289
}