Sunday, February 14, 2016

ADF : VO Query Dynamic WHERE Clause

Recently, I faced a scenario where I need to dynamically change the View Object's query WHERE clause. I followed below steps to address it.

Steps:
  • Override the buildWhereClause(StringBuffer stringBuffer, int i) VO method
  • Call super.buildWhereClause() to allow the framework processing. This call will return a Boolean indicator of whether a WHERE clause was appended to the query, indicated by the Boolean appended local variable.
  • The StringBuffer parameter that is passed to the method is the complete query SQL statement. Do your changes directly onto it. When done, make sure that you return appropriately a true/false boolean to to the framework to indicate whether a WHERE clause was appended to the query or not.

Example: Add/append additional WHERE condition when logged in user is not an ADMIN.

@Override
protected boolean buildWhereClause(StringBuffer stringBuffer, int i) {
        // TODO Implement this method      
      boolean hasWhereClause = super.buildWhereClause(stringBuffer, i);
      boolean isAdmin = ADFContext.getCurrent().getSecurityContext().isUserInRole("ADMIN_ROLE");

      if (hasWhereClause) {
          if (!isAdmin) {
              stringBuffer.append(" AND (USER_EMAIL =  ' " + ADFContext.getCurrent().getSecurityContext().getUserName() + "')" );
           }
        } else {
          if (!isAdmin) {
              stringBuffer.append(" WHERE (USER_EMAIL =  ' " + ADFContext.getCurrent().getSecurityContext().getUserName() + "')" );
              hasWhereClause = true;
            }
        }
        return hasWhereClause;
    }

No comments:

Post a Comment

Provide your thoughts !