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.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc2 / spi / expressionbuilder / formatters / ComputedAttribute.java @ 47781
History | View | Annotate | Download (7.25 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.fmap.dal.store.jdbc2.spi.expressionbuilder.formatters; |
25 |
|
26 |
import org.gvsig.expressionevaluator.Code; |
27 |
import org.gvsig.expressionevaluator.Expression; |
28 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
29 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
30 |
import org.gvsig.expressionevaluator.Formatter; |
31 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
32 |
import org.gvsig.fmap.dal.SQLBuilder; |
33 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_FEATURE_TYPE; |
34 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_JDBCHELPER; |
35 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_QUERY; |
36 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
37 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
38 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
39 |
import org.gvsig.fmap.dal.feature.FeatureExtraColumns; |
40 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
41 |
import org.gvsig.fmap.dal.feature.FeatureType; |
42 |
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase; |
43 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
44 |
|
45 |
/**
|
46 |
*
|
47 |
* @author jjdelcerro
|
48 |
*/
|
49 |
public class ComputedAttribute implements Formatter<Value> { |
50 |
|
51 |
private final SQLBuilder sqlbuilder; |
52 |
private final Formatter<Value> formatter; |
53 |
|
54 |
public ComputedAttribute(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
55 |
this.sqlbuilder = sqlbuilder;
|
56 |
this.formatter = formatter;
|
57 |
} |
58 |
|
59 |
@Override
|
60 |
public boolean canApply(ExpressionBuilder.Value value) { |
61 |
FeatureAttributeDescriptor attr = this.getFeatureAttributeDescriptor(value);
|
62 |
if (attr == null) { |
63 |
return false; |
64 |
} |
65 |
|
66 |
if (!attr.isComputed()) {
|
67 |
return false; |
68 |
} |
69 |
|
70 |
FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator(); |
71 |
if (!(emulator instanceof FeatureAttributeEmulatorExpression)) { |
72 |
return false; |
73 |
} |
74 |
// Expression expr = ((FeatureAttributeEmulatorExpression) emulator).getExpression();
|
75 |
// return jdbcHelper.supportExpression(featureType, expr.getPhrase());
|
76 |
return true; |
77 |
} |
78 |
|
79 |
private FeatureAttributeDescriptor getFeatureAttributeDescriptor(ExpressionBuilder.Value value) {
|
80 |
if (value instanceof SQLBuilderBase.Column) { |
81 |
SQLBuilder.Column column = (SQLBuilder.Column) value; |
82 |
SQLBuilder.TableNameBuilder table = column.table(); |
83 |
if( table!=null ) { |
84 |
FeatureType featureType = table.featureType(); |
85 |
if (featureType != null) { |
86 |
FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(column.name()); |
87 |
if (attr != null) { |
88 |
return attr;
|
89 |
} |
90 |
} |
91 |
} |
92 |
} |
93 |
if (value instanceof ExpressionBuilder.Variable) { |
94 |
FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE); |
95 |
if (featureType == null) { |
96 |
return null; |
97 |
} |
98 |
JDBCHelper jdbcHelper = (JDBCHelper) value.getProperty(PROP_JDBCHELPER); |
99 |
if (jdbcHelper == null) { |
100 |
return null; |
101 |
} |
102 |
ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value; |
103 |
FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(variable.name()); |
104 |
if (attr != null) { |
105 |
return attr;
|
106 |
} |
107 |
|
108 |
FeatureQuery query = (FeatureQuery) value.getProperty(PROP_QUERY); |
109 |
if (query != null) { |
110 |
return null; |
111 |
} |
112 |
FeatureExtraColumns extraColumn = query.getExtraColumn(); |
113 |
if (extraColumn == null) { |
114 |
return null; |
115 |
} |
116 |
attr = extraColumn.get(variable.name()); |
117 |
return attr;
|
118 |
} |
119 |
return null; |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public String format(Value value) { |
124 |
Value valueExpr = this.expandedValue(value);
|
125 |
return "(" + this.formatter.format(valueExpr) + ")"; |
126 |
} |
127 |
|
128 |
public Value expandedValue(Value value) {
|
129 |
// ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value;
|
130 |
// FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE);
|
131 |
// FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(variable.name());
|
132 |
FeatureAttributeDescriptor attr = getFeatureAttributeDescriptor(value); |
133 |
if (attr == null) { |
134 |
return value;
|
135 |
} |
136 |
SQLBuilder.TableNameBuilder table = null;
|
137 |
if (value instanceof SQLBuilderBase.Column) { |
138 |
table = ((SQLBuilder.Column) value).table(); |
139 |
} |
140 |
FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator(); |
141 |
Expression expr = ((FeatureAttributeEmulatorExpression) emulator).getExpression();
|
142 |
Code code = expr.getCode(); |
143 |
GeometryExpressionBuilder builder = this.sqlbuilder.expression();
|
144 |
Value valueExpr = setTableName(table, code.toValue(builder)); |
145 |
return valueExpr;
|
146 |
} |
147 |
|
148 |
private ExpressionBuilder.Value setTableName(SQLBuilder.TableNameBuilder table, ExpressionBuilder.Value value) {
|
149 |
if( table == null ) { |
150 |
return value;
|
151 |
} |
152 |
value.accept(new ExpressionBuilder.Visitor() {
|
153 |
@Override
|
154 |
public void visit(ExpressionBuilder.Visitable value) { |
155 |
if( value instanceof SQLBuilder.Column) { |
156 |
SQLBuilder.Column c = (SQLBuilder.Column) value; |
157 |
SQLBuilder.TableNameBuilder t = c.table(); |
158 |
if( t==null ) { |
159 |
c.table(table); |
160 |
return;
|
161 |
} |
162 |
if( t.equals(table) ) {
|
163 |
t.setFeatureType(table.featureType()); |
164 |
} |
165 |
} |
166 |
} |
167 |
}, null);
|
168 |
return value;
|
169 |
} |
170 |
|
171 |
private ExpressionBuilder.Value replaceVariablesByColumns(SQLBuilder.TableNameBuilder table, ExpressionBuilder.Value value) {
|
172 |
return value;
|
173 |
// if( table == null ) {
|
174 |
// return value;
|
175 |
// }
|
176 |
// value.accept(new ExpressionBuilder.Visitor() {
|
177 |
// @Override
|
178 |
// public void visit(ExpressionBuilder.Visitable value) {
|
179 |
// if( value instanceof ExpressionBuilder.Variable) {
|
180 |
//
|
181 |
// }
|
182 |
// }
|
183 |
// }, null);
|
184 |
// return value;
|
185 |
} |
186 |
} |