| [131] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured | 
|---|
| [136] | 2 | import org.codehaus.groovy.runtime.TimeCategory | 
|---|
| [137] | 3 | import java.text.SimpleDateFormat | 
|---|
| [131] | 4 |  | 
|---|
|  | 5 | class TaskRecurringScheduleDetailedController extends BaseController { | 
|---|
|  | 6 |  | 
|---|
| [137] | 7 | def dateUtilService | 
|---|
|  | 8 |  | 
|---|
| [131] | 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 = "TaskRecurringSchedule 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() | 
|---|
|  | 34 | flash.message = "TaskRecurringSchedule ${params.id} deleted" | 
|---|
|  | 35 | redirect(action:list) | 
|---|
|  | 36 | } | 
|---|
|  | 37 | catch(org.springframework.dao.DataIntegrityViolationException e) { | 
|---|
|  | 38 | flash.message = "TaskRecurringSchedule ${params.id} could not be deleted" | 
|---|
|  | 39 | redirect(action:show,id:params.id) | 
|---|
|  | 40 | } | 
|---|
|  | 41 | } | 
|---|
|  | 42 | else { | 
|---|
|  | 43 | flash.message = "TaskRecurringSchedule 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 = "TaskRecurringSchedule not found with id ${params.id}" | 
|---|
|  | 53 | redirect(action:list) | 
|---|
|  | 54 | } | 
|---|
|  | 55 | else { | 
|---|
| [137] | 56 | return [ taskRecurringScheduleInstance : taskRecurringScheduleInstance] | 
|---|
| [131] | 57 | } | 
|---|
|  | 58 | } | 
|---|
|  | 59 |  | 
|---|
|  | 60 | def update = { | 
|---|
| [137] | 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 TaskRecurringSchedule while you were editing.") | 
|---|
|  | 70 | render(view:'edit',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance]) | 
|---|
|  | 71 | return | 
|---|
|  | 72 | } | 
|---|
|  | 73 | } | 
|---|
|  | 74 |  | 
|---|
|  | 75 | Date originalDate = taskRecurringScheduleInstance.startDate | 
|---|
|  | 76 | taskRecurringScheduleInstance.properties = params  // Domain object is now 'dirty'. | 
|---|
|  | 77 | Date newDate = taskRecurringScheduleInstance.startDate | 
|---|
|  | 78 |  | 
|---|
|  | 79 | // If user changes startDate 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("startDate", "taskRecurring.startDate.NotInTheFuture") | 
|---|
|  | 86 | render(view:'edit',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance]) | 
|---|
|  | 87 | return | 
|---|
|  | 88 | } | 
|---|
|  | 89 | } | 
|---|
|  | 90 |  | 
|---|
|  | 91 | taskRecurringScheduleInstance.nextTargetStartDate = taskRecurringScheduleInstance.startDate | 
|---|
|  | 92 | taskRecurringScheduleInstance.setNextGenerationDate() | 
|---|
|  | 93 | taskRecurringScheduleInstance.setNextTargetCompletionDate() | 
|---|
| [131] | 94 |  | 
|---|
| [137] | 95 | if(!taskRecurringScheduleInstance.hasErrors() && taskRecurringScheduleInstance.save()) | 
|---|
|  | 96 | { | 
|---|
|  | 97 | flash.message = "TaskRecurringSchedule ${params.id} updated" | 
|---|
|  | 98 | redirect(action:show,id:taskRecurringScheduleInstance.id) | 
|---|
|  | 99 | } | 
|---|
|  | 100 | else | 
|---|
|  | 101 | { | 
|---|
|  | 102 | render(view:'edit',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance]) | 
|---|
|  | 103 | } | 
|---|
|  | 104 | } | 
|---|
|  | 105 | else | 
|---|
|  | 106 | { | 
|---|
|  | 107 | flash.message = "TaskRecurringSchedule not found with id ${params.id}" | 
|---|
|  | 108 | redirect(action:edit,id:params.id) | 
|---|
|  | 109 | } | 
|---|
|  | 110 |  | 
|---|
|  | 111 | } // end withTransaction | 
|---|
|  | 112 | } // end update() | 
|---|
| [134] | 113 |  | 
|---|
| [131] | 114 | def create = { | 
|---|
| [134] | 115 | try { | 
|---|
|  | 116 | def taskInstance = Task.get(params.taskInstance.id) | 
|---|
|  | 117 | def taskRecurringScheduleInstance = new TaskRecurringSchedule() | 
|---|
|  | 118 | taskRecurringScheduleInstance.task = taskInstance | 
|---|
|  | 119 | return [taskRecurringScheduleInstance: taskRecurringScheduleInstance] | 
|---|
|  | 120 | } | 
|---|
|  | 121 | catch(Exception e) { | 
|---|
| [135] | 122 | flash.message = "Please select a task, then Create a Recurring Schedule for it" | 
|---|
| [134] | 123 | redirect(controller:"taskDetailed", action:"list") | 
|---|
|  | 124 | } | 
|---|
| [137] | 125 | } // end create() | 
|---|
| [131] | 126 |  | 
|---|
|  | 127 | def save = { | 
|---|
|  | 128 | def taskRecurringScheduleInstance = new TaskRecurringSchedule(params) | 
|---|
| [134] | 129 | def taskInstance = Task.get(params.task.id) | 
|---|
| [135] | 130 |  | 
|---|
|  | 131 | if(taskInstance.taskRecurringSchedule) { | 
|---|
|  | 132 | flash.message = "This task already has a recurring schedule" | 
|---|
|  | 133 | redirect(controller:"taskDetailed", action:"show", id: params.task.id) | 
|---|
|  | 134 | } | 
|---|
|  | 135 | else { | 
|---|
| [134] | 136 |  | 
|---|
| [137] | 137 | if(taskRecurringScheduleInstance.startDate < dateUtilService.getToday()) { | 
|---|
|  | 138 | taskRecurringScheduleInstance.errors.rejectValue("startDate", "taskRecurring.startDate.NotInTheFuture") | 
|---|
|  | 139 | } | 
|---|
|  | 140 |  | 
|---|
| [135] | 141 | if(!taskRecurringScheduleInstance.hasErrors() && taskRecurringScheduleInstance.save()) { | 
|---|
|  | 142 |  | 
|---|
|  | 143 | taskInstance.taskRecurringSchedule = taskRecurringScheduleInstance | 
|---|
|  | 144 |  | 
|---|
|  | 145 | if(taskInstance.save()) { | 
|---|
|  | 146 | flash.message = "TaskRecurringSchedule ${taskRecurringScheduleInstance.id} created" | 
|---|
|  | 147 | redirect(action:show,id:taskRecurringScheduleInstance.id) | 
|---|
|  | 148 | } | 
|---|
|  | 149 | else { | 
|---|
|  | 150 | flash.message = "Task could not be saved and therefore the Recurring Schedule has been disgarded, cause unknown." | 
|---|
|  | 151 | render(view:'create',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance]) | 
|---|
|  | 152 | } | 
|---|
| [134] | 153 | } | 
|---|
|  | 154 | else { | 
|---|
|  | 155 | render(view:'create',model:[taskRecurringScheduleInstance:taskRecurringScheduleInstance]) | 
|---|
|  | 156 | } | 
|---|
|  | 157 | } | 
|---|
|  | 158 |  | 
|---|
| [137] | 159 | } // end save() | 
|---|
| [136] | 160 |  | 
|---|
| [131] | 161 | } | 
|---|