log.info(sqlquery);
log.debug(sqlquery);
    public List query(Admin admin, Query query, int index, int numberofrows) throws IllegalQueryException, AuthorizationDeniedException {        debug(">query(): ");        Connection con = null;        PreparedStatement ps = null;        ResultSet rs = null;                boolean authorizedToApproveCAActions = false; // i.e approvals with endentityprofile ApprovalDataVO.ANY_ENDENTITYPROFILE        boolean authorizedToApproveRAActions = false; // i.e approvals with endentityprofile not ApprovalDataVO.ANY_ENDENTITYPROFILE                 try {			authorizedToApproveCAActions = getAuthorizationSession().isAuthorizedNoLog(admin, AvailableAccessRules.REGULAR_APPROVECAACTION);		} catch (AuthorizationDeniedException e1) {}        try {			authorizedToApproveRAActions = getAuthorizationSession().isAuthorizedNoLog(admin, AvailableAccessRules.REGULAR_APPROVEENDENTITY);		} catch (AuthorizationDeniedException e1) {		}		if(!authorizedToApproveCAActions && !authorizedToApproveRAActions){			throw new AuthorizationDeniedException("Not authorized to query apporvals");		}		        ArrayList returnData = new ArrayList();        GlobalConfiguration globalconfiguration = getRAAdminSession().loadGlobalConfiguration(admin);        RAAuthorization raauthorization = null;        String sqlquery = "select " + APPROVALDATA_COL + " from ApprovalData where ";        // Check if query is legal.        if (query != null && !query.isLegalQuery())            throw new IllegalQueryException();        if (query != null)            sqlquery = sqlquery + query.getQueryString();        raauthorization = new RAAuthorization(admin, getRAAdminSession(), getAuthorizationSession());        String caauthstring = raauthorization.getCAAuthorizationString();        String endentityauth = "";        if (globalconfiguration.getEnableEndEntityProfileLimitations()){        	endentityauth = raauthorization.getEndEntityProfileAuthorizationString(true);        	if(authorizedToApproveCAActions && authorizedToApproveRAActions){        		endentityauth = raauthorization.getEndEntityProfileAuthorizationString(true);        		if(endentityauth != null){        		  endentityauth = "(" + raauthorization.getEndEntityProfileAuthorizationString(false) + " OR endEntityprofileId=" + ApprovalDataVO.ANY_ENDENTITYPROFILE + " ) ";        		}        	}else if (authorizedToApproveCAActions) {        		endentityauth = " endEntityprofileId=" + ApprovalDataVO.ANY_ENDENTITYPROFILE;			}else if (authorizedToApproveRAActions) {				endentityauth = raauthorization.getEndEntityProfileAuthorizationString(true);			}        	        	        }        if (!caauthstring.trim().equals("") && query != null){          sqlquery = sqlquery + " AND " + caauthstring;        }else{          sqlquery = sqlquery + caauthstring;        }        if(endentityauth != null){          if (caauthstring.trim().equals("") && query == null){        	sqlquery = sqlquery + endentityauth;          }else{          	sqlquery = sqlquery + " AND " + endentityauth;          }        }                try {                // Construct SQL query.                con = JDBCUtil.getDBConnection(JNDINames.DATASOURCE);                log.info(sqlquery);                ps = con.prepareStatement(sqlquery);                                // Execute query.                rs = ps.executeQuery();                rs.relative(index);                // Assemble result.                while (rs.next() && returnData.size() < numberofrows) {                	                    // Read the variables in order, some databases (i.e. MS-SQL)                     // seems to not like out-of-order read of columns (i.e. nr 15 before nr 1)                    int id = rs.getInt(1);                    int approvalid = rs.getInt(2);                    int approvaltype = rs.getInt(3);                    int endentityprofileId = rs.getInt(4);                    int caid = rs.getInt(5);                    String reqadmincertissuerdn = rs.getString(6);                    String reqadmincertserial = rs.getString(7);                    int status = rs.getInt(8);                    String approvaldatastring = rs.getString(9);                    String requestdatastring = rs.getString(10);                    long requestdate = rs.getLong(11);                    long expiredate = rs.getLong(12);                    int remainingapprovals = rs.getInt(13);                	ApprovalDataVO data = new ApprovalDataVO(id,approvalid,approvaltype,endentityprofileId,caid,                			                                 reqadmincertissuerdn, reqadmincertserial, status,                			                                 ApprovalDataUtil.getApprovals(approvaldatastring),                			                                 ApprovalDataUtil.getApprovalRequest(requestdatastring),                			                                 new Date(requestdate), new Date(expiredate), remainingapprovals);                 	returnData.add(data);                }                                    debug("<query()");            return returnData;        } catch (Exception e) {            throw new EJBException(e);        } finally {            JDBCUtil.close(con, ps, rs);        }    } // query 