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 / $Constant.java @ 46091
History | View | Annotate | Download (4.13 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 java.util.Objects; |
27 |
import org.apache.commons.lang3.StringUtils; |
28 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
29 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
30 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
31 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
32 |
import org.gvsig.expressionevaluator.Formatter; |
33 |
import org.gvsig.expressionevaluator.SymbolTable; |
34 |
import org.gvsig.fmap.dal.SQLBuilder; |
35 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_SYMBOLTABLE; |
36 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_$CONSTANT; |
37 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
38 |
import org.gvsig.fmap.geom.Geometry; |
39 |
|
40 |
/**
|
41 |
*
|
42 |
* @author jjdelcerro
|
43 |
*/
|
44 |
public class $Constant implements Formatter<Value> { |
45 |
|
46 |
private final SQLBuilder sqlbuilder; |
47 |
private final Formatter<Value> formatter; |
48 |
|
49 |
public $Constant(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.Function) { |
57 |
return StringUtils.equalsIgnoreCase(FUNCTION_$CONSTANT, ((Function) value).name()); |
58 |
} |
59 |
return false; |
60 |
} |
61 |
|
62 |
@Override
|
63 |
public String format(Value value) { |
64 |
return resolve(this.sqlbuilder, this.formatter, value); |
65 |
} |
66 |
|
67 |
public static boolean isConstant(Value value) { |
68 |
if( value instanceof ExpressionBuilder.Constant) { |
69 |
return true; |
70 |
} |
71 |
return value instanceof ExpressionBuilder.Function && |
72 |
StringUtils.equalsIgnoreCase(((ExpressionBuilder.Function)value).name(),FUNCTION_$CONSTANT);
|
73 |
} |
74 |
|
75 |
|
76 |
public static String resolve(SQLBuilder sqlbuilder, Formatter<Value> formatter, Value value) { |
77 |
GeometryExpressionBuilder builder = sqlbuilder.expression(); |
78 |
if( value instanceof ExpressionBuilder.Constant) { |
79 |
Object x = ((ExpressionBuilder.Constant)value).value();
|
80 |
return builder.constant(x).toString(formatter);
|
81 |
} else if( value instanceof ExpressionBuilder.Function && |
82 |
StringUtils.equalsIgnoreCase(((ExpressionBuilder.Function)value).name(),FUNCTION_$CONSTANT) ) {
|
83 |
SymbolTable symbolTable = (SymbolTable) value.getProperty(PROP_SYMBOLTABLE); |
84 |
Object x = ExpressionUtils.evaluate(symbolTable, value.toString());
|
85 |
|
86 |
if( x instanceof Geometry && builder instanceof GeometryExpressionBuilder) { |
87 |
GeometryExpressionBuilder gbuilder = (GeometryExpressionBuilder) builder; |
88 |
Geometry geom = (Geometry) x; |
89 |
String hexwkb = geom.convertToHexWKBQuietly();
|
90 |
|
91 |
Function v = gbuilder.ST_GeomFromWKB( |
92 |
gbuilder.decode( |
93 |
gbuilder.constant(hexwkb), |
94 |
gbuilder.constant("hex")
|
95 |
), |
96 |
gbuilder.constant( |
97 |
sqlbuilder.srs_id(geom.getProjection()) |
98 |
) |
99 |
); |
100 |
return v.toString(formatter);
|
101 |
} |
102 |
return builder.constant(x).toString(formatter);
|
103 |
} |
104 |
throw new RuntimeException("Expected constant value or $$CONSTANT function ("+Objects.toString(value)+")."); |
105 |
} |
106 |
} |