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

History | View | Annotate | Download (9.48 KB)

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

    
3
import org.apache.commons.lang3.Range;
4
import org.gvsig.expressionevaluator.ExpressionBuilder;
5
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
6
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
7
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
8
import org.gvsig.expressionevaluator.ExpressionUtils;
9
import org.gvsig.expressionevaluator.Interpreter;
10
import org.gvsig.expressionevaluator.MutableSymbolTable;
11
import org.gvsig.expressionevaluator.spi.AbstractFunction;
12
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
13
import org.gvsig.fmap.dal.DALLocator;
14
import org.gvsig.fmap.dal.DataManager;
15
import org.gvsig.fmap.dal.DataStore;
16
import org.gvsig.fmap.dal.StoresRepository;
17
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
18
import org.gvsig.fmap.dal.feature.Feature;
19
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
20
import org.gvsig.fmap.dal.feature.FeatureSelection;
21
import org.gvsig.fmap.dal.feature.FeatureStore;
22
import org.gvsig.fmap.dal.feature.FeatureType;
23
import static org.gvsig.fmap.dal.impl.expressionevaluator.DALSymbolTable.FUNCTION_FEATURES;
24
import org.gvsig.fmap.dal.impl.expressionevaluator.DALSymbolTable.FeaturesFunction;
25
import org.gvsig.tools.dynobject.Tags;
26
import org.gvsig.tools.script.Script;
27
import org.gvsig.tools.util.UnmodifiableBasicList;
28

    
29
/**
30
 *
31
 * @author jjdelcerro
32
 */
33
    @SuppressWarnings("UseSpecificCatch")
34
public class DefaultFeatureSymbolTable extends AbstractSymbolTable implements FeatureSymbolTable {
35

    
36
    public static final String FUNCTION_FOREING_VALUE = "FOREING_VALUE";
37

    
38
    public class ForeingValueFunction extends AbstractFunction {
39

    
40
        public ForeingValueFunction() {
41
            super(
42
                    "Data access",
43
                    FUNCTION_FOREING_VALUE,
44
                    Range.is(1),
45
                    "Return the value of a field throw a relation from other tables.",
46
                    FUNCTION_FOREING_VALUE + "({{field_name}})",
47
                    new String[]{
48
                        "fieldName - dot separated field path"
49
                    },
50
                    "OBJECT"
51
            );
52
        }
53

    
54
        @Override
55
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
56
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
57
            String fullFieldName = getStr(args, 0);
58
            String[] nameParts = fullFieldName.split("[.]");
59
            Feature currentFeature = feature;
60
            try {
61
                String namePart;
62
                Object value;
63
                for (int i = 0; i < nameParts.length-1; i++) {
64
                    namePart = nameParts[i];
65
                    value = currentFeature.get(namePart);
66
                    
67
                    FeatureAttributeDescriptor attrdesc = currentFeature.getType().getAttributeDescriptor(namePart);
68
                    Tags tags = attrdesc.getTags();
69
                    String storeName = tags.getString(DataManager.DAL_FOREING_TABLE, null);
70
                    String attrName = tags.getString(DataManager.DAL_FOREING_CODE, null);
71

    
72
                    StoresRepository repository = currentFeature.getStore().getStoresRepository();
73
                    FeatureStore store = (FeatureStore) repository.get(storeName);
74
                    currentFeature = store.findFirst(
75
                            builder.eq(
76
                                    builder.constant(value), 
77
                                    builder.variable(attrName)
78
                            ).toString()
79
                    );
80
                    if( currentFeature==null ) {
81
                        return null;
82
                    }
83
                }
84
                namePart = nameParts[nameParts.length-1];
85
                value = currentFeature.get(namePart);
86
                return value;
87
            } catch (ExpressionRuntimeException ex) {
88
                throw ex;
89
            } catch (Exception ex) {
90
                throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_FEATURES + "' function", ex);
91
            }
92
        }
93
        
94
    }
95

    
96
    private class LocalFeaturesFunction extends FeaturesFunction {
97

    
98
        public LocalFeaturesFunction() {
99
            super();
100
        }
101

    
102
        @Override
103
        protected DataStore getStore(String storeName) {
104
            DataManager dataManager = DALLocator.getDataManager();
105
            DataStore store = null;
106
            if (feature != null) {
107
                store = feature.getStore();
108
                store = store.getChildren().get(storeName);
109
            }
110
            if (store == null) {
111
                StoresRepository repository = dataManager.getStoresRepository();
112
                if (repository.containsKey(storeName)) {
113
                    store = repository.get(storeName);
114
                }
115
            }
116
            return store;
117
        }
118
    }
