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 / expressionevaluator / impl / symboltable / FeatureSymbolTableImpl.java @ 47697

History | View | Annotate | Download (6.74 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.expressionevaluator.impl.symboltable;
25

    
26
import java.util.ArrayList;
27
import java.util.Collection;
28
import java.util.Collections;
29
import java.util.List;
30
import org.gvsig.fmap.dal.expressionevaluator.TableAttributeHandler;
31
import org.apache.commons.lang3.Range;
32
import org.apache.commons.lang3.StringUtils;
33
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
34
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
35
import org.gvsig.expressionevaluator.Function;
36
import org.gvsig.expressionevaluator.Interpreter;
37
import org.gvsig.expressionevaluator.MutableSymbolTable;
38
import org.gvsig.expressionevaluator.impl.DALFunctions;
39
import org.gvsig.expressionevaluator.spi.AbstractFunction;
40
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
41
import static org.gvsig.fmap.dal.DataManager.FUNCTION_CURRENT_ROW;
42
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
43
import org.gvsig.fmap.dal.feature.EditableFeature;
44
import org.gvsig.fmap.dal.feature.Feature;
45
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
46
import org.gvsig.fmap.dal.feature.FeatureStore;
47
import org.gvsig.fmap.dal.feature.FeatureType;
48

    
49
/**
50
 *
51
 * @author jjdelcerro
52
 */
53
@SuppressWarnings("UseSpecificCatch")
54
public class FeatureSymbolTableImpl extends AbstractSymbolTable implements FeatureSymbolTable {
55
  private static final String DAL_SYMBOL_TABLE_FEATURE = "DALSymbolTableFeature";
56

    
57
  private final Function current_row;
58
  private Feature feature;
59
  private FeatureType type;
60
  private String storeName;
61
  private final TableAttributeHandler storeAttributeHandler;
62

    
63
  @SuppressWarnings("OverridableMethodCallInConstructor")
64
  public FeatureSymbolTableImpl() {
65
    this(DAL_SYMBOL_TABLE_FEATURE);
66
  }
67
  
68
  public FeatureSymbolTableImpl(String name) {
69
    super(name);
70
    this.current_row = new AbstractFunction(
71
            DALFunctions.GROUP_DATA_ACCESS,
72
            FUNCTION_CURRENT_ROW,
73
            Range.is(0),
74
            "Return the current row when used in a table filter.\n"
75
            + "Return null if used outer a table filter.",
76
            FUNCTION_CURRENT_ROW + "()",
77
            null,
78
            "Feature"
79
    ) {
80
      @Override
81
      public Object call(Interpreter interpreter, Object[] args) throws Exception {
82
        return feature;
83
      }
84
    };
85
    this.storeAttributeHandler = new TableAttributeHandler() {
86
      @Override
87
      public Object get(String name) {
88
        if( feature==null ) {
89
            return null;
90
        }
91
        try {
92
          return feature.get(name);
93
        } catch (Exception ex) {
94
          return feature.getExtraValue(name);
95
        }
96
      }
97

    
98
      @Override
99
      public String getName() {
100
        return storeName;
101
      }
102
    };
103
  }
104

    
105
    @Override
106
    public Collection<String> localvariables() {
107
        List<String> v = new ArrayList<>();
108
        v.add(SYMBOL_CURRENT_TABLE);
109
        v.add(SYMBOL_CURRENT_ROW);
110
        try {
111
            if( feature != null ) {
112
                for (FeatureAttributeDescriptor attr : feature.getType()) {
113
                    v.add(attr.getName());
114
                }
115
            }
116
        } catch(Throwable t) {
117

    
118
        }
119
        v.addAll(super.localvariables());
120
        return Collections.unmodifiableCollection(v);        
121
    }
122
  
123
  @Override
124
  public FeatureSymbolTableImpl clone() throws CloneNotSupportedException {
125
    FeatureSymbolTableImpl other = (FeatureSymbolTableImpl) super.clone();
126
    return other;
127
  }
128

    
129
  @Override
130
  public Function function(String name) {
131
    if (StringUtils.equalsIgnoreCase(name, FUNCTION_CURRENT_ROW)) {
132
      return this.current_row;
133
    }
134
    return super.function(name);
135
  }
136

    
137
  @Override
138
  public boolean exists(String name) {
139
    if (feature != null) {
140
        if (this.feature.hasValue(name)) {
141
            return true;
142
        }
143
    }
144
    if (StringUtils.equalsIgnoreCase(name, this.storeName) ||
145
        StringUtils.equalsIgnoreCase(name, SYMBOL_CURRENT_TABLE) ||
146
        StringUtils.equalsIgnoreCase(name, SYMBOL_CURRENT_ROW) ) {
147
      return true;
148
    }
149
    return super.exists(name);
150
  }
151

    
152
  @Override
153
  public Object value(String name) {
154
      if (feature != null) {          
155
        try {
156
            return this.feature.get(name);
157
        } catch (Exception ex) {
158
            //DO NOTHING
159
        }
160
        if (StringUtils.equalsIgnoreCase(name, SYMBOL_CURRENT_ROW)) {
161
            return this.feature;
162
        }
163
      }
164
      if (StringUtils.equalsIgnoreCase(name, this.storeName)
165
              || StringUtils.equalsIgnoreCase(name, SYMBOL_CURRENT_TABLE)) {
166
          return this.storeAttributeHandler;
167
      }
168
      return super.value(name);
169
  }
170

    
171
  public void setVar(String name, Object value) {
172
      if (feature instanceof EditableFeature ) {
173
          if( this.feature.hasValue(name) ) {
174
              ((EditableFeature)this.feature).set(name, value);
175
              return;
176
          }
177
      }
178
      super.setVar(name, value);
179
  }
180
  
181
  @Override
182
  public boolean isSQLCompatible(String name) {
183
    if (this.type == null) {
184
      return super.isSQLCompatible(name);
185
    }
186
    FeatureAttributeDescriptor attrdesc = this.type.getAttributeDescriptor(name);
187
    if (attrdesc == null) {
188
      return true;
189
    }
190
    if (attrdesc.isComputed()) {
191
      return false;
192
    }
193
    return true;
194
  }
195

    
196
  @Override
197
  public void setFeature(Feature feature) {
198
    this.feature = feature;
199
    if (this.type == null && this.feature!=null ) {
200
      this.type = feature.getType();
201
      FeatureStore store = feature.getStore();
202
      if (store != null) {
203
        this.storeName = store.getName();
204
      }
205
    }
206
  }
207

    
208
  @Override
209
  public MutableSymbolTable createParent() {
210
    ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
211

    
212
    MutableSymbolTable symbolTable = expManager.createSymbolTable();
213
    symbolTable.addSymbolTable(this);
214
    return symbolTable;
215
  }
216

    
217
    @Override
218
    public Feature getFeature() {
219
        return this.feature;
220
    }
221

    
222
}