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 / DefaultFeatureAttributeEmulatorExpression.java @ 43989

History | View | Annotate | Download (6.33 KB)

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

    
3
import java.util.ArrayList;
4
import java.util.List;
5
import org.apache.commons.lang3.StringUtils;
6
import org.gvsig.expressionevaluator.Code;
7
import org.gvsig.expressionevaluator.Code.Identifier;
8
import org.gvsig.expressionevaluator.Expression;
9
import org.gvsig.fmap.dal.feature.EditableFeature;
10
import org.gvsig.fmap.dal.feature.Feature;
11
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
12
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
13
import org.gvsig.expressionevaluator.SymbolTable;
14
import org.gvsig.fmap.dal.DataManager;
15
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
16
import org.gvsig.fmap.dal.feature.FeatureType;
17
import org.gvsig.tools.ToolsLocator;
18
import org.gvsig.tools.dynobject.DynStruct;
19
import org.gvsig.tools.exception.BaseException;
20
import org.gvsig.tools.persistence.PersistenceManager;
21
import org.gvsig.tools.persistence.PersistentState;
22
import org.gvsig.tools.persistence.exception.PersistenceException;
23
import org.gvsig.tools.visitor.VisitCanceledException;
24
import org.gvsig.tools.visitor.Visitor;
25
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
26

    
27
/**
28
 *
29
 * @author osc
30
 */
31
@SuppressWarnings("UseSpecificCatch")
32
public class DefaultFeatureAttributeEmulatorExpression implements FeatureAttributeEmulatorExpression {
33

    
34
    private static final String EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME = "EmulatedFieldExpression";
35

    
36
    private String[] requiredFields = null;
37
    private Expression expression = null;
38
    private SymbolTable symbolTable = null;
39
    private FeatureSymbolTable featureSymbolTable = null;
40
    
41
    private boolean valid;
42
    private String errorMessage;
43

    
44
    public DefaultFeatureAttributeEmulatorExpression() {
45
    }
46

    
47
    public DefaultFeatureAttributeEmulatorExpression(FeatureType featureType, Expression expression) {
48
        this.expression = expression;
49
        this.checkVars(featureType);
50
    }
51

    
52
    private void checkVars(final FeatureType featureType) {
53
        this.valid = true;
54
        final List<String> theUsedFields = new ArrayList<>();
55
        final List<String> undefinedFields = new ArrayList<>();
56
        
57
        try {
58
            Code code = this.expression.getCode();
59
            code.accept(new Visitor() {
60
                @Override
61
                public void visit(Object obj) throws VisitCanceledException, BaseException {
62
                    Code code = (Code) obj;
63
                    if (code instanceof Identifier) {
64
                        String name = ((Identifier) code).name();
65
                        if (featureType.get(name) == null) {
66
                            undefinedFields.add(name);
67
                            valid = false;
68
                        } else {
69
                            theUsedFields.add(name);
70
                        }
71
                    }
72
                }
73
            });
74
            if(!undefinedFields.isEmpty()){
75
                this.errorMessage = "undefined fields " + StringUtils.join(undefinedFields,", ");
76
            }
77
        } catch (Exception ex) {
78
            valid = false;
79
            this.errorMessage = ex.getMessage();
80
        }
81
        this.requiredFields = theUsedFields.toArray(new String[theUsedFields.size()]);
82
    }
83

    
84
    @Override
85
    public SymbolTable getSymbolTable() {
86
        if (this.symbolTable == null) {
87
            ExpressionEvaluatorManager expressionManager = ExpressionEvaluatorLocator.getManager();
88
            this.featureSymbolTable = (FeatureSymbolTable) expressionManager.getSymbolTable(
89
                    DataManager.FEATURE_SYMBOL_TABLE
90
            );
91
            this.symbolTable = expressionManager.createSymbolTable();
92
            this.symbolTable.addSymbolTable(this.featureSymbolTable);
93
        }
94
        return this.symbolTable;
95
    }
96

    
97
    private FeatureSymbolTable getFeatureSymbolTable() {
98
        if (this.featureSymbolTable == null) {
99
            this.getSymbolTable();
100
        }
101
        return this.featureSymbolTable;
102
    }
103

    
104
    @Override
105
    public boolean isValid() {
106
        return this.valid;
107
    }
108

    
109
    @Override
110
    public Object get(Feature feature) {
111
        if (!this.valid) {
112
            return null;
113
        }
114
        this.getFeatureSymbolTable().setFeature(feature);
115
        Object result = this.expression.execute(this.getSymbolTable());
116
        return result;
117
    }
118

    
119
    @Override
120
    public void set(EditableFeature feature, Object value) {
121
        // Do nothing
122
    }
123

    
124
    @Override
125
    public boolean allowSetting() {
126
        return false;
127
    }
128

    
129
    @Override
130
    public String[] getRequiredFieldNames() {
131
        return this.requiredFields;
132
    }
133

    
134
    @Override
135
    public Expression getExpression() {
136
        return this.expression;
137
    }
138

    
139
    @Override
140
    public String getErrorMessage() {
141
        return this.errorMessage;
142
    }
143

    
144
    @Override
145
    public void saveToState(PersistentState state) throws PersistenceException {
146
        state.set("fields", this.requiredFields);
147
        state.set("expression", this.expression);
148
        state.set("valid", this.valid);
149

    
150
    }
151

    
152
    @Override
153
    public void loadFromState(PersistentState state) throws PersistenceException {
154
        this.valid = state.getBoolean("valid");
155
        this.expression = (Expression) state.get("expression");
156
        this.requiredFields = state.getStringArray("fields");
157
    }
158

    
159
    public static void registerPersistenceDefinition() {
160
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
161

    
162
        if (manager.getDefinition(EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME)
163
                == null) {
164
            DynStruct definition = manager.addDefinition(DefaultFeatureAttributeEmulatorExpression.class,
165
                    EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME,
166
                    EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME
167
                    + " Persistent definition",
168
                    null,
169
                    null
170
            );
171
            definition.addDynFieldObject("expression")
172
                    .setMandatory(true)
173
                    .setPersistent(true)
174
                    .setClassOfValue(Expression.class);
175
            definition.addDynFieldArray("fields")
176
                    .setClassOfItems(String.class)
177
                    .setMandatory(true)
178
                    .setPersistent(true);
179
            definition.addDynFieldBoolean("valid")
180
                    .setMandatory(true)
181
                    .setPersistent(true);
182
        }
183
    }
184

    
185
}