Esempio n. 1
0
        public int GetTasksPagesCount(int pageSize, TaskSearchCriteria criteria)
        {
            using (var session = Hibernate.SessionFactory.OpenSession())
            {
                Task         t  = null;
                TaskStatus   s  = null;
                TaskPriority p  = null;
                TaskGenre    g  = null;
                TaskComment  c  = null;
                User         uc = null;

                var query = session.QueryOver(() => t)
                            .JoinAlias(() => t.Status, () => s)
                            .JoinAlias(() => t.Priority, () => p)
                            .JoinAlias(() => t.Genre, () => g)
                            .Left.JoinAlias(() => t.Comments, () => c)
                            .Left.JoinAlias(() => c.User, () => uc);

                if (criteria.TaskAuthorUsername.IsNotNull())
                {
                    query.Where(() => t.Author == criteria.TaskAuthorUsername);
                }
                if (criteria.ShowTasksOnly)
                {
                    query.Where(() => g.Id == 4);
                }
                if (criteria.ShowInstallationsOnly)
                {
                    query.Where(() => g.Id == 2);
                }
                if (criteria.ShowTonersOnly)
                {
                    query.Where(() => g.Id == 3);
                }
                if (criteria.ShowUpdatesOnly)
                {
                    query.Where(() => g.Id == 1);
                }
                if (criteria.ShowWithoutCanceledTasks)
                {
                    query.Where(Res.Not(Res.Eq(Pro.Property("s.Id"), 1)));
                }
                if (criteria.TaskId.IsNotNull())
                {
                    query.Where(() => t.Id == criteria.TaskId.Value);
                }
                if (criteria.Topic.IsNotNull())
                {
                    query.Where(Res.InsensitiveLike(Pro.Property("t.Topic"), criteria.Topic, MatchMode.Anywhere));
                }
                if (criteria.Content.IsNotNull())
                {
                    query.Where(Res.InsensitiveLike(Pro.Property("t.Content"), criteria.Content, MatchMode.Anywhere));
                }
                if (criteria.StartDate.IsNotNull())
                {
                    query.Where(Res.Eq(Pro.Cast(SqlType.Date, Pro.Property("t.StartPeriod")), criteria.StartDate.Value));
                }
                if (criteria.StartHour.IsNotNull())
                {
                    query.Where(Res.Eq(Pro.Cast(SqlType.Time, Pro.Property("t.StartPeriod")), criteria.StartHour.Value.ToString()));
                }
                if (criteria.EndDate.IsNotNull())
                {
                    query.Where(Res.Eq(Pro.Cast(SqlType.Date, Pro.Property("t.EndPeriod")), criteria.EndDate.Value));
                }
                if (criteria.EndDate.IsNotNull())
                {
                    query.Where(Res.Eq(Pro.Cast(SqlType.Time, Pro.Property("t.EndPeriod")), criteria.EndHour.Value.ToString()));
                }
                if (criteria.TaskPriorityId.IsNotNull())
                {
                    query.Where(() => p.Id == criteria.TaskPriorityId);
                }
                if (criteria.TaskStatusId.IsNotNull())
                {
                    query.Where(() => s.Id == criteria.TaskStatusId);
                }

                if (criteria.TaskParticipantId.IsNotNull())
                {
                    Task subT = null;
                    User subU = null;

                    var subquery = QueryOver.Of(() => subT)
                                   .JoinAlias(() => subT.Users, () => subU)
                                   .Where(Res.And(
                                              Res.EqProperty(Pro.Property("t.Id"), Pro.Property("subT.Id")),
                                              Res.Eq(Pro.Property("subU.Id"), criteria.TaskParticipantId)))
                                   .SelectList(l => l
                                               .Select(() => subU.Id));

                    query.WithSubquery.WhereExists(subquery);
                }


                if (criteria.Comment.IsNotNull())
                {
                    TaskComment subC = null;
                    Task        subT = null;

                    var subquery = QueryOver.Of(() => subC)
                                   .JoinAlias(() => subC.Task, () => subT)
                                   .Where(Res.And(
                                              Res.EqProperty(Pro.Property("subT.Id"), Pro.Property("t.Id")),
                                              Res.InsensitiveLike(Pro.Property("subC.Content"), criteria.Comment, MatchMode.Anywhere)))
                                   .SelectList(l => l
                                               .Select(() => subT.Id));

                    query.WithSubquery.WhereExists(subquery);
                }

                if (criteria.CommentAuthorId.IsNotNull())
                {
                    TaskComment subC = null;
                    Task        subT = null;
                    User        subU = null;

                    var subquery = QueryOver.Of(() => subC)
                                   .JoinAlias(() => subC.Task, () => subT)
                                   .JoinAlias(() => subC.User, () => subU)
                                   .Where(Res.And(
                                              Res.EqProperty(Pro.Property("subT.Id"), Pro.Property("t.Id")),
                                              Res.Eq(Pro.Property("subU.Id"), criteria.CommentAuthorId)))
                                   .SelectList(l => l
                                               .Select(() => subT.Id));

                    query.WithSubquery.WhereExists(subquery);
                }

                var result = Math.Ceiling((1.0 * query.RowCount()) / pageSize);
                return(result == 0 ? 1 : Convert.ToInt32(result));
            }
        }
