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

History | View | Annotate | Download (7.25 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 47781 jjdelcerro
        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 46010 jjdelcerro
            FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE);
95 47781 jjdelcerro
            if (featureType == null) {
96
                return null;
97 46010 jjdelcerro
            }
98
            JDBCHelper jdbcHelper = (JDBCHelper) value.getProperty(PROP_JDBCHELPER);
99 47781 jjdelcerro
            if (jdbcHelper == null) {
100
                return null;
101 46010 jjdelcerro
            }
102
            ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value;
103 46517 fdiaz
            FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(variable.name());
104 47781 jjdelcerro
            if (attr != null) {
105
                return attr;
106 46517 fdiaz
            }
107 46050 omartinez
108 47781 jjdelcerro
            FeatureQuery query = (FeatureQuery) value.getProperty(PROP_QUERY);
109
            if (query != null) {
110
                return null;
111 46010 jjdelcerro
            }
112 47781 jjdelcerro
            FeatureExtraColumns extraColumn = query.getExtraColumn();
113
            if (extraColumn == null) {
114
                return null;
115 46010 jjdelcerro
            }
116 47781 jjdelcerro
            attr = extraColumn.get(variable.name());
117
            return attr;
118 46010 jjdelcerro
        }
119 47781 jjdelcerro
        return null;
120 46010 jjdelcerro
    }
121 47781 jjdelcerro
122
    @Override
123 46104 omartinez
    public String format(Value value) {
124
        Value valueExpr = this.expandedValue(value);
125 47781 jjdelcerro
        return "(" + this.formatter.format(valueExpr) + ")";
126
    }
127 46010 jjdelcerro
128 46104 omartinez
    public Value expandedValue(Value value) {
129 47781 jjdelcerro
//        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 46050 omartinez
        if (attr == null) {
134 47781 jjdelcerro
            return value;
135 46050 omartinez
        }
136 47781 jjdelcerro
        SQLBuilder.TableNameBuilder table = null;
137
        if (value instanceof SQLBuilderBase.Column) {
138
            table = ((SQLBuilder.Column) value).table();
139
        }
140 46010 jjdelcerro
        FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator();
141 47781 jjdelcerro
        Expression expr = ((FeatureAttributeEmulatorExpression) emulator).getExpression();
142 46050 omartinez
        Code code = expr.getCode();
143
        GeometryExpressionBuilder builder = this.sqlbuilder.expression();
144 47781 jjdelcerro
        Value valueExpr = setTableName(table, code.toValue(builder));
145 46104 omartinez
        return valueExpr;
146 46010 jjdelcerro
    }
147
148 47781 jjdelcerro
    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 46010 jjdelcerro
}