1 23 package org.mdarad.framework.expr; 24 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 28 import org.hibernate.criterion.DetachedCriteria; 29 import org.hibernate.criterion.Property; 30 import org.apache.commons.lang.StringUtils; 31 32 41 public class HibernateCriteriaTranslator extends CriteriaTranslator { 42 43 51 public String getExpressionStatement(Class entity, Collection criteria) { 52 53 String className = entity.getName().substring(entity.getName().lastIndexOf('.') + 1, entity.getName().length()); 54 String lcClassName = StringUtils.uncapitalise(className); 55 56 StringBuffer hqlStatement = new StringBuffer ("from ").append(className).append(" as ").append(lcClassName).append("_alias"); 58 59 Criterion localizedCriterion = getFirstLocalizedCriterion(criteria, entity); 61 if (localizedCriterion != null) { 62 String criterionClassName = localizedCriterion.getEntity().getName().substring(localizedCriterion.getEntity().getName().lastIndexOf('.') + 1, localizedCriterion.getEntity().getName().length()); 63 String criterionLCClassName = StringUtils.uncapitalise(criterionClassName); 64 hqlStatement.append(" join ").append(lcClassName).append("_alias").append("."); 65 hqlStatement.append(lcClassName).append("Localizations as ").append(criterionLCClassName); 66 } 67 68 Iterator i = criteria.iterator(); 70 if (i.hasNext()) { 71 hqlStatement.append(" where "); 72 } 73 while (i.hasNext()) { 74 Criterion criterion = (Criterion) i.next(); 75 String criterionClassName = className; 76 String criterionLCClassName = lcClassName + "_alias"; 77 78 try { 80 if(!criterion.getEntity().isInstance(entity.newInstance())) { 81 criterionClassName = criterion.getEntity().getName().substring(criterion.getEntity().getName().lastIndexOf('.') + 1, criterion.getEntity().getName().length()); 82 criterionLCClassName = StringUtils.uncapitalise(criterionClassName); 83 } 84 } catch (InstantiationException e) { 85 throw new IllegalArgumentException (); 86 } catch (IllegalAccessException e) { 87 throw new IllegalArgumentException (); 88 } 89 90 if (criterion != null 91 && criterion.getValue() != null 92 && criterion.getValue().toString() != "") { 93 } 94 OperatorType operator = criterion.getOperator(); 95 String property = criterion.getProperty(); 96 97 if (operator.equals(OperatorTypes.EQUAL)) { 100 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" = ").append(criterion.getValue()); 101 } else if (operator.equals(OperatorTypes.GREATER_THAN)) { 103 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" > ").append(criterion.getValue()); 104 } else if (operator.equals(OperatorTypes.GREATER_THAN_OR_EQUAL)) { 106 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" >= ").append(criterion.getValue()); 107 } else if (operator.equals(OperatorTypes.LOWER_THAN)) { 109 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" < ").append(criterion.getValue()); 110 } else if (operator.equals(OperatorTypes.LOWER_THAN_OR_EQUAL)) { 112 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" <= ").append(criterion.getValue()); 113 } else if (operator.equals(OperatorTypes.IN)) { 115 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" in '").append(criterion.getValue()).append("'"); 116 } else if (operator.equals(OperatorTypes.LIKE)) { 118 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" like '").append(criterion.getValue()).append("'"); 119 } else if (operator.equals(OperatorTypes.START_WITH)) { 121 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" like '").append(criterion.getValue()).append("%'"); 122 } else if (operator.equals(OperatorTypes.END_WITH)) { 124 hqlStatement.append(criterionLCClassName).append(".").append(property).append(" like '%").append(criterion.getValue()).append("'"); 125 } 126 127 if (i.hasNext()) { 129 hqlStatement.append(" and "); 130 } 131 } 132 133 if (localizedCriterion != null) { 135 String criterionClassName = localizedCriterion.getEntity().getName().substring(localizedCriterion.getEntity().getName().lastIndexOf('.') + 1, localizedCriterion.getEntity().getName().length()); 136 String criterionLCClassName = StringUtils.uncapitalise(criterionClassName); 137 hqlStatement.append(" and index(").append(criterionLCClassName).append(") ='").append(localizedCriterion.getLocale()).append("' "); 138 } 139 140 return hqlStatement.toString(); 141 } 142 143 private Criterion getFirstLocalizedCriterion(Collection criteria, Class entity) { 144 Iterator i = criteria.iterator(); 145 while (i.hasNext()) { 146 Criterion criterion = (Criterion) i.next(); 147 148 try { 150 if (!criterion.getEntity().isInstance(entity.newInstance())) { 151 return criterion; 152 } 153 } catch (InstantiationException e) { 154 throw new IllegalArgumentException (); 155 } catch (IllegalAccessException e) { 156 throw new IllegalArgumentException (); 157 } 158 } 159 return null; 160 } 161 162 163 174 public Object getCriterionObject(Class entity, Collection criteria) { 175 176 DetachedCriteria query = DetachedCriteria.forClass(entity); 177 178 for (Iterator i = criteria.iterator(); i.hasNext();) { 180 Criterion criterion = (Criterion) i.next(); 181 182 if (criterion != null 183 && criterion.getValue() != null 184 && criterion.getValue().toString() != "") { 185 OperatorType operator = criterion.getOperator(); 186 Property property = Property.forName(criterion.getProperty()); 187 Object value = criterion.getValue(); 188 if (operator.equals(OperatorTypes.EQUAL)) { 191 query.add(property.eq(value)); 192 } else if (operator.equals(OperatorTypes.GREATER_THAN)) { 194 query.add(property.gt(value)); 195 } else if (operator.equals(OperatorTypes.GREATER_THAN_OR_EQUAL)) { 197 query.add(property.ge(value)); 198 } else if (operator.equals(OperatorTypes.LOWER_THAN)) { 200 query.add(property.lt(value)); 201 } else if (operator.equals(OperatorTypes.LOWER_THAN_OR_EQUAL)) { 203 query.add(property.le(value)); 204 } else if (operator.equals(OperatorTypes.IN)) { 206 query.add(property.in((Object []) value)); 207 } else if (operator.equals(OperatorTypes.LIKE)) { 209 query.add(property.like(value)); 210 } else if (operator.equals(OperatorTypes.START_WITH)) { 212 query.add(property.like(value + "%")); 213 } else if (operator.equals(OperatorTypes.END_WITH)) { 215 query.add(property.like("%" + value)); 216 } 217 } 218 } 219 220 return query; 221 } 222 } 223 | Popular Tags |