Ignore:
Timestamp:
Dec 1, 2009, 4:51:32 AM (14 years ago)
Author:
gav
Message:

Refactor taskRecurringSchedule update() to a service method.

File:
1 edited

Legend:

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

    r203 r207  
    8181                return fail()
    8282
    83             // All went well if we get to here.
     83             // If we get here all went well.
    8484            return result
    8585
     
    8787    } // end create()
    8888
     89    /**
     90    * Updates an existing recurring schedule.
     91    * @param params The params to update for recurring schedule with id of params.id.
     92    * @returns A map containing result.error=true (if any error) and result.taskRecurringScheduleInstance (if available).
     93    */
     94    def update(params) {
     95        TaskRecurringSchedule.withTransaction { status ->
     96            def result = [:]
     97
     98            def fail = { Object[] args ->
     99                status.setRollbackOnly()
     100                if(args.size() == 2) result.taskRecurringScheduleInstance.errors.rejectValue(args[0], args[1])
     101                result.error = true
     102                return result
     103            }
     104
     105            result.taskRecurringScheduleInstance = TaskRecurringSchedule.get(params.id)
     106
     107            if(!result.taskRecurringScheduleInstance)
     108                return fail('id', "taskRecurringSchedule.notFound")
     109
     110            // Optimistic locking check.
     111            if(params.version) {
     112                def version = params.version.toLong()
     113                if(result.taskRecurringScheduleInstance.version > version)
     114                    return fail("version", "default.optimistic.locking.failure")
     115            }
     116
     117            result.taskRecurringScheduleInstance.validate()
     118
     119            Date originalDate = result.taskRecurringScheduleInstance.nextTargetStartDate
     120            result.taskRecurringScheduleInstance.properties = params
     121            Date newDate = result.taskRecurringScheduleInstance.nextTargetStartDate
     122
     123            // If user changes nextTargetStartDate then ensure it is in the future, otherwise it's ok to keep the original date.
     124            if(originalDate.getTime() != newDate.getTime())
     125            {
     126                if(newDate < dateUtilService.getToday())
     127                    return fail("nextTargetStartDate", "taskRecurring.nextTargetStartDate.NotInTheFuture")
     128            }
     129
     130            result.taskRecurringScheduleInstance.setNextGenerationDate()
     131            result.taskRecurringScheduleInstance.setNextTargetCompletionDate()
     132
     133            if(result.taskRecurringScheduleInstance.hasErrors() || !result.taskRecurringScheduleInstance.save())
     134                return fail()
     135
     136            // If we get here all went well.
     137            return result
     138
     139        } //end withTransaction
     140    }  // end update()
     141
    89142} // end of class
Note: See TracChangeset for help on using the changeset viewer.