Ignore:
Timestamp:
Nov 30, 2009, 10:28:58 PM (15 years ago)
Author:
gav
Message:

Move task recurring schedule creation to a service.

Location:
trunk/grails-app/services
Files:
2 edited

Legend:

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

    r199 r203  
    66    def taskService
    77
     8    /**
     9    * Generate all enabled recurring tasks.
     10    */
    811    def generateAll() {
    912
    10         /// @todo: disable recurringSchedule when moving a task to trash.
    1113        def taskRecurringScheduleList = TaskRecurringSchedule.findAllByEnabled(true)
    1214
     
    1719                    p.targetStartDate = it.nextTargetStartDate
    1820                    p.targetCompletionDate = it.nextTargetCompletionDate
     21                    if(it.task.taskProcedure) p.taskProcedure = it.task.taskProcedure
    1922                    def result = taskService.createSubTask(it.task, p)
    2023                    if( !result.error ) {
     
    2629                    }
    2730                    else {
    28                         log.error result
     31                        log.error "Sub task generation for recurring schedule ${it.id} failed."
     32                        log.error result.taskInstance.errors
    2933                    }
    3034            }
     
    3337    }
    3438
     39    /**
     40    * Creates a new recurring schedule for a task with the given params.
     41    * @param params The params to use when creating the new recurring schedule.
     42    * @returns A map containing result.error=true (if any error) and result.taskRecurringScheduleInstance and result.taskId.
     43    */
     44    def create(params) {
     45        TaskRecurringSchedule.withTransaction { status ->
     46            def result = [:]
     47
     48            def fail = { Object[] args ->
     49                status.setRollbackOnly()
     50                if(args.size() == 2) result.taskRecurringScheduleInstance.errors.rejectValue(args[0], args[1])
     51                result.error = true
     52                return result
     53            }
     54
     55            result.taskRecurringScheduleInstance = new TaskRecurringSchedule(params)
     56            result.taskId = result.taskRecurringScheduleInstance.task.id
     57
     58            if(!result.taskRecurringScheduleInstance.validate())
     59                return fail()
     60
     61            def taskInstance = Task.lock(result.taskId)
     62
     63            if(!taskInstance)
     64                return fail('task', "task.notFound")
     65
     66            if(taskInstance.taskRecurringSchedule)
     67                return fail('task', "tast.taskRecurringSchedule.alreadyExists")
     68
     69            if(taskInstance.taskStatus.id == 3)
     70                return fail('task', "task.operationNotPermittedOnCompleteTask")
     71
     72            if(taskInstance.trash)
     73                return fail('task', "task.operationNotPermittedOnTaskInTrash")
     74
     75            if(result.taskRecurringScheduleInstance.nextTargetStartDate < dateUtilService.getToday())
     76                return fail("nextTargetStartDate", "taskRecurring.nextTargetStartDate.NotInTheFuture")
     77
     78            taskInstance.taskRecurringSchedule = result.taskRecurringScheduleInstance
     79
     80            if(!result.taskRecurringScheduleInstance.save() || !taskInstance.save())
     81                return fail()
     82
     83            // All went well if we get to here.
     84            return result
     85
     86        } //end withTransaction
     87    } // end create()
     88
    3589} // end of class
  • trunk/grails-app/services/TaskService.groovy

    r202 r203  
    1111
    1212    /**
    13     * Determines and returns a possible parent list
     13    * Determines and returns a possible parent list for a task.
    1414    * @param taskInstance The task to use when determining the possible parent list.
    1515    * @returns A list of the possible parents.
     
    123123
    124124            if(result.entryInstance.validate()) {
    125                 def taskInstance = Task.lock(result.entryInstance.task.id)
    126125                result.taskId = result.entryInstance.task.id
     126                def taskInstance = Task.lock(result.taskId)
    127127
    128128                if(!taskInstance) {
    129129                    status.setRollbackOnly()
    130                     result.entryInstance.errors.rejectValue('task', "entry.task.notFound")
     130                    result.entryInstance.errors.rejectValue('task', "task.notFound")
    131131                    result.error = true
    132132                    return result
     
    150150                    if(!taskModification.save()) {
    151151                        status.setRollbackOnly()
    152                         taskInstance.errors.rejectValue("task", "entry.task.failedToSaveTaskModification")
     152                        taskInstance.errors.rejectValue("task", "task.modifications.failedToSave")
    153153                        result.error = true
    154154                        return result
     
    160160                    if(!taskInstance.save()) {
    161161                        status.setRollbackOnly()
    162                         result.entryInstance.errors.rejectValue("task", "entry.task.failedToSave")
     162                        result.entryInstance.errors.rejectValue("task", "task.failedToSave")
    163163                        result.error = true
    164164                        return result
Note: See TracChangeset for help on using the changeset viewer.