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 @ 46104
History | View | Annotate | Download (4.85 KB)
1 | 46010 | jjdelcerro | /**
|
---|---|---|---|
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 | 46050 | omartinez | import org.gvsig.expressionevaluator.Code; |
27 | 46010 | jjdelcerro | 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 | 46050 | omartinez | import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
32 | 46010 | jjdelcerro | import org.gvsig.fmap.dal.SQLBuilder; |
33 | import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
||
34 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
35 | import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
||
36 | 46064 | omartinez | import org.gvsig.fmap.dal.feature.FeatureExtraColumns; |
37 | 46050 | omartinez | import org.gvsig.fmap.dal.feature.FeatureQuery; |
38 | 46010 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
39 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
40 | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
||
41 | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_JDBCHELPER; |
||
42 | 46050 | omartinez | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_QUERY; |
43 | 46010 | jjdelcerro | |
44 | /**
|
||
45 | *
|
||
46 | * @author jjdelcerro
|
||
47 | */
|
||
48 | public class ComputedAttribute implements Formatter<Value> { |
||
49 | |||
50 | private final SQLBuilder sqlbuilder; |
||
51 | private final Formatter<Value> formatter; |
||
52 | |||
53 | public ComputedAttribute(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
||
54 | this.sqlbuilder = sqlbuilder;
|
||
55 | this.formatter = formatter;
|
||
56 | } |
||
57 | |||
58 | @Override
|
||
59 | public boolean canApply(ExpressionBuilder.Value value) { |
||
60 | if (value instanceof ExpressionBuilder.Variable) { |
||
61 | FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE); |
||
62 | if( featureType==null ) { |
||
63 | return false; |
||
64 | } |
||
65 | JDBCHelper jdbcHelper = (JDBCHelper) value.getProperty(PROP_JDBCHELPER); |
||
66 | if( jdbcHelper==null ) { |
||
67 | return false; |
||
68 | } |
||
69 | ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value; |
||
70 | 46064 | omartinez | FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(variable.name()); |
71 | if (attr == null) { |
||
72 | FeatureQuery query = (FeatureQuery) value.getProperty(PROP_QUERY); |
||
73 | if (query == null) { |
||
74 | return false; |
||
75 | } |
||
76 | FeatureExtraColumns extraColumn = query.getExtraColumn(); |
||
77 | if (extraColumn == null) { |
||
78 | return false; |
||
79 | } |
||
80 | attr = extraColumn.get(variable.name()); |
||
81 | if (attr == null ) { |
||
82 | return false; |
||
83 | } |
||
84 | } |
||
85 | 46050 | omartinez | |
86 | 46064 | omartinez | if (!attr.isComputed()) {
|
87 | 46010 | jjdelcerro | return false; |
88 | } |
||
89 | 46050 | omartinez | |
90 | 46010 | jjdelcerro | FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator(); |
91 | if( !(emulator instanceof FeatureAttributeEmulatorExpression) ) { |
||
92 | return false; |
||
93 | } |
||
94 | Expression expr = ((FeatureAttributeEmulatorExpression)emulator).getExpression();
|
||
95 | return jdbcHelper.supportExpression(featureType, expr.getPhrase());
|
||
96 | } |
||
97 | return false; |
||
98 | } |
||
99 | 46104 | omartinez | |
100 | @Override
|
||
101 | public String format(Value value) { |
||
102 | Value valueExpr = this.expandedValue(value);
|
||
103 | return "("+ this.formatter.format(valueExpr)+")"; |
||
104 | } |
||
105 | 46010 | jjdelcerro | |
106 | 46104 | omartinez | |
107 | public Value expandedValue(Value value) {
|
||
108 | 46010 | jjdelcerro | FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE); |
109 | ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value; |
||
110 | FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(variable.name()); |
||
111 | 46050 | omartinez | if (attr == null) { |
112 | FeatureQuery query = (FeatureQuery) value.getProperty(PROP_QUERY); |
||
113 | attr = query.getExtraColumn().get(variable.name()); |
||
114 | } |
||
115 | 46010 | jjdelcerro | FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator(); |
116 | Expression expr = ((FeatureAttributeEmulatorExpression)emulator).getExpression();
|
||
117 | 46050 | omartinez | Code code = expr.getCode(); |
118 | GeometryExpressionBuilder builder = this.sqlbuilder.expression();
|
||
119 | Value valueExpr = code.toValue(builder); |
||
120 | 46104 | omartinez | return valueExpr;
|
121 | 46010 | jjdelcerro | } |
122 | |||
123 | } |