Пример #1
0
    /// <summary>
    /// Sets data to database.
    /// </summary>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        // Check "modify" permission
        if (!MembershipContext.AuthenticatedUser.IsAuthorizedPerResource("CMS.ScheduledTasks", "Modify"))
        {
            RedirectToAccessDenied("CMS.ScheduledTasks", "Modify");
        }

        // Check required fields format
        string errorMessage = new Validator()
                              .NotEmpty(txtTaskDisplayName.Text, rfvDisplayName.ErrorMessage)
                              .NotEmpty(txtTaskName.Text, rfvName.ErrorMessage)
                              .IsCodeName(txtTaskName.Text, GetString("Task_Edit.InvalidTaskName"))
                              .MatchesCondition(schedInterval.StartTime.SelectedDateTime, DataTypeManager.IsValidDate, String.Format("{0} {1}.", GetString("BasicForm.ErrorInvalidDateTime"), DateTime.Now))
                              .Result;

        if ((errorMessage == String.Empty) && !schedInterval.CheckIntervalPreceedings())
        {
            errorMessage = GetString("Task_Edit.BetweenIntervalPreceedingsError");
        }

        if ((errorMessage == String.Empty) && !schedInterval.CheckOneDayMinimum())
        {
            errorMessage = GetString("Task_Edit.AtLeastOneDayError");
        }

        // Validate assembly, but only if task is enabled (so tasks for not-installed modules can be disabled)
        if ((errorMessage == String.Empty) && chkTaskEnabled.Checked && !assemblyElem.IsValid())
        {
            errorMessage = assemblyElem.ErrorMessage;
        }

        // Checking date/time limit (SQL limit)
        if (errorMessage == String.Empty)
        {
            TaskInterval ti = SchedulingHelper.DecodeInterval(schedInterval.ScheduleInterval);
            if ((ti != null) && ((ti.StartTime < DataTypeManager.MIN_DATETIME) || (ti.StartTime > DataTypeManager.MAX_DATETIME)))
            {
                ti.StartTime = DateTime.Now;
                schedInterval.ScheduleInterval = SchedulingHelper.EncodeInterval(ti);
            }
        }

        // Check macro condition length
        if ((errorMessage == String.Empty) && (ucMacroEditor.Text.Length > 400))
        {
            errorMessage = String.Format(GetString("task_edit.invalidlength"), 400);
        }

        if (!String.IsNullOrEmpty(errorMessage))
        {
            ShowError(errorMessage);
        }
        else
        {
            // Check existing task name
            TaskInfo existingTask = TaskInfoProvider.GetTaskInfo(txtTaskName.Text.Trim(), SiteInfo != null ? SiteInfo.SiteName : null);

            if ((existingTask != null) && ((TaskInfo == null) || (existingTask.TaskID != TaskInfo.TaskID)))
            {
                ShowError(GetString("Task_Edit.TaskNameExists").Replace("%%name%%", existingTask.TaskName));
                return;
            }

            if (TaskInfo == null)
            {
                // create new item -> insert
                TaskInfo = new TaskInfo {
                    TaskSiteID = SiteID
                };
                if (!developmentMode)
                {
                    TaskInfo.TaskAllowExternalService = true;
                }
            }

            TaskInfo.TaskAssemblyName               = assemblyElem.AssemblyName.Trim();
            TaskInfo.TaskClass                      = assemblyElem.ClassName.Trim();
            TaskInfo.TaskData                       = txtTaskData.Text.Trim();
            TaskInfo.TaskName                       = txtTaskName.Text.Trim();
            TaskInfo.TaskEnabled                    = chkTaskEnabled.Checked;
            TaskInfo.TaskDeleteAfterLastRun         = chkTaskDeleteAfterLastRun.Checked;
            TaskInfo.TaskInterval                   = schedInterval.ScheduleInterval;
            TaskInfo.TaskDisplayName                = txtTaskDisplayName.Text.Trim();
            TaskInfo.TaskServerName                 = txtServerName.Text.Trim();
            TaskInfo.TaskRunInSeparateThread        = chkRunTaskInSeparateThread.Checked;
            TaskInfo.TaskUseExternalService         = chkTaskUseExternalService.Checked;
            TaskInfo.TaskCondition                  = ucMacroEditor.Text;
            TaskInfo.TaskRunIndividuallyForEachSite = chkRunIndividually.Checked;

            if (plcAllowExternalService.Visible)
            {
                TaskInfo.TaskAllowExternalService = chkTaskAllowExternalService.Checked;
            }
            if (plcUseExternalService.Visible)
            {
                TaskInfo.TaskUseExternalService = chkTaskUseExternalService.Checked;
            }

            TaskInfo.TaskNextRunTime = SchedulingHelper.GetFirstRunTime(SchedulingHelper.DecodeInterval(TaskInfo.TaskInterval));

            if (drpModule.Visible)
            {
                TaskInfo.TaskResourceID = ValidationHelper.GetInteger(drpModule.Value, 0);
            }

            TaskInfo.TaskUserID = ValidationHelper.GetInteger(ucUser.Value, 0);

            // Set synchronization to true (default is false for Scheduled task)
            TaskInfo.Generalized.StoreSettings();
            TaskInfo.Generalized.LogSynchronization = SynchronizationTypeEnum.LogSynchronization;
            TaskInfo.Generalized.LogIntegration     = true;
            TaskInfo.Generalized.LogEvents          = true;

            // If web farm support, create the tasks for all servers
            if (chkAllServers.Checked)
            {
                TaskInfoProvider.CreateWebFarmTasks(TaskInfo);
            }
            else
            {
                TaskInfoProvider.SetTaskInfo(TaskInfo);
            }

            // Restore original settings
            TaskInfo.Generalized.RestoreSettings();

            bool   notSystem = (TaskInfo == null) || (TaskInfo.TaskType != ScheduledTaskTypeEnum.System);
            string url       = UIContextHelper.GetElementUrl("CMS.ScheduledTasks", GetElementName(notSystem ? "EditTask" : "EditSystemTask"), true);

            // Add task ID and saved="1" query parameters
            url = URLHelper.AddParameterToUrl(String.Format(url, TaskInfo.TaskID), "saved", "1");

            // Add site ID query parameter and redirect to the finished URL
            URLHelper.Redirect(URLHelper.AddParameterToUrl(url, "siteid", SiteID.ToString()));
        }
    }
