Revision 43981 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app.document.table.app/org.gvsig.app.document.table.app.mainplugin/src/main/java/org/gvsig/app/project/documents/table/gui/EmulatedFieldExpression.java

View differences:

EmulatedFieldExpression.java
5 5
 */
6 6
package org.gvsig.app.project.documents.table.gui;
7 7

  
8
import java.util.ArrayList;
9
import java.util.List;
10
import java.util.logging.Level;
11
import java.util.logging.Logger;
12
import org.apache.commons.collections.ListUtils;
13
import org.apache.commons.lang3.StringUtils;
8 14
import org.gvsig.expressionevaluator.Code;
15
import org.gvsig.expressionevaluator.Code.Identifier;
9 16
import org.gvsig.fmap.dal.feature.EditableFeature;
10 17
import org.gvsig.fmap.dal.feature.Feature;
11 18
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
......
13 20
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
14 21
import org.gvsig.expressionevaluator.MutableSymbolTable;
15 22
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
23
import org.gvsig.fmap.dal.feature.FeatureType;
16 24
import org.gvsig.tools.ToolsLocator;
17 25
import org.gvsig.tools.dynobject.DynStruct;
26
import org.gvsig.tools.exception.BaseException;
18 27
import org.gvsig.tools.persistence.PersistenceManager;
19 28
import org.gvsig.tools.persistence.Persistent;
20 29
import org.gvsig.tools.persistence.PersistentState;
21 30
import org.gvsig.tools.persistence.exception.PersistenceException;
31
import org.gvsig.tools.visitor.VisitCanceledException;
32
import org.gvsig.tools.visitor.Visitor;
22 33

  
23 34
/**
24 35
 *
25 36
 * @author osc
26 37
 */
