Example #1
0
        public JobQueueToken UpdateQueue(JobQueue JobQueue)
        {
            var token = JobQueueToken.FromJobQueue(JobQueue);
            JobQueueToken existingToken;

            if (_Cache.TryGetValue(JobQueue.Id, out existingToken))
            {
                if (_Cache.TryUpdate(JobQueue.Id, token, existingToken))
                {
                    if (existingToken.JobQueue.SubjectIds != token.JobQueue.SubjectIds)
                        CalculateSubjectCache();

                    return token;
                }
                else
                    return null;
            }
            else
            {
                if (_Cache.TryAdd(JobQueue.Id, token))
                {
                    CalculateSubjectCache();
                    return token;
                }
                else
                    return null;
            }
        }
Example #2
0
        public static JobQueueToken UpdateJobQueue(DiscoDataContext Database, JobQueue JobQueue)
        {
            // Verify
            if (string.IsNullOrWhiteSpace(JobQueue.Name))
                throw new ArgumentException("The Job Queue Name is required");

            // Name Unique
            if (_cache.GetQueues().Any(q => q.JobQueue.Id != JobQueue.Id && q.JobQueue.Name == JobQueue.Name))
                throw new ArgumentException("Another Job Queue already exists with that name", "JobQueue");

            // Sanitize Subject Ids
            if (string.IsNullOrWhiteSpace(JobQueue.SubjectIds))
            {
                JobQueue.SubjectIds = null;
            }
            else
            {
                var subjectIds = JobQueue.SubjectIds.Split(',');
                foreach (var subjectId in subjectIds)
                {
                    UserService.GetUser(subjectId, Database);
                }
                JobQueue.SubjectIds = string.Join(",", Database.Users.Where(u => subjectIds.Contains(u.UserId)).Select(u => u.UserId));
            }

            Database.SaveChanges();

            return _cache.UpdateQueue(JobQueue);
        }
Example #3
0
        public static JobQueueToken FromJobQueue(JobQueue JobQueue)
        {
            string[] sg = (JobQueue.SubjectIds == null ? new string[0] : JobQueue.SubjectIds.Split(',').ToArray());

            return new JobQueueToken()
            {
                JobQueue = JobQueue,
                SubjectIdHashes = new HashSet<string>(sg.Select(i => i.ToLower())),
                SubjectIds = sg.ToList().AsReadOnly()
            };
        }
Example #4
0
        public static JobQueueToken CreateJobQueue(DiscoDataContext Database, JobQueue JobQueue)
        {
            // Verify
            if (string.IsNullOrWhiteSpace(JobQueue.Name))
                throw new ArgumentException("The Job Queue Name is required");

            // Name Unique
            if (_cache.GetQueues().Any(q => q.JobQueue.Name == JobQueue.Name))
                throw new ArgumentException("Another Job Queue already exists with that name", "JobQueue");

            // Sanitize Subject Ids
            if (string.IsNullOrWhiteSpace(JobQueue.SubjectIds))
            {
                JobQueue.SubjectIds = null;
            }
            else
            {
                var subjectIds = JobQueue.SubjectIds.Split(',');
                foreach (var subjectId in subjectIds)
                {
                    UserService.GetUser(subjectId, Database);
                }
                JobQueue.SubjectIds = string.Join(",", Database.Users.Where(u => subjectIds.Contains(u.UserId)).Select(u => u.UserId));
            }

            // Clone to break reference
            var queue = new JobQueue()
            {
                Name = JobQueue.Name,
                Description = JobQueue.Description,
                Icon = JobQueue.Icon,
                IconColour = JobQueue.IconColour,
                DefaultSLAExpiry = JobQueue.DefaultSLAExpiry,
                Priority = JobQueue.Priority,
                SubjectIds = JobQueue.SubjectIds
            };

            Database.JobQueues.Add(queue);
            Database.SaveChanges();

            return _cache.UpdateQueue(queue);
        }
