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 / DALSymbolTable.java @ 44533
History | View | Annotate | Download (4.3 KB)
1 |
package org.gvsig.fmap.dal.impl.expressionevaluator; |
---|---|
2 |
|
3 |
import java.util.List; |
4 |
import org.apache.commons.lang3.Range; |
5 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
6 |
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
7 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
8 |
import org.gvsig.expressionevaluator.Interpreter; |
9 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
10 |
import org.gvsig.expressionevaluator.spi.AbstractSymbolTable; |
11 |
import org.gvsig.fmap.dal.DALLocator; |
12 |
import org.gvsig.fmap.dal.DataManager; |
13 |
import org.gvsig.fmap.dal.DataStore; |
14 |
import org.gvsig.fmap.dal.feature.Feature; |
15 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
16 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
17 |
|
18 |
/**
|
19 |
*
|
20 |
* @author jjdelcerro
|
21 |
*/
|
22 |
@SuppressWarnings("UseSpecificCatch") |
23 |
public class DALSymbolTable extends AbstractSymbolTable { |
24 |
|
25 |
public static final String FUNCTION_FEATURES = "FEATURES"; |
26 |
|
27 |
public static class FeaturesFunction extends AbstractFunction { |
28 |
|
29 |
public FeaturesFunction() {
|
30 |
super(
|
31 |
"Data access",
|
32 |
FUNCTION_FEATURES, |
33 |
Range.between(1, 3), |
34 |
"Return a list of the features selecteds with filter in the store.",
|
35 |
FUNCTION_FEATURES + "({{store_name}}, filter, order)",
|
36 |
new String[]{ |
37 |
"store - data store name to be used",
|
38 |
"where - Optional. String value with a filter expression",
|
39 |
"order - Optional. String value with the order. must be a string with the names of separate fields with commas"
|
40 |
}, |
41 |
"LIST"
|
42 |
); |
43 |
} |
44 |
|
45 |
@Override
|
46 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
47 |
String storeName = getStr(args, 0); |
48 |
String where = null; |
49 |
String order = null; |
50 |
switch (args.length) {
|
51 |
case 2: |
52 |
where = getStr(args, 1);
|
53 |
break;
|
54 |
case 3: |
55 |
where = getStr(args, 1);
|
56 |
order = getStr(args, 2);
|
57 |
break;
|
58 |
} |
59 |
try {
|
60 |
DataStore store = this.getStore(storeName);
|
61 |
if (store == null || !(store instanceof FeatureStore)) { |
62 |
throw new ExpressionRuntimeException("Cant locate the store '" + storeName + "' in function '" + FUNCTION_FEATURES + "'."); |
63 |
} |
64 |
if (!(store instanceof FeatureStore)) { |
65 |
throw new ExpressionRuntimeException("The store'" + storeName + "' is not valid for function '" + FUNCTION_FEATURES + "', a FeatureStore is required."); |
66 |
} |
67 |
FeatureStore featureStore = (FeatureStore) store; |
68 |
List<Feature> features;
|
69 |
if (where == null && order == null) { |
70 |
features = featureStore.getFeatures(); |
71 |
} else {
|
72 |
FeatureQuery query = featureStore.createFeatureQuery(); |
73 |
if (where != null) { |
74 |
query.addFilter(where); |
75 |
} |
76 |
if (order != null) { |
77 |
query.getOrder().add(order); |
78 |
} |
79 |
query.retrievesAllAttributes(); |
80 |
features = featureStore.getFeatures(query); |
81 |
} |
82 |
return features;
|
83 |
} catch (ExpressionRuntimeException ex) {
|
84 |
throw ex;
|
85 |
} catch (Exception ex) { |
86 |
throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_FEATURES + "' function", ex); |
87 |
} |
88 |
} |
89 |
|
90 |
protected DataStore getStore(String storeName) { |
91 |
DataManager dataManager = DALLocator.getDataManager(); |
92 |
DataStore store = dataManager.getStoresRepository().getStore(storeName); |
93 |
return store;
|
94 |
} |
95 |
} |
96 |
|
97 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
98 |
public DALSymbolTable() {
|
99 |
super(DataManager.DAL_SYMBOL_TABLE);
|
100 |
|
101 |
this.addFunction(new FeaturesFunction()); |
102 |
} |
103 |
|
104 |
public static void selfRegister() { |
105 |
ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager(); |
106 |
manager.registerSymbolTable(new DALSymbolTableFactory());
|
107 |
} |
108 |
|
109 |
} |