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

History | View | Annotate | Download (4.85 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 org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
34
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
35
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
36
import org.gvsig.fmap.dal.feature.FeatureExtraColumns;
37
import org.gvsig.fmap.dal.feature.FeatureQuery;
38
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
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_QUERY;
43

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

    
86
            if (!attr.isComputed()) {
87
                return false;
88
            }
89
            
90
            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
        
100
        @Override
101
    public String format(Value value) {
102
        Value valueExpr = this.expandedValue(value);
103
        return "("+ this.formatter.format(valueExpr)+")";
104
        }
105

    
106
    
107
    public Value expandedValue(Value value) {
108
        FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE);
109
        ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value;
110
        FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(variable.name());
111
        if (attr == null) {
112
            FeatureQuery query = (FeatureQuery) value.getProperty(PROP_QUERY);
113
            attr = query.getExtraColumn().get(variable.name());
114
        }
115
        FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator();
116
        Expression expr = ((FeatureAttributeEmulatorExpression)emulator).getExpression();
117
        Code code = expr.getCode();
118
        GeometryExpressionBuilder builder = this.sqlbuilder.expression();
119
        Value valueExpr = code.toValue(builder);
120
        return valueExpr;
121
    }
122
    
123
}