27 38
public class EmulatedFieldExpression implements FeatureAttributeEmulator, Persistent {
28
            
29
    String[] fields = null;
39

  
40
    String[] requiredFields = null;
30 41
    String code = "";
31
    private static final String EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME = 
32
            "EmulatedFieldExpression";
42
    Code codeCompiled = null;
43
    MutableSymbolTable symbolTable = null;
44
    ExpressionEvaluatorManager expressionManager = null;
45
    private static final String EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME
46
            = "EmulatedFieldExpression";
47
    private boolean valid;
48
    private String errorMessage;
33 49

  
34 50
    public EmulatedFieldExpression() {
35 51
    }
36 52

  
37
    
38
    EmulatedFieldExpression(String[] fields, String code){
39
        this.fields = fields;
53
    EmulatedFieldExpression(FeatureType featureType, String code) {
40 54
        this.setCode(code);
55
        this.checkVars(featureType);
56

  
41 57
    }
42
    
58

  
59
    public boolean isValid() {
60
        return this.valid;
61
    }
62

  
63
    private void checkVars(final FeatureType featureType) {
64
        this.valid = true;
65
        final List<String> theUsedFields = new ArrayList<>();
66
        final List<String> undefinedFields = new ArrayList<>();
67
        
68
        try {
69
            Code theCodeCompiled = this.getCodeCompiled();
70
            theCodeCompiled.accept(new Visitor() {
71
                @Override
72
                public void visit(Object obj) throws VisitCanceledException, BaseException {
73
                    Code code = (Code) obj;
74
                    if (code instanceof Identifier) {
75
                        String name = ((Identifier) code).name();
76
                        if (featureType.get(name) == null) {
77
                            undefinedFields.add(name);
78
                            valid = false;
79
                        } else {
80
                            theUsedFields.add(name);
81
                        }
82
                    }
83
                }
84
            });
85
            if(!undefinedFields.isEmpty()){
86
                this.errorMessage = "undefined fields " + StringUtils.join(undefinedFields,", ");
87
            }
88
        } catch (BaseException ex) {
89
            valid = false;
90
            this.errorMessage = ex.getMessage();
91
        }
92
        this.requiredFields = theUsedFields.toArray(new String[theUsedFields.size()]);
93
    }
94

  
43 95
    public static void registerPersistenceDefinition() {
44 96
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
45
    
46
        if (manager.getDefinition(EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME) == 
47
                null) {
97

  
98
        if (manager.getDefinition(EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME)
99
                == null) {
48 100
            DynStruct definition = manager.addDefinition(
49
                EmulatedFieldExpression.class,
50
                EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME, 
51
                EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME +       
52
                        " Persistent definition", 
53
                null, 
54
                null
101
                    EmulatedFieldExpression.class,
102
                    EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME,
103
                    EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME
104
                    + " Persistent definition",
105
                    null,
106
                    null
55 107
            );
56 108
            definition.addDynFieldString("code")
57
                .setMandatory(true)
58
                .setPersistent(true);
109
                    .setMandatory(true)
110
                    .setPersistent(true);
59 111
            definition.addDynFieldArray("fields")
60
                .setClassOfItems(String.class)
61
                .setMandatory(true)
62
                .setPersistent(true);
112
                    .setClassOfItems(String.class)
113
                    .setMandatory(true)
114
                    .setPersistent(true);
115
            definition.addDynFieldBoolean("valid")
116
                    .setMandatory(true)
117
                    .setPersistent(true);
63 118
        }
64 119
    }
65
        
120

  
121
    private Code getCodeCompiled() {
122
        if (this.codeCompiled == null) {
123
            this.codeCompiled = this.getExpressionManager().compile(code);
124
        }
125
        return this.codeCompiled;
126
    }
127

  
128
    private MutableSymbolTable getSymbolTable() {
129
        if (this.symbolTable == null) {
130
            this.symbolTable = this.getExpressionManager().createSymbolTable();
131
        }
132
        return this.symbolTable;
133
    }
134

  
135
    private ExpressionEvaluatorManager getExpressionManager() {
136
        if (this.expressionManager == null) {
137
            this.expressionManager
138
                    = ExpressionEvaluatorLocator.getManager();
139
        }
140
        return this.expressionManager;
141
    }
142

  
66 143
    @Override
67 144
    public Object get(Feature feature) {
68
        ExpressionEvaluatorManager expManager = 
69
                ExpressionEvaluatorLocator.getManager();
70
        MutableSymbolTable symbol = expManager.createSymbolTable();
71
        FeatureAttributeDescriptor[] descriptors = 
72
                feature.getType().getAttributeDescriptors();
73
        for (FeatureAttributeDescriptor descriptor : descriptors) {
74
            String fieldName = descriptor.getName();
145
        if (!this.valid) {
146
            return null;
147
        }
148
        MutableSymbolTable symbol = this.getSymbolTable();
149
        for (FeatureAttributeDescriptor descriptor : feature.getType()) {
75 150
            if (!descriptor.isComputed()) { // si eres tu mismo 
151
                String fieldName = descriptor.getName();
76 152
                Object value = feature.get(fieldName);
77 153
                symbol.setVar(fieldName, value);
78 154
            }
79 155
        }
80
        Code codeCompilation = expManager.compile(code);
81
        Object result = expManager.evaluate(symbol, codeCompilation);
156
        Code codeCompilation = this.getCodeCompiled();
157
        Object result = this.getExpressionManager().evaluate(symbol, codeCompilation);
82 158
        return result;
83 159
    }
84 160

  
......
94 170

  
95 171
    @Override
96 172
    public String[] getRequiredFieldNames() {
97
        return this.fields;
173
        return this.requiredFields;
98 174
    }
99
    
175

  
100 176
    public String getCode() {
101 177
        return this.code;
102 178
    }
103 179

  
104 180
    @Override
105 181
    public void saveToState(PersistentState state) throws PersistenceException {
106
        state.set("fields", this.getFields());
182
        state.set("fields", this.getRequiredFieldNames());
107 183
        state.set("code", this.getCode());
108
        
184
        state.set("valid", this.isValid());
185

  
109 186
    }
110 187

  
111 188
    @Override
112 189
    public void loadFromState(PersistentState state) throws PersistenceException {
113 190
        String stateCode = state.getString("code");
114 191
        String[] stateFields = state.getStringArray("fields");
192
        this.valid = state.getBoolean("valid");
115 193
        this.setCode(stateCode);
116 194
        this.setFields(stateFields);
117 195
    }
118 196

  
119
    public String[] getFields() {
120
        return this.fields;
121
    }
122 197
    private void setCode(String code) {
123 198
        this.code = code;
124 199
    }
125 200

  
126 201
    private void setFields(String[] fields) {
127
        this.fields = fields;
202
        this.requiredFields = fields;
128 203
    }
129
}
204

  
205
    public String getErrorMessage() {
206
        return this.errorMessage;
207
    }
208
}

Also available in: Unified diff