Example #5
0
        private void UpdateSubjects(JobQueue jobQueue, string[] Subjects)
        {
            string subjectIds = null;

            // Validate Subjects
            if (Subjects != null && Subjects.Length > 0)
            {
                var subjects = Subjects
                    .Where(s => !string.IsNullOrWhiteSpace(s))
                    .Select(s => s.Trim())
                    .Select(s => Tuple.Create(s, ActiveDirectory.RetrieveADObject(s, Quick: true)))
                    .Where(s => s.Item2 is ADUserAccount || s.Item2 is ADGroup)
                    .ToList();
                var invalidSubjects = subjects.Where(s => s.Item2 == null).ToList();

                if (invalidSubjects.Count > 0)
                    throw new ArgumentException(string.Format("Subjects not found: {0}", string.Join(", ", invalidSubjects)), "Subjects");

                var proposedSubjects = subjects.Select(s => s.Item2.Id).OrderBy(s => s).ToArray();

                subjectIds = string.Join(",", proposedSubjects);

                if (string.IsNullOrEmpty(subjectIds))
                    subjectIds = null;
            }

            if (jobQueue.SubjectIds != subjectIds)
            {
                jobQueue.SubjectIds = subjectIds;
                JobQueueService.UpdateJobQueue(Database, jobQueue);
            }
        }
Example #6
0
        private void UpdateDefaultSLAExpiry(JobQueue jobQueue, string DefaultSLAExpiry)
        {
            int? defaultSLAExpiry = null;

            if (!string.IsNullOrEmpty(DefaultSLAExpiry))
            {
                int intValue;

                if (!int.TryParse(DefaultSLAExpiry, out intValue))
                    throw new ArgumentException("Invalid Default SLA Expiry Value", "DefaultSLAPriority");

                if (intValue < 0)
                    throw new ArgumentException("Default SLA Expiry Value must be greater than zero", "DefaultSLAPriority");

                // if intValue == 0, then no SLA.
                if (intValue > 0)
                    defaultSLAExpiry = intValue;
            }

            jobQueue.DefaultSLAExpiry = defaultSLAExpiry;
            JobQueueService.UpdateJobQueue(Database, jobQueue);
        }
Example #7
0
        private void UpdatePriority(JobQueue jobQueue, string Priority)
        {
            JobQueuePriority priority;

            if (!Enum.TryParse<JobQueuePriority>(Priority, out priority))
                throw new ArgumentException("Invalid Priority Value", "Priority");

            jobQueue.Priority = priority;
            JobQueueService.UpdateJobQueue(Database, jobQueue);
        }
Example #8
0
 private void UpdateDescription(JobQueue jobQueue, string Description)
 {
     jobQueue.Description = Description;
     JobQueueService.UpdateJobQueue(Database, jobQueue);
 }
Example #9
0
 private void UpdateName(JobQueue jobQueue, string Name)
 {
     jobQueue.Name = Name;
     JobQueueService.UpdateJobQueue(Database, jobQueue);
 }
Example #10
0
        private void UpdateIconColour(JobQueue jobQueue, string IconColour)
        {
            if (string.IsNullOrWhiteSpace(IconColour))
                throw new ArgumentNullException("IconColour");

            jobQueue.IconColour = IconColour;
            JobQueueService.UpdateJobQueue(Database, jobQueue);
        }
Example #11
0
        public static JobQueueJob OnAddQueue(this Job j, DiscoDataContext Database, JobQueue jq, User Technician, string Comment, DateTime? SLAExpires, JobQueuePriority Priority)
        {
            if (!j.CanAddQueue(jq))
                throw new InvalidOperationException("Adding job to queue is denied");

            if (SLAExpires.HasValue && SLAExpires.Value < DateTime.Now)
                throw new ArgumentException("The SLA Date must be greater than the current time", "SLAExpires");

            var jqj = new JobQueueJob()
            {
                JobQueueId = jq.Id,
                JobId = j.Id,
                AddedDate = DateTime.Now,
                AddedUserId = Technician.UserId,
                AddedComment = string.IsNullOrWhiteSpace(Comment) ? null : Comment.Trim(),
                SLAExpiresDate = SLAExpires,
                Priority = Priority
            };

            Database.JobQueueJobs.Add(jqj);
            return jqj;
        }
Example #12
0
        public static bool CanAddQueue(this Job j, JobQueue jq)
        {
            // Shortcut
            if (!j.CanAddQueues())
                return false;

            // Already in Queue?
            if (j.JobQueues.Any(jjq => !jjq.RemovedDate.HasValue && jjq.JobQueueId == jq.Id))
                return false;

            // Can add ANY queue
            if (UserService.CurrentAuthorization.Has(Claims.Job.Actions.AddAnyQueues))
                return true;

            // Can add OWN queue
            if (UserService.CurrentAuthorization.Has(Claims.Job.Actions.AddOwnQueues))
            {
                return JobQueueService.UsersQueues(UserService.CurrentUser).Any(q => q.JobQueue.Id == jq.Id);
            }

            return false;
        }