KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > mapping > sql > dynamic > elements > ConditionalTagHandler


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.sqlmap.engine.mapping.sql.dynamic.elements;
17
18 import com.ibatis.common.beans.Probe;
19 import com.ibatis.common.beans.ProbeFactory;
20 import com.ibatis.common.exception.NestedRuntimeException;
21
22 import java.math.BigDecimal JavaDoc;
23 import java.math.BigInteger JavaDoc;
24 import java.text.DateFormat JavaDoc;
25 import java.text.ParseException JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.Date JavaDoc;
28
29 public abstract class ConditionalTagHandler extends BaseTagHandler {
30
31   private static final Probe PROBE = ProbeFactory.getProbe();
32
33   public static final long NOT_COMPARABLE = Long.MIN_VALUE;
34   private static final String JavaDoc DATE_MASK = "yyyy/MM/dd hh:mm:ss";
35   private static final DateFormat JavaDoc DATE_FORMAT = new SimpleDateFormat JavaDoc(DATE_MASK);
36
37   public abstract boolean isCondition(SqlTagContext ctx, SqlTag tag, Object JavaDoc parameterObject);
38
39   public int doStartFragment(SqlTagContext ctx, SqlTag tag, Object JavaDoc parameterObject) {
40     
41     ctx.pushRemoveFirstPrependMarker(tag);
42     
43     if (isCondition(ctx, tag, parameterObject)) {
44       return SqlTagHandler.INCLUDE_BODY;
45     } else {
46       return SqlTagHandler.SKIP_BODY;
47     }
48   }
49
50   public int doEndFragment(SqlTagContext ctx, SqlTag tag, Object JavaDoc parameterObject, StringBuffer JavaDoc bodyContent) {
51     
52     IterateContext iterate = ctx.peekIterateContext();
53     
54     if(null != iterate && iterate.isAllowNext()){
55       iterate.next();
56       iterate.setAllowNext(false);
57       if(!iterate.hasNext()) {
58         iterate.setFinal(true);
59       }
60     }
61     
62     //iteratePropertyReplace(bodyContent,ctx.peekIterateContext());
63

64     return super.doEndFragment(ctx,tag,parameterObject,bodyContent);
65   }
66
67   protected long compare(SqlTagContext ctx, SqlTag tag, Object JavaDoc parameterObject) {
68     String JavaDoc propertyName = tag.getPropertyAttr();
69     String JavaDoc comparePropertyName = tag.getComparePropertyAttr();
70     String JavaDoc compareValue = tag.getCompareValueAttr();
71
72     String JavaDoc prop = tag.getPropertyAttr();
73     Object JavaDoc value1;
74     Class JavaDoc type;
75     if (prop != null) {
76       value1 = PROBE.getObject(parameterObject, propertyName);
77       type = PROBE.getPropertyTypeForGetter(parameterObject, propertyName);
78     } else {
79       value1 = parameterObject;
80       if (value1 != null) {
81         type = parameterObject.getClass();
82       } else {
83         type = Object JavaDoc.class;
84       }
85     }
86     if (comparePropertyName != null) {
87       Object JavaDoc value2 = PROBE.getObject(parameterObject, comparePropertyName);
88       return compareValues(type, value1, value2);
89     } else if (compareValue != null) {
90       return compareValues(type, value1, compareValue);
91     } else {
92       throw new NestedRuntimeException("Error comparing in conditional fragment. Uknown 'compare to' values.");
93     }
94   }
95
96   protected long compareValues(Class JavaDoc type, Object JavaDoc value1, Object JavaDoc value2) {
97     long result = NOT_COMPARABLE;
98
99     if (value1 == null || value2 == null) {
100       result = value1 == value2 ? 0 : NOT_COMPARABLE;
101     } else {
102       if (value2.getClass() != type) {
103         value2 = convertValue(type, value2.toString());
104       }
105       if (value2 instanceof String JavaDoc && type != String JavaDoc.class) {
106         value1 = value1.toString();
107       }
108       if (!(value1 instanceof Comparable JavaDoc && value2 instanceof Comparable JavaDoc)) {
109         value1 = value1.toString();
110         value2 = value2.toString();
111       }
112       result = ((Comparable JavaDoc) value1).compareTo(value2);
113     }
114
115     return result;
116   }
117
118   protected Object JavaDoc convertValue(Class JavaDoc type, String JavaDoc value) {
119     if (type == String JavaDoc.class) {
120       return value;
121     } else if (type == Byte JavaDoc.class || type == byte.class) {
122       return Byte.valueOf(value);
123     } else if (type == Short JavaDoc.class || type == short.class) {
124       return Short.valueOf(value);
125     } else if (type == Character JavaDoc.class || type == char.class) {
126       return new Character JavaDoc(value.charAt(0));
127     } else if (type == Integer JavaDoc.class || type == int.class) {
128       return Integer.valueOf(value);
129     } else if (type == Long JavaDoc.class || type == long.class) {
130       return Long.valueOf(value);
131     } else if (type == Float JavaDoc.class || type == float.class) {
132       return Float.valueOf(value);
133     } else if (type == Double JavaDoc.class || type == double.class) {
134       return Double.valueOf(value);
135     } else if (type == Boolean JavaDoc.class || type == boolean.class) {
136       return Boolean.valueOf(value);
137     } else if (type == Date JavaDoc.class) {
138       try {
139         return DATE_FORMAT.parse(value);
140       } catch (ParseException JavaDoc e) {
141         throw new NestedRuntimeException("Error parsing date. Cause: " + e, e);
142       }
143     } else if (type == BigInteger JavaDoc.class) {
144       return new BigInteger JavaDoc(value);
145     } else if (type == BigDecimal JavaDoc.class) {
146       return new BigDecimal JavaDoc(value);
147     } else {
148       return value;
149     }
150
151   }
152
153 }
154
Popular Tags