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 @ 45739

History | View | Annotate | Download (5.68 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 org.gvsig.fmap.dal.expressionevaluator.TableAttributeHandler;
27
import org.apache.commons.lang3.Range;
28
import org.apache.commons.lang3.StringUtils;
29
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
30
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
31
import org.gvsig.expressionevaluator.Function;
32
import org.gvsig.expressionevaluator.Interpreter;
33
import org.gvsig.expressionevaluator.MutableSymbolTable;
34
import org.gvsig.expressionevaluator.impl.DALFunctions;
35
import org.gvsig.expressionevaluator.spi.AbstractFunction;
36
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable;
37
import static org.gvsig.fmap.dal.DataManager.FUNCTION_CURRENT_ROW;
38
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
39
import org.gvsig.fmap.dal.feature.Feature;
40
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
41
import org.gvsig.fmap.dal.feature.FeatureStore;
42
import org.gvsig.fmap.dal.feature.FeatureType;
43

    
44
/**
45
 *
46
 * @author jjdelcerro
47
 */
48
@SuppressWarnings("UseSpecificCatch")
49
public class FeatureSymbolTableImpl extends AbstractSymbolTable implements FeatureSymbolTable {
50
  private static final String DAL_SYMBOL_TABLE_FEATURE = "DALSymbolTableFeature";
51

    
52
  private final Function current_row;
53
  private Feature feature;
54
  private FeatureType type;
55
  private String storeName;
56
  private final TableAttributeHandler storeAttributeHandler;
57

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

    
93
      @Override
94
      public String getName() {
95
        return storeName;
96
      }
97
    };
98
  }
99

    
100
  @Override
101
  public FeatureSymbolTableImpl clone() throws CloneNotSupportedException {
102
    FeatureSymbolTableImpl other = (FeatureSymbolTableImpl) super.clone();
103
    return other;
104
  }
105

    
106
  @Override
107
  public Function function(String name) {
108
    if (StringUtils.equalsIgnoreCase(name, FUNCTION_CURRENT_ROW)) {
109
      return this.current_row;
110
    }
111
    return super.function(name);
112
  }
113

    
114
  @Override
115
  public boolean exists(String name) {
116
    if (feature == null) {
117
      return false;
118
    }
119
    if (this.feature.hasValue(name)) {
120
        return true;
121
    }
122
    if (StringUtils.equalsIgnoreCase(name, this.storeName) ||
123
        StringUtils.equalsIgnoreCase(name, SYMBOL_CURRENT_TABLE) ||
124
        StringUtils.equalsIgnoreCase(name, SYMBOL_CURRENT_ROW) ) {
125
      return true;
126
    }
127
    return false;
128
  }
129

    
130
  @Override
131
  public Object value(String name) {
132
      if (feature == null) {
133
          return null;
134
      }
135
      try {
136
          return this.feature.get(name);
137
      } catch (Exception ex) {
138
          //DO NOTHING
139
      }
140
      if (StringUtils.equalsIgnoreCase(name, SYMBOL_CURRENT_ROW)) {
141
          return this.feature;
142
      }
143
      if (StringUtils.equalsIgnoreCase(name, this.storeName)
144
              || StringUtils.equalsIgnoreCase(name, SYMBOL_CURRENT_TABLE)) {
145
          return this.storeAttributeHandler;
146
      }
147
      return null;
148
  }
149

    
150
  @Override
151
  public boolean isSQLCompatible(String name) {
152
    if (this.type == null) {
153
      return super.isSQLCompatible(name);
154
    }
155
    FeatureAttributeDescriptor attrdesc = this.type.getAttributeDescriptor(name);
156
    if (attrdesc == null) {
157
      return true;
158
    }
159
    if (attrdesc.isComputed()) {
160
      return false;
161
    }
162
    return true;
163
  }
164

    
165
  @Override
166
  public void setFeature(Feature feature) {
167
    this.feature = feature;
168
    if (this.type == null && this.feature!=null ) {
169
      this.type = feature.getType();
170
      FeatureStore store = feature.getStore();
171
      if (store != null) {
172
        this.storeName = store.getName();
173
      }
174
    }
175
  }
176

    
177
  @Override
178
  public MutableSymbolTable createParent() {
179
    ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
180

    
181
    MutableSymbolTable symbolTable = expManager.createSymbolTable();
182
    symbolTable.addSymbolTable(this);
183
    return symbolTable;
184
  }
185

    
186
}