Statistics
| Revision:

svn-gvsig-desktop / 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 @ 43981

History | View | Annotate | Download (6.97 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.app.project.documents.table.gui;
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;
14
import org.gvsig.expressionevaluator.Code;
15
import org.gvsig.expressionevaluator.Code.Identifier;
16
import org.gvsig.fmap.dal.feature.EditableFeature;
17
import org.gvsig.fmap.dal.feature.Feature;
18
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
19
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
20
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
21
import org.gvsig.expressionevaluator.MutableSymbolTable;
22
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
23
import org.gvsig.fmap.dal.feature.FeatureType;
24
import org.gvsig.tools.ToolsLocator;
25
import org.gvsig.tools.dynobject.DynStruct;
26
import org.gvsig.tools.exception.BaseException;
27
import org.gvsig.tools.persistence.PersistenceManager;
28
import org.gvsig.tools.persistence.Persistent;
29
import org.gvsig.tools.persistence.PersistentState;
30
import org.gvsig.tools.persistence.exception.PersistenceException;
31
import org.gvsig.tools.visitor.VisitCanceledException;
32
import org.gvsig.tools.visitor.Visitor;
33

    
34
/**
35
 *
36
 * @author osc
37
 */
38
public class EmulatedFieldExpression implements FeatureAttributeEmulator, Persistent {
39

    
40
    String[] requiredFields = null;
41
    String code = "";
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;
49

    
50
    public EmulatedFieldExpression() {
51
    }
52

    
53
    EmulatedFieldExpression(FeatureType featureType, String code) {
54
        this.setCode(code);
55
        this.checkVars(featureType);
56

    
57
    }
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

    
95
    public static void registerPersistenceDefinition() {
96
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
97

    
98
        if (manager.getDefinition(EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME)
99
                == null) {
100
            DynStruct definition = manager.addDefinition(
101
                    EmulatedFieldExpression.class,
102
                    EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME,
103
                    EMULEFIELDEXP_PERSISTENCE_DEFINITION_NAME
104
                    + " Persistent definition",
105
                    null,
106
                    null
107
            );
108
            definition.addDynFieldString("code")
109
                    .setMandatory(true)
110
                    .setPersistent(true);
111
            definition.addDynFieldArray("fields")
112
                    .setClassOfItems(String.class)
113
                    .setMandatory(true)
114
                    .setPersistent(true);
115
            definition.addDynFieldBoolean("valid")
116
                    .setMandatory(true)
117
                    .setPersistent(true);
118
        }
119
    }
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

    
143
    @Override
144
    public Object get(Feature feature) {
145
        if (!this.valid) {
146
            return null;
147
        }
148
        MutableSymbolTable symbol = this.getSymbolTable();
149
        for (FeatureAttributeDescriptor descriptor : feature.getType()) {
150
            if (!descriptor.isComputed()) { // si eres tu mismo 
151
                String fieldName = descriptor.getName();
152
                Object value = feature.get(fieldName);
153
                symbol.setVar(fieldName, value);
154
            }
155
        }
156
        Code codeCompilation = this.getCodeCompiled();
157
        Object result = this.getExpressionManager().evaluate(symbol, codeCompilation);
158
        return result;
159
    }
160

    
161
    @Override
162
    public void set(EditableFeature feature, Object value) {
163
        //throw new UnsupportedOperationException("Not supported yet."); 
164
    }
165

    
166
    @Override
167
    public boolean allowSetting() {
168
        return false;
169
    }
170

    
171
    @Override
172
    public String[] getRequiredFieldNames() {
173
        return this.requiredFields;
174
    }
175

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

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

    
186
    }
187

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

    
197
    private void setCode(String code) {
198
        this.code = code;
199
    }
200

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

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