Пример #2
0
    /// <summary>
    /// Sets data to database.
    /// </summary>
    protected void btnOK_Click(object sender, EventArgs e)
    {
        // Check "modify" permission
        if (!CMSContext.CurrentUser.IsAuthorizedPerResource("CMS.ScheduledTasks", "Modify"))
        {
            RedirectToAccessDenied("CMS.ScheduledTasks", "Modify");
        }

        // Check required fields format
        string errorMessage = new Validator().NotEmpty(txtTaskDisplayName.Text, rfvDisplayName.ErrorMessage).NotEmpty(txtTaskName.Text, rfvName.ErrorMessage).NotEmpty(txtTaskAssemblyName.Text, rfvAssembly.ErrorMessage).NotEmpty(txtTaskClass.Text, rfvClass.ErrorMessage).
                              IsCodeName(txtTaskName.Text, GetString("Task_Edit.InvalidTaskName")).
                              IsCodeName(txtTaskAssemblyName.Text, GetString("Task_Edit.InvalidTaskAssemblyName")).
                              IsCodeName(txtTaskClass.Text, GetString("Task_Edit.InvalidTaskClass"))
                              .Result;

        if ((errorMessage == String.Empty) && !ScheduleInterval1.CheckIntervalPreceedings())
        {
            errorMessage = GetString("Task_Edit.BetweenIntervalPreceedingsError");
        }

        if ((errorMessage == String.Empty) && !ScheduleInterval1.CheckOneDayMinimum())
        {
            errorMessage = GetString("Task_Edit.AtLeastOneDayError");
        }

        // Checking date/time limit (SQL limit)
        if (errorMessage == String.Empty)
        {
            TaskInterval ti = SchedulingHelper.DecodeInterval(ScheduleInterval1.ScheduleInterval);
            if ((ti != null) && ((ti.StartTime < new DateTime(1755, 1, 1, 0, 0, 0)) ||
                                 (ti.StartTime > new DateTime(9997, 12, 31, 23, 59, 59))))
            {
                ti.StartTime = DateTime.Now;
                ScheduleInterval1.ScheduleInterval = SchedulingHelper.EncodeInterval(ti);
            }
        }

        if (errorMessage != "")
        {
            lblError.Visible = true;
            lblError.Text    = errorMessage;
        }
        else
        {
            // Check existing task name
            TaskInfo existingTask;
            if (si != null)
            {
                existingTask = TaskInfoProvider.GetTaskInfo(txtTaskName.Text.Trim(), si.SiteName);
            }
            else
            {
                existingTask = TaskInfoProvider.GetTaskInfo(txtTaskName.Text.Trim(), null);
            }

            if ((existingTask != null) && ((taskObj == null) || (existingTask.TaskID != taskObj.TaskID)))
            {
                lblError.Visible = true;
                lblError.Text    = GetString("Task_Edit.TaskNameExists").Replace("%%name%%", existingTask.TaskName);
                return;
            }

            if (taskObj == null)
            {
                // create new item -> insert
                taskObj            = new TaskInfo();
                taskObj.TaskSiteID = siteID;
                if (!developmentMode)
                {
                    taskObj.TaskAllowExternalService = true;
                }
            }

            taskObj.TaskAssemblyName        = txtTaskAssemblyName.Text.Trim();
            taskObj.TaskClass               = txtTaskClass.Text.Trim();
            taskObj.TaskData                = txtTaskData.Text.Trim();
            taskObj.TaskName                = txtTaskName.Text.Trim();
            taskObj.TaskEnabled             = chkTaskEnabled.Checked;
            taskObj.TaskDeleteAfterLastRun  = chkTaskDeleteAfterLastRun.Checked;
            taskObj.TaskInterval            = ScheduleInterval1.ScheduleInterval;
            taskObj.TaskDisplayName         = txtTaskDisplayName.Text.Trim();
            taskObj.TaskServerName          = txtServerName.Text.Trim();
            taskObj.TaskRunInSeparateThread = chkRunTaskInSeparateThread.Checked;
            taskObj.TaskUseExternalService  = chkTaskUseExternalService.Checked;
            if (plcAllowExternalService.Visible)
            {
                taskObj.TaskAllowExternalService = chkTaskAllowExternalService.Checked;
            }
            if (plcUseExternalService.Visible)
            {
                taskObj.TaskUseExternalService = chkTaskUseExternalService.Checked;
            }


            taskObj.TaskNextRunTime = SchedulingHelper.GetNextTime(taskObj.TaskInterval, new DateTime(), new DateTime());

            if (drpModule.Visible)
            {
                taskObj.TaskResourceID = ValidationHelper.GetInteger(drpModule.Value, 0);
            }

            // Set synchronization to true (default is false for Scheduled task)
            taskObj.Generalized.LogSynchronization = SynchronizationTypeEnum.LogSynchronization;
            taskObj.Generalized.LogIntegration     = true;
            taskObj.Generalized.LogEvents          = true;

            // If web farm support, create the tasks for all servers
            if (chkAllServers.Checked)
            {
                TaskInfoProvider.CreateWebFarmTasks(taskObj);
            }
            else
            {
                TaskInfoProvider.SetTaskInfo(taskObj);
            }

            if (cmsdesk)
            {
                URLHelper.Redirect("Task_Edit.aspx?taskname=" + taskObj.TaskID + "&saved=1&siteid=" + siteID);
            }
            else
            {
                URLHelper.Redirect("Task_Edit.aspx?taskname=" + taskObj.TaskID + "&saved=1&selectedsiteid=" + siteID);
            }
        }
    }