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

History | View | Annotate | Download (4.59 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.FeatureQuery;
37
import org.gvsig.fmap.dal.feature.FeatureType;
38
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
39
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
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
                attr = query.getExtraColumn().get(variable.name());
74
            }
75

    
76
            if (attr == null || !attr.isComputed()) {
77
                return false;
78
            }
79
            
80
            FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator();
81
            if( !(emulator instanceof FeatureAttributeEmulatorExpression) ) {
82
                return false;
83
            }
84
            Expression expr = ((FeatureAttributeEmulatorExpression)emulator).getExpression();
85
            return jdbcHelper.supportExpression(featureType, expr.getPhrase());
86
        }
87
        return false;
88
    }
89

    
90
    @Override
91
    public String format(Value value) {
92
        FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE);
93
        ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value;
94
        FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(variable.name());
95
        if (attr == null) {
96
            FeatureQuery query = (FeatureQuery) value.getProperty(PROP_QUERY);
97
            attr = query.getExtraColumn().get(variable.name());
98
        }
99
        FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator();
100
        Expression expr = ((FeatureAttributeEmulatorExpression)emulator).getExpression();
101
        Code code = expr.getCode();
102
        GeometryExpressionBuilder builder = this.sqlbuilder.expression();
103
        Value valueExpr = code.toValue(builder);
104
        return "("+ this.formatter.format(valueExpr)+")";
105
    }
106
    
107
}