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

Adding an entry to a task with no time booked will now leave the task as "Not Started".
Reopening a task will check if the task should be "In Progress" or "Not Started".
Added integration tests for above.
Added TaskService.delete() since it is required for integration tests.

File:
1 edited

Legend:

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

    r482 r510  
     1import grails.util.Environment
     2
    13/**
    24* Provides a service class for the Task domain class.
     
    221223
    222224    /**
     225    * In production tasks are NEVER deleted, only the trash flag is set!
     226    * However during testing it may be required to delete a task and that
     227    * is why this method exists.
     228    */
     229    def delete(params) {
     230        Task.withTransaction { status ->
     231            def result = [:]
     232
     233            def fail = { Map m ->
     234                status.setRollbackOnly()
     235                if(result.taskInstance && m.field)
     236                    result.taskInstance.errors.rejectValue(m.field, m.code)
     237                result.error = [ code: m.code, args: ["Task", params.id] ]
     238                return result
     239            }
     240
     241            if(Environment.current == Environment.PRODUCTION)
     242                return fail(code:"task.delete.failure.production")
     243
     244            result.taskInstance = Task.get(params.id)
     245
     246            if(!result.taskInstance)
     247                return fail(code:"default.not.found")
     248
     249            // Handle taskModifications.
     250            def taskModifications = TaskModification.findAllByTask(result.taskInstance)
     251            taskModifications.each() {
     252                result.taskInstance.removeFromTaskModifications(it)
     253                it.delete()
     254            }
     255
     256            if(result.error)
     257                return result
     258
     259            try {
     260                result.taskInstance.delete(flush:true)
     261                return result //Success.
     262            }
     263            catch(org.springframework.dao.DataIntegrityViolationException e) {
     264                return fail(code:"default.delete.failure")
     265            }
     266
     267        } // end withTransaction
     268    } // delete()
     269
     270    /**
    223271    * Creates a new task entry.
    224272    * @param params The params to use when creating the new entry.
     
    241289
    242290            def taskInstance
    243             if(result.entryInstance.task.id) {
     291            if(result.entryInstance.task?.id) {
    244292                result.taskId = result.entryInstance.task.id
    245293                taskInstance = Task.lock(result.entryInstance.task.id)
     
    255303                return fail(field:"task", code:"task.operationNotPermittedOnCompleteTask")
    256304
    257             // If task status is "Not Started" and entry type is "Work Done" then we create the started modification and set the status.
    258             if(taskInstance.taskStatus.id == 1 && result.entryInstance.entryType.id == 3) {
     305            // If task status is "Not Started" and entry type is "Work Done" and time has been booked.
     306            // Then we create the started modification and set task status.
     307            if(taskInstance.taskStatus.id == 1 && result.entryInstance.entryType.id == 3
     308                && (result.entryInstance.durationHour + result.entryInstance.durationMinute > 0)) {
    259309
    260310                // Create the "Started" task modification, this provides the "Actual Started Date".
    261311                def taskModification = new TaskModification(person: authService.currentUser,
    262                                                         taskModificationType: TaskModificationType.get(2),
     312                                                        taskModificationType: TaskModificationType.read(2),
    263313                                                        task: taskInstance)
    264314
     
    267317
    268318                // Set task status to "In Progress".
    269                 taskInstance.taskStatus = TaskStatus.get(2)
     319                taskInstance.taskStatus = TaskStatus.read(2)
    270320
    271321                if(taskInstance.hasErrors() || !taskInstance.save())
     
    494544            }
    495545
    496             result.taskInstance.taskStatus = TaskStatus.get(2)
     546            def isInProgress = false
     547            result.taskInstance.entries.each() {
     548                if(it.entryType.id == 3 && (it.durationHour + it.durationMinute > 0) )
     549                    isInProgress = true
     550            }
     551
     552            if(isInProgress)
     553                result.taskInstance.taskStatus = TaskStatus.read(2) // In Progress
     554            else
     555                result.taskInstance.taskStatus = TaskStatus.read(1) // Not Started
    497556
    498557            if(result.taskInstance.hasErrors() || !result.taskInstance.save())
Note: See TracChangeset for help on using the changeset viewer.