119

    
120
    private class FeatureFunction extends AbstractFunction {
121

    
122
        public FeatureFunction() {
123
            super(
124
                    "Data access",
125
                    "feature",
126
                    Range.is(0),
127
                    "Access to the current feature object when used in a filter.\n"
128
                    + "Return null if used outer a filter.",
129
                    "feature()",
130
                    null,
131
                    "Feature"
132
            );
133
        }
134

    
135
        @Override
136
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
137
            return feature;
138
        }
139

    
140
    }
141

    
142
    public class FeatureStoreFunction extends AbstractFunction {
143

    
144
        public FeatureStoreFunction() {
145
            super(
146
                    "Data access",
147
                    "store",
148
                    Range.is(0),
149
                    "Access to the current store object when used in a filter.\n"
150
                    + "Return null if used outer a filter.",
151
                    "store()",
152
                    null,
153
                    "FeatureStore"
154
            );
155
        }
156

    
157
        @Override
158
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
159
            if (feature == null) {
160
                return null;
161
            }
162
            return feature.getStore();
163
        }
164

    
165
    }
166

    
167
    public class IsSelectedFunction extends AbstractFunction {
168

    
169
        public IsSelectedFunction() {
170
            super(
171
                    "Data access",
172
                    "isSelected",
173
                    Range.is(0),
174
                    "Return if the current feature is selected in the store when used in a filter.\n"
175
                    + "Return false if used outer a filter.",
176
                    "isSelected()",
177
                    null,
178
                    "Boolean"
179
            );
180
        }
181

    
182
        @Override
183
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
184
            if (feature == null) {
185
                return false;
186
            }
187
            FeatureStore store = feature.getStore();
188
            FeatureSelection selection = store.getFeatureSelection();
189
            return selection.isSelected(feature);
190
        }
191

    
192
    }
193

    
194
    private Feature feature;
195
    private FeatureType type;
196

    
197
    @SuppressWarnings("OverridableMethodCallInConstructor")
198
    public DefaultFeatureSymbolTable() {
199
        super(DataManager.FEATURE_SYMBOL_TABLE);
200

    
201
        this.addFunction(new FeatureFunction());
202
        this.addFunction(new FeatureStoreFunction());
203
        this.addFunction(new IsSelectedFunction());
204
        this.addFunction(new LocalFeaturesFunction());
205
        this.addFunction(new ForeingValueFunction());
206
    }
207

    
208
    @SuppressWarnings("OverridableMethodCallInConstructor")
209
    public DefaultFeatureSymbolTable(Script userScript, UnmodifiableBasicList<Script> scripts) {
210
        this();
211
        if (userScript != null) {
212
            this.getScripts().add(userScript);
213
        }
214
        if (scripts != null && !scripts.isEmpty()) {
215
            for (Script script : scripts) {
216
                this.getScripts().add(script);
217
            }
218
        }
219
    }
220

    
221
    @Override
222
    public FeatureSymbolTable clone() throws CloneNotSupportedException {
223
        DefaultFeatureSymbolTable other = (DefaultFeatureSymbolTable) super.clone();
224
        return other;
225
    }
226

    
227
    @Override
228
    public boolean exists(String name) {
229
        if (type != null && type.get(name) != null) {
230
            return true;
231
        }
232
        return false;
233
    }
234

    
235
    @Override
236
    public Object value(String name) {
237
        if (feature == null) {
238
            return null;
239
        }
240
        return this.feature.get(name);
241
    }
242

    
243
    @Override
244
    public boolean isSQLCompatible(String name) {
245
        if (this.type == null) {
246
            return super.isSQLCompatible(name);
247
        }
248
        FeatureAttributeDescriptor attrdesc = this.type.getAttributeDescriptor(name);
249
        if (attrdesc == null) {
250
            return true;
251
        }
252
        if (attrdesc.isComputed()) {
253
            return false;
254
        }
255
        return true;
256
    }
257

    
258
    @Override
259
    public void setFeature(Feature feature) {
260
        this.feature = feature;
261
        this.type = feature.getType();
262
    }
263

    
264
    @Override
265
    public MutableSymbolTable createParent() {
266
        ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
267

    
268
        MutableSymbolTable symbolTable = expManager.createSymbolTable();
269
        symbolTable.addSymbolTable(this);
270
        return symbolTable;
271
    }
272

    
273
    public static void selfRegister() {
274
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
275
        manager.registerSymbolTable(new DefaultfeatureSymbolTableFactory());
276
    }
277
}