Changeset 203


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

Move task recurring schedule creation to a service.

Location:
trunk/grails-app
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/TaskRecurringScheduleDetailedController.groovy

    r199 r203  
    66
    77    def dateUtilService
     8    def taskRecurringScheduleService
    89
    910    def index = { redirect(action:list,params:params) }
     
    112113
    113114    def create = {
    114         try {
    115             def taskInstance = Task.get(params.taskInstance.id)
    116             def taskRecurringScheduleInstance = new TaskRecurringSchedule()
    117             taskRecurringScheduleInstance.task = taskInstance
    118             return [taskRecurringScheduleInstance: taskRecurringScheduleInstance]
     115        if(!params.task?.id || !Task.exists(params.task?.id)) {
     116            flash.message = "Please select a task, then Create a Recurring Schedule for it."
     117            redirect(controller:"taskDetailed", action:"search")
     118            return
    119119        }
    120         catch(Exception e) {
    121             flash.message = "Please select a task, then Create a Recurring Schedule for it"
    122             redirect(controller:"taskDetailed", action:"search")
    123         }
     120        def taskRecurringScheduleInstance = new TaskRecurringSchedule()
     121        taskRecurringScheduleInstance.properties = params
     122        return [taskRecurringScheduleInstance: taskRecurringScheduleInstance]
    124123    } // end create()
    125124
    126125    def save = {
    127         def taskRecurringScheduleInstance = new TaskRecurringSchedule(params)
    128         def taskInstance = Task.get(params.task.id)
     126        def result = taskRecurringScheduleService.create(params)
    129127
    130         if(taskInstance.taskRecurringSchedule) {
    131             flash.message = "This task already has a recurring schedule"
    132             redirect(controller:"taskDetailed", action:"show", id: params.task.id)
     128        if(!result.error) {
     129            flash.message = "Recurring Schedule ${result.taskRecurringScheduleInstance.id} created."
     130            redirect(action:show, id: result.taskRecurringScheduleInstance.id)
    133131        }
    134132        else {
    135 
    136             if(taskRecurringScheduleInstance.nextTargetStartDate < dateUtilService.getToday()) {
    137                 taskRecurringScheduleInstance.errors.rejectValue("nextTargetStartDate", "taskRecurring.nextTargetStartDate.NotInTheFuture")
    138             }
    139 
    140             if(!taskRecurringScheduleInstance.hasErrors() && taskRecurringScheduleInstance.save(flush: true)) {
    141 
    142                 taskInstance.taskRecurringSchedule = taskRecurringScheduleInstance
    143 
    144                 if(taskInstance.save(flush: true)) {
    145                     flash.message = "Recurring Schedule ${taskRecurringScheduleInstance.id} created"
    146                     redirect(action:show,id:taskRecurringScheduleInstance.id)
    147                 }
    148                 else {
    149                     flash.message = "Task could not be saved and therefore the Recurring Schedule has been disgarded, cause unknown."   
    150                     render(view:'create',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance])
    151                 }
    152             }
    153             else {
    154                 render(view:'create',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance])
    155             }
     133            render(view:'create',model:[taskRecurringScheduleInstance: result.taskRecurringScheduleInstance])
    156134        }
    157 
    158135    } // end save()
    159136
  • trunk/grails-app/i18n/messages.properties

    r199 r203  
    2525assignedPerson.estimatedDuration.help=The estimated amount of time (hh:mm) that you would like to assign this person to the task.
    2626
    27 task.modifications.failedToSave=Could not complete operation, as task modification record failed to save.
     27task.notFound=Could not complete operation, task not found.
    2828task.operationNotPermittedOnCompleteTask=This operation is not permitted on a complete task.
    2929task.operationNotPermittedOnTaskInTrash=This operation is not permitted on a task that is in the trash.
     30task.failedToSave=Could not complete operation, task failed to save.
     31task.modifications.failedToSave=Could not complete operation, as task modification record failed to save.
     32tast.taskRecurringSchedule.alreadyExists=This task already has a recurring schedule.
    3033
    3134task.assignedPersons=Assigned Persons
     
    5356inventoryMovement.quantity.insufficientItemsInStock=Could not complete operation, insufficient items in stock.
    5457inventoryMovement.inventoryItem.notFound=Inventory Item not found.
    55 
    56 entry.task.notFound=Could not complete operation, task not found.
    57 entry.task.failedToSave=Could not complete operation, task failed to save.
    58 entry.task.failedToSaveTaskModification=Could not complete operation, task modification failed to save.
    5958
    6059default.doesnt.match.message=Property [{0}] of class [{1}] with value [{2}] does not match the required pattern [{3}]
  • 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
  • trunk/grails-app/views/taskDetailed/show.gsp

    r199 r203  
    344344                            <br />
    345345                            <g:form controller="taskProcedureDetailed" >
    346                                 <input type="hidden" name="taskInstance.id" value="${taskInstance?.id}" />
     346                                <g:hiddenField name="taskInstance.id" value="${taskInstance?.id}" />
    347347
    348348                                <div class="buttons">
     
    442442                            <br />
    443443                            <g:form controller="taskRecurringScheduleDetailed" >
    444                                 <input type="hidden" name="taskInstance.id" value="${taskInstance?.id}" />
     444                                <g:hiddenField name="task.id" value="${taskInstance.id}" />
    445445
    446446                                <div class="buttons">
Note: See TracChangeset for help on using the changeset viewer.