Revision 47781

View differences:

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
39 39
import org.gvsig.fmap.dal.feature.FeatureExtraColumns;
40 40
import org.gvsig.fmap.dal.feature.FeatureQuery;
41 41
import org.gvsig.fmap.dal.feature.FeatureType;
42
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase;
42 43
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
43 44

  
44 45
/**
......
46 47
 * @author jjdelcerro
47 48
 */
48 49
public class ComputedAttribute implements Formatter<Value> {
49
    
50

  
50 51
    private final SQLBuilder sqlbuilder;
51 52
    private final Formatter<Value> formatter;
52
    
53

  
53 54
    public ComputedAttribute(SQLBuilder sqlbuilder, Formatter<Value> formatter) {
54 55
        this.sqlbuilder = sqlbuilder;
55 56
        this.formatter = formatter;
56 57
    }
57
    
58

  
58 59
    @Override
59 60
    public boolean canApply(ExpressionBuilder.Value value) {
60
        if (value instanceof ExpressionBuilder.Variable) { 
61
        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) {
61 94
            FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE);
62
            if( featureType==null ) {
63
                return false;
95
            if (featureType == null) {
96
                return null;
64 97
            }
65 98
            JDBCHelper jdbcHelper = (JDBCHelper) value.getProperty(PROP_JDBCHELPER);
66
            if( jdbcHelper==null ) {
67
                return false;
99
            if (jdbcHelper == null) {
100
                return null;
68 101
            }
69 102
            ExpressionBuilder.Variable variable = (ExpressionBuilder.Variable) value;
70 103
            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
                    }
104
            if (attr != null) {
105
                return attr;
84 106
            }
85 107

  
86
            if (!attr.isComputed()) {
87
                return false;
108
            FeatureQuery query = (FeatureQuery) value.getProperty(PROP_QUERY);
109
            if (query != null) {
110
                return null;
88 111
            }
89
            
90
            FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator();
91
            if( !(emulator instanceof FeatureAttributeEmulatorExpression) ) {
92
                return false;
112
            FeatureExtraColumns extraColumn = query.getExtraColumn();
113
            if (extraColumn == null) {
114
                return null;
93 115
            }
94
            Expression expr = ((FeatureAttributeEmulatorExpression)emulator).getExpression();
95
            return jdbcHelper.supportExpression(featureType, expr.getPhrase());
116
            attr = extraColumn.get(variable.name());
117
            return attr;
96 118
        }
97
        return false;
119
        return null;
98 120
    }
99
	
100
	@Override
121

  
122
    @Override
101 123
    public String format(Value value) {
102 124
        Value valueExpr = this.expandedValue(value);
103
        return "("+ this.formatter.format(valueExpr)+")";
104
	}
125
        return "(" + this.formatter.format(valueExpr) + ")";
126
    }
105 127

  
106
    
107 128
    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());
129
//        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);
111 133
        if (attr == null) {
112
            FeatureQuery query = (FeatureQuery) value.getProperty(PROP_QUERY);
113
            attr = query.getExtraColumn().get(variable.name());
134
            return value;
114 135
        }
136
        SQLBuilder.TableNameBuilder table = null;
137
        if (value instanceof SQLBuilderBase.Column) {
138
            table = ((SQLBuilder.Column) value).table();
139
        }
115 140
        FeatureAttributeEmulator emulator = attr.getFeatureAttributeEmulator();
116
        Expression expr = ((FeatureAttributeEmulatorExpression)emulator).getExpression();
141
        Expression expr = ((FeatureAttributeEmulatorExpression) emulator).getExpression();
117 142
        Code code = expr.getCode();
118 143
        GeometryExpressionBuilder builder = this.sqlbuilder.expression();
119
        Value valueExpr = code.toValue(builder);
144
        Value valueExpr = setTableName(table, code.toValue(builder));
120 145
        return valueExpr;
121 146
    }
122 147
    
148
    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
    }
123 186
}

Also available in: Unified diff