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

History | View | Annotate | Download (3.86 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.Expression;
27
import org.gvsig.expressionevaluator.ExpressionBuilder;
28
import org.gvsig.expressionevaluator.ExpressionBuilder.Value;
29
import org.gvsig.expressionevaluator.Formatter;
30
import org.gvsig.fmap.dal.SQLBuilder;
31
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression;
32
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
33
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
34
import org.gvsig.fmap.dal.feature.FeatureType;
35
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
36
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
37
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE;
38
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_JDBCHELPER;
39

    
40
/**
41
 *
42
 * @author jjdelcerro
43
 */
44
public class ComputedAttribute implements Formatter<Value> {
45
    
46
    private final SQLBuilder sqlbuilder;
47
    private final Formatter<Value> formatter;
48
    
49
    public ComputedAttribute(SQLBuilder sqlbuilder, Formatter<Value> formatter) {
50
        this.sqlbuilder = sqlbuilder;
51
        this.formatter = formatter;
52
    }
53
    
54
    @Override
55
    public boolean canApply(ExpressionBuilder.Value value) {
56
        if (value instanceof ExpressionBuilder.Variable) { 
57
            FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE);
58
            if( featureType==null ) {
59
                return false;
60
            }
61
            JDBCHelper jdbcHelper = (JDBCHelper) value.getProperty(PROP_JDBCHELPER);
62
            if( jdbcHelper==null ) {
63
                return false;
64
            }
65
            ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value;
66
            FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(variable.name());
67
            if( attr==null || !attr.isComputed() ) {
68
                return false;
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
        }
77
        return false;
78
    }
79

    
80
    @Override
81
    public String format(Value value) {
82
        FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE);
83
        ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value;
84
        FeatureAttributeDescriptor attr = featureType.getAttributeDescriptor(variable.name());
85
        FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator();
86
        Expression expr = ((FeatureAttributeEmulatorExpression)emulator).getExpression();
87
        return "("+ this.formatter.format(expr.getCode().toValue(this.sqlbuilder.expression()))+")";
88
    }
89
    
90
}