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

History | View | Annotate | Download (4.78 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 org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
41
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE;
42
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_JDBCHELPER;
43
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_QUERY;
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
        if (value instanceof ExpressionBuilder.Variable) { 
62
            FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE);
63
            if( featureType==null ) {
64
                return false;
65
            }
66
            JDBCHelper jdbcHelper = (JDBCHelper) value.getProperty(PROP_JDBCHELPER);
67
            if( jdbcHelper==null ) {
68
                return false;
69
            }
70
            ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value;
71
                        FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(variable.name());
72
                        if (attr == null) {
73
                                FeatureQuery query = (FeatureQuery) value.getProperty(PROP_QUERY);
74
                                if (query == null) {
75
                                        return false;
76
                                }
77
                                FeatureExtraColumns extraColumn = query.getExtraColumn();
78
                                if (extraColumn == null) {
79
                                        return false;
80
                                }
81
                                attr = extraColumn.get(variable.name());
82
                                if (attr == null ) {
83
                                        return false;
84
                                }
85
                        }
86

    
87
            if (!attr.isComputed()) {
88
                return false;
89
            }
90
            
91
            FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator();
92
            if( !(emulator instanceof FeatureAttributeEmulatorExpression) ) {
93
                return false;
94
            }
95
            Expression expr = ((FeatureAttributeEmulatorExpression)emulator).getExpression();
96
            return jdbcHelper.supportExpression(featureType, expr.getPhrase());
97
        }
98
        return false;
99
    }
100

    
101
    @Override
102
    public String format(Value value) {
103
        FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE);
104
        ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value;
105
        FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(variable.name());
106
        if (attr == null) {
107
            FeatureQuery query = (FeatureQuery) value.getProperty(PROP_QUERY);
108
            attr = query.getExtraColumn().get(variable.name());
109
        }
110
        FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator();
111
        Expression expr = ((FeatureAttributeEmulatorExpression)emulator).getExpression();
112
        Code code = expr.getCode();
113
        GeometryExpressionBuilder builder = this.sqlbuilder.expression();
114
        Value valueExpr = code.toValue(builder);
115
        return "("+ this.formatter.format(valueExpr)+")";
116
    }
117
    
118
}