| 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
|---|
| 2 | import org.codehaus.groovy.runtime.TimeCategory |
|---|
| 3 | import java.text.SimpleDateFormat |
|---|
| 4 | |
|---|
| 5 | class TaskRecurringScheduleDetailedController extends BaseController { |
|---|
| 6 | |
|---|
| 7 | def dateUtilService |
|---|
| 8 | |
|---|
| 9 | def index = { redirect(action:list,params:params) } |
|---|
| 10 | |
|---|
| 11 | // the delete, save and update actions only accept POST requests |
|---|
| 12 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
|---|
| 13 | |
|---|
| 14 | def list = { |
|---|
| 15 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
|---|
| 16 | [ taskRecurringScheduleInstanceList: TaskRecurringSchedule.list( params ), taskRecurringScheduleInstanceTotal: TaskRecurringSchedule.count() ] |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | def show = { |
|---|
| 20 | def taskRecurringScheduleInstance = TaskRecurringSchedule.get( params.id ) |
|---|
| 21 | |
|---|
| 22 | if(!taskRecurringScheduleInstance) { |
|---|
| 23 | flash.message = "Recurring Schedule not found with id ${params.id}" |
|---|
| 24 | redirect(action:list) |
|---|
| 25 | } |
|---|
| 26 | else { return [ taskRecurringScheduleInstance : taskRecurringScheduleInstance ] } |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | def delete = { |
|---|
| 30 | def taskRecurringScheduleInstance = TaskRecurringSchedule.get( params.id ) |
|---|
| 31 | if(taskRecurringScheduleInstance) { |
|---|
| 32 | try { |
|---|
| 33 | taskRecurringScheduleInstance.delete(flush: true) |
|---|
| 34 | flash.message = "Recurring Schedule ${params.id} deleted" |
|---|
| 35 | redirect(action:list) |
|---|
| 36 | } |
|---|
| 37 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
|---|
| 38 | flash.message = "Recurring Schedule ${params.id} could not be deleted" |
|---|
| 39 | redirect(action:show,id:params.id) |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | else { |
|---|
| 43 | flash.message = "Recurring Schedule not found with id ${params.id}" |
|---|
| 44 | redirect(action:list) |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | def edit = { |
|---|
| 49 | def taskRecurringScheduleInstance = TaskRecurringSchedule.get( params.id ) |
|---|
| 50 | |
|---|
| 51 | if(!taskRecurringScheduleInstance) { |
|---|
| 52 | flash.message = "Recurring Schedule not found with id ${params.id}" |
|---|
| 53 | redirect(action:list) |
|---|
| 54 | } |
|---|
| 55 | else { |
|---|
| 56 | return [ taskRecurringScheduleInstance : taskRecurringScheduleInstance] |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | def update = { |
|---|
| 61 | TaskRecurringSchedule.withTransaction { status -> |
|---|
| 62 | |
|---|
| 63 | def taskRecurringScheduleInstance = TaskRecurringSchedule.get( params.id ) |
|---|
| 64 | if(taskRecurringScheduleInstance) { |
|---|
| 65 | |
|---|
| 66 | if(params.version) { |
|---|
| 67 | def version = params.version.toLong() |
|---|
| 68 | if(taskRecurringScheduleInstance.version > version) { |
|---|
| 69 | taskRecurringScheduleInstance.errors.rejectValue("version", "taskRecurringSchedule.optimistic.locking.failure", "Another user has updated this Recurring Schedule while you were editing.") |
|---|
| 70 | render(view:'edit',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance]) |
|---|
| 71 | return |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | Date originalDate = taskRecurringScheduleInstance.nextTargetStartDate |
|---|
| 76 | taskRecurringScheduleInstance.properties = params // Domain object is now 'dirty'. |
|---|
| 77 | Date newDate = taskRecurringScheduleInstance.nextTargetStartDate |
|---|
| 78 | |
|---|
| 79 | // If user changes nextTargetStartDate then ensure it is in the future, otherwise it's ok to keep the original date. |
|---|
| 80 | if(originalDate.getTime() != newDate.getTime()) |
|---|
| 81 | { |
|---|
| 82 | if(newDate < dateUtilService.getToday()) |
|---|
| 83 | { |
|---|
| 84 | status.setRollbackOnly() // Only allow the transaction to Rollback, preventing flush due to 'dirty'. |
|---|
| 85 | taskRecurringScheduleInstance.errors.rejectValue("nextTargetStartDate", "taskRecurring.nextTargetStartDate.NotInTheFuture") |
|---|
| 86 | render(view:'edit',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance]) |
|---|
| 87 | return |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | taskRecurringScheduleInstance.setNextGenerationDate() |
|---|
| 92 | taskRecurringScheduleInstance.setNextTargetCompletionDate() |
|---|
| 93 | |
|---|
| 94 | if(!taskRecurringScheduleInstance.hasErrors() && taskRecurringScheduleInstance.save(flush: true)) |
|---|
| 95 | { |
|---|
| 96 | flash.message = "Recurring Schedule ${params.id} updated" |
|---|
| 97 | redirect(action:show,id:taskRecurringScheduleInstance.id) |
|---|
| 98 | } |
|---|
| 99 | else |
|---|
| 100 | { |
|---|
| 101 | render(view:'edit',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance]) |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | else |
|---|
| 105 | { |
|---|
| 106 | flash.message = "Recurring Schedule not found with id ${params.id}" |
|---|
| 107 | redirect(action:edit,id:params.id) |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | } // end withTransaction |
|---|
| 111 | } // end update() |
|---|
| 112 | |
|---|
| 113 | 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] |
|---|
| 119 | } |
|---|
| 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 | } |
|---|
| 124 | } // end create() |
|---|
| 125 | |
|---|
| 126 | def save = { |
|---|
| 127 | def taskRecurringScheduleInstance = new TaskRecurringSchedule(params) |
|---|
| 128 | def taskInstance = Task.get(params.task.id) |
|---|
| 129 | |
|---|
| 130 | if(taskInstance.taskRecurringSchedule) { |
|---|
| 131 | flash.message = "This task already has a recurring schedule" |
|---|
| 132 | redirect(controller:"taskDetailed", action:"show", id: params.task.id) |
|---|
| 133 | } |
|---|
| 134 | 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 | } |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | } // end save() |
|---|
| 159 | |
|---|
| 160 | } |
|---|