Changeset 524 for trunk


Ignore:
Timestamp:
May 6, 2010, 3:51:02 PM (15 years ago)
Author:
gav
Message:

Refactor TaskSearchServicve getBudgetTasks() to use HQL.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/services/TaskSearchService.groovy

    r512 r524  
    252252    */
    253253    def getBudgetTasks(params, budgetStatus=null, startDate=null, endDate=null) {
    254         params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
    255         params.offset = params?.offset?.toInteger() ?: 0
    256         params.sort = params?.sort ?: "targetStartDate"
    257         params.order = params?.order ?: "asc"
    258 
    259         budgetStatus = budgetStatus ?: TaskBudgetStatus.read(2) // Planned.
    260         startDate = startDate ?: dateUtilService.today
    261         endDate = endDate ?: dateUtilService.tomorrow
    262 
    263         def taskInstanceList = Task.createCriteria().list(
    264             max: params.max,
    265             offset: params.offset,
    266             sort: params.sort,
    267             order: params.order) {
    268                 eq("taskBudgetStatus", budgetStatus)
    269                 lt("targetStartDate", dateUtilService.tomorrow)
    270                 ge("targetCompletionDate", dateUtilService.oneWeekAgo)
    271                 eq("trash", false)
    272             } // createCriteria
     254        def paginateParams = [:]
     255        paginateParams.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
     256        paginateParams.offset = params?.offset?.toInteger() ?: 0
     257
     258        def sort = "task." + (params?.sort ?: "targetStartDate")
     259        def order = params?.order == "desc" ? "desc" : "asc"
     260        def orderBy = " order by " + sort + ' ' + order
     261
     262        def namedParams = [:]
     263        namedParams.budgetStatus = budgetStatus ?: TaskBudgetStatus.read(2) // Planned.
     264        namedParams.startDate = startDate ?: dateUtilService.today
     265        namedParams.endDate = endDate ?: dateUtilService.oneWeekAgo
     266
     267        def baseQuery = "from Task as task \
     268                                        where (task.trash = false \
     269                                                    and task.budgetStatus = :budgetStatus \
     270                                                    and task.targetStartDate < :endDate \
     271                                                    and task.targetCompletionDate >= :startDate \
     272                                        )"
     273
     274        def searchQuery = "select distinct task " + baseQuery + orderBy
     275        def list = Task.executeQuery(searchQuery, namedParams, paginateParams)
     276
     277        def countQuery = "select count(distinct task) as taskCount " + baseQuery
     278        def totalCount = Task.executeQuery(countQuery, namedParams)[0].toInteger()
     279
     280        def taskInstanceList = new PagedResultList(list, totalCount)
     281        return taskInstanceList
    273282    } // getBudgetTasks()
    274283
Note: See TracChangeset for help on using the changeset viewer.