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 / DefaultFeatureSymbolTable.java @ 44259
History | View | Annotate | Download (9.48 KB)
1 | 43983 | jjdelcerro | package org.gvsig.fmap.dal.impl.expressionevaluator; |
---|---|---|---|
2 | |||
3 | import org.apache.commons.lang3.Range; |
||
4 | 44259 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder; |
5 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
6 | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
||
7 | 44259 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
8 | import org.gvsig.expressionevaluator.ExpressionUtils; |
||
9 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
10 | 44100 | jjdelcerro | import org.gvsig.expressionevaluator.MutableSymbolTable; |
11 | 43983 | jjdelcerro | import org.gvsig.expressionevaluator.spi.AbstractFunction; |
12 | import org.gvsig.expressionevaluator.spi.AbstractSymbolTable; |
||
13 | 44253 | jjdelcerro | import org.gvsig.fmap.dal.DALLocator; |
14 | 43987 | jjdelcerro | import org.gvsig.fmap.dal.DataManager; |
15 | 44253 | jjdelcerro | import org.gvsig.fmap.dal.DataStore; |
16 | import org.gvsig.fmap.dal.StoresRepository; |
||
17 | 43983 | jjdelcerro | import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable; |
18 | import org.gvsig.fmap.dal.feature.Feature; |
||
19 | 44205 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
20 | 43983 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureSelection; |
21 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
22 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
23 | 44259 | jjdelcerro | import static org.gvsig.fmap.dal.impl.expressionevaluator.DALSymbolTable.FUNCTION_FEATURES; |
24 | 44253 | jjdelcerro | import org.gvsig.fmap.dal.impl.expressionevaluator.DALSymbolTable.FeaturesFunction; |
25 | 44259 | jjdelcerro | import org.gvsig.tools.dynobject.Tags; |
26 | 43984 | jjdelcerro | import org.gvsig.tools.script.Script; |
27 | import org.gvsig.tools.util.UnmodifiableBasicList; |
||
28 | 43983 | jjdelcerro | |
29 | /**
|
||
30 | *
|
||
31 | * @author jjdelcerro
|
||
32 | */
|
||
33 | 44259 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
34 | 43983 | jjdelcerro | public class DefaultFeatureSymbolTable extends AbstractSymbolTable implements FeatureSymbolTable { |
35 | 44259 | jjdelcerro | |
36 | public static final String FUNCTION_FOREING_VALUE = "FOREING_VALUE"; |
||
37 | |||
38 | public class ForeingValueFunction extends AbstractFunction { |
||
39 | |||
40 | public ForeingValueFunction() {
|
||
41 | super(
|
||
42 | "Data access",
|
||
43 | FUNCTION_FOREING_VALUE, |
||
44 | Range.is(1),
|
||
45 | "Return the value of a field throw a relation from other tables.",
|
||
46 | FUNCTION_FOREING_VALUE + "({{field_name}})",
|
||
47 | new String[]{ |
||
48 | "fieldName - dot separated field path"
|
||
49 | }, |
||
50 | "OBJECT"
|
||
51 | ); |
||
52 | } |
||
53 | |||
54 | @Override
|
||
55 | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
||
56 | ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
||
57 | String fullFieldName = getStr(args, 0); |
||
58 | String[] nameParts = fullFieldName.split("[.]"); |
||
59 | Feature currentFeature = feature; |
||
60 | try {
|
||
61 | String namePart;
|
||
62 | Object value;
|
||
63 | for (int i = 0; i < nameParts.length-1; i++) { |
||
64 | namePart = nameParts[i]; |
||
65 | value = currentFeature.get(namePart); |
||
66 | |||
67 | FeatureAttributeDescriptor attrdesc = currentFeature.getType().getAttributeDescriptor(namePart); |
||
68 | Tags tags = attrdesc.getTags(); |
||
69 | String storeName = tags.getString(DataManager.DAL_FOREING_TABLE, null); |
||
70 | String attrName = tags.getString(DataManager.DAL_FOREING_CODE, null); |
||
71 | |||
72 | StoresRepository repository = currentFeature.getStore().getStoresRepository(); |
||
73 | FeatureStore store = (FeatureStore) repository.get(storeName); |
||
74 | currentFeature = store.findFirst( |
||
75 | builder.eq( |
||
76 | builder.constant(value), |
||
77 | builder.variable(attrName) |
||
78 | ).toString() |
||
79 | ); |
||
80 | if( currentFeature==null ) { |
||
81 | return null; |
||
82 | } |
||
83 | } |
||
84 | namePart = nameParts[nameParts.length-1];
|
||
85 | value = currentFeature.get(namePart); |
||
86 | return value;
|
||
87 | } catch (ExpressionRuntimeException ex) {
|
||
88 | throw ex;
|
||
89 | } catch (Exception ex) { |
||
90 | throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_FEATURES + "' function", ex); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | } |
||
95 | |||
96 | 44253 | jjdelcerro | private class LocalFeaturesFunction extends FeaturesFunction { |
97 | 43983 | jjdelcerro | |
98 | 44253 | jjdelcerro | public LocalFeaturesFunction() {
|
99 | super();
|
||
100 | } |
||
101 | |||
102 | @Override
|
||
103 | protected DataStore getStore(String storeName) { |
||
104 | DataManager dataManager = DALLocator.getDataManager(); |
||
105 | DataStore store = null;
|
||
106 | 44259 | jjdelcerro | if (feature != null) { |
107 | 44253 | jjdelcerro | store = feature.getStore(); |
108 | 44259 | jjdelcerro | store = store.getChildren().get(storeName); |
109 | 44253 | jjdelcerro | } |
110 | 44259 | jjdelcerro | if (store == null) { |
111 | 44253 | jjdelcerro | StoresRepository repository = dataManager.getStoresRepository(); |
112 | 44259 | jjdelcerro | if (repository.containsKey(storeName)) {
|
113 | 44253 | jjdelcerro | store = repository.get(storeName); |
114 | } |
||
115 | } |
||
116 | return store;
|
||
117 | } |
||
118 | } |
||
119 | |||
120 | 43983 | jjdelcerro | private class FeatureFunction extends AbstractFunction { |
121 | |||
122 | public FeatureFunction() {
|
||
123 | super(
|
||
124 | "Data access",
|
||
125 | "feature",
|
||
126 | Range.is(0),
|
||
127 | "Access to the current feature object when used in a filter.\n"
|
||
128 | + "Return null if used outer a filter.",
|
||
129 | "feature()",
|
||
130 | null,
|
||
131 | "Feature"
|
||
132 | ); |
||
133 | } |
||
134 | |||
135 | @Override
|
||
136 | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
||
137 | return feature;
|
||
138 | } |
||
139 | |||
140 | } |
||
141 | |||
142 | public class FeatureStoreFunction extends AbstractFunction { |
||
143 | |||
144 | public FeatureStoreFunction() {
|
||
145 | super(
|
||
146 | "Data access",
|
||
147 | "store",
|
||
148 | Range.is(0),
|
||
149 | "Access to the current store object when used in a filter.\n"
|
||
150 | + "Return null if used outer a filter.",
|
||
151 | "store()",
|
||
152 | null,
|
||
153 | "FeatureStore"
|
||
154 | ); |
||
155 | } |
||
156 | |||
157 | @Override
|
||
158 | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
||
159 | 44259 | jjdelcerro | if (feature == null) { |
160 | 43987 | jjdelcerro | return null; |
161 | } |
||
162 | 43983 | jjdelcerro | return feature.getStore();
|
163 | } |
||
164 | |||
165 | } |
||
166 | |||
167 | public class IsSelectedFunction extends AbstractFunction { |
||
168 | |||
169 | public IsSelectedFunction() {
|
||
170 | super(
|
||
171 | "Data access",
|
||
172 | "isSelected",
|
||
173 | Range.is(0),
|
||
174 | "Return if the current feature is selected in the store when used in a filter.\n"
|
||
175 | + "Return false if used outer a filter.",
|
||
176 | "isSelected()",
|
||
177 | null,
|
||
178 | "Boolean"
|
||
179 | ); |
||
180 | } |
||
181 | |||
182 | @Override
|
||
183 | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
||
184 | 44259 | jjdelcerro | if (feature == null) { |
185 | 43987 | jjdelcerro | return false; |
186 | } |
||
187 | 43983 | jjdelcerro | FeatureStore store = feature.getStore(); |
188 | FeatureSelection selection = store.getFeatureSelection(); |
||
189 | return selection.isSelected(feature);
|
||
190 | } |
||
191 | |||
192 | } |
||
193 | |||
194 | private Feature feature;
|
||
195 | private FeatureType type;
|
||
196 | |||
197 | 43987 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
198 | 43983 | jjdelcerro | public DefaultFeatureSymbolTable() {
|
199 | 43987 | jjdelcerro | super(DataManager.FEATURE_SYMBOL_TABLE);
|
200 | 43983 | jjdelcerro | |
201 | this.addFunction(new FeatureFunction()); |
||
202 | this.addFunction(new FeatureStoreFunction()); |
||
203 | this.addFunction(new IsSelectedFunction()); |
||
204 | 44253 | jjdelcerro | this.addFunction(new LocalFeaturesFunction()); |
205 | 44259 | jjdelcerro | this.addFunction(new ForeingValueFunction()); |
206 | 43983 | jjdelcerro | } |
207 | |||
208 | 43984 | jjdelcerro | @SuppressWarnings("OverridableMethodCallInConstructor") |
209 | public DefaultFeatureSymbolTable(Script userScript, UnmodifiableBasicList<Script> scripts) {
|
||
210 | this();
|
||
211 | if (userScript != null) { |
||
212 | this.getScripts().add(userScript);
|
||
213 | } |
||
214 | if (scripts != null && !scripts.isEmpty()) { |
||
215 | for (Script script : scripts) {
|
||
216 | this.getScripts().add(script);
|
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | |||
221 | 43983 | jjdelcerro | @Override
|
222 | public FeatureSymbolTable clone() throws CloneNotSupportedException { |
||
223 | DefaultFeatureSymbolTable other = (DefaultFeatureSymbolTable) super.clone();
|
||
224 | return other;
|
||
225 | } |
||
226 | |||
227 | @Override
|
||
228 | public boolean exists(String name) { |
||
229 | 44259 | jjdelcerro | if (type != null && type.get(name) != null) { |
230 | 43983 | jjdelcerro | return true; |
231 | } |
||
232 | return false; |
||
233 | } |
||
234 | |||
235 | @Override
|
||
236 | public Object value(String name) { |
||
237 | 44259 | jjdelcerro | if (feature == null) { |
238 | 43987 | jjdelcerro | return null; |
239 | } |
||
240 | 43983 | jjdelcerro | return this.feature.get(name); |
241 | } |
||
242 | |||
243 | @Override
|
||
244 | 44205 | jjdelcerro | public boolean isSQLCompatible(String name) { |
245 | 44259 | jjdelcerro | if (this.type == null) { |
246 | 44205 | jjdelcerro | return super.isSQLCompatible(name); |
247 | } |
||
248 | FeatureAttributeDescriptor attrdesc = this.type.getAttributeDescriptor(name);
|
||
249 | 44259 | jjdelcerro | if (attrdesc == null) { |
250 | 44205 | jjdelcerro | return true; |
251 | } |
||
252 | 44259 | jjdelcerro | if (attrdesc.isComputed()) {
|
253 | 44205 | jjdelcerro | return false; |
254 | } |
||
255 | return true; |
||
256 | } |
||
257 | |||
258 | @Override
|
||
259 | 43983 | jjdelcerro | public void setFeature(Feature feature) { |
260 | this.feature = feature;
|
||
261 | this.type = feature.getType();
|
||
262 | } |
||
263 | |||
264 | 44100 | jjdelcerro | @Override
|
265 | public MutableSymbolTable createParent() {
|
||
266 | ExpressionEvaluatorManager expManager = ExpressionEvaluatorLocator.getManager(); |
||
267 | |||
268 | MutableSymbolTable symbolTable = expManager.createSymbolTable(); |
||
269 | symbolTable.addSymbolTable(this);
|
||
270 | return symbolTable;
|
||
271 | } |
||
272 | |||
273 | 43983 | jjdelcerro | public static void selfRegister() { |
274 | ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
||
275 | 44128 | jjdelcerro | manager.registerSymbolTable(new DefaultfeatureSymbolTableFactory());
|
276 | 43983 | jjdelcerro | } |
277 | } |