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

View differences:

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

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

  
......
25 30
 *
26 31
 * @author jjdelcerro
27 32
 */
33
    @SuppressWarnings("UseSpecificCatch")
28 34
public class DefaultFeatureSymbolTable extends AbstractSymbolTable implements FeatureSymbolTable {
29
    
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

  
30 96
    private class LocalFeaturesFunction extends FeaturesFunction {
31 97

  
32 98
        public LocalFeaturesFunction() {
......
37 103
        protected DataStore getStore(String storeName) {
38 104
            DataManager dataManager = DALLocator.getDataManager();
39 105
            DataStore store = null;
40
            if( feature!=null ) {
106
            if (feature != null) {
41 107
                store = feature.getStore();
42
                store = store.getChild(storeName);
108
                store = store.getChildren().get(storeName);
43 109
            }
44
            if( store == null ) {
110
            if (store == null) {
45 111
                StoresRepository repository = dataManager.getStoresRepository();
46
                if( repository.containsKey(storeName) ) {
112
                if (repository.containsKey(storeName)) {
47 113
                    store = repository.get(storeName);
48 114
                }
49 115
            }
......
90 156

  
91 157
        @Override
92 158
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
93
            if( feature==null ) {
159
            if (feature == null) {
94 160
                return null;
95 161
            }
96 162
            return feature.getStore();
......
115 181

  
116 182
        @Override
117 183
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
118
            if( feature==null ) {
184
            if (feature == null) {
119 185
                return false;
120 186
            }
121 187
            FeatureStore store = feature.getStore();
......
136 202
        this.addFunction(new FeatureStoreFunction());
137 203
        this.addFunction(new IsSelectedFunction());
138 204
        this.addFunction(new LocalFeaturesFunction());
205
        this.addFunction(new ForeingValueFunction());
139 206
    }
140 207

  
141 208
    @SuppressWarnings("OverridableMethodCallInConstructor")
......
159 226

  
160 227
    @Override
161 228
    public boolean exists(String name) {
162
        if (type!=null && type.get(name) != null) {
229
        if (type != null && type.get(name) != null) {
163 230
            return true;
164 231
        }
165 232
        return false;
......
167 234

  
168 235
    @Override
169 236
    public Object value(String name) {
170
        if( feature==null ) {
237
        if (feature == null) {
171 238
            return null;
172 239
        }
173 240
        return this.feature.get(name);
......
175 242

  
176 243
    @Override
177 244
    public boolean isSQLCompatible(String name) {
178
        if( this.type == null ) {
245
        if (this.type == null) {
179 246
            return super.isSQLCompatible(name);
180 247
        }
181 248
        FeatureAttributeDescriptor attrdesc = this.type.getAttributeDescriptor(name);
182
        if( attrdesc == null ) {
249
        if (attrdesc == null) {
183 250
            return true;
184 251
        }
185
        if( attrdesc.isComputed() ) {
252
        if (attrdesc.isComputed()) {
186 253
            return false;
187 254
        }
188 255
        return true;
......
203 270
        return symbolTable;
204 271
    }
205 272

  
206
    
207 273
    public static void selfRegister() {
208 274
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
209 275
        manager.registerSymbolTable(new DefaultfeatureSymbolTableFactory());

Also available in: Unified diff