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 / SelectCountFunction.java @ 44738

History | View | Annotate | Download (4.29 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.function.dataaccess;
25

    
26
import java.util.Objects;
27
import org.apache.commons.lang3.Range;
28
import org.gvsig.expressionevaluator.Codes;
29
import org.gvsig.expressionevaluator.ExpressionRuntimeException;
30
import org.gvsig.expressionevaluator.Interpreter;
31
import org.gvsig.expressionevaluator.impl.DALFunctions;
32
import org.gvsig.expressionevaluator.spi.AbstractFunction;
33
import org.gvsig.fmap.dal.DALLocator;
34
import org.gvsig.fmap.dal.DataManager;
35
import org.gvsig.fmap.dal.DataStore;
36
import org.gvsig.fmap.dal.feature.FeatureQuery;
37
import org.gvsig.fmap.dal.feature.FeatureSet;
38
import org.gvsig.fmap.dal.feature.FeatureStore;
39
import org.gvsig.tools.dispose.DisposeUtils;
40

    
41
/**
42
 *
43
 * @author jjdelcerro
44
 */
45
@SuppressWarnings("UseSpecificCatch")
46
public class SelectCountFunction extends AbstractFunction {
47

    
48
  public SelectCountFunction() {
49
    super(DALFunctions.GROUP_DATA_ACCESS,
50
            DALFunctions.FUNCTION_SELECT_COUNT,
51
            Range.between(1,4),
52
            "Returns the number of features of the indicated table by applying the filter indicated.",
53
            DALFunctions.FUNCTION_SELECT_COUNT + "({{table_name}},filter)",
54
            new String[]{
55
              "table_name - Name of the table",
56
              "filter - filter to apply",
57
            },
58
            "Long",
59
            true
60
    );
61
  }
62

    
63
  @Override
64
  public boolean isHidden() {
65
    return true;
66
  }
67

    
68
  @Override
69
  public boolean allowConstantFolding() {
70
    return false;
71
  }
72

    
73
  @Override
74
  public boolean allowArgNames() {
75
    return true;
76
  }
77

    
78
  @Override
79
  public boolean useArgumentsInsteadObjects() {
80
    return true;
81
  }
82

    
83
  @Override
84
  public Object call(Interpreter interpreter, Object[] args) throws Exception {
85
    throw new UnsupportedOperationException();
86
  }
87

    
88
  @Override
89
  public Object call(Interpreter interpreter, Codes args) throws Exception {
90

    
91
    String storeName =  (String) getObject(interpreter, args, "TABLE");
92
    String where = Objects.toString(args.get("WHERE"),null);
93
    
94
    FeatureStore featureStore = null;
95
    FeatureSet set = null;
96
    try {
97
      DataStore store = this.getStore(storeName);
98
      if (store == null ) {
99
        throw new ExpressionRuntimeException("Cant locate the store '" + storeName + "' in function '" + DALFunctions.FUNCTION_SELECT + "'.");
100
      }
101
      if (!(store instanceof FeatureStore)) {
102
        throw new ExpressionRuntimeException("The store'" + storeName + "' is not valid for function '" + DALFunctions.FUNCTION_SELECT + "', a FeatureStore is required.");
103
      }
104
      featureStore = (FeatureStore) store;
105
      if (where == null ) {
106
        set = featureStore.getFeatureSet();
107
      } else {
108
        FeatureQuery query = featureStore.createFeatureQuery();
109
        query.addFilter(where);
110
        query.retrievesAllAttributes();
111
        set = featureStore.getFeatureSet(query);
112
      }
113
      return set.getSize();
114
    } catch (ExpressionRuntimeException ex) {
115
      throw ex;
116
    } catch (Exception ex) {
117
      throw new ExpressionRuntimeException("Problems calling '" + DALFunctions.FUNCTION_SELECT + "' function", ex);
118
    } finally {
119
      DisposeUtils.disposeQuietly(set);
120
      DisposeUtils.disposeQuietly(featureStore);
121
    }
122
  }
123

    
124
  protected DataStore getStore(String storeName) {
125
    DataManager dataManager = DALLocator.getDataManager();
126
    DataStore store = dataManager.getStoresRepository().getStore(storeName);
127
    return store;
128
  }
129

    
130
}