public ActionResult ValidateSystemUser(SystemUserWizardVM systemUserWizardViewModel)
        {
            SystemUser systemUser = new SystemUser();

            systemUser = systemUserRepository.GetUserBySystemUserGuid(systemUserWizardViewModel.SystemUser.SystemUserGuid);
            systemUserWizardViewModel.SystemUser = systemUser;

            if (systemUserWizardViewModel.SystemUserLocation == null)
            {
                ModelState.Clear();
                ModelState.AddModelError("SystemUser.LocationName", "Location Required");

                //Validation Error
                string msg = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        msg += error.ErrorMessage;
                    }
                }

                return(Json(new WizardJSONResponse
                {
                    html = ControllerExtension.RenderPartialViewToString(this, "SystemUserDetailsScreen", systemUserWizardViewModel),
                    message = "ValidationError: " + msg,
                    success = false
                }));
            }

            //Validate SystemUser data against Table
            if (!ModelState.IsValid)
            {
                //not needed
            }

            //SystemUser + List of SystemUser's Teams
            SystemUserTeamsVM systemUserTeamsViewModel = new SystemUserTeamsVM();

            //Add Systemuser
            systemUserTeamsViewModel.SystemUser = systemUser;

            //Add SystemUser's Teams
            SystemUserTeamRepository systemUserTeamRepository             = new SystemUserTeamRepository();
            List <spDDAWizard_SelectSystemUserTeams_v1Result> systemUsers = new List <spDDAWizard_SelectSystemUserTeams_v1Result>();

            systemUserTeamsViewModel.Teams = systemUserWizardRepository.GetSystemUserTeams(systemUser.SystemUserGuid);

            //Return Page with list of SystemUser's Teams
            return(Json(new WizardJSONResponse
            {
                html = ControllerExtension.RenderPartialViewToString(this, "SystemUserTeamsScreen", systemUserTeamsViewModel),
                message = "Success",
                success = true
            }));
        }
        public ActionResult ConfirmChangesScreen(SystemUserWizardVM updatedSystemUser)
        {
            //Messages that will be displayed to User
            WizardMessages wizardMessages = new WizardMessages();

            //Model to Store Original Item for Comparison
            SystemUserWizardVM originalSystemUserWizardViewModel = new SystemUserWizardVM();

            //Location
            SystemUserLocationRepository systemUserLocationRepository = new SystemUserLocationRepository();
            SystemUserLocation           originalSystemUserLocation   = new SystemUserLocation();

            originalSystemUserLocation = systemUserLocationRepository.GetSystemUserLocation(updatedSystemUser.SystemUser.SystemUserGuid);
            if (originalSystemUserLocation != null)
            {
                originalSystemUserWizardViewModel.SystemUserLocation = originalSystemUserLocation;
            }

            //System User
            SystemUser originalSystemUser = new SystemUser();

            originalSystemUser = systemUserRepository.GetUserBySystemUserGuid(updatedSystemUser.SystemUser.SystemUserGuid);
            if (originalSystemUser != null)
            {
                originalSystemUserWizardViewModel.SystemUser = originalSystemUser;
            }

            //GDSs
            SystemUserGDSRepository systemUserGDSRepository = new SystemUserGDSRepository();
            List <fnDesktopDataAdmin_SelectSystemUserGDSs_v1Result> originalSystemuserGDSs = new List <fnDesktopDataAdmin_SelectSystemUserGDSs_v1Result>();

            originalSystemuserGDSs = systemUserGDSRepository.GetSystemUserGDSs(updatedSystemUser.SystemUser.SystemUserGuid).ToList();
            if (originalSystemuserGDSs != null)
            {
                originalSystemUserWizardViewModel.SystemUserGDSs = originalSystemuserGDSs;
            }

            //ExternalSystemLoginSystemUserCountries
            ExternalSystemLoginRepository externalSystemLoginRepository = new ExternalSystemLoginRepository();
            List <ExternalSystemLoginSystemUserCountry> originalExternalSystemLoginSystemUserCountries = externalSystemLoginRepository.GetBackOfficeIdentifiers(updatedSystemUser.SystemUser.SystemUserGuid);

            if (originalExternalSystemLoginSystemUserCountries != null && originalExternalSystemLoginSystemUserCountries.Count > 0)
            {
                originalSystemUserWizardViewModel.ExternalSystemLoginSystemUserCountries = originalExternalSystemLoginSystemUserCountries;
            }

            systemUserWizardRepository.BuildSystemUserChangeMessages(wizardMessages, originalSystemUserWizardViewModel, updatedSystemUser);

            return(Json(new WizardJSONResponse
            {
                html = ControllerExtension.RenderPartialViewToString(this, "ConfirmChangesScreen", wizardMessages),
                message = "Success",
                success = true
            }));
        }
        public ActionResult SystemUserDetailsScreen(string systemUserGuid)
        {
            SystemUser systemUser = new SystemUser();

            systemUser = systemUserRepository.GetUserBySystemUserGuid(systemUserGuid);

            //Check Exists
            if (systemUser == null)
            {
                return(PartialView("Error", "System User Does Not Exist"));
            }


            //add linked information including location
            systemUserRepository.EditForDisplay(systemUser);

            SystemUserWizardVM systemUserWizardViewModel = new SystemUserWizardVM();

            SystemUserGDSRepository systemUserGDSRepository = new SystemUserGDSRepository();
            List <fnDesktopDataAdmin_SelectSystemUserGDSs_v1Result> systemUserGDSs = new List <fnDesktopDataAdmin_SelectSystemUserGDSs_v1Result>();

            systemUserGDSs = systemUserGDSRepository.GetSystemUserGDSs(systemUser.SystemUserGuid).ToList();

            GDSRepository gdsRepository = new GDSRepository();
            List <GDS>    gdss          = new List <GDS>();

            gdss = gdsRepository.GetAllGDSs().ToList();

            SystemUserLocationRepository systemUserLocationRepository = new SystemUserLocationRepository();
            SystemUserLocation           systemUserLocation           = new SystemUserLocation();

            systemUserLocation = systemUserLocationRepository.GetSystemUserLocation(systemUser.SystemUserGuid);

            HierarchyRepository hierarchyRepository = new HierarchyRepository();

            //AccessRights
            if (systemUserLocation != null)
            {
                RolesRepository rolesRepository = new RolesRepository();
                systemUserWizardViewModel.HasWriteAccessToLocation = rolesRepository.HasWriteAccessToLocation(systemUserLocation.LocationId);
            }
            else
            {
                systemUserWizardViewModel.HasWriteAccessToLocation = hierarchyRepository.AdminHasDomainWriteAccess("Location");
            }

            //Countries
            CountryRepository countryRepository = new CountryRepository();

            systemUserWizardViewModel.Countries = new SelectList(countryRepository.GetAllCountries().ToList(), "CountryCode", "CountryName");

            //ExternalSystemLogins
            ExternalSystemLoginRepository externalSystemLoginRepository = new ExternalSystemLoginRepository();
            List <ExternalSystemLoginSystemUserCountry> externalSystemLoginSystemUserCountries = new List <ExternalSystemLoginSystemUserCountry>();

            externalSystemLoginSystemUserCountries = externalSystemLoginRepository.GetBackOfficeIdentifiers(systemUser.SystemUserGuid);
            foreach (ExternalSystemLoginSystemUserCountry externalSystemLoginSystemUserCountry in externalSystemLoginSystemUserCountries)
            {
                externalSystemLoginSystemUserCountry.Countries = new SelectList(countryRepository.GetAllCountries().ToList(), "CountryCode", "CountryName", externalSystemLoginSystemUserCountry.CountryCode);
            }

            //Check Compliance Access
            ViewData["ComplianceAdministratorAccess"] = "";
            if (hierarchyRepository.AdminHasDomainWriteAccess("Compliance Administrator"))
            {
                ViewData["ComplianceAdministratorAccess"] = "WriteAccess";
            }

            //Check Restricted System User Access
            ViewData["RestrictedSystemUserAdministratorAccess"] = "";
            if (hierarchyRepository.AdminHasDomainWriteAccess("Restricted System User Administrator"))
            {
                ViewData["RestrictedSystemUserAdministratorAccess"] = "WriteAccess";
            }

            systemUserWizardViewModel.SystemUser         = systemUser;
            systemUserWizardViewModel.SystemUserGDSs     = systemUserGDSs;
            systemUserWizardViewModel.GDSs               = gdss;
            systemUserWizardViewModel.SystemUserLocation = systemUserLocation;
            systemUserWizardViewModel.ExternalSystemLoginSystemUserCountries = externalSystemLoginSystemUserCountries;


            return(PartialView("SystemUserDetailsScreen", systemUserWizardViewModel));
        }
        public ActionResult CommitChanges(SystemUserWizardVM systemUserChanges)
        {
            SystemUserLocation systemUserLocation = new SystemUserLocation();

            systemUserLocation = systemUserChanges.SystemUserLocation;


            WizardMessages wizardMessages = new WizardMessages();

            try
            {
                TryUpdateModel(systemUserChanges.SystemUserLocation, "SystemUserLocation");
            }
            catch
            {
                //Validation Error
                string msg = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        msg += error.ErrorMessage;
                    }
                }
                return(Json(new WizardJSONResponse
                {
                    html = ControllerExtension.RenderPartialViewToString(this, "Error", msg),
                    message = msg,
                    success = false
                }));
            }


            //Editing A SystemUser Location
            try
            {
                SystemUserLocationRepository systemUserLocationRepository = new SystemUserLocationRepository();
                SystemUserLocation           originalSystemUserLocation   = new SystemUserLocation();
                originalSystemUserLocation = systemUserLocationRepository.GetSystemUserLocation(systemUserChanges.SystemUser.SystemUserGuid);
                if (originalSystemUserLocation == null)
                {
                    systemUserLocationRepository.Add(systemUserLocation);
                    wizardMessages.AddMessage("User's Location successfully updated", true);
                }
                else
                {
                    if (originalSystemUserLocation.LocationId != systemUserChanges.SystemUserLocation.LocationId)
                    {
                        systemUserLocationRepository.Update(systemUserLocation);
                        wizardMessages.AddMessage("User's Location successfully updated", true);
                    }
                }
            }
            catch (SqlException ex)
            {
                //If there is error we will continue, but store error to return to user

                //Versioning Error
                if (ex.Message == "SQLVersioningError")
                {
                    wizardMessages.AddMessage("User's Location was not updated. Another user has already changed this Location.", false);
                }
                else                 //Other Error
                {
                    LogRepository logRepository = new LogRepository();
                    logRepository.LogError(ex.Message);

                    wizardMessages.AddMessage("User's Location was not updated, please check Event Log for details", false);
                    wizardMessages.AddMessage("There was a problem with your request, please see the log file or contact an administrator for details", false);
                }
            }


            //Editing Default Profile Identifier
            try
            {
                SystemUserRepository systemUserRepository = new SystemUserRepository();
                SystemUser           originalSystemUser   = new SystemUser();
                originalSystemUser = systemUserRepository.GetUserBySystemUserGuid(systemUserChanges.SystemUser.SystemUserGuid);

                if (originalSystemUser.DefaultProfileIdentifier == null)
                {
                    originalSystemUser.DefaultProfileIdentifier = false;
                }
                if (originalSystemUser.DefaultProfileIdentifier != systemUserChanges.SystemUser.DefaultProfileIdentifier)
                {
                    wizardMessages.AddMessage("User's Default Profile successfully updated", true);
                    systemUserRepository.EditDefaultProfileIdentifier(systemUserChanges.SystemUser);
                }
            }
            catch (SqlException ex)
            {
                //If there is error we will continue, but store error to return to user

                //Versioning Error
                if (ex.Message == "SQLVersioningError")
                {
                    wizardMessages.AddMessage("User's Default Profile was not updated. Another user has already changed this.", false);
                }
                else                 //Other Error
                {
                    LogRepository logRepository = new LogRepository();
                    logRepository.LogError(ex.Message);

                    wizardMessages.AddMessage("User's Default Profile was not updated, please check Event Log for details", false);
                    wizardMessages.AddMessage("There was a problem with your request, please see the log file or contact an administrator for details", false);
                }
            }

            //Editing Restricted Flag
            try
            {
                SystemUserRepository systemUserRepository = new SystemUserRepository();
                SystemUser           originalSystemUser   = new SystemUser();
                originalSystemUser = systemUserRepository.GetUserBySystemUserGuid(systemUserChanges.SystemUser.SystemUserGuid);

                if (originalSystemUser.RestrictedFlag == null)
                {
                    originalSystemUser.RestrictedFlag = false;
                }
                if (originalSystemUser.RestrictedFlag != systemUserChanges.SystemUser.RestrictedFlag)
                {
                    wizardMessages.AddMessage("User's Restricted Flag successfully updated", true);
                    systemUserRepository.EditRestrictedFlag(systemUserChanges.SystemUser);
                }
            }
            catch (SqlException ex)
            {
                //If there is error we will continue, but store error to return to user

                //Versioning Error
                if (ex.Message == "SQLVersioningError")
                {
                    wizardMessages.AddMessage("User's Restricted Flag was not updated. Another user has already changed this.", false);
                }
                else                 //Other Error
                {
                    LogRepository logRepository = new LogRepository();
                    logRepository.LogError(ex.Message);

                    wizardMessages.AddMessage("User's Restricted Flag was not updated, please check Event Log for details", false);
                    wizardMessages.AddMessage("There was a problem with your request, please see the log file or contact an administrator for details", false);
                }
            }

            //Editing Cuba Booking Allowed Identifier
            try
            {
                SystemUserRepository systemUserRepository = new SystemUserRepository();
                SystemUser           originalSystemUser   = new SystemUser();
                originalSystemUser = systemUserRepository.GetUserBySystemUserGuid(systemUserChanges.SystemUser.SystemUserGuid);

                if (originalSystemUser.CubaBookingAllowanceIndicator == null)
                {
                    originalSystemUser.CubaBookingAllowanceIndicator = false;
                }

                if (originalSystemUser.CubaBookingAllowanceIndicator != systemUserChanges.SystemUser.CubaBookingAllowanceIndicator)
                {
                    wizardMessages.AddMessage("User's Cuba Booking Allowed successfully updated", true);
                    systemUserRepository.EditCubaBookingAllowanceIndicator(systemUserChanges.SystemUser);
                }
            }
            catch (SqlException ex)
            {
                //If there is error we will continue, but store error to return to user

                //Versioning Error
                if (ex.Message == "SQLVersioningError")
                {
                    wizardMessages.AddMessage("User's Cuba Booking Allowed was not updated. Another user has already changed this.", false);
                }
                else                 //Other Error
                {
                    LogRepository logRepository = new LogRepository();
                    logRepository.LogError(ex.Message);

                    wizardMessages.AddMessage("User's Cuba Booking Allowed was not updated, please check Event Log for details", false);
                    wizardMessages.AddMessage("There was a problem with your request, please see the log file or contact an administrator for details", false);
                }
            }


            //ExternalSystemLoginSystemUserCountries
            ExternalSystemLoginRepository externalSystemLoginRepository = new ExternalSystemLoginRepository();
            List <ExternalSystemLoginSystemUserCountry> originalExternalSystemLoginSystemUserCountries = externalSystemLoginRepository.GetBackOfficeIdentifiers(systemUserChanges.SystemUser.SystemUserGuid);

            if (originalExternalSystemLoginSystemUserCountries != null && originalExternalSystemLoginSystemUserCountries.Count == 0)
            {
                originalExternalSystemLoginSystemUserCountries = null;
            }

            if (originalExternalSystemLoginSystemUserCountries != systemUserChanges.ExternalSystemLoginSystemUserCountries)
            {
                string systemUserGuid = systemUserChanges.SystemUser.SystemUserGuid;
                try
                {
                    externalSystemLoginRepository.AddBackOfficeIdentifiers(systemUserGuid, systemUserChanges.ExternalSystemLoginSystemUserCountries);
                    wizardMessages.AddMessage("User's Back Office Identifiers successfully updated", true);
                }
                catch (SqlException ex)
                {
                    //Versioning Error
                    if (ex.Message == "SQLVersioningError")
                    {
                        wizardMessages.AddMessage("User's BackOffice Identifier was not updated. Another user has already changed this item.", false);
                    }
                    else                     //Other Error
                    {
                        LogRepository logRepository = new LogRepository();
                        logRepository.LogError(ex.Message);

                        wizardMessages.AddMessage("User's BackOffice Identifier was not updated, please check Event Log for details", false);
                        wizardMessages.AddMessage("There was a problem with your request, please see the log file or contact an administrator for details", false);
                    }
                }
            }

            //we continue to add Teams
            try
            {
                wizardMessages = systemUserWizardRepository.UpdateSystemUserTeams(systemUserChanges, wizardMessages);
            }
            catch (SqlException ex)
            {
                LogRepository logRepository = new LogRepository();
                logRepository.LogError(ex.Message);

                wizardMessages.AddMessage("User's Teams were not changed, please check Event Log for details", false);
                wizardMessages.AddMessage("There was a problem with your request, please see the log file or contact an administrator for details", false);
            }

            //we continue to add GDSs
            if (systemUserChanges.GDSChanged)
            {
                try
                {
                    wizardMessages = systemUserWizardRepository.UpdateSystemUserGDSs(systemUserChanges, wizardMessages);
                }
                catch (SqlException ex)
                {
                    LogRepository logRepository = new LogRepository();
                    logRepository.LogError(ex.Message);

                    wizardMessages.AddMessage("User's GDS settings were not changed, please check Event Log for details", false);
                    wizardMessages.AddMessage("There was a problem with your request, please see the log file or contact an administrator for details", false);
                }
            }
            return(Json(new
            {
                html = ControllerExtension.RenderPartialViewToString(this, "FinishedScreen", wizardMessages),
                message = "Success",
                success = true
            }));
        }
        //Compare two SystemUsers and return a list of messages about changes
        public WizardMessages BuildSystemUserChangeMessages(WizardMessages wizardMessages, SystemUserWizardVM originalSystemUser, SystemUserWizardVM systemUserChanges)
        {
            TeamRepository                teamRepository                = new TeamRepository();
            LocationRepository            locationRepository            = new LocationRepository();
            SystemUserRepository          systemUserRepository          = new SystemUserRepository();
            ExternalSystemLoginRepository externalSystemLoginRepository = new ExternalSystemLoginRepository();

            //Messages for DefaultProfile changes
            if (originalSystemUser.SystemUser.DefaultProfileIdentifier == null)
            {
                originalSystemUser.SystemUser.DefaultProfileIdentifier = false;
            }

            if (originalSystemUser.SystemUser.DefaultProfileIdentifier != systemUserChanges.SystemUser.DefaultProfileIdentifier)
            {
                wizardMessages.AddMessage("Default Profile will be updated to \"" + systemUserChanges.SystemUser.DefaultProfileIdentifier + "\".", true);
            }

            //Messages for CubaBookingAllowed changes
            if (originalSystemUser.SystemUser.CubaBookingAllowanceIndicator == null)
            {
                originalSystemUser.SystemUser.CubaBookingAllowanceIndicator = false;
            }

            if (originalSystemUser.SystemUser.CubaBookingAllowanceIndicator != systemUserChanges.SystemUser.CubaBookingAllowanceIndicator)
            {
                wizardMessages.AddMessage("Cuba Booking Allowed will be updated to \"" + systemUserChanges.SystemUser.CubaBookingAllowanceIndicator + "\".", true);
            }

            //Messages for RestrictedFlag changes
            if (originalSystemUser.SystemUser.RestrictedFlag == null)
            {
                originalSystemUser.SystemUser.RestrictedFlag = false;
            }

            if (originalSystemUser.SystemUser.RestrictedFlag != systemUserChanges.SystemUser.RestrictedFlag)
            {
                wizardMessages.AddMessage("Restricted will be updated to \"" + systemUserChanges.SystemUser.RestrictedFlag + "\".", true);
            }

            //Messages for Location
            if (originalSystemUser.SystemUserLocation == null)
            {
                if (systemUserChanges.SystemUserLocation != null)
                {
                    Location location = new Location();
                    location = locationRepository.GetLocation(systemUserChanges.SystemUserLocation.LocationId);
                    wizardMessages.AddMessage("Location will be updated to \"" + location.LocationName + "\".", true);
                }
            }
            else
            {
                if (systemUserChanges.SystemUserLocation != null)
                {
                    if (systemUserChanges.SystemUserLocation.LocationId != originalSystemUser.SystemUserLocation.LocationId)
                    {
                        Location location = new Location();
                        location = locationRepository.GetLocation(systemUserChanges.SystemUserLocation.LocationId);
                        wizardMessages.AddMessage("Location will be updated to \"" + location.LocationName + "\".", true);
                    }
                }
            }

            //Messages for BackOfficeAgentIdentifier
            if (originalSystemUser.ExternalSystemLoginSystemUserCountries != systemUserChanges.ExternalSystemLoginSystemUserCountries)
            {
                wizardMessages.AddMessage("User's Back Office Identifiers will be updated.", true);
            }

            //sort List<systemUserChanges> for compare
            if (systemUserChanges.GDSChanged)
            {
                wizardMessages.AddMessage("SystemUserGDSs will be updated.", true);
            }

            //originalSystemUser.SystemUserGDSs.Sort((x, y) => string.Compare(x.GDSCode, y.GDSCode));
            //systemUserChanges.SystemUserGDSs.Sort((x, y) => string.Compare(x.GDSCode, y.GDSCode));
            //compare GDSs
            //if (!originalSystemUser.SystemUserGDSs.SequenceEqual(systemUserChanges.SystemUserGDSs))
            //{
            // wizardMessages.AddMessage("SystemUserGDSs will be updated.", true);
            //}

            //Systemuser teams
            if (systemUserChanges.TeamsAdded != null)
            {
                if (systemUserChanges.TeamsAdded.Count > 0)
                {
                    foreach (Team item in systemUserChanges.TeamsAdded)
                    {
                        Team team = new Team();
                        team = teamRepository.GetTeam(item.TeamId);
                        if (team != null)
                        {
                            wizardMessages.AddMessage("You will add user to Team \"" + team.TeamName + "\".", true);
                        }
                    }
                }
            }
            if (systemUserChanges.TeamsRemoved != null)
            {
                if (systemUserChanges.TeamsRemoved.Count > 0)
                {
                    foreach (Team item in systemUserChanges.TeamsRemoved)
                    {
                        Team team = new Team();
                        team = teamRepository.GetTeam(item.TeamId);
                        if (team != null)
                        {
                            wizardMessages.AddMessage("You will remove user from Team \"" + team.TeamName + "\".", true);
                        }
                    }
                }
            }
            return(wizardMessages);
        }
        //Update SystemUser GDSss
        public WizardMessages UpdateSystemUserGDSs(SystemUserWizardVM systemuserChanges, WizardMessages wizardMessages)
        {
            string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0];

            // Create the xml document container
            XmlDocument    doc = new XmlDocument();// Create the XML Declaration, and append it to XML document
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);

            doc.AppendChild(dec);
            XmlElement root = doc.CreateElement("SystemUserGDSs");

            doc.AppendChild(root);


            GDSRepository gdsRepository = new GDSRepository();

            if (systemuserChanges.SystemUserGDSs != null)
            {
                if (systemuserChanges.SystemUserGDSs.Count > 0)
                {
                    foreach (fnDesktopDataAdmin_SelectSystemUserGDSs_v1Result item in systemuserChanges.SystemUserGDSs)
                    {
                        GDS gds = new GDS();
                        gds = gdsRepository.GetGDS(item.GDSCode);
                        if (gds != null)
                        {
                            XmlElement xmlGDS = doc.CreateElement("GDS");
                            root.AppendChild(xmlGDS);

                            XmlElement xmlGDSName = doc.CreateElement("GDSName");
                            xmlGDSName.InnerText = gds.GDSName;
                            xmlGDS.AppendChild(xmlGDSName);

                            XmlElement xmlGDSCode = doc.CreateElement("GDSCode");
                            xmlGDSCode.InnerText = gds.GDSCode;
                            xmlGDS.AppendChild(xmlGDSCode);

                            XmlElement xmlPseudoCityOrOfficeId = doc.CreateElement("PseudoCityOrOfficeId");
                            xmlPseudoCityOrOfficeId.InnerText = item.PseudoCityOrOfficeId;
                            xmlGDS.AppendChild(xmlPseudoCityOrOfficeId);

                            XmlElement xmlGDSSignOn = doc.CreateElement("GDSSignOn");
                            xmlGDSSignOn.InnerText = item.GDSSignOn;
                            xmlGDS.AppendChild(xmlGDSSignOn);

                            XmlElement xmlDefaultGDS = doc.CreateElement("DefaultGDS");
                            xmlDefaultGDS.InnerText = item.DefaultGDS == true ? "1" : "0";
                            xmlGDS.AppendChild(xmlDefaultGDS);
                        }
                    }
                }
            }

            var output = (from n in db.spDDAWizard_UpdateSystemUserGDSs_v1(
                              systemuserChanges.SystemUser.SystemUserGuid,
                              System.Xml.Linq.XElement.Parse(doc.OuterXml),
                              adminUserGuid)
                          select n).ToList();

            if (output != null)
            {
                foreach (spDDAWizard_UpdateSystemUserGDSs_v1Result message in output)
                {
                    if (message != null)
                    {
                        if (message.MessageText != null && message.Success != null)
                        {
                            wizardMessages.AddMessage(message.MessageText.ToString(), (bool)message.Success);
                        }
                    }
                }
            }
            return(wizardMessages);
        }
        //Update SystemUser Teams
        public WizardMessages UpdateSystemUserTeams(SystemUserWizardVM systemuserChanges, WizardMessages wizardMessages)
        {
            string adminUserGuid = HttpContext.Current.User.Identity.Name.Split(new[] { '|' })[0];
            bool   changesExist  = false;

            // Create the xml document container
            XmlDocument    doc = new XmlDocument();// Create the XML Declaration, and append it to XML document
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);

            doc.AppendChild(dec);
            XmlElement root = doc.CreateElement("SystemUserTeams");

            doc.AppendChild(root);

            //string xml = "<SystemUserTeams>";

            TeamRepository teamRepository = new TeamRepository();

            if (systemuserChanges.TeamsAdded != null)
            {
                if (systemuserChanges.TeamsAdded.Count > 0)
                {
                    changesExist = true;
                    XmlElement xmlTeamsAdded = doc.CreateElement("TeamsAdded");

                    foreach (Team item in systemuserChanges.TeamsAdded)
                    {
                        Team team = new Team();
                        team = teamRepository.GetTeam(item.TeamId);
                        if (team != null)
                        {
                            //xml = xml + "<Team>";
                            //xml = xml + "<TeamName>" + team.TeamName + "</TeamName>";
                            //xml = xml + "<TeamId>" + item.TeamId + "</TeamId>";
                            //xml = xml + "</Team>";

                            XmlElement xmlTeam = doc.CreateElement("Team");
                            xmlTeamsAdded.AppendChild(xmlTeam);

                            XmlElement xmlTeamName = doc.CreateElement("TeamName");
                            xmlTeamName.InnerText = team.TeamName;
                            xmlTeam.AppendChild(xmlTeamName);

                            XmlElement xmlTeamId = doc.CreateElement("TeamId");
                            xmlTeamId.InnerText = item.TeamId.ToString();
                            xmlTeam.AppendChild(xmlTeamId);
                        }
                    }
                    root.AppendChild(xmlTeamsAdded);
                    //xml = xml + "</TeamsAdded>";
                }
            }
            if (systemuserChanges.TeamsRemoved != null)
            {
                if (systemuserChanges.TeamsRemoved.Count > 0)
                {
                    changesExist = true;
                    // xml = xml + "<TeamsRemoved>";
                    XmlElement xmlTeamsRemoved = doc.CreateElement("TeamsRemoved");

                    foreach (Team item in systemuserChanges.TeamsRemoved)
                    {
                        Team team = new Team();
                        team = teamRepository.GetTeam(item.TeamId);
                        if (team != null)
                        {
                            // xml = xml + "<Team>";
                            //xml = xml + "<TeamName>" + team.TeamName + "</TeamName>";
                            //xml = xml + "<TeamId>" + item.TeamId + "</TeamId>";
                            //xml = xml + "</Team>";
                            XmlElement xmlTeam = doc.CreateElement("Team");
                            xmlTeamsRemoved.AppendChild(xmlTeam);

                            XmlElement xmlTeamName = doc.CreateElement("TeamName");
                            xmlTeamName.InnerText = team.TeamName;
                            xmlTeam.AppendChild(xmlTeamName);

                            XmlElement xmlTeamId = doc.CreateElement("TeamId");
                            xmlTeamId.InnerText = item.TeamId.ToString();
                            xmlTeam.AppendChild(xmlTeamId);
                        }
                    }
                    root.AppendChild(xmlTeamsRemoved);
                    //xml = xml + "</TeamsRemoved>";
                }
            }
            //xml = xml + "</SystemUserTeams>";

            //only run if changes made to SystemUser's Teams
            if (changesExist)
            {
                var output = (from n in db.spDDAWizard_UpdateSystemUserTeams_v1(
                                  systemuserChanges.SystemUser.SystemUserGuid,
                                  System.Xml.Linq.XElement.Parse(doc.OuterXml),
                                  adminUserGuid)
                              select n).ToList();

                foreach (spDDAWizard_UpdateSystemUserTeams_v1Result message in output)
                {
                    wizardMessages.AddMessage(message.MessageText.ToString(), (bool)message.Success);
                }
            }
            return(wizardMessages);
        }