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