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

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

    
46
/**
47
 *
48
 * @author jjdelcerro
49
 */
50
@SuppressWarnings("UseSpecificCatch")
51
public class FeatureSymbolTableImpl extends AbstractSymbolTable implements FeatureSymbolTable {
52

    
53
  private final Function current_row;
54
  private Feature feature;
55
  private FeatureType type;
56
  private String storeName;
57
  private DynObject storeAdapter;
58

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

    
87
        @Override
88
        public boolean hasDynValue(String name) {
89
          if( feature.getType().get(name)!=null ) {
90
            return true;
91
          }
92
          return feature.hasExtraValue(name);
93
        }
94
        
95
      };
96
  }
97

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

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

    
112
  @Override
113
  public boolean exists(String name) {
114
    if (type == null) {
115
      return false;
116
    }
117
    if ( type.get(name) != null) {
118
      return true;
119
    }
120
    if (StringUtils.equalsIgnoreCase(name, this.storeName)) {
121
      return true;
122
    }
123
    if( feature==null ) {
124
      return false;
125
    }
126
    if (this.feature.hasExtraValue(name)) {
127
      return true;
128
    }
129
    return false;
130
  }
131

    
132
  @Override
133
  public Object value(String name) {
134
    if (feature == null) {
135
      return null;
136
    }
137
    if (StringUtils.equalsIgnoreCase(name, this.storeName)) {
138
      return this.storeAdapter;
139
    }
140
    try {
141
      return this.feature.get(name);
142
    } catch (Exception ex) {
143
      return this.feature.getExtraValue(name);
144
    }
145
  }
146

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

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

    
174
  @Override
175
  public MutableSymbolTable createParent() {
176
    ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager();
177

    
178
    MutableSymbolTable symbolTable = expManager.createSymbolTable();
179
    symbolTable.addSymbolTable(this);
180
    return symbolTable;
181
  }
182

    
183
}