}
}
String createQueryString() {
final OBContext obContext = OBContext.getOBContext();
final Entity e = getEntity();
// split the orderby and where
final String qryStr = getWhereAndOrderBy();
final String orderByClause;
String whereClause;
final int orderByIndex = qryStr.toLowerCase().indexOf("order by");
if (orderByIndex != -1) {
whereClause = qryStr.substring(0, orderByIndex);
orderByClause = qryStr.substring(orderByIndex);
} else {
whereClause = qryStr;
orderByClause = "";
}
// strip the where, is added later
if (whereClause.trim().toLowerCase().startsWith("where")) {
final int whereIndex = whereClause.toLowerCase().indexOf("where");
if (whereIndex != -1) {
whereClause = whereClause.substring(1 + whereIndex + "where".length());
}
}
// the query can start with an alias to support joins
//
String alias = null;
// this is a space on purpose
String prefix = " ";
if (whereClause.toLowerCase().trim().startsWith("as")) {
// strip the as
final String strippedWhereClause = whereClause.toLowerCase().trim().substring(2).trim();
// get the next space
final int index = strippedWhereClause.indexOf(" ");
alias = strippedWhereClause.substring(0, index);
prefix = alias + ".";
}
// The following if is there because the clauses which are added should
// all be and-ed. Special cases which need to be handled:
// left join a left join b where a.id is not null or b.id is not null
// id='0' and exists (from ADModelObject as mo where mo.id=id)
// id='0'
boolean addWhereClause = true;
if (whereClause.trim().length() > 0) {
if (!whereClause.toLowerCase().contains("where")) {
// simple case: id='0's
whereClause = " where (" + whereClause + ")";
addWhereClause = false;
} else {
// check if the where is before
final int fromIndex = whereClause.toLowerCase().indexOf("from");
int whereIndex = -1;
if (fromIndex == -1) {
// already there and no from
// now find the place where to put the brackets
// case: left join a left join b where a.id is not null or
// b.id is not null
whereIndex = whereClause.toLowerCase().indexOf("where");
Check.isTrue(whereIndex != -1, "Where not found in string: " + whereClause);
} else {
// example: id='0' and exists (from ADModelObject as mo
// where mo.id=id)
// example: left join x where id='0' and x.id=id and exists
// (from ADModelObject as mo where mo.id=id)
// check if the whereClause is before the first from
whereIndex = whereClause.toLowerCase().substring(0, fromIndex).indexOf("where");
}
if (whereIndex != -1) {
// example: left join x where id='0' and x.id=id and exists
// (from ADModelObject as mo where mo.id=id)
addWhereClause = false;
// now put the ( at the correct place
final int endOfWhere = whereIndex + "where".length();
whereClause = whereClause.substring(0, endOfWhere) + " ("
+ whereClause.substring(endOfWhere) + ")";
} else { // no whereclause before the from
// example: id='0' and exists (from ADModelObject as mo
// where mo.id=id)
whereClause = " where (" + whereClause + ")";
addWhereClause = false;
}
}
}
OBContext.getOBContext().getEntityAccessChecker().checkReadable(e);
if (isFilterOnReadableOrganization() && e.isOrganizationPartOfKey()) {
whereClause = (addWhereClause ? " where " : "") + addAnd(whereClause) + prefix
+ "id.organization.id " + createInClause(obContext.getReadableOrganizations());
if (addWhereClause) {
addWhereClause = false;
}
} else if (isFilterOnReadableOrganization() && e.isOrganizationEnabled()) {
whereClause = (addWhereClause ? " where " : "") + addAnd(whereClause) + prefix
+ "organization.id " + createInClause(obContext.getReadableOrganizations());
if (addWhereClause) {
addWhereClause = false;
}
}
if (isFilterOnReadableClients() && getEntity().isClientEnabled()) {
whereClause = (addWhereClause ? " where " : "") + addAnd(whereClause) + prefix + "client.id "
+ createInClause(obContext.getReadableClients());
if (addWhereClause) {
addWhereClause = false;
}
}
if (isFilterOnActive() && e.isActiveEnabled()) {
whereClause = (addWhereClause ? " where " : "") + addAnd(whereClause) + prefix
+ "active='Y' ";
if (addWhereClause) {
addWhereClause = false;
}