public Grant[] GetProcessAgentAdminGrants(int agentID, int groupID)
        {
            ArrayList list = new ArrayList();
            DbConnection myConnection = FactoryDB.GetConnection();
            DbCommand myCommand = FactoryDB.CreateCommand("ProcessAgent_RetrieveAdminGrants", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.Add(FactoryDB.CreateParameter( "@agentID", agentID, DbType.Int32));
            myCommand.Parameters.Add(FactoryDB.CreateParameter( "@groupID", groupID, DbType.Int32));

            try
            {
                myConnection.Open();
                DbDataReader reader = myCommand.ExecuteReader();
                while (reader.Read())
                {
                    Grant grant = new Grant();
                    grant.agentID = reader.GetInt32(0);
                    grant.function = reader.GetString(1);
                    grant.grantID = reader.GetInt32(2);
                    grant.qualifierID = reader.GetInt32(3);
                    list.Add(grant);
                }
                reader.Close();
            }
            catch
            {
            }
            finally
            {
                myConnection.Close();
            }
            Grant dummy = new Grant();
            Grant[] grants = (Grant[])list.ToArray(dummy.GetType());
            return grants;
        }
        /* !------------------------------------------------------------------------------!
         *							CALLS FOR GRANTS
         * !------------------------------------------------------------------------------!
         */
        /// <summary>
        /// to add a grant - returns the grantID
        /// </summary>
        public static int InsertGrant(Grant grant)
        {
            int grantID=-1;

            DbConnection myConnection = FactoryDB.GetConnection();
            DbCommand myCommand = FactoryDB.CreateCommand("AddGrant", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;

            myCommand.Parameters.Add(FactoryDB.CreateParameter(myCommand, "@agentID", grant.agentID, DbType.Int32));
            myCommand.Parameters.Add(FactoryDB.CreateParameter(myCommand, "@functionName", grant.function, DbType.AnsiString,128));
            myCommand.Parameters.Add(FactoryDB.CreateParameter(myCommand, "@qualifierID", grant.qualifierID, DbType.Int32));

            try
            {
                myConnection.Open();
                grantID = Convert.ToInt32 ( myCommand.ExecuteScalar());
            }
            catch (Exception ex)
            {
                throw new Exception("Exception thrown in InsertGrant. " + ex.Message, ex);
            }
            finally
            {
                myConnection.Close();
            }

            //Update Grant cache
            AuthCache.GrantSet = InternalAuthorizationDB.RetrieveGrants();
            return grantID;
        }
        /// <summary>
        /// adds a grant to the agent (User or Group) agentID 
        /// </summary>
        /// <param name="agentID">the ID of the agent to which the grant is to be added</param>
        /// <param name="function">the Function of grant to be added; function must be one of the fixed set of FunctionTypes recognized by the Service Broker</param>
        /// <param name="qualifierID">the ID of a previously created Qualifier whose QualifierType is that required by the function, if the FunctionType requires one; otherwise null</param>
        /// <returns>the ID of the new Grant if agentID is a registered user or group, if function is a legitimate FunctionType, and, in the case where qualifierID is specified, if qualifierID is a recognized ID of the appropriate QualifierType for the function. In addition, the specified grant must not already have been made to the specified agent. If any of these conditions fails, then this method returns null. Note that most of the other Service Broker AddXXX() methods specify the ID of the object being added; the AddGrant() method does not do this; grantIDs are arbitrary strings generated by the Service Broker implementation</returns>
        public static int AddGrant( int agentID, string function, int qualifierID )
        {
            Grant g = new Grant();
            g.agentID = agentID;
            g.function= function;
            g.qualifierID = qualifierID;

            return InternalAuthorizationDB.InsertGrant(g);
        }
        /// <summary>
        /// returns an array of the immutable Grant objects for the registered grants whose IDs are supplied in grantIDs
        /// </summary>
        /// <param name="grantIDs">returns an array of the immutable Grant objects for the registered grants whose IDs are supplied in grantIDs</param>
        /// <returns>an array of Grant objects describing the registered grants specified in grantIDs; if the nth grant ID does not correspond to a grant, the nth entry in the return array will be null</returns>
        /* Check to see if converting all the nulls to -1's work.
         * Also make sure the correct datarow from the table is taken as in
         * function_name and not function_id - CV 12/19/2004*/
        public static Grant[] GetGrantsFromDS( int[] grantIDs )
        {
            DataTable grantsTable = AuthCache.GrantSet.Tables[0];
            ArrayList grantList = new ArrayList();

            foreach(int grantID in grantIDs)
            {
                bool grantIDFound= false;
                foreach(DataRow dataRow in grantsTable.Rows)
                {
                    if(Convert.ToInt32(dataRow["grant_id"])== grantID)
                    {
                        grantIDFound = true;
                        Grant g = new Grant();
                        g.grantID = Convert.ToInt32(dataRow["grant_id"]);
                        g.agentID = Convert.ToInt32(dataRow["agent_id"]);
                        g.function = dataRow["function_name"].ToString();
                        g.qualifierID = Convert.ToInt32(dataRow["qualifier_id"]);

                        grantList.Add(g);

                        break;
                    }
                }
                if(!grantIDFound)
                {
                    Grant g = new Grant();
                    g.grantID = -1;
                    g.agentID = -1;
                    g.function = null;
                    g.qualifierID = -1;

                    grantList.Add(g);
                }
            }

            // Convert to a grants array
            Grant[] getGrants = new Grant[grantIDs.Length];

            for(int i = 0; i<grantIDs.Length; i++)
            {
                Grant g = (Grant) grantList[i];
                getGrants[i] = (Grant) grantList[i];
            }

            return getGrants;
        }