Revision 44262 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
import java.util.ArrayList;
4
import java.util.Arrays;
5
import java.util.List;
3 6
import org.apache.commons.lang3.Range;
4 7
import org.gvsig.expressionevaluator.ExpressionBuilder;
5 8
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
......
12 15
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
13 16
import org.gvsig.fmap.dal.DALLocator;
14 17
import org.gvsig.fmap.dal.DataManager;
18
import static org.gvsig.fmap.dal.DataManager.FUNCTION_FOREING_VALUE;
15 19
import org.gvsig.fmap.dal.DataStore;
16 20
import org.gvsig.fmap.dal.StoresRepository;
17 21
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
......
20 24
import org.gvsig.fmap.dal.feature.FeatureSelection;
21 25
import org.gvsig.fmap.dal.feature.FeatureStore;
22 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;
23 29
import static org.gvsig.fmap.dal.impl.expressionevaluator.DALSymbolTable.FUNCTION_FEATURES;
24 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;
25 34
import org.gvsig.tools.dynobject.Tags;
26 35
import org.gvsig.tools.script.Script;
27 36
import org.gvsig.tools.util.UnmodifiableBasicList;
37
import org.gvsig.tools.util.UnmodifiableBasicListAdapter;
28 38

  
29 39
/**
30 40
 *
......
33 43
    @SuppressWarnings("UseSpecificCatch")
34 44
public class DefaultFeatureSymbolTable extends AbstractSymbolTable implements FeatureSymbolTable {
35 45

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

  
38 46
    public class ForeingValueFunction extends AbstractFunction {
39 47

  
40 48
        public ForeingValueFunction() {
......
45 53
                    "Return the value of a field throw a relation from other tables.",
46 54
                    FUNCTION_FOREING_VALUE + "({{field_name}})",
47 55
                    new String[]{
48
                        "fieldName - dot separated field path"
56
                        "fieldName - dot separated field path or list of strings with field names."
49 57
                    },
50 58
                    "OBJECT"
51 59
            );
......
54 62
        @Override
55 63
        public Object call(Interpreter interpreter, Object[] args) throws Exception {
56 64
            ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder();
57
            String fullFieldName = getStr(args, 0);
58
            String[] nameParts = fullFieldName.split("[.]");
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
            }
59 79
            Feature currentFeature = feature;
60 80
            try {
61
                String namePart;
81
                String fieldName;
62 82
                Object value;
63
                for (int i = 0; i < nameParts.length-1; i++) {
64
                    namePart = nameParts[i];
65
                    value = currentFeature.get(namePart);
83
                for (int i = 0; i < fieldNames.size()-1; i++) {
84
                    fieldName = fieldNames.get(i);
85
                    value = currentFeature.get(fieldName);
66 86
                    
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
                    );
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);
80 92
                    if( currentFeature==null ) {
81 93
                        return null;
82 94
                    }
83 95
                }
84
                namePart = nameParts[nameParts.length-1];
85
                value = currentFeature.get(namePart);
96
                fieldName = fieldNames.get(fieldNames.size()-1);
97
                value = currentFeature.get(fieldName);
86 98
                return value;
87 99
            } catch (ExpressionRuntimeException ex) {
88 100
                throw ex;
......
196 208

  
197 209
    @SuppressWarnings("OverridableMethodCallInConstructor")
198 210
    public DefaultFeatureSymbolTable() {
199
        super(DataManager.FEATURE_SYMBOL_TABLE);
211
        super(DataManager.DAL_SYMBOL_TABLE_FEATURE);
200 212

  
201 213
        this.addFunction(new FeatureFunction());
202 214
        this.addFunction(new FeatureStoreFunction());

Also available in: Unified diff