svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.h2 / src / main / java / org / gvsig / fmap / dal / store / h2 / H2SetProvider.java @ 41719
History | View | Annotate | Download (6.79 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 IVER T.I {{Task}}
|
26 |
*/
|
27 |
|
28 |
/**
|
29 |
*
|
30 |
*/
|
31 |
package org.gvsig.fmap.dal.store.h2; |
32 |
|
33 |
import java.util.ArrayList; |
34 |
import java.util.List; |
35 |
|
36 |
import org.cresques.cts.IProjection; |
37 |
import org.slf4j.Logger; |
38 |
import org.slf4j.LoggerFactory; |
39 |
|
40 |
import org.gvsig.fmap.dal.exception.DataException; |
41 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
42 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
43 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
44 |
import org.gvsig.fmap.dal.feature.FeatureType; |
45 |
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException; |
46 |
import org.gvsig.fmap.dal.store.db.DBStoreParameters; |
47 |
import org.gvsig.fmap.dal.store.jdbc.JDBCSetProvider; |
48 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider; |
49 |
import org.gvsig.fmap.geom.primitive.Envelope; |
50 |
import org.gvsig.tools.evaluator.Evaluator; |
51 |
import org.gvsig.tools.evaluator.EvaluatorFieldValue; |
52 |
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo; |
53 |
|
54 |
/**
|
55 |
* @author jmvivo
|
56 |
*
|
57 |
*/
|
58 |
public class H2SetProvider extends JDBCSetProvider { |
59 |
|
60 |
private static Logger logger = LoggerFactory.getLogger(H2SetProvider.class); |
61 |
|
62 |
public H2SetProvider(JDBCStoreProvider store, FeatureQuery query,
|
63 |
FeatureType featureType) throws DataException {
|
64 |
super(store, query, featureType);
|
65 |
} |
66 |
|
67 |
|
68 |
/*
|
69 |
* (non-Javadoc)
|
70 |
*
|
71 |
* @see org.gvsig.fmap.dal.feature.spi.FeatureSetProvider#canFilter()
|
72 |
*/
|
73 |
public boolean canFilter() { |
74 |
// TODO more checks
|
75 |
if (!super.canFilter()) { |
76 |
return false; |
77 |
} |
78 |
return true; |
79 |
|
80 |
} |
81 |
|
82 |
protected String getSqlForEvaluator(Evaluator filter) { |
83 |
|
84 |
String resp = null; |
85 |
if (filter != null && filter.getSQL() != null) { |
86 |
// =================================================
|
87 |
EvaluatorFieldsInfo info = filter.getFieldsInfo(); |
88 |
String filterString = filter.getSQL();
|
89 |
|
90 |
String[] filterNames = null; |
91 |
String[] finalNames = null; |
92 |
|
93 |
if (info == null) { |
94 |
filterNames = getFieldNames(getFeatureType()); |
95 |
} else {
|
96 |
filterNames = info.getFieldNames(); |
97 |
} |
98 |
|
99 |
finalNames = new String[filterNames.length]; |
100 |
FeatureAttributeDescriptor attr; |
101 |
for (int i = 0; i < filterNames.length; i++) { |
102 |
attr = getFeatureType().getAttributeDescriptor(filterNames[i]); |
103 |
if (attr == null) { |
104 |
finalNames[i] = filterNames[i]; |
105 |
continue;
|
106 |
} |
107 |
finalNames[i] = getEscapedFieldName(attr.getName()); |
108 |
} |
109 |
|
110 |
for (int i = 0; i < filterNames.length; i++) { |
111 |
if (!filterNames[i].equals(finalNames[i])) {
|
112 |
filterString = filterString.replaceAll( |
113 |
"\\b" + filterNames[i] + "\\b", |
114 |
finalNames[i]); |
115 |
} |
116 |
} |
117 |
resp = filterString; |
118 |
} |
119 |
// ================================
|
120 |
// In any case, append working area filter
|
121 |
|
122 |
try {
|
123 |
resp = appendWorkingAreaCondition(resp); |
124 |
} catch (Exception e) { |
125 |
logger.error("While appending working area condition.", e);
|
126 |
} |
127 |
return resp;
|
128 |
} |
129 |
|
130 |
private String[] getFieldNames(FeatureType ft) { |
131 |
|
132 |
if (ft == null) { |
133 |
return new String[0]; |
134 |
} |
135 |
FeatureAttributeDescriptor[] atts = ft.getAttributeDescriptors();
|
136 |
String[] resp = new String[atts.length]; |
137 |
for (int i=0; i<atts.length; i++) { |
138 |
resp[i] = atts[i].getName(); |
139 |
} |
140 |
return resp;
|
141 |
} |
142 |
|
143 |
private String getFunctionName(String newFunctionName) { |
144 |
|
145 |
H2StoreProvider pg_sto_prov = (H2StoreProvider) this.getStore();
|
146 |
H2Helper hpr = pg_sto_prov.getPgHelper(); |
147 |
if (hpr == null) { |
148 |
logger.info("Unable to get PG helper.", new Exception("Helper is null")); |
149 |
return newFunctionName;
|
150 |
} else {
|
151 |
return hpr.getFunctionName(newFunctionName);
|
152 |
} |
153 |
} |
154 |
|
155 |
|
156 |
private String appendWorkingAreaCondition(String sql) throws Exception { |
157 |
|
158 |
|
159 |
DBStoreParameters dbParams = |
160 |
(DBStoreParameters) getStore().getParameters(); |
161 |
|
162 |
Envelope wa = dbParams.getWorkingArea(); |
163 |
if (wa == null) { |
164 |
return sql;
|
165 |
} else {
|
166 |
|
167 |
FeatureStore fstore = this.getStore().getFeatureStore();
|
168 |
String geoname =
|
169 |
fstore.getDefaultFeatureType().getDefaultGeometryAttributeName(); |
170 |
|
171 |
StringBuffer strbuf = new StringBuffer(); |
172 |
|
173 |
if (sql == null) { |
174 |
strbuf.append( |
175 |
getFunctionName("ST_Intersects") + "(" |
176 |
+ getFunctionName("ST_GeomFromText") + "('"); |
177 |
} else {
|
178 |
strbuf.append("(");
|
179 |
strbuf.append(sql); |
180 |
strbuf.append(") AND "
|
181 |
+ getFunctionName("ST_Intersects") + "(" |
182 |
+ getFunctionName("ST_GeomFromText") + "('"); |
183 |
} |
184 |
|
185 |
String workAreaWkt = null; |
186 |
workAreaWkt = wa.getGeometry().convertToWKT(); |
187 |
strbuf.append(workAreaWkt); |
188 |
strbuf.append("', ");
|
189 |
|
190 |
H2StoreProvider sprov = (H2StoreProvider) getStore(); |
191 |
H2Helper helper = sprov.getPgHelper(); |
192 |
|
193 |
IProjection proj = dbParams.getCRS(); |
194 |
int sridInt = helper.getProviderSRID(proj);
|
195 |
if (sridInt == -1) { |
196 |
throw new CreateGeometryException( |
197 |
new Exception("CRS is null or unknown.")); |
198 |
} else {
|
199 |
strbuf.append(Integer.toString(sridInt));
|
200 |
} |
201 |
strbuf.append("), " + getFunctionName("ST_Envelope") + "("); |
202 |
strbuf.append(helper.escapeFieldName(geoname)); |
203 |
strbuf.append("))");
|
204 |
|
205 |
return strbuf.toString();
|
206 |
} |
207 |
} |
208 |
|
209 |
|
210 |
} |