Пример #1
0
        public TaskCreationStatus AddModifyTask(TaskCreationDTO task)
        {
            if (task.EndPeriod.HasValue && task.EndPeriod.Value < task.StartPeriod)
            {
                return(TaskCreationStatus.EndPeriodEarlierThanStartPeriod);
            }

            if (!task.GenreId.HasValue)
            {
                return(TaskCreationStatus.GenreNotSelected);
            }
            if (!task.StatusId.HasValue)
            {
                return(TaskCreationStatus.StatusNotSelected);
            }
            if (!task.PriorityId.HasValue)
            {
                return(TaskCreationStatus.PriorityNotSelected);
            }

            if (!task.ParticipantsIds.Any())
            {
                return(TaskCreationStatus.PatricipantsNotSelected);
            }

            var textFields = new List <string> {
                task.Topic, task.Content
            };

            if (textFields.Any(t => string.IsNullOrWhiteSpace(t)))
            {
                return(TaskCreationStatus.ElementEmpty);
            }

            using (var session = Hibernate.SessionFactory.OpenSession())
            {
                var closedTaskStatusId = session.QueryOver <TaskStatus>()
                                         .Where(s => s.Name == Constants.TaskStatus_Closed)
                                         .Select(s => s.Id)
                                         .SingleOrDefault <int>();

                if (task.StatusId.HasValue && task.StatusId.Value == closedTaskStatusId && !task.EndPeriod.HasValue)
                {
                    return(TaskCreationStatus.ClosedOrCanceledWithNoEndPeriod);
                }

                using (var transaction = session.BeginTransaction())
                {
                    Task taskEntity = task.Id.HasValue
                        ? session.QueryOver <Task>().Where(t => t.Id == task.Id.Value).SingleOrDefault()
                        : new Task();

                    taskEntity.Topic       = task.Topic;
                    taskEntity.Content     = task.Content;
                    taskEntity.Author      = task.Author;
                    taskEntity.StartPeriod = task.StartPeriod;
                    taskEntity.EndPeriod   = task.EndPeriod;
                    taskEntity.Status      = session.QueryOver <TaskStatus>().Where(s => s.Id == task.StatusId.Value).SingleOrDefault();
                    taskEntity.Genre       = session.QueryOver <TaskGenre>().Where(g => g.Id == task.GenreId.Value).SingleOrDefault();
                    taskEntity.Priority    = session.QueryOver <TaskPriority>().Where(p => p.Id == task.PriorityId.Value).SingleOrDefault();
                    taskEntity.Users       = session.QueryOver <User>().Where(Res.In("Id", task.ParticipantsIds.ToArray())).List();

                    if (task.Id.HasValue)
                    {
                        IList <TaskComment> currentCommentsEntitities = session.QueryOver <TaskComment>()
                                                                        .JoinQueryOver(c => c.Task)
                                                                        .Where(t => t.Id == task.Id.Value)
                                                                        .List();

                        foreach (var comment in currentCommentsEntitities)
                        {
                            session.Delete(comment);
                            session.Flush();
                        }
                    }

                    var commentsEntities = new List <TaskComment>();
                    foreach (var c in task.Comments.OrderBy(c => c.Date))
                    {
                        commentsEntities.Add(new TaskComment
                        {
                            Content = c.Content,
                            Date    = c.Date,
                            User    = session.QueryOver <User>().Where(u => u.Id == c.AuthorId).SingleOrDefault(),
                            Task    = taskEntity
                        });
                    }

                    taskEntity.Comments = commentsEntities;

                    session.SaveOrUpdate(taskEntity);
                    session.Flush();
                    transaction.Commit();

                    return(task.Id.HasValue ? TaskCreationStatus.Modified : TaskCreationStatus.Added);
                }
            }
        }
Пример #2
0
        public TaskCreationDTO GetSelectedTask(int taskId)
        {
            using (var session = Hibernate.SessionFactory.OpenSession())
            {
                Task         t = null;
                TaskPriority p = null;
                TaskStatus   s = null;
                TaskGenre    g = null;

                TaskCreationDTO taskDTO = null;

                var task = session.QueryOver(() => t)
                           .JoinAlias(() => t.Status, () => s)
                           .JoinAlias(() => t.Priority, () => p)
                           .JoinAlias(() => t.Genre, () => g)
                           .Where(() => t.Id == taskId)
                           .SelectList(l => l
                                       .Select(() => t.Id).WithAlias(() => taskDTO.Id)
                                       .Select(() => t.Topic).WithAlias(() => taskDTO.Topic)
                                       .Select(() => t.Content).WithAlias(() => taskDTO.Content)
                                       .Select(() => t.Author).WithAlias(() => taskDTO.Author)
                                       .Select(() => t.StartPeriod).WithAlias(() => taskDTO.StartPeriod)
                                       .Select(() => t.EndPeriod).WithAlias(() => taskDTO.EndPeriod)
                                       .Select(() => s.Id).WithAlias(() => taskDTO.StatusId)
                                       .Select(() => p.Id).WithAlias(() => taskDTO.PriorityId)
                                       .Select(() => g.Id).WithAlias(() => taskDTO.GenreId))
                           .TransformUsing(Transformers.AliasToBean <TaskCreationDTO>())
                           .SingleOrDefault <TaskCreationDTO>();

                TaskComment tc = null;
                User        u  = null;

                TaskCommentCreationDTO commentDTO = null;

                var comments = session.QueryOver(() => tc)
                               .JoinAlias(() => tc.Task, () => t)
                               .JoinAlias(() => tc.User, () => u)
                               .Where(() => t.Id == taskId)
                               .SelectList(l => l
                                           .Select(() => tc.Id).WithAlias(() => commentDTO.Id)
                                           .Select(() => tc.Content).WithAlias(() => commentDTO.Content)
                                           .Select(() => tc.Date).WithAlias(() => commentDTO.Date)
                                           .Select(() => u.Id).WithAlias(() => commentDTO.AuthorId)
                                           .Select(() => u.Username).WithAlias(() => commentDTO.AuthorUsername)
                                           .Select(() => u.Name).WithAlias(() => commentDTO.AuthorName)
                                           .Select(() => u.Surname).WithAlias(() => commentDTO.AuthorSurname)
                                           .Select(() => t.Id).WithAlias(() => commentDTO.TaskId))
                               .OrderBy(() => tc.Date).Desc
                               .TransformUsing(Transformers.AliasToBean <TaskCommentCreationDTO>())
                               .List <TaskCommentCreationDTO>();

                task.Comments = comments;

                var participants = session.QueryOver(() => t)
                                   .JoinAlias(() => t.Users, () => u)
                                   .Where(() => t.Id == taskId)
                                   .SelectList(l => l
                                               .Select(() => u.Id))
                                   .List <int>();

                task.ParticipantsIds = participants;

                return(task);
            }
        }