// param criteria
// return
func (this *TaskBoardServiceImpl) FetchUsers(ctx maze.IContext, criteria dto.UserSearchDTO) (app.Page, error) {
	store := ctx.(*AppCtx).Store
	admin := store.Query(T.ROLE).Alias("r").
		CountAll().
		Where(
			T.ROLE_C_USER_ID.Matches(T.USER_C_ID.For("u")),
			T.ROLE_C_KIND.Matches(lov.ERole_ADMIN),
		)

	// password field not included
	q := store.Query(T.USER).Alias("u").Column(
		T.USER_C_ID,
		T.USER_C_VERSION,
		T.USER_C_NAME,
		T.USER_C_USERNAME,
	).Column(admin).As("Admin")
	if !IsEmpty(criteria.Name) {
		// insenstive case like
		q.Where(T.USER_C_NAME.ILike("%" + *criteria.Name + "%"))
	}
	order := criteria.OrderBy
	if !IsEmpty(order) {
		switch *order {
		case "name":
			q.Order(T.USER_C_NAME)
		}

		applyDirection(q, criteria.Criteria)
	}

	return app.QueryForPage(q, criteria.Criteria, (*dto.UserDTO)(nil), nil)

}
// param criteria
// return
func (this *TaskBoardServiceImpl) FetchBoardAllUsers(c maze.IContext, criteria dto.BoardUserSearchDTO) (app.Page, error) {
	store := c.(*AppCtx).Store
	belongs := store.Query(T.BOARD_USER).Alias("b").
		CountAll().
		Where(
			T.BOARD_USER_C_BOARDS_ID.Matches(criteria.BoardId),
			T.BOARD_USER_C_USERS_ID.Matches(T.USER_C_ID.For("u")),
		)

	q := store.Query(T.USER).Alias("u").Column(
		T.USER_C_ID,
		T.USER_C_VERSION,
		T.USER_C_NAME,
	).Column(belongs).As("Belongs")

	if !IsEmpty(criteria.Name) {
		// insenstive case like
		q.Where(T.USER_C_NAME.ILike("%" + *criteria.Name + "%"))
	}
	order := criteria.OrderBy
	if !IsEmpty(order) {
		switch *order {
		case "name":
			q.Order(T.USER_C_NAME)
		}
		applyDirection(q, criteria.Criteria)
	}

	return app.QueryForPage(q, criteria.Criteria, (*dto.BoardUserDTO)(nil), nil)
}
// param taskId
// return
func (this *TaskBoardServiceImpl) FetchNotifications(c maze.IContext, criteria dto.NotificationSearchDTO) (app.Page, error) {
	q := c.(*AppCtx).Store.Query(T.NOTIFICATION).
		All().
		Where(T.NOTIFICATION_C_TASK_ID.Matches(criteria.TaskId)).
		Outer(T.NOTIFICATION_A_LANE).
		Fetch()

	order := criteria.OrderBy
	if !IsEmpty(order) {
		switch *order {
		case "email":
			q.Order(T.NOTIFICATION_C_EMAIL)

		case "column":
			q.OrderBy(T.LANE_C_NAME)
		}

		applyDirection(q, criteria.Criteria)
	}

	return app.QueryForPage(q, criteria.Criteria, (*entity.Notification)(nil), nil)
}
// param criteria
// return
func (this *TaskBoardServiceImpl) FetchBoards(c maze.IContext, criteria dto.BoardSearchDTO) (app.Page, error) {
	var ctx = c.(*AppCtx)
	q := ctx.Store.Query(T.BOARD).All()
	p := ctx.Principal
	// if not admin restrict to boards that the user has access
	if !p.HasRole(lov.ERole_ADMIN) {
		q.Inner(T.BOARD_A_USERS).On(T.USER_C_ID.Matches(p.UserId)).Join()
	}
	if !IsEmpty(criteria.Name) {
		// insenstive case like
		q.Where(T.BOARD_C_NAME.ILike("%" + *criteria.Name + "%"))
	}
	order := criteria.OrderBy
	if !IsEmpty(order) {
		switch *order {
		case "name":
			q.Order(T.BOARD_C_NAME)
		}

		applyDirection(q, criteria.Criteria)
	}

	return app.QueryForPage(q, criteria.Criteria, (*entity.Board)(nil), nil)
}