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

Move task recurring schedule creation to a service.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.