Statistics
| Revision:

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 @ 47787

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