Changeset 512 for trunk


Ignore:
Timestamp:
Apr 28, 2010, 11:14:56 PM (15 years ago)
Author:
gav
Message:

Refactor TaskSearchService.getTasks() to use HQL and avoid bug in ticket #64, also extend associated tests.

Location:
trunk
Files:
2 edited

Legend:

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

    r511 r512  
    157157    */
    158158    def getTasks(params, startDate=null, endDate=null) {
    159         params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
    160         params.offset = params?.offset?.toInteger() ?: 0
    161         params.sort = params?.sort ?: "attentionFlag"
    162         params.order = params?.order ?: "desc"
    163 
    164         startDate = startDate ?: dateUtilService.today
    165         endDate = endDate ?: dateUtilService.tomorrow
    166 
    167         def taskInstanceList = Task.createCriteria().list(
    168             max: params.max,
    169             offset: params.offset,
    170             sort: params.sort,
    171             order: params.order) {
    172                 lt("targetStartDate", endDate)
    173                 ge("targetCompletionDate", startDate)
    174                 eq("trash", false)
    175             } // createCriteria
    176     } // getTasks()
     159        def paginateParams = [:]
     160        paginateParams.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
     161        paginateParams.offset = params?.offset?.toInteger() ?: 0
     162
     163        def sort = "task." + (params?.sort ?: "attentionFlag")
     164        def order = params?.order == "asc" ? "asc" : "desc"
     165        def orderBy = " order by " + sort + ' ' + order
     166
     167        def namedParams = [:]
     168        namedParams.startDate = startDate ?: dateUtilService.today
     169        namedParams.endDate = endDate ?: dateUtilService.tomorrow
     170
     171        def baseQuery = "from Task as task \
     172                                        where (task.trash = false \
     173                                                    and task.targetStartDate < :endDate \
     174                                                    and task.targetCompletionDate >= :startDate \
     175                                        )"
     176
     177        def searchQuery = "select distinct task " + baseQuery + orderBy
     178        def list = Task.executeQuery(searchQuery, namedParams, paginateParams)
     179
     180        def countQuery = "select count(distinct task) as taskCount " + baseQuery
     181        def totalCount = Task.executeQuery(countQuery, namedParams)[0].toInteger()
     182
     183        def taskInstanceList = new PagedResultList(list, totalCount)
     184        return taskInstanceList
     185    } // getPTasks()
    177186
    178187    /**
  • trunk/test/integration/TaskSearchServiceTests.groovy

    r511 r512  
    7070
    7171        // Tasks in the trash should not be returned.
    72         tasks[0].trash = true
    73         tasks[0].save(flush:true)
     72        taskA.trash = true
     73        taskA.save(flush:true)
    7474        assert taskSearchService.getTasks([:]).totalCount == taskCount - 1
    75         tasks[1].trash = true
    76         tasks[1].save(flush:true)
     75        taskB.trash = true
     76        taskB.save(flush:true)
    7777        assert taskSearchService.getTasks([:]).totalCount == taskCount - 2
    7878
    7979        // Restored tasks should be returned.
    80         tasks[0].trash = false
    81         tasks[0].save(flush:true)
     80        taskA.trash = false
     81        taskA.save(flush:true)
    8282        assert taskSearchService.getTasks([:]).totalCount == taskCount - 1
    83         tasks[1].trash = false
    84         tasks[1].save(flush:true)
     83        taskB.trash = false
     84        taskB.save(flush:true)
    8585        assert taskSearchService.getTasks([:]).totalCount == taskCount
    8686
    8787        // Tomorrows tasks should not be returned.
    88         tasks[0].targetStartDate = dateUtilService.tomorrow
    89         tasks[0].targetCompletionDate = dateUtilService.tomorrow
    90         tasks[0].save(flush:true)
     88        taskA.targetStartDate = dateUtilService.tomorrow
     89        taskA.targetCompletionDate = dateUtilService.tomorrow
     90        taskA.save(flush:true)
    9191        assert taskSearchService.getTasks([:]).totalCount == taskCount - 1
    9292
     93        // Tomorrows tasks should be returned, if we ask for them.
     94        assert taskSearchService.getTasks([:], dateUtilService.today, dateUtilService.tomorrow+1).totalCount == taskCount
     95
    9396        // Yesterdays tasks should not be returned.
    94         tasks[0].targetStartDate = dateUtilService.yesterday
    95         tasks[0].targetCompletionDate = dateUtilService.yesterday
    96         tasks[0].save(flush:true)
     97        taskA.targetStartDate = dateUtilService.yesterday
     98        taskA.targetCompletionDate = dateUtilService.yesterday
     99        taskA.save(flush:true)
    97100        assert taskSearchService.getTasks([:]).totalCount == taskCount - 1
    98101
     102        // Yesterdays tasks should be returned, if we ask for them.
     103        assert taskSearchService.getTasks([:], dateUtilService.yesterday, dateUtilService.tomorrow).totalCount == taskCount
     104
    99105        // Tasks that span today should be returned.
    100         tasks[0].targetStartDate = dateUtilService.yesterday
    101         tasks[0].targetCompletionDate = dateUtilService.tomorrow
    102         tasks[0].save(flush:true)
     106        taskA.targetStartDate = dateUtilService.yesterday
     107        taskA.targetCompletionDate = dateUtilService.tomorrow
     108        taskA.save(flush:true)
    103109        assert taskSearchService.getTasks([:]).totalCount == taskCount
    104110    } // testGetTasks()
Note: See TracChangeset for help on using the changeset viewer.