// GET: /GDSEndWarningConfiguration/Edit
        public ActionResult Edit(int id)
        {
            //Get Item From Database
            GDSEndWarningConfiguration gdsEndWarningConfiguration = new GDSEndWarningConfiguration();

            gdsEndWarningConfiguration = gdsEndWarningConfigurationRepository.GetGroup(id);

            //Check Exists
            if (gdsEndWarningConfiguration == null)
            {
                ViewData["ActionMethod"] = "EditGet";
                return(View("RecordDoesNotExistError"));
            }
            //Check Access
            if (!rolesRepository.HasWriteAccessToGDSEndWarningConfiguration())
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            GDSEndWarningConfigurationVM gdsEndWarningConfigurationVM = new GDSEndWarningConfigurationVM();

            gdsEndWarningConfigurationVM.GDSEndWarningConfiguration = gdsEndWarningConfiguration;

            //GDS List
            GDSRepository gDSRepository = new GDSRepository();
            SelectList    gDSs          = new SelectList(gDSRepository.GetClientProfileBuilderGDSs().ToList(), "GDSCode", "GDSName", gdsEndWarningConfiguration.GDSCode);

            gdsEndWarningConfigurationVM.GDSs = gDSs;

            //GDSEndWarningBehaviorTypes
            GDSEndWarningBehaviorTypeRepository gdsEndWarningBehaviorTypeRepository = new GDSEndWarningBehaviorTypeRepository();
            SelectList gdsEndWarningBehaviorTypes = new SelectList(gdsEndWarningBehaviorTypeRepository.GetAllGDSEndWarningBehaviorTypes().ToList(), "GDSEndWarningBehaviorTypeId", "GDSEndWarningBehaviorTypeDescription", gdsEndWarningConfiguration.GDSEndWarningBehaviorTypeId);

            gdsEndWarningConfigurationVM.GDSEndWarningBehaviorTypes = gdsEndWarningBehaviorTypes;

            //Automated Commands
            if (gdsEndWarningConfigurationVM.GDSEndWarningConfiguration.AutomatedCommands == null ||
                (gdsEndWarningConfigurationVM.GDSEndWarningConfiguration.AutomatedCommands != null && gdsEndWarningConfigurationVM.GDSEndWarningConfiguration.AutomatedCommands.Count < 1))
            {
                List <AutomatedCommand> automatedCommands = new List <AutomatedCommand>();
                AutomatedCommand        automatedCommand  = new AutomatedCommand()
                {
                    CommandExecutionSequenceNumber = 1
                };
                automatedCommands.Add(automatedCommand);
                ViewData["AutomatedCommands"] = automatedCommands;
            }
            else
            {
                ViewData["AutomatedCommands"] = gdsEndWarningConfigurationVM.GDSEndWarningConfiguration.AutomatedCommands;
            }

            ViewData["WarningMessage"] = gdsEndWarningConfiguration.IdentifyingWarningMessage;

            return(View(gdsEndWarningConfigurationVM));
        }
        //
        // GET: /GDSEndWarningConfiguration/Create

        public ActionResult Create()
        {
            //Check Access Rights to Domain
            if (!hierarchyRepository.AdminHasDomainWriteAccess(groupName))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            GDSEndWarningConfigurationVM gdsEndWarningConfigurationVM = new GDSEndWarningConfigurationVM();

            //GDS List
            GDSRepository gDSRepository = new GDSRepository();
            SelectList    gDSs          = new SelectList(gDSRepository.GetClientProfileBuilderGDSs().ToList(), "GDSCode", "GDSName");

            gdsEndWarningConfigurationVM.GDSs = gDSs;

            //GDSEndWarningBehaviorTypes
            GDSEndWarningBehaviorTypeRepository gdsEndWarningBehaviorTypeRepository = new GDSEndWarningBehaviorTypeRepository();
            SelectList gdsEndWarningBehaviorTypes = new SelectList(gdsEndWarningBehaviorTypeRepository.GetAllGDSEndWarningBehaviorTypes().ToList(), "GDSEndWarningBehaviorTypeId", "GDSEndWarningBehaviorTypeDescription");

            gdsEndWarningConfigurationVM.GDSEndWarningBehaviorTypes = gdsEndWarningBehaviorTypes;

            //Automated Commands
            List <AutomatedCommand> automatedCommands = new List <AutomatedCommand>();
            AutomatedCommand        automatedCommand  = new AutomatedCommand();

            automatedCommands.Add(automatedCommand);
            gdsEndWarningConfigurationVM.AutomatedCommands = automatedCommands;

            GDSEndWarningConfiguration gdsEndWarningConfiguration = new GDSEndWarningConfiguration();

            gdsEndWarningConfigurationVM.GDSEndWarningConfiguration = gdsEndWarningConfiguration;

            return(View(gdsEndWarningConfigurationVM));
        }
        public ActionResult Edit(GDSEndWarningConfigurationVM gdsEndWarningConfigurationVM, FormCollection formCollection)
        {
            //Get Item
            GDSEndWarningConfiguration gdsEndWarningConfiguration = new GDSEndWarningConfiguration();

            gdsEndWarningConfiguration = gdsEndWarningConfigurationRepository.GetGroup(gdsEndWarningConfigurationVM.GDSEndWarningConfiguration.GDSEndWarningConfigurationId);

            //Check Exists
            if (gdsEndWarningConfiguration == null)
            {
                ViewData["ActionMethod"] = "EditPost";
                return(View("RecordDoesNotExistError"));
            }

            //Check Access Rights
            if (!rolesRepository.HasWriteAccessToGDSEndWarningConfiguration())
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            //Create Automated Commands from Post values
            System.Data.Linq.EntitySet <AutomatedCommand> AutomatedCommands = new System.Data.Linq.EntitySet <AutomatedCommand>();

            foreach (string key in formCollection)
            {
                if (key.StartsWith("AutomatedCommand") && !string.IsNullOrEmpty(formCollection[key]))
                {
                    AutomatedCommand automatedCommand = new AutomatedCommand()
                    {
                        CommandText = formCollection[key],
                        CommandExecutionSequenceNumber = int.Parse(key.Replace("AutomatedCommand_", ""))
                    };

                    AutomatedCommands.Add(automatedCommand);
                }
            }

            //Remove Automated Commands if not set, otherwise add new ones in
            gdsEndWarningConfiguration.AutomatedCommands = (AutomatedCommands != null && AutomatedCommands.Count > 0) ? AutomatedCommands : null;

            //Update Model From Form + Validate against DB
            try
            {
                UpdateModel <GDSEndWarningConfiguration>(gdsEndWarningConfiguration, "GDSEndWarningConfiguration");
            }
            catch
            {
                string n = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        n += error.ErrorMessage;
                    }
                }
                ViewData["Message"] = "ValidationError : " + n;
                return(View("Error"));
            }

            //Database Update
            try
            {
                gdsEndWarningConfigurationRepository.Edit(gdsEndWarningConfiguration);
            }
            catch (SqlException ex)
            {
                //Non-Unique Name
                if (ex.Message == "NonUniqueName")
                {
                    return(View("NonUniqueNameError"));
                }
                //Versioning Error
                if (ex.Message == "SQLVersioningError")
                {
                    ViewData["ReturnURL"] = "/GDSEndWarningConfiguration.mvc/Edit/" + gdsEndWarningConfiguration.GDSEndWarningConfigurationId;
                    return(View("VersionError"));
                }
                LogRepository logRepository = new LogRepository();
                logRepository.LogError(ex.Message);

                ViewData["Message"] = "There was a problem with your request, please see the log file or contact an administrator for details";
                return(View("Error"));
            }

            return(RedirectToAction("List"));
        }
        public ActionResult Create(GDSEndWarningConfigurationVM GDSEndWarningConfigurationVM, FormCollection formCollection)
        {
            //Check Access Rights to Domain
            if (!hierarchyRepository.AdminHasDomainWriteAccess(groupName))
            {
                ViewData["Message"] = "You do not have access to this item";
                return(View("Error"));
            }

            //We need to extract group from groupVM
            GDSEndWarningConfiguration gdsEndWarningConfiguration = new GDSEndWarningConfiguration();

            gdsEndWarningConfiguration = GDSEndWarningConfigurationVM.GDSEndWarningConfiguration;
            if (gdsEndWarningConfiguration == null)
            {
                ViewData["Message"] = "ValidationError : missing item";;
                return(View("Error"));
            }

            //Create Automated Commands from Post values
            System.Data.Linq.EntitySet <AutomatedCommand> AutomatedCommands = new System.Data.Linq.EntitySet <AutomatedCommand>();

            foreach (string key in formCollection)
            {
                if (key.StartsWith("AutomatedCommand") && !string.IsNullOrEmpty(formCollection[key]))
                {
                    AutomatedCommand automatedCommand = new AutomatedCommand()
                    {
                        CommandText = formCollection[key],
                        CommandExecutionSequenceNumber = int.Parse(key.Replace("AutomatedCommand_", ""))
                    };

                    AutomatedCommands.Add(automatedCommand);
                }
            }

            if (AutomatedCommands != null && AutomatedCommands.Count > 0)
            {
                gdsEndWarningConfiguration.AutomatedCommands = AutomatedCommands;
            }

            //Update Model From Form + Validate against DB
            try
            {
                UpdateModel <GDSEndWarningConfiguration>(gdsEndWarningConfiguration, "GDSEndWarningConfiguration");
            }
            catch
            {
                string n = "";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        n += error.ErrorMessage;
                    }
                }
                ViewData["Message"] = "ValidationError : " + n;
                return(View("Error"));
            }

            //Database Update
            try
            {
                gdsEndWarningConfigurationRepository.Add(gdsEndWarningConfiguration);
            }
            catch (SqlException ex)
            {
                //Non-Unique Name
                if (ex.Message == "NonUniqueName")
                {
                    return(View("NonUniqueNameError"));
                }

                LogRepository logRepository = new LogRepository();
                logRepository.LogError(ex.Message);

                ViewData["Message"] = "There was a problem with your request, please see the log file or contact an administrator for details";
                return(View("Error"));
            }

            ViewData["NewSortOrder"] = 0;
            return(RedirectToAction("List"));
        }