/// <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> Constructor for a new instance of the User_Group_AdminViewer class </summary>
        /// <param name="User"> Authenticated user information </param>
        /// <param name="currentMode"> Mode / navigation information for the current request</param>
        /// <param name="Code_Manager"> List of valid collection codes, including mapping from the Sobek collections to Greenstone collections</param>
        /// <param name="Tracer">Trace object keeps a list of each method executed and important milestones in rendering</param>
        /// <remarks> Postback from a user group edit is handled here in the constructor </remarks>
        public User_Group_AdminViewer(User_Object User, SobekCM_Navigation_Object currentMode, Aggregation_Code_Manager Code_Manager, Custom_Tracer Tracer)
            : base(User)
        {
            Tracer.Add_Trace("User_Group_AdminViewer.Constructor", String.Empty);

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

            // Get the user to edit, if there was a user id in the submode
            int edit_usergroupid = -100;

            editGroup = null;
            if (currentMode.My_Sobek_SubMode.Length > 0)
            {
                if (currentMode.My_Sobek_SubMode == "new")
                {
                    edit_usergroupid = -1;
                }
                else
                {
                    if (Int32.TryParse(currentMode.My_Sobek_SubMode.Replace("a", "").Replace("b", "").Replace("c", "").Replace("v", ""), out edit_usergroupid))
                    {
                        editGroup = SobekCM_Database.Get_User_Group(edit_usergroupid, Tracer);
                    }
                }
            }

            // Determine the mode
            mode = Users_Group_Admin_Mode_Enum.Error;
            if ((editGroup != null) || (edit_usergroupid == -1))
            {
                if ((currentMode.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
            {
                currentMode.My_Sobek_SubMode = String.Empty;
                currentMode.Admin_Type       = Admin_Type_Enum.Users;
                HttpContext.Current.Response.Redirect(currentMode.Redirect_URL());
            }

            // Set an empty user group object for a new item
            if (edit_usergroupid < 0)
            {
                editGroup = new User_Group(String.Empty, String.Empty, -1);
            }

            // Perform post back work
            if (currentMode.isPostBack)
            {
                if ((mode == Users_Group_Admin_Mode_Enum.Edit_User_Group) && (editGroup != null))
                {
                    // Get a reference to this form
                    NameValueCollection form    = HttpContext.Current.Request.Form;
                    string[]            getKeys = form.AllKeys;

                    bool successful_save = true;
                    bool can_editall     = editGroup.Editable_Regular_Expressions.Any(thisRegularExpression => thisRegularExpression == "[A-Z]{2}[A-Z|0-9]{4}[0-9]{4}");

                    bool   can_submit  = false;
                    bool   is_internal = false;
                    bool   is_admin    = false;
                    bool   is_portal   = false;
                    string name        = editGroup.Name;
                    string description = editGroup.Description;

                    List <string> projects  = new List <string>();
                    List <string> templates = new List <string>();

                    Dictionary <string, User_Editable_Aggregation> aggregations = new Dictionary <string, User_Editable_Aggregation>();

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

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

                        case "admin_user_submit":
                            can_submit = true;
                            break;

                        case "admin_user_internal":
                            is_internal = true;
                            break;

                        case "admin_user_editall":
                            can_editall = true;
                            break;

                        case "admin_user_admin":
                            is_admin = true;
                            break;

                        case "admin_user_portaladmin":
                            is_portal = true;
                            break;

                        default:
                            if (thisKey.IndexOf("admin_user_template_") == 0)
                            {
                                templates.Add(thisKey.Replace("admin_user_template_", ""));
                            }
                            if (thisKey.IndexOf("admin_user_project_") == 0)
                            {
                                projects.Add(thisKey.Replace("admin_user_project_", ""));
                            }
                            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_Editable_Aggregation(select_project, String.Empty, false, false, false, true));
                                }
                            }
                            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_Editable_Aggregation(select_project, String.Empty, true, false, false, false));
                                }
                            }
                            if (thisKey.IndexOf("admin_project_edit_") == 0)
                            {
                                string edit_project = thisKey.Replace("admin_project_edit_", "");
                                if (aggregations.ContainsKey(edit_project))
                                {
                                    aggregations[edit_project].CanEditItems = true;
                                }
                                else
                                {
                                    aggregations.Add(edit_project, new User_Editable_Aggregation(edit_project, String.Empty, 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].IsCurator = true;
                                }
                                else
                                {
                                    aggregations.Add(admin_project, new User_Editable_Aggregation(admin_project, String.Empty, false, false, true, false));
                                }
                            }
                            break;
                        }
                    }

                    // Determine if the projects and templates need to be updated
                    bool update_templates_projects = false;
                    if ((templates.Count != editGroup.Templates.Count) || (projects.Count != editGroup.Projects.Count))
                    {
                        update_templates_projects = true;
                    }
                    else
                    {
                        // Check all of the templates
                        if (templates.Any(template => !editGroup.Templates.Contains(template)))
                        {
                            update_templates_projects = true;
                        }

                        // Check all the projects
                        if (!update_templates_projects)
                        {
                            if (projects.Any(project => !editGroup.Projects.Contains(project)))
                            {
                                update_templates_projects = true;
                            }
                        }
                    }

                    // Determine if the aggregations need to be edited
                    bool update_aggregations = false;
                    if (aggregations.Count != editGroup.Aggregations.Count)
                    {
                        update_aggregations = true;
                    }
                    else
                    {
                        // Build a dictionary of the user aggregations as well
                        Dictionary <string, User_Editable_Aggregation> existingAggr = editGroup.Aggregations.ToDictionary(thisAggr => thisAggr.Code);

                        // Check all the aggregations
                        foreach (User_Editable_Aggregation adminAggr in aggregations.Values)
                        {
                            if (existingAggr.ContainsKey(adminAggr.Code))
                            {
                                if ((adminAggr.CanSelect != existingAggr[adminAggr.Code].CanSelect) || (adminAggr.CanEditItems != existingAggr[adminAggr.Code].CanEditItems) || (adminAggr.IsCurator != existingAggr[adminAggr.Code].IsCurator))
                                {
                                    update_aggregations = true;
                                    break;
                                }
                            }
                            else
                            {
                                update_aggregations = true;
                                break;
                            }
                        }
                    }

                    // Must have a name to continue
                    if (name.Length > 0)
                    {
                        // Update the basic user information
                        int newid = SobekCM_Database.Save_User_Group(editGroup.UserGroupID, name, description, can_submit, is_internal, can_editall, is_admin, is_portal, false, update_templates_projects, update_aggregations, false, Tracer);
                        if (editGroup.UserGroupID < 0)
                        {
                            editGroup.UserGroupID = newid;
                        }

                        if (editGroup.UserGroupID > 0)
                        {
                            // Update the templates and projects, if requested
                            if (update_templates_projects)
                            {
                                // Update projects, if necessary
                                if (projects.Count > 0)
                                {
                                    if (!SobekCM_Database.Update_SobekCM_User_Group_Projects(editGroup.UserGroupID, projects, Tracer))
                                    {
                                        successful_save = false;
                                    }
                                }

                                // Update templates, if necessary
                                if (templates.Count > 0)
                                {
                                    if (!SobekCM_Database.Update_SobekCM_User_Group_Templates(editGroup.UserGroupID, templates, Tracer))
                                    {
                                        successful_save = false;
                                    }
                                }
                            }

                            // Update the aggregations, if requested
                            if (update_aggregations)
                            {
                                if (aggregations.Count > 0)
                                {
                                    List <User_Editable_Aggregation> aggregationList = aggregations.Values.ToList();
                                    if (!SobekCM_Database.Update_SobekCM_User_Group_Aggregations(editGroup.UserGroupID, aggregationList, Tracer))
                                    {
                                        successful_save = false;
                                    }
                                }
                            }
                        }
                        else
                        {
                            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)
                    {
                        currentMode.My_Sobek_SubMode = String.Empty;
                        HttpContext.Current.Response.Redirect(currentMode.Redirect_URL());
                    }
                }
            }
        }
        /// <summary> Constructor for a new instance of the User_Group_AdminViewer class </summary>
        /// <param name="User"> Authenticated user information </param>
        /// <param name="currentMode"> Mode / navigation information for the current request</param>
        /// <param name="Code_Manager"> List of valid collection codes, including mapping from the Sobek collections to Greenstone collections</param>
        /// <param name="Tracer">Trace object keeps a list of each method executed and important milestones in rendering</param>
        /// <remarks> Postback from a user group edit is handled here in the constructor </remarks>
        public User_Group_AdminViewer(User_Object User, SobekCM_Navigation_Object currentMode, Aggregation_Code_Manager Code_Manager, Custom_Tracer Tracer)
            : base(User)
        {
            Tracer.Add_Trace("User_Group_AdminViewer.Constructor", String.Empty);

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

            // Get the user to edit, if there was a user id in the submode
            int edit_usergroupid = -100;
            editGroup = null;
            if (currentMode.My_Sobek_SubMode.Length > 0)
            {
                if (currentMode.My_Sobek_SubMode == "new")
                {
                    edit_usergroupid = -1;
                }
                else
                {
                    if (Int32.TryParse(currentMode.My_Sobek_SubMode.Replace("a", "").Replace("b", "").Replace("c", "").Replace("v", ""), out edit_usergroupid))
                        editGroup = SobekCM_Database.Get_User_Group(edit_usergroupid, Tracer);
                }
            }

            // Determine the mode
            mode = Users_Group_Admin_Mode_Enum.Error;
            if ((editGroup != null) || (edit_usergroupid == -1))
            {
                if ((currentMode.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
            {
                currentMode.My_Sobek_SubMode = String.Empty;
                currentMode.Admin_Type = Admin_Type_Enum.Users;
                currentMode.Redirect();
                return;
            }

            // Set an empty user group object for a new item
            if (edit_usergroupid < 0)
            {
                editGroup = new User_Group(String.Empty, String.Empty, -1);
            }

            // Perform post back work
            if (currentMode.isPostBack)
            {
                if ((mode == Users_Group_Admin_Mode_Enum.Edit_User_Group) && (editGroup != null))
                {
                    // Get a reference to this form
                    NameValueCollection form = HttpContext.Current.Request.Form;
                    string[] getKeys = form.AllKeys;

                    bool successful_save = true;
                    bool can_editall = editGroup.Editable_Regular_Expressions.Any(thisRegularExpression => thisRegularExpression == "[A-Z]{2}[A-Z|0-9]{4}[0-9]{4}");

                    bool can_submit = false;
                    bool is_internal = false;
                    bool is_admin = false;
                    bool is_portal = false;
                    string name = editGroup.Name;
                    string description = editGroup.Description;
                    bool is_sobek_default = false;
                    bool is_shibboleth_default = false;
                    bool is_ldap_default = false;

                    List<string> projects = new List<string>();
                    List<string> templates = new List<string>();

                    Dictionary<string, User_Editable_Aggregation> aggregations = new Dictionary<string, User_Editable_Aggregation>();

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

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

                            case "admin_user_submit":
                                can_submit = true;
                                break;

                            case "admin_user_internal":
                                is_internal = true;
                                break;

                            case "admin_user_editall":
                                can_editall = true;
                                break;

                            case "admin_user_admin":
                                is_admin = true;
                                break;

                            case "admin_user_portaladmin":
                                is_portal = true;
                                break;

                            default:
                                if (thisKey.IndexOf("admin_user_template_") == 0)
                                {
                                    templates.Add(thisKey.Replace("admin_user_template_", ""));
                                }
                                if (thisKey.IndexOf("admin_user_project_") == 0)
                                {
                                    projects.Add(thisKey.Replace("admin_user_project_", ""));
                                }
                                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_Editable_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_Editable_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_Editable_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_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.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_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.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_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.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_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.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_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.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_Editable_Aggregation thisAggrLink = new User_Editable_Aggregation(edit_project, String.Empty, false, false, false, false, false);
                                        thisAggrLink.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_Editable_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_Editable_Aggregation(admin_project, String.Empty, false, false, false, false, true));
                                    }
                                }
                                break;
                        }
                    }

                    // Determine if the projects and templates need to be updated
                    bool update_templates_projects = false;
                    if ((templates.Count != editGroup.Templates.Count) || (projects.Count != editGroup.Default_Metadata_Sets.Count))
                    {
                        update_templates_projects = true;
                    }
                    else
                    {
                        // Check all of the templates
                        if (templates.Any(template => !editGroup.Templates.Contains(template)))
                        {
                            update_templates_projects = true;
                        }

                        // Check all the projects
                        if (!update_templates_projects)
                        {
                            if (projects.Any(project => !editGroup.Default_Metadata_Sets.Contains(project)))
                            {
                                update_templates_projects = true;
                            }
                        }
                    }

                    // Determine if the aggregations need to be edited
                    bool update_aggregations = false;
                    if (aggregations.Count != editGroup.Aggregations.Count)
                    {
                        update_aggregations = true;
                    }
                    else
                    {
                        // Build a dictionary of the user aggregations as well
                        Dictionary<string, User_Editable_Aggregation> existingAggr = editGroup.Aggregations.ToDictionary(thisAggr => thisAggr.Code);

                        // Check all the aggregations
                        foreach (User_Editable_Aggregation adminAggr in aggregations.Values)
                        {
                            if (existingAggr.ContainsKey(adminAggr.Code))
                            {
                                if ((adminAggr.CanSelect != existingAggr[adminAggr.Code].CanSelect) || (adminAggr.CanEditMetadata != existingAggr[adminAggr.Code].CanEditMetadata) ||
                                                (adminAggr.CanEditBehaviors != existingAggr[adminAggr.Code].CanEditBehaviors) || (adminAggr.CanPerformQc != existingAggr[adminAggr.Code].CanPerformQc) ||
                                                (adminAggr.CanUploadFiles != existingAggr[adminAggr.Code].CanUploadFiles) || (adminAggr.CanChangeVisibility != existingAggr[adminAggr.Code].CanChangeVisibility) ||
                                                (adminAggr.CanDelete != existingAggr[adminAggr.Code].CanDelete) || (adminAggr.IsCurator != existingAggr[adminAggr.Code].IsCurator) || (adminAggr.OnHomePage != existingAggr[adminAggr.Code].OnHomePage))
                                {
                                    update_aggregations = true;
                                    break;
                                }
                            }
                            else
                            {
                                update_aggregations = true;
                                break;
                            }
                        }
                    }

                    // Must have a name to continue
                    if (name.Length > 0)
                    {
                        // Update the basic user information
                        int newid = SobekCM_Database.Save_User_Group(editGroup.UserGroupID, name, description, can_submit, is_internal, can_editall, is_admin, is_portal, false, update_templates_projects, update_aggregations, false, is_sobek_default, is_shibboleth_default, is_ldap_default, Tracer);
                        if (editGroup.UserGroupID < 0)
                        {
                            editGroup.UserGroupID = newid;
                        }

                        if (editGroup.UserGroupID > 0)
                        {
                            // Update the templates and projects, if requested
                            if (update_templates_projects)
                            {
                                // Update projects, if necessary
                                if (projects.Count > 0)
                                {
                                    if (!SobekCM_Database.Update_SobekCM_User_Group_DefaultMetadata(editGroup.UserGroupID, projects, Tracer))
                                    {
                                        successful_save = false;
                                    }
                                }

                                // Update templates, if necessary
                                if (templates.Count > 0)
                                {
                                    if (!SobekCM_Database.Update_SobekCM_User_Group_Templates(editGroup.UserGroupID, templates, Tracer))
                                    {
                                        successful_save = false;
                                    }
                                }
                            }

                            // Update the aggregations, if requested
                            if (update_aggregations)
                            {
                                if (aggregations.Count > 0)
                                {
                                    List<User_Editable_Aggregation> aggregationList = aggregations.Values.ToList();
                                    if (!SobekCM_Database.Update_SobekCM_User_Group_Aggregations(editGroup.UserGroupID, aggregationList, Tracer))
                                    {
                                        successful_save = false;
                                    }
                                }
                            }
                        }
                        else
                        {
                            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)
                    {
                        currentMode.My_Sobek_SubMode = String.Empty;
                        currentMode.Redirect();
                    }
                }
            }
        }