public static AgentJobInfo ConvertToAgentJobInfo(JobProperties job)
 {
     return(new AgentJobInfo
     {
         Name = job.Name,
         Description = job.Description,
         CurrentExecutionStatus = (Contracts.JobExecutionStatus)job.CurrentExecutionStatus,
         LastRunOutcome = (Contracts.CompletionResult)job.LastRunOutcome,
         CurrentExecutionStep = job.CurrentExecutionStep,
         Enabled = job.Enabled,
         HasTarget = job.HasTarget,
         HasSchedule = job.HasSchedule,
         HasStep = job.HasStep,
         Runnable = job.Runnable,
         Category = job.Category,
         CategoryId = job.CategoryID,
         CategoryType = job.CategoryType,
         LastRun = job.LastRun != null?job.LastRun.ToString() : string.Empty,
                       NextRun = job.NextRun != null?job.NextRun.ToString() : string.Empty,
                                     JobId = job.JobID != null?job.JobID.ToString() : null,
                                                 OperatorToEmail = job.OperatorToEmail,
                                                 OperatorToPage = job.OperatorToPage,
                                                 StartStepId = job.StartStepID,
                                                 EmailLevel = job.EmailLevel,
                                                 PageLevel = job.PageLevel,
                                                 EventLogLevel = job.EventLogLevel,
                                                 DeleteLevel = job.DeleteLevel,
                                                 Owner = job.Owner
     });
 }
        /// <summary>
        /// Check if name filter specified matches given jobproperty
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="jobProperties"></param>
        /// <returns></returns>
        private bool CheckIfNameMatchesJob(JobActivityFilter filter, JobProperties jobProperties)
        {
            bool isNameMatched = false;

            //
            // job name (can be comma-separated list)
            // we count it as a match if the job name contains
            // a case-insensitive match for any of the filter strings.
            //
            if (filter.Name.Length > 0)
            {
                string   jobname  = jobProperties.Name.ToLower(CultureInfo.CurrentCulture);
                string[] jobNames = filter.Name.ToLower(CultureInfo.CurrentCulture).Split(',');
                int      length   = jobNames.Length;

                for (int j = 0; j < length; ++j)
                {
                    if (jobname.IndexOf(jobNames[j].Trim(), StringComparison.Ordinal) > -1)
                    {
                        isNameMatched = true;
                        break;
                    }
                }
            }
            else
            {
                // No name filter was specified
                isNameMatched = true;
            }

            return(isNameMatched);
        }
        /// <summary>
        /// Check if job enabled status matches job
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="jobProperties"></param>
        /// <returns></returns>
        private bool CheckIfEnabledStatusMatchesJob(JobActivityFilter filter, JobProperties jobProperties)
        {
            bool isEnabledMatched = false;

            // apply filter - if job was enabled or not
            switch (filter.Enabled)
            {
            // if All was selected, include in match
            case EnumThreeState.All:
                isEnabledMatched = true;
                break;

            // if Yes was selected, include only if job has schedule
            case EnumThreeState.Yes:
                if (jobProperties.Enabled)
                {
                    isEnabledMatched = true;
                }
                break;

            // if Yes was selected, include only if job does not have schedule
            case EnumThreeState.No:
                if (!jobProperties.Enabled)
                {
                    isEnabledMatched = true;
                }
                break;
            }

            return(isEnabledMatched);
        }
        /// <summary>
        /// Check if a category matches given jobproperty
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="jobProperties"></param>
        /// <returns></returns>
        private bool CheckIfCategoryMatchesJob(JobActivityFilter filter, JobProperties jobProperties)
        {
            bool isCategoryMatched = false;

            // Apply category filter if specified
            if (filter.Category.Length > 0)
            {
                //
                // we count it as a match if the job category contains
                // a case-insensitive match for the filter string.
                //
                string jobCategory = jobProperties.Category.ToLower(CultureInfo.CurrentCulture);
                if (String.Compare(jobCategory, filter.Category.Trim().ToLower(CultureInfo.CurrentCulture), StringComparison.Ordinal) == 0)
                {
                    isCategoryMatched = true;
                }
            }
            else
            {
                // No category filter was specified
                isCategoryMatched = true;
            }

            return(isCategoryMatched);
        }
        /// <summary>
        /// check if job runnable status in filter matches given job property
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="jobProperties"></param>
        /// <returns></returns>
        private bool CheckJobRunnableStatusMatchesJob(JobActivityFilter filter, JobProperties jobProperties)
        {
            bool isRunnableMatched = false;

            // filter based on job runnable
            switch (filter.Runnable)
            {
            // if All was selected, include in match
            case EnumThreeState.All:
                isRunnableMatched = true;
                break;

            // if Yes was selected, include only if job that is runnable
            case EnumThreeState.Yes:
                if (jobProperties.Runnable)
                {
                    isRunnableMatched = true;
                }
                break;

            // if Yes was selected, include only if job is not runnable
            case EnumThreeState.No:
                if (!jobProperties.Runnable)
                {
                    isRunnableMatched = true;
                }
                break;
            }
            return(isRunnableMatched);
        }
        /// <summary>
        /// Fetch jobs for a given Urn
        /// </summary>
        /// <param name="urn"></param>
        /// <returns></returns>
        public Dictionary <Guid, JobProperties> FetchJobs(string urn)
        {
            if (String.IsNullOrEmpty(urn))
            {
                throw new ArgumentNullException("urn");
            }

            Request request = new Request();

            request.Urn    = urn;
            request.Fields = new string[]
            {
                "Name",
                "IsEnabled",
                "Category",
                "CategoryID",
                "CategoryType",
                "CurrentRunStatus",
                "CurrentRunStep",
                "HasSchedule",
                "HasStep",
                "HasServer",
                "LastRunOutcome",
                "JobID",
                "Description",
                "LastRunDate",
                "NextRunDate",
                "OperatorToEmail",
                "OperatorToNetSend",
                "OperatorToPage",
                "OwnerLoginName",
                "PageLevel",
                "StartStepID",
                "NetSendLevel",
                "EventLogLevel",
                "EmailLevel",
                "DeleteLevel"
            };

            DataTable dt      = enumerator.Process(connection, request);
            int       numJobs = dt.Rows.Count;

            if (numJobs == 0)
            {
                return(null);
            }

            Dictionary <Guid, JobProperties> foundJobs = new Dictionary <Guid, JobProperties>(numJobs);

            for (int i = 0; i < numJobs; ++i)
            {
                JobProperties jobProperties = new JobProperties(dt.Rows[i]);
                foundJobs.Add(jobProperties.JobID, jobProperties);
            }

            return(foundJobs);
        }
        /// <summary>
        /// Check if next run date for given job property is greater than the one specified in the filter
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="jobProperties"></param>
        /// <returns></returns>
        private bool CheckifNextRunDateIsGreater(JobActivityFilter filter, JobProperties jobProperties)
        {
            bool isNextRunOutDateMatched = false;

            // filter next run date
            if (filter.NextRunDate.Ticks == 0 ||
                jobProperties.NextRun >= filter.NextRunDate)
            {
                isNextRunOutDateMatched = true;
            }
            return(isNextRunOutDateMatched);
        }
        /// <summary>
        /// Check if job status filter matches given jobproperty
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="jobProperties"></param>
        /// <returns></returns>
        private bool CheckIfJobStatusMatchesJob(JobActivityFilter filter, JobProperties jobProperties)
        {
            bool isStatusMatched = false;

            // filter - job run status
            if (filter.Status == EnumStatus.All ||
                jobProperties.CurrentExecutionStatus == (int)filter.Status)
            {
                isStatusMatched = true;
            }

            return(isStatusMatched);
        }
        /// <summary>
        /// check if last run status filter matches given job property
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="jobProperties"></param>
        /// <returns></returns>
        private bool CheckIfLastRunOutcomeMatchesJob(JobActivityFilter filter, JobProperties jobProperties)
        {
            bool isLastRunOutcomeMatched = false;

            // filter - last run outcome
            if (filter.LastRunOutcome == EnumCompletionResult.All ||
                jobProperties.LastRunOutcome == (int)filter.LastRunOutcome)
            {
                isLastRunOutcomeMatched = true;
            }

            return(isLastRunOutcomeMatched);
        }
 public static AgentJobInfo ConvertToAgentJobInfo(JobProperties job)
 {
     return(new AgentJobInfo
     {
         Name = job.Name,
         CurrentExecutionStatus = job.CurrentExecutionStatus,
         LastRunOutcome = job.LastRunOutcome,
         CurrentExecutionStep = job.CurrentExecutionStep,
         Enabled = job.Enabled,
         HasTarget = job.HasTarget,
         HasSchedule = job.HasSchedule,
         HasStep = job.HasStep,
         Runnable = job.Runnable,
         Category = job.Category,
         CategoryId = job.CategoryID,
         CategoryType = job.CategoryType,
         LastRun = job.LastRun != null?job.LastRun.ToString() : string.Empty,
                       NextRun = job.NextRun != null?job.NextRun.ToString() : string.Empty,
                                     JobId = job.JobID != null?job.JobID.ToString() : null
     });
 }