String actualQuery = this.baseQuery;
// Replace where clause (if any)
if (this.baseQuery.indexOf(this.whereClausePlaceholder) > 0) {
XString xsWhere = new XString();
if (this.filterObject != null) {
// Construct where clause using filter object
try {
BeanInfo bi = Introspector.getBeanInfo(this.filterObject.getClass());
for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
String name = pd.getName();
if (!this.excludedFilterObjectProperties.contains(name)) {
String mappedName = this.getActualColumnName(name);
// Workaround for boolean property that uses the "is" instead of "get" syntax
Method readMethod = pd.getReadMethod();
if (readMethod == null && pd.getPropertyType() == Boolean.class) {
String methodName = "is" + StringUtils.capitalize(pd.getName());
try {
readMethod = this.filterObject.getClass().getMethod(methodName);
} catch (NoSuchMethodException ex) {
logger.debug(String.format("Couldn't find method %s on object %s"
, methodName, filterObject.getClass()));
// Ignore...
}
}
AssertUtil.assertNotNull("Property is not readable: " + pd.getName(), readMethod);
Object value = readMethod.invoke(this.filterObject);
if (value != null) {
xsWhere.assertEmptyOrText(" AND ");
// Handle wildcard search
String operator = "=";
if (this.wildcardSearch && value instanceof String && value.toString().indexOf(DB_WILDCARD_CHAR) >= 0) {
// TODO: Wildcard escaping...
operator = " like ";
}
// Handle case insensitive search
String convertedColumn = mappedName;
if (this.caseInsensitiveSearch && value instanceof String) {
convertedColumn = this.emUtil.asConnUtil().getDbHandler().wrapInLowerString(mappedName);
value = value.toString().toLowerCase();
}
xsWhere.addFormatted("%s%s:%s", convertedColumn, operator, mappedName);
this.queryAttributes.put(mappedName, value);
}
}
}
} catch (Exception ex) {
throw new JuRuntimeException("Couldn't introspect bean", ex);
}
}
if (xsWhere.isEmpty()) {
xsWhere.addText("1 = 1");
}
String whereClause = String.format("(%s)", xsWhere.toString());
while (actualQuery.indexOf(this.whereClausePlaceholder) > 0) {
actualQuery = actualQuery.replace(this.whereClausePlaceholder, whereClause);
}
}
// Add order by clause
if (this.ordering.size() > 0) {
XString xsOrderBy = new XString();
for (AttributeOrdering ao : this.ordering) {
xsOrderBy.assertEmptyOrText(", ");
xsOrderBy.addText(this.getActualColumnName(ao.getAttributeName()));
if (ao.getOrdering() == Ordering.DESCENDING) xsOrderBy.addText(" desc");
}
while (actualQuery.indexOf(this.orderByClausePlaceholder) > 0) {
actualQuery = actualQuery.replace(this.orderByClausePlaceholder, xsOrderBy.toString());
}
}
logger.debug(actualQuery);