Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / dataaccess / GetattrFunction.java @ 47787

History | View | Annotate | Download (3.83 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.expressionevaluator.impl.function.dataaccess;
7

    
8
import java.util.Objects;
9
import java.util.function.Function;
10
import org.apache.commons.lang3.StringUtils;
11
import org.gvsig.expressionevaluator.Code;
12
import org.gvsig.expressionevaluator.Codes;
13
import org.gvsig.expressionevaluator.ExpressionBuilder;
14
import static org.gvsig.expressionevaluator.impl.function.dataaccess.SelectFunction.PROP_FEATURETYPESUPPLIER;
15
import org.gvsig.fmap.dal.DALLocator;
16
import org.gvsig.fmap.dal.DataManager;
17
import org.gvsig.fmap.dal.SQLBuilder;
18
import static org.gvsig.fmap.dal.SQLBuilder.PROP_SQLBUILDER;
19
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
20
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
21
import org.gvsig.fmap.dal.feature.FeatureQuery;
22
import org.gvsig.fmap.dal.feature.FeatureType;
23

    
24
/**
25
 *
26
 * @author fdiaz
27
 */
28
public class GetattrFunction extends org.gvsig.expressionevaluator.impl.function.programming.GetattrFunction {
29

    
30
    @Override
31
    public ExpressionBuilder.Value toValue(ExpressionBuilder builder, Codes args) {
32
        try {
33
            SQLBuilder sqlBuilder = (SQLBuilder) builder.getProperty(PROP_SQLBUILDER);
34
            if(sqlBuilder == null){
35
                return super.toValue(builder, args);
36
            }
37
            if(!(args.get(0) instanceof Code.Identifier)){
38
                return super.toValue(builder, args);
39
            }
40
            if(!(args.get(1) instanceof Code.Constant)){
41
                return super.toValue(builder, args);
42
            }
43
            
44
            Function<String, FeatureType> featureTypeSupplier = (Function<String, FeatureType>) builder.getProperty(PROP_FEATURETYPESUPPLIER);
45
            if (featureTypeSupplier == null) {
46
                featureTypeSupplier = (String tableName) -> {
47
                    DataManager dataManager = DALLocator.getDataManager();
48
                    FeatureType featureType = dataManager.getStoresRepository().getFeatureType(tableName);
49
                    return featureType;
50
                };
51
            }
52
            
53
            String tableName = ((Code.Identifier)args.get(0)).name();
54
            String columnName = Objects.toString(((Code.Constant)args.get(1)).value(), null);
55
            if(StringUtils.isBlank(tableName) || StringUtils.isBlank(columnName)){
56
                return super.toValue(builder, args);
57
            }
58
            FeatureAttributeDescriptor attr = null;
59
            FeatureQuery query = (FeatureQuery) builder.getProperty(SQLBuilder.PROP_QUERY);
60
            if(query != null){
61
                attr = query.getExtraColumns().get(columnName);
62
            }
63
            if(attr == null) {
64
                String builderTableName = (String) builder.getProperty(SQLBuilder.PROP_TABLENAME);
65
                FeatureType featureType = null;
66
                if (StringUtils.equalsIgnoreCase(builderTableName, tableName)) {
67
                    featureType = (FeatureType) builder.getProperty(SQLBuilder.PROP_FEATURE_TYPE);
68
                } else {
69
                    featureType = featureTypeSupplier.apply(tableName);
70
                }
71
                if (featureType == null) {
72
                    return super.toValue(builder, args);
73
                }
74
                attr = featureType.getAttributeDescriptor(columnName);
75
                if (attr == null) {
76
                    return super.toValue(builder, args);
77
                }
78
            }
79
            SQLBuilder.TableNameBuilder table = sqlBuilder.createTableNameBuilder();
80
            table.name(tableName);
81
            SQLBuilder.Column column = sqlBuilder.column(table, columnName);
82
            return column;
83
        } catch (Exception ex) {
84
            return super.toValue(builder, args);
85
        }
86
    }
87
    
88
    
89
}