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 / function / dataaccess / AbstractFeatureFunction.java @ 47053

History | View | Annotate | Download (4.08 KB)

1
package org.gvsig.expressionevaluator.impl.function.dataaccess;
2

    
3
import javax.json.JsonObject;
4
import org.apache.commons.lang3.Range;
5
import org.apache.commons.lang3.StringUtils;
6
import org.gvsig.expressionevaluator.I18N;
7
import org.gvsig.expressionevaluator.Interpreter;
8
import org.gvsig.expressionevaluator.spi.AbstractFunction;
9
import org.gvsig.expressionevaluator.spi.JsonUtils;
10
import org.gvsig.fmap.dal.DALLocator;
11
import org.gvsig.fmap.dal.DataManager;
12
import static org.gvsig.fmap.dal.DataManager.FUNCTION_CURRENT_ROW;
13
import static org.gvsig.fmap.dal.DataManager.FUNCTION_CURRENT_STORE;
14
import org.gvsig.fmap.dal.DataStore;
15
import org.gvsig.fmap.dal.feature.EditableFeatureType;
16
import org.gvsig.fmap.dal.feature.Feature;
17
import org.gvsig.fmap.dal.feature.FeatureStore;
18
import org.gvsig.fmap.dal.feature.FeatureType;
19
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
20
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
21

    
22
/**
23
 *
24
 * @author gvSIG Team
25
 */
26
@SuppressWarnings("UseSpecificCatch")
27
public abstract class AbstractFeatureFunction extends AbstractFunction {
28

    
29
  protected AbstractFeatureFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs, String returnType, boolean sqlCompatible) {
30
    super(group, name, argc, description, template, descriptionArgs, returnType, sqlCompatible);
31
  }
32
  
33
  protected AbstractFeatureFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs, String returnType) {
34
    super(group, name, argc, description, template, descriptionArgs, returnType);
35
  }
36

    
37
  public Feature current_row(Interpreter interpreter) {
38
    try {
39
      Feature f = (Feature) interpreter.call(FUNCTION_CURRENT_ROW);
40
      return f;
41
    } catch (Exception ex) {
42
      return null;
43
    }
44
  }
45
  
46
  public FeatureStore current_store(Interpreter interpreter) {
47
    try {
48
        FeatureStore store = (FeatureStore) interpreter.call(FUNCTION_CURRENT_STORE);
49
      return store;
50
    } catch (Exception ex) {
51
      return null;
52
    }
53
  }
54
  
55
  protected NewFeatureStoreParameters getNewFeatureStoreParameters(Object[] args, int n) {      
56
      Object v = getObject(args, n);
57
      if( v instanceof NewFeatureStoreParameters ) {
58
          return (NewFeatureStoreParameters) v;
59
      }
60
      NewFeatureStoreParameters parameters;
61
      JsonObject jsonObj = getJsonObject(args, n);
62
      try {
63
        String providerName = jsonObj.getString(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
64

    
65
        DataManager dataManager = DALLocator.getDataManager();
66
        parameters = (NewFeatureStoreParameters) dataManager.createNewStoreParameters(providerName);
67
        for (String paramName : jsonObj.keySet()) {
68
            if( StringUtils.equalsIgnoreCase(paramName, DataStoreProviderServices.PROVIDER_PARAMTER_NAME)) {
69
                continue;
70
            }
71
            parameters.setDynValue(paramName, JsonUtils.getitem(jsonObj, paramName));
72
        }
73
      } catch (Exception ex) {
74
        String type = v.getClass().getCanonicalName();
75
        throw new IllegalArgumentException(
76
                I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
77
                I18N.Expected_XexpectedX_and_found_XfoundX("NewFeatureStoreParameters",type)
78
        );
79
      }
80
      return parameters;
81
  }
82

    
83
  protected FeatureType getFeatureType(Object[] args, int n) {      
84
      Object v = getObject(args, n);
85
      if( v instanceof FeatureType ) {
86
          return (FeatureType) v;
87
      }
88
      EditableFeatureType featureType;
89
      JsonObject jsonObj = getJsonObject(args, n);
90
      try {
91
        DataManager dataManager = DALLocator.getDataManager();
92
        featureType = dataManager.createFeatureType(jsonObj);
93
      } catch (Exception ex) {
94
        String type = v.getClass().getCanonicalName();
95
        throw new IllegalArgumentException(
96
                I18N.The_type_of_the_argument_XargnX_for_the_XIdentifierX_function_is_incorrect(name(), n) + " " +
97
                I18N.Expected_XexpectedX_and_found_XfoundX("NewFeatureStoreParameters",type)
98
        );
99
      }
100
      return featureType;      
101
  }
102
  
103
}