/// <summary> Gets the list of all user groups </summary>
        /// <param name="Tracer"> Trace object keeps a list of each method executed and important milestones in rendering</param>
        /// <returns> List of partly built <see cref="SobekCM.Core.Users.User_Group"/> object </returns>
        /// <remarks> This calls the 'mySobek_Get_All_User_Groups' stored procedure </remarks> 
        public static List<User_Group> Get_All_User_Groups(Custom_Tracer Tracer)
        {
            if (Tracer != null)
            {
                Tracer.Add_Trace("Engine_Database.Get_All_User_Groups", String.Empty);
            }

            try
            {
                DataSet resultSet = EalDbAccess.ExecuteDataset( DatabaseType, Connection_String, CommandType.StoredProcedure, "mySobek_Get_All_User_Groups");

                List<User_Group> returnValue = new List<User_Group>();

                foreach (DataRow thisRow in resultSet.Tables[0].Rows)
                {
                    string name = thisRow["GroupName"].ToString();
                    string description = thisRow["GroupDescription"].ToString();
                    int usergroupid = Convert.ToInt32(thisRow["UserGroupID"]);
                    bool specialGroup = Convert.ToBoolean(thisRow["IsSpecialGroup"]);

                    User_Group userGroup = new User_Group(name, description, usergroupid) {IsSpecialGroup = specialGroup};

                    returnValue.Add(userGroup);

                }

                return returnValue;

            }
            catch (Exception ee)
            {
                Last_Exception = ee;
                if (Tracer != null)
                {
                    Tracer.Add_Trace("Engine_Database.Get_All_User_Groups", "Exception caught during database work", Custom_Trace_Type_Enum.Error);
                    Tracer.Add_Trace("Engine_Database.Get_All_User_Groups", ee.Message, Custom_Trace_Type_Enum.Error);
                    Tracer.Add_Trace("Engine_Database.Get_All_User_Groups", ee.StackTrace, Custom_Trace_Type_Enum.Error);
                }
                return null;
            }
        }
        /// <summary> Constructor for a new instance of the User_Group_AdminViewer class </summary>
        /// <param name="RequestSpecificValues"> Mode / navigation information for the current request</param>
        /// <remarks> Postback from a user group edit is handled here in the constructor </remarks>
        public User_Group_AdminViewer(RequestCache RequestSpecificValues)
            : base(RequestSpecificValues)
        {
            RequestSpecificValues.Tracer.Add_Trace("User_Group_AdminViewer.Constructor", String.Empty);

            // Set the action message to clear initially
            actionMessage = String.Empty;

            // Get the user to edit, if there was a user id in the submode
            int edit_usergroupid = -100;
            editGroup = null;
            if ( !String.IsNullOrEmpty(RequestSpecificValues.Current_Mode.My_Sobek_SubMode))
            {
                if (RequestSpecificValues.Current_Mode.My_Sobek_SubMode == "new")
                {
                    edit_usergroupid = -1;

                    // Check this admin's session for this RequestSpecificValues.Current_User object
                    Object sessionEditUser = HttpContext.Current.Session["Edit_UserGroup_" + edit_usergroupid];
                    if (sessionEditUser != null)
                        editGroup = (User_Group)sessionEditUser;
                    else
                        editGroup = new User_Group(String.Empty, String.Empty, -1);
                }
                else
                {

                    if (Int32.TryParse(RequestSpecificValues.Current_Mode.My_Sobek_SubMode.Replace("a", "").Replace("b", "").Replace("c", "").Replace("v", ""), out edit_usergroupid))
                    {
                        Object sessionEditUser = HttpContext.Current.Session["Edit_UserGroup_" + edit_usergroupid];
                        if (sessionEditUser != null)
                            editGroup = (User_Group) sessionEditUser;
                        else
                        {
                            editGroup = SobekCM_Database.Get_User_Group(edit_usergroupid, RequestSpecificValues.Tracer);
                            editGroup.Should_Be_Able_To_Edit_All_Items = false;
                            bool canEditAll = (editGroup.Editable_Regular_Expressions != null) && (editGroup.Editable_Regular_Expressions.Any(ThisRegularExpression => ThisRegularExpression == "[A-Z]{2}[A-Z|0-9]{4}[0-9]{4}"));
                            if (editGroup.Editable_Regular_Expressions != null)
                                canEditAll = editGroup.Editable_Regular_Expressions.Any(ThisRegularExpression => ThisRegularExpression == "[A-Z]{2}[A-Z|0-9]{4}[0-9]{4}");
                            if (canEditAll)
                                editGroup.Should_Be_Able_To_Edit_All_Items = true;
                        }
                    }
                }
            }

            // Determine the mode
            mode = Users_Group_Admin_Mode_Enum.Error;
            if ((editGroup != null) || (edit_usergroupid == -1))
            {
                if ((RequestSpecificValues.Current_Mode.My_Sobek_SubMode.IndexOf("v") > 0) && (edit_usergroupid > 0))
                    mode = Users_Group_Admin_Mode_Enum.View_User_Group;
                else
                    mode = Users_Group_Admin_Mode_Enum.Edit_User_Group;
            }
            else
            {
                RequestSpecificValues.Current_Mode.My_Sobek_SubMode = String.Empty;
                RequestSpecificValues.Current_Mode.Admin_Type = Admin_Type_Enum.Users;
                UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                return;
            }

            // Perform post back work
            if (RequestSpecificValues.Current_Mode.isPostBack)
            {
                if ((mode == Users_Group_Admin_Mode_Enum.Edit_User_Group) && (editGroup != null))
                {
                    // Determine which page you are on
                    int page = 1;
                    if (RequestSpecificValues.Current_Mode.My_Sobek_SubMode.IndexOf("b") > 0)
                        page = 2;
                    else if (RequestSpecificValues.Current_Mode.My_Sobek_SubMode.IndexOf("c") > 0)
                        page = 3;

                    // Get a reference to this form
                    NameValueCollection form = HttpContext.Current.Request.Form;
                    string[] getKeys = form.AllKeys;

                    // Get the curret action
                    string action = form["admin_user_group_save"];

                    // If this is CANCEL, get rid of the currrent edit object in the session
                    if (action == "cancel")
                    {
                        // Clear the RequestSpecificValues.Current_User from the sessions
                        HttpContext.Current.Session["Edit_UserGroup_" + editGroup.UserGroupID] = null;

                        // Redirect the RequestSpecificValues.Current_User
                        RequestSpecificValues.Current_Mode.My_Sobek_SubMode = String.Empty;
                        UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                        return;
                    }

                    bool successful_save = true;
                    switch (page)
                    {
                        case 1:
                            if ( editGroup.Templates_Count > 0 ) editGroup.Templates.Clear();
                            if ( editGroup.Default_Metadata_Sets_Count > 0 ) editGroup.Default_Metadata_Sets.Clear();

                            // First, set some flags to FALSE
                            editGroup.CanSubmit = false;
                            editGroup.IsInternalUser = false;
                            editGroup.Should_Be_Able_To_Edit_All_Items = false;
                            editGroup.IsPortalAdmin = false;
                            editGroup.IsSystemAdmin = false;

                            // Step through each key
                            foreach (string thisKey in getKeys)
                            {
                                switch (thisKey)
                                {
                                    case "groupName":
                                        editGroup.Name = form[thisKey].Trim();
                                        break;

                                    case "groupDescription":
                                        editGroup.Description = form[thisKey].Trim();
                                        break;

                                    case "admin_user_submit":
                                        editGroup.CanSubmit = true;
                                        break;

                                    case "admin_user_internal":
                                        editGroup.IsInternalUser = true;
                                        break;

                                    case "admin_user_editall":
                                        editGroup.Should_Be_Able_To_Edit_All_Items = true;
                                        break;

                                    case "admin_user_admin":
                                        editGroup.IsSystemAdmin = true;
                                        break;

                                    case "admin_user_portaladmin":
                                        editGroup.IsPortalAdmin = true;
                                        break;

                                    default:
                                        if (thisKey.IndexOf("admin_user_template_") == 0)
                                        {
                                            editGroup.Add_Template(thisKey.Replace("admin_user_template_", ""));
                                        }
                                        if (thisKey.IndexOf("admin_user_project_") == 0)
                                        {
                                            editGroup.Add_Default_Metadata_Set(thisKey.Replace("admin_user_project_", ""));
                                        }
                                        break;
                                }
                            }
                            break;

                        case 2:
                            Dictionary<string, User_Permissioned_Aggregation> aggregations = new Dictionary<string, User_Permissioned_Aggregation>();

                            // Step through each key
                            foreach (string thisKey in getKeys)
                            {

                                if (thisKey.IndexOf("admin_project_onhome_") == 0)
                                {
                                    string select_project = thisKey.Replace("admin_project_onhome_", "");
                                    if (aggregations.ContainsKey(select_project))
                                    {
                                        aggregations[select_project].OnHomePage = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(select_project, new User_Permissioned_Aggregation(select_project, String.Empty, false, false, false, true, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_select_") == 0)
                                {
                                    string select_project = thisKey.Replace("admin_project_select_", "");
                                    if (aggregations.ContainsKey(select_project))
                                    {
                                        aggregations[select_project].CanSelect = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(select_project, new User_Permissioned_Aggregation(select_project, String.Empty, true, false, false, false, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_editall_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_editall_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanEditItems = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(edit_project, new User_Permissioned_Aggregation(edit_project, String.Empty, false, true, false, false, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_edit_metadata_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_edit_metadata_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanEditMetadata = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false)
                                        {
                                            CanEditMetadata = true
                                        };
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_edit_behavior_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_edit_behavior_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanEditBehaviors = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false)
                                        {
                                            CanEditBehaviors = true
                                        };
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_perform_qc_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_perform_qc_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanPerformQc = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false)
                                        {
                                            CanPerformQc = true
                                        };
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_upload_files_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_upload_files_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanUploadFiles = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false)
                                        {
                                            CanUploadFiles = true
                                        };
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_change_visibility_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_change_visibility_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanChangeVisibility = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false)
                                        {
                                            CanChangeVisibility = true
                                        };
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_can_delete_") == 0)
                                {
                                    string edit_project = thisKey.Replace("admin_project_can_delete_", "");
                                    if (aggregations.ContainsKey(edit_project))
                                    {
                                        aggregations[edit_project].CanDelete = true;
                                    }
                                    else
                                    {
                                        User_Permissioned_Aggregation thisAggrLink = new User_Permissioned_Aggregation(edit_project, String.Empty, false, false, false, false, false)
                                        {
                                            CanDelete = true
                                        };
                                        aggregations.Add(edit_project, thisAggrLink);
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_curator_") == 0)
                                {
                                    string admin_project = thisKey.Replace("admin_project_curator_", "");
                                    if (aggregations.ContainsKey(admin_project))
                                    {
                                        aggregations[admin_project].IsCurator = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(admin_project, new User_Permissioned_Aggregation(admin_project, String.Empty, false, false, true, false, false));
                                    }
                                }
                                if (thisKey.IndexOf("admin_project_admin_") == 0)
                                {
                                    string admin_project = thisKey.Replace("admin_project_admin_", "");
                                    if (aggregations.ContainsKey(admin_project))
                                    {
                                        aggregations[admin_project].IsAdmin = true;
                                    }
                                    else
                                    {
                                        aggregations.Add(admin_project, new User_Permissioned_Aggregation(admin_project, String.Empty, false, false, false, false, true));
                                    }
                                }
                            }

                            // Copy to the object now
                            if ( editGroup.Aggregations != null ) editGroup.Aggregations.Clear();
                            foreach (User_Permissioned_Aggregation thisPermissionsAggregation in aggregations.Values)
                                editGroup.Add_Aggregation(thisPermissionsAggregation);
                            break;
                    }

                    // Should this be saved to the database?
                    if (action == "save")
                    {
                        // Must have a name to continue
                        if (editGroup.Name.Length > 0)
                        {
                            // Update the basic user information
                            int newid = SobekCM_Database.Save_User_Group(editGroup.UserGroupID, editGroup.Name, editGroup.Description, editGroup.CanSubmit, editGroup.IsInternalUser, editGroup.Should_Be_Able_To_Edit_All_Items, editGroup.IsSystemAdmin, editGroup.IsPortalAdmin, false, true, true, false, editGroup.IsSobekDefault, editGroup.IsShibbolethDefault, editGroup.IsLdapDefault, RequestSpecificValues.Tracer);
                            if (editGroup.UserGroupID < 0)
                            {
                                editGroup.UserGroupID = newid;
                            }

                            if (editGroup.UserGroupID > 0)
                            {
                                // Update projects, if necessary
                                if (editGroup.Default_Metadata_Sets_Count > 0)
                                {
                                    if (!SobekCM_Database.Update_SobekCM_User_Group_DefaultMetadata(editGroup.UserGroupID, editGroup.Default_Metadata_Sets, RequestSpecificValues.Tracer))
                                    {
                                        successful_save = false;
                                    }
                                }

                                // Update templates, if necessary
                                if (editGroup.Templates_Count > 0)
                                {
                                    if (!SobekCM_Database.Update_SobekCM_User_Group_Templates(editGroup.UserGroupID, editGroup.Templates, RequestSpecificValues.Tracer))
                                    {
                                        successful_save = false;
                                    }
                                }
                            }
                            // Update the aggregationPermissions, if requested
                            if (editGroup.Aggregations_Count > 0)
                            {
                                List<User_Permissioned_Aggregation> aggregationList = editGroup.Aggregations;
                                if (!SobekCM_Database.Update_SobekCM_User_Group_Aggregations(editGroup.UserGroupID, aggregationList, RequestSpecificValues.Tracer))
                                {
                                    successful_save = false;
                                }
                            }
                        }
                        else
                        {
                            actionMessage = "User group's name must have a length greater than zero";
                            successful_save = false;
                        }

                        // Forward back to the list of users, if this was successful
                        if (successful_save)
                        {
                            // Clear the RequestSpecificValues.Current_User from the sessions
                            HttpContext.Current.Session["Edit_UserGroup_" + editGroup.UserGroupID] = null;

                            RequestSpecificValues.Current_Mode.My_Sobek_SubMode = String.Empty;
                            UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                        }
                    }
                    else
                    {
                        // Save to the admins session
                        HttpContext.Current.Session["Edit_UserGroup_" + editGroup.UserGroupID] = editGroup;
                        RequestSpecificValues.Current_Mode.My_Sobek_SubMode = action;
                        UrlWriterHelper.Redirect(RequestSpecificValues.Current_Mode);
                    }
                }
            }
        }
        /// <summary> Gets the information about a single user group </summary>
        /// <param name="UserGroupID"> Primary key for this user group from the database </param>
        /// <param name="Tracer"> Trace object keeps a list of each method executed and important milestones in rendering</param>
        /// <returns> Fully built <see cref="SobekCM.Core.Users.User_Group"/> object </returns>
        /// <remarks> This calls the 'mySobek_Get_User_Group' stored procedure </remarks> 
        public static User_Group Get_User_Group(int UserGroupID, Custom_Tracer Tracer)
        {
            if (Tracer != null)
            {
                Tracer.Add_Trace("SobekCM_Database.Get_User_Group", String.Empty);
            }

            try
            {
                // Execute this non-query stored procedure
                EalDbParameter[] paramList = new EalDbParameter[1];
                paramList[0] = new EalDbParameter("@usergroupid", UserGroupID);

                DataSet resultSet = EalDbAccess.ExecuteDataset(DatabaseType, connectionString, CommandType.StoredProcedure, "mySobek_Get_User_Group", paramList);

                if ((resultSet.Tables.Count > 0) && (resultSet.Tables[0].Rows.Count > 0))
                {

                    DataRow userRow = resultSet.Tables[0].Rows[0];
                    string name = userRow["GroupName"].ToString();
                    string description = userRow["GroupDescription"].ToString();
                    int usergroupid = Convert.ToInt32(userRow["UserGroupID"]);
                    User_Group group = new User_Group(name, description, usergroupid);
                    group.CanSubmit = Convert.ToBoolean(userRow["Can_Submit_Items"]);
                    group.IsInternalUser = Convert.ToBoolean(userRow["Internal_User"]);
                    group.IsSystemAdmin = Convert.ToBoolean(userRow["IsSystemAdmin"]);

                    foreach (DataRow thisRow in resultSet.Tables[1].Rows)
                    {
                        group.Add_Template(thisRow["TemplateCode"].ToString());
                    }

                    foreach (DataRow thisRow in resultSet.Tables[2].Rows)
                    {
                        group.Add_Default_Metadata_Set(thisRow["MetadataCode"].ToString());
                    }

                    // Add links to regular expressions
                    foreach (DataRow thisRow in resultSet.Tables[3].Rows)
                    {
                        group.Add_Editable_Regular_Expression(thisRow["EditableRegex"].ToString());
                    }

                    // Add links to aggregationPermissions
                    foreach (DataRow thisRow in resultSet.Tables[4].Rows)
                    {
                        group.Add_Aggregation(thisRow["Code"].ToString(), thisRow["Name"].ToString(), Convert.ToBoolean(thisRow["CanSelect"]), Convert.ToBoolean(thisRow["CanEditMetadata"]), Convert.ToBoolean(thisRow["CanEditBehaviors"]), Convert.ToBoolean(thisRow["CanPerformQc"]), Convert.ToBoolean(thisRow["CanUploadFiles"]), Convert.ToBoolean(thisRow["CanChangeVisibility"]), Convert.ToBoolean(thisRow["CanDelete"]), Convert.ToBoolean(thisRow["IsCurator"]), Convert.ToBoolean(thisRow["IsAdmin"]));
                    }

                    // Add the basic information about users in this user group
                    foreach (DataRow thisRow in resultSet.Tables[5].Rows)
                    {
                        int userid = Convert.ToInt32(thisRow["UserID"]);
                        string username = thisRow["UserName"].ToString();
                        string email = thisRow["EmailAddress"].ToString();
                        string firstname = thisRow["FirstName"].ToString();
                        string nickname = thisRow["NickName"].ToString();
                        string lastname = thisRow["LastName"].ToString();
                        string fullname = firstname + " " + lastname;
                        if (nickname.Length > 0)
                        {
                            fullname = nickname + " " + lastname;
                        }

                        group.Add_User(username, fullname, email, userid);
                    }

                    return group;
                }

                // Return NULL if there was an error
                return null;

            }
            catch (Exception ee)
            {
                lastException = ee;
                if (Tracer != null)
                {
                    Tracer.Add_Trace("SobekCM_Database.Get_User_Group", "Exception caught during database work", Custom_Trace_Type_Enum.Error);
                    Tracer.Add_Trace("SobekCM_Database.Get_User_Group", ee.Message, Custom_Trace_Type_Enum.Error);
                    Tracer.Add_Trace("SobekCM_Database.Get_User_Group", ee.StackTrace, Custom_Trace_Type_Enum.Error);
                }
                return null;
            }
        }