| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.core.jpa.criteria; |
| 17 | |
|
| 18 | |
import java.math.BigDecimal; |
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.HashMap; |
| 21 | |
import java.util.Iterator; |
| 22 | |
import java.util.LinkedHashMap; |
| 23 | |
import java.util.List; |
| 24 | |
import java.util.Map; |
| 25 | |
|
| 26 | |
import javax.persistence.Query; |
| 27 | |
|
| 28 | |
import org.kuali.rice.core.jpa.criteria.QueryByCriteria.QueryByCriteriaType; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
@SuppressWarnings("unchecked") |
| 38 | 0 | public class Criteria { |
| 39 | |
|
| 40 | |
private Integer searchLimit; |
| 41 | |
|
| 42 | |
private String entityName; |
| 43 | |
|
| 44 | |
private String alias; |
| 45 | |
|
| 46 | |
private int bindParamCount; |
| 47 | |
|
| 48 | 0 | private boolean distinct = false; |
| 49 | |
|
| 50 | 0 | protected List tokens = new ArrayList(); |
| 51 | |
|
| 52 | 0 | private List orderByTokens = new ArrayList(); |
| 53 | |
|
| 54 | 0 | protected Map<String, Object> params = new LinkedHashMap<String, Object>(); |
| 55 | |
|
| 56 | |
public Criteria(String entityName) { |
| 57 | 0 | this(entityName, "a"); |
| 58 | 0 | } |
| 59 | |
|
| 60 | 0 | public Criteria(String entityName, String alias) { |
| 61 | 0 | this.entityName = entityName; |
| 62 | 0 | this.alias = alias; |
| 63 | 0 | } |
| 64 | |
|
| 65 | |
public void between(String attribute, Object value1, Object value2) { |
| 66 | 0 | String fixedAttr = fixAttr(attribute); |
| 67 | 0 | tokens.add(" (" + alias + "." + attribute + " BETWEEN :" + fixedAttr + "-b1 AND :" + fixedAttr + "-b2) "); |
| 68 | 0 | params.put(fixedAttr+ "-b1", value1); |
| 69 | 0 | params.put(fixedAttr+ "-b2", value2); |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
private String fixAttr(String string) { |
| 79 | 0 | return string.replace(".", "_"); |
| 80 | |
} |
| 81 | |
|
| 82 | |
public void eq(String attribute, Object value) { |
| 83 | 0 | String fixedAttr = fixAttr(attribute); |
| 84 | 0 | tokens.add(alias + "." + attribute + " = :" + fixedAttr + " "); |
| 85 | 0 | params.put(fixedAttr, value); |
| 86 | 0 | } |
| 87 | |
|
| 88 | |
public void gt(String attribute, Object value) { |
| 89 | 0 | String fixedAttr = fixAttr(attribute); |
| 90 | 0 | tokens.add(alias + "." + attribute + " > :" + fixedAttr + " "); |
| 91 | 0 | params.put(fixedAttr, value); |
| 92 | 0 | } |
| 93 | |
|
| 94 | |
public void gte(String attribute, Object value) { |
| 95 | 0 | String fixedAttr = fixAttr(attribute); |
| 96 | 0 | tokens.add(alias + "." + attribute + " >= :" + fixedAttr + " "); |
| 97 | 0 | params.put(fixedAttr, value); |
| 98 | 0 | } |
| 99 | |
|
| 100 | |
public void like(String attribute, Object value) { |
| 101 | 0 | String fixedAttr = fixAttr(attribute); |
| 102 | 0 | if (attribute.contains("__JPA_ALIAS__")) { |
| 103 | 0 | String bind = "BIND_PARAM_" + (++bindParamCount); |
| 104 | 0 | tokens.add(attribute + " LIKE :" + bind + " "); |
| 105 | 0 | params.put(bind, value); |
| 106 | 0 | } else { |
| 107 | 0 | tokens.add(alias + "." + attribute + " LIKE :" + stripFunctions(fixedAttr) + " "); |
| 108 | 0 | params.put(fixedAttr, value); |
| 109 | |
} |
| 110 | 0 | } |
| 111 | |
|
| 112 | |
public void notLike(String attribute, Object value) { |
| 113 | 0 | String fixedAttr = fixAttr(attribute); |
| 114 | 0 | tokens.add(alias + "." + attribute + " NOT LIKE :" + fixedAttr + " "); |
| 115 | 0 | params.put(fixedAttr, value); |
| 116 | 0 | } |
| 117 | |
|
| 118 | |
public void lt(String attribute, Object value) { |
| 119 | 0 | String fixedAttr = fixAttr(attribute); |
| 120 | 0 | tokens.add(alias + "." + attribute + " < :" + fixedAttr + " "); |
| 121 | 0 | params.put(fixedAttr, value); |
| 122 | 0 | } |
| 123 | |
|
| 124 | |
public void lte(String attribute, Object value) { |
| 125 | 0 | String fixedAttr = fixAttr(attribute); |
| 126 | 0 | tokens.add(alias + "." + attribute + " <= :" + fixedAttr + " "); |
| 127 | 0 | params.put(fixedAttr, value); |
| 128 | 0 | } |
| 129 | |
|
| 130 | |
public void ne(String attribute, Object value) { |
| 131 | 0 | String fixedAttr = fixAttr(attribute); |
| 132 | 0 | tokens.add(alias + "." + attribute + " != :" + fixedAttr + " "); |
| 133 | 0 | params.put(fixedAttr, value); |
| 134 | 0 | } |
| 135 | |
|
| 136 | |
public void isNull(String attribute) { |
| 137 | 0 | tokens.add(alias + "." + attribute + " IS NULL "); |
| 138 | 0 | } |
| 139 | |
|
| 140 | |
public void rawJpql(String jpql) { |
| 141 | 0 | tokens.add(" " + jpql + " "); |
| 142 | 0 | } |
| 143 | |
|
| 144 | |
public void in(String attribute, List values) { |
| 145 | 0 | String in = ""; |
| 146 | 0 | for (Object object : values) { |
| 147 | 0 | in += "'"+object + "',"; |
| 148 | |
} |
| 149 | 0 | if (!"".equals(in)) { |
| 150 | 0 | in = in.substring(0, in.length()-1); |
| 151 | |
} |
| 152 | 0 | tokens.add(alias + "." + attribute + " IN (" + in + ") "); |
| 153 | 0 | } |
| 154 | |
|
| 155 | |
public void notIn(String attribute, List values) { |
| 156 | 0 | String in = ""; |
| 157 | 0 | for (Object object : values) { |
| 158 | 0 | in += "'"+object + "',"; |
| 159 | |
} |
| 160 | 0 | if (!"".equals(in)) { |
| 161 | 0 | in = in.substring(in.length()-1); |
| 162 | |
} |
| 163 | 0 | tokens.add(alias + "." + attribute + " NOT IN (" + in + ") "); |
| 164 | 0 | } |
| 165 | |
|
| 166 | |
public void orderBy(String attribute, boolean sortAscending) { |
| 167 | 0 | String sort = (sortAscending ? "ASC" : "DESC"); |
| 168 | 0 | orderByTokens.add(alias + "." + attribute + " " + sort + " "); |
| 169 | 0 | } |
| 170 | |
|
| 171 | |
public void and(Criteria and) { |
| 172 | 0 | tokens.add(new AndCriteria(and)); |
| 173 | 0 | } |
| 174 | |
|
| 175 | |
public void or(Criteria or) { |
| 176 | 0 | tokens.add(new OrCriteria(or)); |
| 177 | 0 | } |
| 178 | |
|
| 179 | |
public String toQuery(QueryByCriteriaType type) { |
| 180 | 0 | String queryType = type.toString(); |
| 181 | 0 | if (type.equals(QueryByCriteriaType.SELECT)) { |
| 182 | 0 | if(distinct){ |
| 183 | 0 | queryType += " " + "DISTINCT"; |
| 184 | |
} |
| 185 | |
|
| 186 | 0 | queryType += " " + alias; |
| 187 | |
} |
| 188 | 0 | String queryString = queryType + " FROM " + entityName + " AS " + alias; |
| 189 | 0 | if (!tokens.isEmpty()) { |
| 190 | 0 | queryString += " WHERE " + buildWhere(); |
| 191 | |
} |
| 192 | 0 | if (!orderByTokens.isEmpty()) { |
| 193 | 0 | queryString += " ORDER BY "; |
| 194 | 0 | int count = 0; |
| 195 | 0 | for (Iterator iterator = orderByTokens.iterator(); iterator.hasNext();) { |
| 196 | 0 | Object token = (Object) iterator.next(); |
| 197 | 0 | if (count == 0) { |
| 198 | 0 | count++; |
| 199 | |
} else { |
| 200 | 0 | queryString += ", "; |
| 201 | |
} |
| 202 | 0 | queryString += (String) token; |
| 203 | 0 | } |
| 204 | |
} |
| 205 | 0 | return fix(queryString); |
| 206 | |
} |
| 207 | |
|
| 208 | |
public String toCountQuery() { |
| 209 | 0 | String queryString = "SELECT COUNT(*) FROM " + entityName + " AS " + alias; |
| 210 | 0 | if (!tokens.isEmpty()) { |
| 211 | 0 | queryString += " WHERE " + buildWhere(); |
| 212 | |
} |
| 213 | 0 | return fix(queryString); |
| 214 | |
} |
| 215 | |
|
| 216 | |
private String fix(String queryString) { |
| 217 | 0 | queryString = queryString.replaceAll("__JPA_ALIAS__", alias); |
| 218 | 0 | return queryString; |
| 219 | |
} |
| 220 | |
|
| 221 | |
private String buildWhere() { |
| 222 | 0 | String queryString = ""; |
| 223 | 0 | int i = 0; |
| 224 | 0 | for (Iterator iterator = tokens.iterator(); iterator.hasNext();) { |
| 225 | 0 | Object token = (Object) iterator.next(); |
| 226 | 0 | if (token instanceof Criteria) { |
| 227 | 0 | String logic = ""; |
| 228 | 0 | if (i>0 && token instanceof AndCriteria) { |
| 229 | 0 | logic = " AND "; |
| 230 | 0 | } else if (i>0 && token instanceof OrCriteria) { |
| 231 | 0 | logic = " OR "; |
| 232 | |
} |
| 233 | 0 | queryString += logic + " (" + ((Criteria) token).buildWhere() + ") "; |
| 234 | 0 | } else { |
| 235 | 0 | if(i>0){ |
| 236 | 0 | queryString += " AND " + (String) token; |
| 237 | |
}else{ |
| 238 | 0 | queryString += (String) token; |
| 239 | |
} |
| 240 | |
} |
| 241 | 0 | i++; |
| 242 | 0 | } |
| 243 | 0 | return queryString; |
| 244 | |
} |
| 245 | |
|
| 246 | |
|
| 247 | |
void prepareParameters(Query query) { |
| 248 | 0 | prepareParameters(query, tokens, params); |
| 249 | 0 | } |
| 250 | |
|
| 251 | |
void prepareParameters(Query query, List tokens, Map<String, Object> params) { |
| 252 | 0 | for (Map.Entry<String, Object> param : params.entrySet()) { |
| 253 | 0 | Object value = param.getValue(); |
| 254 | 0 | if (value instanceof BigDecimal) { |
| 255 | 0 | value = new Long(((BigDecimal)value).longValue()); |
| 256 | |
} |
| 257 | 0 | if (value instanceof String) { |
| 258 | 0 | value = ((String)value).replaceAll("\\*", "%"); |
| 259 | |
} |
| 260 | 0 | query.setParameter(param.getKey(), value); |
| 261 | 0 | } |
| 262 | 0 | for (Iterator iterator = tokens.iterator(); iterator.hasNext();) { |
| 263 | 0 | Object token = (Object) iterator.next(); |
| 264 | 0 | if (token instanceof Criteria) { |
| 265 | 0 | prepareParameters(query, ((Criteria)token).tokens, ((Criteria)token).params); |
| 266 | |
} |
| 267 | 0 | } |
| 268 | 0 | } |
| 269 | |
|
| 270 | |
private class AndCriteria extends Criteria { |
| 271 | 0 | public AndCriteria(Criteria and) { |
| 272 | 0 | super(and.entityName, and.alias); |
| 273 | 0 | this.tokens = new ArrayList(and.tokens); |
| 274 | 0 | this.params = new HashMap(and.params); |
| 275 | 0 | } |
| 276 | |
} |
| 277 | |
|
| 278 | |
private class OrCriteria extends Criteria { |
| 279 | 0 | public OrCriteria(Criteria or) { |
| 280 | 0 | super(or.entityName, or.alias); |
| 281 | 0 | this.tokens = new ArrayList(or.tokens); |
| 282 | 0 | this.params = new HashMap(or.params); |
| 283 | 0 | } |
| 284 | |
} |
| 285 | |
|
| 286 | |
public Integer getSearchLimit() { |
| 287 | 0 | return this.searchLimit; |
| 288 | |
} |
| 289 | |
|
| 290 | |
public void setSearchLimit(Integer searchLimit) { |
| 291 | 0 | this.searchLimit = searchLimit; |
| 292 | 0 | } |
| 293 | |
|
| 294 | |
|
| 295 | |
public void notNull(String attribute) { |
| 296 | 0 | tokens.add(alias + "." + attribute + " IS NOT NULL "); |
| 297 | 0 | } |
| 298 | |
|
| 299 | |
public void distinct(boolean distinct){ |
| 300 | 0 | this.distinct = distinct; |
| 301 | 0 | } |
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
public void notBetween(String attribute, Object value1, |
| 311 | |
Object value2) { |
| 312 | 0 | String fixedAttr = fixAttr(attribute); |
| 313 | 0 | tokens.add(" (" + alias + "." + attribute + " NOT BETWEEN :" + fixedAttr + "-b1 AND :" + fixedAttr + "-b2) "); |
| 314 | 0 | params.put(fixedAttr + "-b1", value1); |
| 315 | 0 | params.put(fixedAttr + "-b2", value2); |
| 316 | 0 | } |
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
public void in(String match, Criteria subQuery, String attribute) { |
| 325 | 0 | if("a".equals(subQuery.alias)){ |
| 326 | 0 | subQuery.alias="b"; |
| 327 | |
} |
| 328 | 0 | String whereClause = ""; |
| 329 | 0 | if(subQuery.tokens.isEmpty()){ |
| 330 | 0 | whereClause = "WHERE "; |
| 331 | |
}else{ |
| 332 | 0 | whereClause = "AND "; |
| 333 | |
} |
| 334 | 0 | whereClause += subQuery.alias+"."+attribute + " = " + alias+"."+match; |
| 335 | |
|
| 336 | 0 | tokens.add("EXISTS (" + subQuery.toQuery(QueryByCriteriaType.SELECT) + whereClause + " ) "); |
| 337 | |
|
| 338 | 0 | } |
| 339 | |
|
| 340 | |
private String stripFunctions(String attribute) { |
| 341 | 0 | int index = attribute.lastIndexOf('('); |
| 342 | 0 | if(index != -1) { |
| 343 | 0 | return attribute.substring(index+1, attribute.indexOf(')')); |
| 344 | |
} |
| 345 | |
|
| 346 | 0 | return attribute; |
| 347 | |
} |
| 348 | |
|
| 349 | |
public String getAlias() { |
| 350 | 0 | return this.alias; |
| 351 | |
} |
| 352 | |
|
| 353 | |
} |