1 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 ; 23 import java.math.BigInteger ; 24 import java.text.DateFormat ; 25 import java.text.ParseException ; 26 import java.text.SimpleDateFormat ; 27 import java.util.Date ; 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 DATE_MASK = "yyyy/MM/dd hh:mm:ss"; 35 private static final DateFormat DATE_FORMAT = new SimpleDateFormat (DATE_MASK); 36 37 public abstract boolean isCondition(SqlTagContext ctx, SqlTag tag, Object parameterObject); 38 39 public int doStartFragment(SqlTagContext ctx, SqlTag tag, Object 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 parameterObject, StringBuffer 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 64 return super.doEndFragment(ctx,tag,parameterObject,bodyContent); 65 } 66 67 protected long compare(SqlTagContext ctx, SqlTag tag, Object parameterObject) { 68 String propertyName = tag.getPropertyAttr(); 69 String comparePropertyName = tag.getComparePropertyAttr(); 70 String compareValue = tag.getCompareValueAttr(); 71 72 String prop = tag.getPropertyAttr(); 73 Object value1; 74 Class 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 .class; 84 } 85 } 86 if (comparePropertyName != null) { 87 Object 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 type, Object value1, Object 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 && type != String .class) { 106 value1 = value1.toString(); 107 } 108 if (!(value1 instanceof Comparable && value2 instanceof Comparable )) { 109 value1 = value1.toString(); 110 value2 = value2.toString(); 111 } 112 result = ((Comparable ) value1).compareTo(value2); 113 } 114 115 return result; 116 } 117 118 protected Object convertValue(Class type, String value) { 119 if (type == String .class) { 120 return value; 121 } else if (type == Byte .class || type == byte.class) { 122 return Byte.valueOf(value); 123 } else if (type == Short .class || type == short.class) { 124 return Short.valueOf(value); 125 } else if (type == Character .class || type == char.class) { 126 return new Character (value.charAt(0)); 127 } else if (type == Integer .class || type == int.class) { 128 return Integer.valueOf(value); 129 } else if (type == Long .class || type == long.class) { 130 return Long.valueOf(value); 131 } else if (type == Float .class || type == float.class) { 132 return Float.valueOf(value); 133 } else if (type == Double .class || type == double.class) { 134 return Double.valueOf(value); 135 } else if (type == Boolean .class || type == boolean.class) { 136 return Boolean.valueOf(value); 137 } else if (type == Date .class) { 138 try { 139 return DATE_FORMAT.parse(value); 140 } catch (ParseException e) { 141 throw new NestedRuntimeException("Error parsing date. Cause: " + e, e); 142 } 143 } else if (type == BigInteger .class) { 144 return new BigInteger (value); 145 } else if (type == BigDecimal .class) { 146 return new BigDecimal (value); 147 } else { 148 return value; 149 } 150 151 } 152 153 } 154 | Popular Tags |