return (Long) q.getSingleResult();
}
private Query buildQuery(Map parameters) {
// _entityManager.clear();
UserRoles user = (UserRoles) parameters.get(FETCH_USER);
Class taskClass = (Class) parameters.get(FETCH_CLASS);
String subQuery = MapUtils.getString(parameters, FETCH_SUB_QUERY, "");
ArrayList userIdList = new ArrayList();
userIdList.add(user.getUserID());
String baseQuery = parameters.containsKey(FETCH_COUNT) ? QUERY_GENERIC_COUNT
: QUERY_GENERIC1;
Query q;
if (StringUtils.isEmpty(subQuery)) {
q = _entityManager.createQuery(
baseQuery + taskClass.getSimpleName() + QUERY_GENERIC2)
.setParameter(1, userIdList).setParameter(2,
user.getAssignedRoles());
} else {
StringBuffer buffer = new StringBuffer();
buffer.append(baseQuery).append(taskClass.getSimpleName()).append(
QUERY_GENERIC2);
String trim = subQuery.toLowerCase().trim();
int orderIndex = trim.indexOf("order");
if (orderIndex == -1) {
buffer.append(" and ").append(" ( ").append(subQuery).append(
" ) ");
} else {
if (!trim.startsWith("order"))
buffer.append(" and (").append(
subQuery.substring(0, orderIndex)).append(") ")
.append(subQuery.substring(orderIndex));
else {
buffer.append(subQuery);
}
}
if (_logger.isDebugEnabled()){
_logger.debug(buffer.toString());
_logger.debug("Parameter 1:" + userIdList);
_logger.debug("Parameter 2:" + user.getAssignedRoles());
}
q = _entityManager.createQuery(buffer.toString()).setParameter(1,
userIdList).setParameter(2, user.getAssignedRoles());
}
return q;
}