Esempio n. 2
0
        public IList <TaskPrimaryDataDTO> GetTasks(int pageNo, int pageSize, TaskSearchCriteria criteria)
        {
            using (var session = Hibernate.SessionFactory.OpenSession())
            {
                Task         t     = null;
                TaskStatus   s     = null;
                TaskPriority p     = null;
                TaskGenre    g     = null;
                TaskComment  c     = null;
                TaskComment  c_sub = null;
                User         uc    = null;

                var query = session.QueryOver(() => t)
                            .JoinAlias(() => t.Status, () => s)
                            .JoinAlias(() => t.Priority, () => p)
                            .JoinAlias(() => t.Genre, () => g)
                            .Left.JoinAlias(() => t.Comments, () => c)
                            .Left.JoinAlias(() => c.User, () => uc)
                            .Where(
                    Res.EqProperty(
                        Pro.Conditional(Res.IsNull("c.Id"), Pro.Constant(0), Pro.Property("c.Id")),
                        Pro.SubQuery(QueryOver.Of(() => c_sub)
                                     .Where(() => c_sub.Task.Id == c.Task.Id)
                                     .Select(Pro.Conditional(Res.IsNull("c_sub.Id"), Pro.Constant(0), Pro.Max("c_sub.Id"))))
                        ));

                if (criteria.TaskAuthorUsername.IsNotNull())
                {
                    query.Where(() => t.Author == criteria.TaskAuthorUsername);
                }
                if (criteria.ShowTasksOnly)
                {
                    query.Where(() => g.Id == 4);
                }
                if (criteria.ShowInstallationsOnly)
                {
                    query.Where(() => g.Id == 2);
                }
                if (criteria.ShowTonersOnly)
                {
                    query.Where(() => g.Id == 3);
                }
                if (criteria.ShowUpdatesOnly)
                {
                    query.Where(() => g.Id == 1);
                }
                if (criteria.ShowWithoutCanceledTasks)
                {
                    query.Where(Res.Not(Res.Eq(Pro.Property("s.Id"), 1)));
                }
                if (criteria.TaskId.IsNotNull())
                {
                    query.Where(() => t.Id == criteria.TaskId.Value);
                }
                if (criteria.Topic.IsNotNull())
                {
                    query.Where(Res.InsensitiveLike(Pro.Property("t.Topic"), criteria.Topic, MatchMode.Anywhere));
                }
                if (criteria.Content.IsNotNull())
                {
                    query.Where(Res.InsensitiveLike(Pro.Property("t.Content"), criteria.Content, MatchMode.Anywhere));
                }
                if (criteria.StartDate.IsNotNull())
                {
                    query.Where(Res.Eq(Pro.Cast(SqlType.Date, Pro.Property("t.StartPeriod")), criteria.StartDate.Value));
                }
                if (criteria.StartHour.IsNotNull())
                {
                    query.Where(Res.Eq(Pro.Cast(SqlType.Time, Pro.Property("t.StartPeriod")), criteria.StartHour.Value.ToString()));
                }
                if (criteria.EndDate.IsNotNull())
                {
                    query.Where(Res.Eq(Pro.Cast(SqlType.Date, Pro.Property("t.EndPeriod")), criteria.EndDate.Value));
                }
                if (criteria.EndDate.IsNotNull())
                {
                    query.Where(Res.Eq(Pro.Cast(SqlType.Time, Pro.Property("t.EndPeriod")), criteria.EndHour.Value.ToString()));
                }
                if (criteria.TaskPriorityId.IsNotNull())
                {
                    query.Where(() => p.Id == criteria.TaskPriorityId);
                }
                if (criteria.TaskStatusId.IsNotNull())
                {
                    query.Where(() => s.Id == criteria.TaskStatusId);
                }

                if (criteria.TaskParticipantId.IsNotNull())
                {
                    Task subT = null;
                    User subU = null;

                    var subquery = QueryOver.Of(() => subT)
                                   .JoinAlias(() => subT.Users, () => subU)
                                   .Where(Res.And(
                                              Res.EqProperty(Pro.Property("t.Id"), Pro.Property("subT.Id")),
                                              Res.Eq(Pro.Property("subU.Id"), criteria.TaskParticipantId)))
                                   .SelectList(l => l
                                               .Select(() => subU.Id));

                    query.WithSubquery.WhereExists(subquery);
                }

                if (criteria.Comment.IsNotNull())
                {
                    TaskComment subC = null;
                    Task        subT = null;

                    var subquery = QueryOver.Of(() => subC)
                                   .JoinAlias(() => subC.Task, () => subT)
                                   .Where(Res.And(
                                              Res.EqProperty(Pro.Property("subT.Id"), Pro.Property("t.Id")),
                                              Res.InsensitiveLike(Pro.Property("subC.Content"), criteria.Comment, MatchMode.Anywhere)))
                                   .SelectList(l => l
                                               .Select(() => subT.Id));

                    query.WithSubquery.WhereExists(subquery);
                }

                if (criteria.CommentAuthorId.IsNotNull())
                {
                    TaskComment subC = null;
                    Task        subT = null;
                    User        subU = null;

                    var subquery = QueryOver.Of(() => subC)
                                   .JoinAlias(() => subC.Task, () => subT)
                                   .JoinAlias(() => subC.User, () => subU)
                                   .Where(Res.And(
                                              Res.EqProperty(Pro.Property("subT.Id"), Pro.Property("t.Id")),
                                              Res.Eq(Pro.Property("subU.Id"), criteria.CommentAuthorId)))
                                   .SelectList(l => l
                                               .Select(() => subT.Id));

                    query.WithSubquery.WhereExists(subquery);
                }

                query.SelectList(l => l
                                 .Select(() => t.Id)
                                 .Select(() => t.Topic)
                                 .Select(() => t.Content)
                                 .Select(() => t.StartPeriod)
                                 .Select(() => t.EndPeriod)
                                 .Select(() => c.Id)
                                 .Select(() => c.Content)
                                 .Select(() => c.Date)
                                 .Select(() => uc.Username)
                                 .Select(() => s.Name)
                                 .Select(() => p.Name)
                                 .Select(() => g.Name)
                                 .Select(() => t.Author))
                .OrderBy(() => t.Id).Desc
                .TransformUsing(Transformers.AliasToBeanConstructor(typeof(TaskPrimaryDataDTO).GetConstructors()[1]))
                .Skip(pageSize * (pageNo - 1)).Take(pageSize);

                return(query.List <TaskPrimaryDataDTO>());

                #region Above generates sql like...

                /*
                 *  SELECT this_.Id AS y0_,
                 *         this_.Topic AS y1_,
                 *         this_.StartPeriod AS y2_,
                 *         this_.EndPeriod AS y3_,
                 *         c4_.Id AS y4_,
                 *         c4_.Content AS y5_,
                 *         c4_.Date AS y6_,
                 *         u5_.Username AS y7_,
                 *         s1_.Name AS y8_,
                 *         p2_.Name AS y9_,
                 *         g3_.Name AS y10_
                 *  FROM `Task` this_
                 *      LEFT OUTER JOIN `TaskComment` c4_ ON this_.Id=c4_.Task_id
                 *      LEFT OUTER JOIN `User` u5_ ON c4_.User_id=u5_.Id
                 *      INNER JOIN `TaskGenre` g3_ ON this_.Genre_id=g3_.Id
                 *      INNER JOIN `TaskStatus` s1_ ON this_.Status_id=s1_.Id
                 *      INNER JOIN `TaskPriority` p2_ ON this_.Priority_id=p2_.Id
                 *  WHERE (CASE WHEN c4_.Id IS NULL THEN ?p0 ELSE c4_.Id END) =
                 *        (SELECT (CASE WHEN this_0_.Id IS NULL THEN ?p1 ELSE max(this_0_.Id) END) AS y0_ FROM `TaskComment` this_0_)
                 *  ORDER BY this_.Id DESC
                 *  LIMIT ?p2;
                 *
                 *  ?p0 = 0 [Type: Int32 (0)],
                 *  ?p1 = 0 [Type: Int32 (0)],
                 *  ?p2 = 50 [Type: Int32 (0)]
                 */
                #endregion
            }
        }