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.mdb / src / main / java / org / gvsig / fmap / dal / store / mdb / expressionbuilderformatter / TimeFunction.java @ 44951
History | View | Annotate | Download (2.13 KB)
1 |
package org.gvsig.fmap.dal.store.mdb.expressionbuilderformatter; |
---|---|
2 |
|
3 |
import java.text.ParseException; |
4 |
import java.text.SimpleDateFormat; |
5 |
import java.util.Date; |
6 |
import java.util.logging.Level; |
7 |
import java.util.logging.Logger; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
10 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
11 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
12 |
import org.gvsig.expressionevaluator.Formatter; |
13 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_EXISTS; |
14 |
import org.gvsig.fmap.dal.SQLBuilder; |
15 |
|
16 |
/**
|
17 |
*
|
18 |
* @author jjdelcerro
|
19 |
*/
|
20 |
public class TimeFunction implements Formatter<Value> { |
21 |
|
22 |
private final SQLBuilder sqlbuilder; |
23 |
private final Formatter<Value> formatter; |
24 |
|
25 |
public TimeFunction(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
26 |
this.sqlbuilder = sqlbuilder;
|
27 |
this.formatter = formatter;
|
28 |
} |
29 |
@Override
|
30 |
public boolean canApply(ExpressionBuilder.Value value) { |
31 |
if (value instanceof ExpressionBuilder.Function) { |
32 |
return StringUtils.equalsIgnoreCase(ExpressionBuilder.FUNCTION_TIME, ((Function) value).name());
|
33 |
} |
34 |
return false; |
35 |
} |
36 |
|
37 |
@Override
|
38 |
public String format(Value function0) { |
39 |
Function function = (Function) function0; |
40 |
Value exp = function.parameters().get(0);
|
41 |
if (exp instanceof ExpressionBuilder.Constant) { |
42 |
ExpressionBuilder.Constant x = (ExpressionBuilder.Constant) exp; |
43 |
Object value = x.value();
|
44 |
if (value instanceof String) { |
45 |
String strValue = (String) value; |
46 |
String[] splits = StringUtils.split(strValue, " "); |
47 |
if (splits.length > 0 && splits.length<3) { |
48 |
try {
|
49 |
Date spm = new SimpleDateFormat("H:m:s").parse(strValue); |
50 |
return "#"+value+"#"; |
51 |
} catch (ParseException ex) { |
52 |
|
53 |
} |
54 |
} |
55 |
} |
56 |
} |
57 |
return "CDATE('1899-12-30 '|| FORMAT(CDATE(" + exp.toString(formatter) + "),'Long Time'))"; |
58 |
} |
59 |
|
60 |
} |