protected DataTable ConvertToRolePermissionsBindingTable(ThinkgatePermissionsCollection permissions, ThinkgateRole role)
		{
			DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("ID", typeof(System.Guid)));
            table.Columns.Add(new DataColumn("ParentID", typeof(System.Guid)));
			table.Columns.Add(new DataColumn("Name", typeof(string)));
			table.Columns.Add(new DataColumn("Member", typeof(bool)));
			table.Columns.Add(new DataColumn("PermissionLevelValue", typeof(int)));
			table.Columns.Add(new DataColumn("Description", typeof(string)));

			foreach (ThinkgatePermission permission in permissions)
			{
				DataRow row = table.NewRow();
                row["ID"] = permission.PermissionId;
				row["ParentID"] = permission.ParentPermissionId;
				row["Name"] = permission.PermissionName;
				row["Member"] = role.PermissionsHT.Contains(permission);
				row["PermissionLevelValue"] = role.PermissionsHT.Contains(permission) ? role.PermissionsHT.GetPermission(permission.ToString()).PermissionLevelValue : DataIntegrity.ConvertToInt(ThinkgatePermission.PermissionLevelValues.NoValue.ToString());
				row["Description"] = permission.Description;
				table.Rows.Add(row);
			}
			return table;
		}
        protected void rgRoles_UpdateCommand(Object source, GridCommandEventArgs e)
        {
            var editedItem = e.Item as GridEditableItem;

            if (editedItem == null) return;

            //Get the new values:
            var newValues = new Hashtable();
            e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
            Guid roleID = new Guid();
            Guid.TryParse(editedItem.GetDataKeyValue("RoleID").ToString(), out roleID);
            string roleName = newValues["RoleName"] == null ? string.Empty : newValues["RoleName"].ToString();
            string roleDescription = newValues["Description"] == null ? string.Empty : newValues["Description"].ToString();
            bool active = newValues["Active"] != null && DataIntegrity.ConvertToBool(newValues["Active"]);
            int portalSelection = newValues["RolePortalSelection"] == null ? 0 : DataIntegrity.ConvertToInt(newValues["RolePortalSelection"]);

            if (String.IsNullOrEmpty(roleName)) return;

            ThinkgateRole role = new ThinkgateRole(roleID, roleName, roleDescription, active, portalSelection);
            role.UpdateRole();
            LoadRoles();

        }
        protected void rtlRolePermissionsForEditing_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            Guid roleID = new Guid();
            Guid.TryParse(Request.QueryString["ID"], out roleID);
            if (roleID == default(Guid)) return;

            ThinkgatePermissionsCollection permissions = new ThinkgatePermissionsCollection();
            permissions.GetPermissionsCollection(PermissionCollectionTypes.All, 1);
            ThinkgateRole role = new ThinkgateRole(roleID);
            lblHeaderItemID.Text = role.RoleId.ToString();
            lblHeaderItemName.Text = String.Format("Permissions in {0}  Role:", role.RoleName);

            DataTable test  = ConvertToRolePermissionsBindingTable(permissions, role);
            rtlRolePermissionsForEditing.DataSource = test;
        }
        protected void rtlRolePermissionsForEditing_UpdateCommand(Object source, TreeListCommandEventArgs e)
        {
            var editedItem = e.Item as TreeListDataItem;
            if (editedItem == null) return;

            Guid newRoleID = new Guid();
            Guid.TryParse(lblHeaderItemID.Text, out newRoleID);
            if (newRoleID == default(Guid)) return;

            ThinkgateRole role = new ThinkgateRole(newRoleID);
            if (String.IsNullOrEmpty(role.RoleName)) return;

            //Get the new values:
            var newValues = new Hashtable();
            e.Item.OwnerTreeList.ExtractValuesFromItem(newValues, editedItem, true);
            Guid permissionID = new Guid();
            Guid.TryParse(newValues["ID"].ToString(), out permissionID);
            if (permissionID == default(Guid)) return;

            string permissionName = newValues["Name"] == null ? string.Empty : newValues["Name"].ToString();
            int permissionLevel = newValues["PermissionLevelValue"] == null ? 0 : DataIntegrity.ConvertToInt(newValues["PermissionLevelValue"].ToString());
            bool hasPermission = newValues["Member"] != null && DataIntegrity.ConvertToBool(newValues["Member"]);

            ThinkgatePermission permission = new ThinkgatePermission(permissionID);
            SessionObject sessionObject = (SessionObject)Session["SessionObject"];
            ThinkgateUser user = sessionObject.LoggedInUser;
                
            if (hasPermission)
            {
                permission.PermissionLevel = (ThinkgatePermission.PermissionLevelValues)Enum.ToObject(typeof(ThinkgatePermission.PermissionLevelValues), permissionLevel);
                
                //pricingModule.Permissions.Add(permission);        // Not sure what point of this was
                role.UpdateRolePermissions(permissionID, permissionLevel, user.UserName);
                lblResultMessage.Text = string.Format("Permission {0} was added to Role {1}.", permissionName, role.RoleName);

                // WSH: Going to loop through the children and deal with them. In theory, you could devise a strategy that involved fewer calls to the DB and did a mass update. However you have to do a uniqueness check on each child insert or do a complete delete of all children recs to then do a mass insert. Since this will be a vary rare update, I decided performance was not important.
                foreach (TreeListDataItem item in editedItem.ChildItems)
                {
                    permissionID = new Guid();
                    Guid.TryParse(item.GetDataKeyValue("ID").ToString(), out permissionID);
                    if (permissionID == default(Guid)) return;

                    role.UpdateRolePermissions(permissionID, permissionLevel, user.UserName);
                }
            }
            else
            {
                //pricingModule.Permissions.Remove(permission);   // WSH : Not sure what point of this was
                role.DeleteRolePermissions(permissionID);
                lblResultMessage.Text = string.Format("Permission {0} was revoked from Role {1}.", permissionName, role.RoleName);

                // WSH: Going to loop through the children and deal with them. In theory, you could devise a strategy that involved fewer calls to the DB and did a mass update. However you have to do a uniqueness check on each child insert or do a complete delete of all children recs to then do a mass insert. Since this will be a vary rare update, I decided performance was not important.
                foreach (TreeListDataItem item in editedItem.ChildItems)
                {
                    permissionID = new Guid();
                    Guid.TryParse(item.GetDataKeyValue("ID").ToString(), out permissionID);
                    if (permissionID == default(Guid)) return;

                    role.DeleteRolePermissions(permissionID);
                }
            }

        }