[133] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
| 2 | |
---|
[298] | 3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager']) |
---|
[133] | 4 | class TaskProcedureDetailedController extends BaseController { |
---|
[147] | 5 | |
---|
[155] | 6 | def filterService |
---|
[772] | 7 | def authService |
---|
[155] | 8 | |
---|
[133] | 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 | |
---|
[298] | 14 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser']) |
---|
[133] | 15 | def list = { |
---|
| 16 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
[147] | 17 | |
---|
| 18 | if(!params.filter) |
---|
| 19 | { return [taskProcedureInstanceList: TaskProcedure.list(params), taskProcedureInstanceTotal: TaskProcedure.count()] } |
---|
| 20 | |
---|
| 21 | // filterPane: |
---|
| 22 | return[ taskProcedureInstanceList: filterService.filter( params, TaskProcedure ), |
---|
| 23 | taskProcedureInstanceTotal: filterService.count( params, TaskProcedure ), |
---|
| 24 | filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params), |
---|
| 25 | params:params ] |
---|
[133] | 26 | } |
---|
[147] | 27 | |
---|
[298] | 28 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser']) |
---|
[134] | 29 | def search = { |
---|
| 30 | redirect(action:list) |
---|
| 31 | } |
---|
[133] | 32 | |
---|
[298] | 33 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser']) |
---|
[133] | 34 | def show = { |
---|
[147] | 35 | |
---|
| 36 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 37 | if(params._action_Show) |
---|
[375] | 38 | params.action='show' |
---|
[147] | 39 | |
---|
[133] | 40 | def taskProcedureInstance = TaskProcedure.get( params.id ) |
---|
| 41 | |
---|
| 42 | if(!taskProcedureInstance) { |
---|
[770] | 43 | flash.errorMessage = "TaskProcedure not found with id ${params.id}" |
---|
| 44 | redirect(controller:'taskDetailed', action:'search') |
---|
| 45 | return |
---|
[133] | 46 | } |
---|
[770] | 47 | |
---|
| 48 | redirect(controller:'taskDetailed', |
---|
| 49 | action:'show', |
---|
| 50 | id:taskProcedureInstance.linkedTask?.id, |
---|
| 51 | params:[showTab:"showProcedureTab"]) |
---|
[133] | 52 | } |
---|
| 53 | |
---|
| 54 | def delete = { |
---|
| 55 | def taskProcedureInstance = TaskProcedure.get( params.id ) |
---|
| 56 | if(taskProcedureInstance) { |
---|
[770] | 57 | def taskInstance = taskProcedureInstance.linkedTask |
---|
[133] | 58 | try { |
---|
[758] | 59 | taskProcedureInstance.tasks.each { |
---|
| 60 | it.taskProcedure = null |
---|
| 61 | } |
---|
[365] | 62 | taskProcedureInstance.delete(flush:true) |
---|
[133] | 63 | flash.message = "TaskProcedure ${params.id} deleted" |
---|
[770] | 64 | redirect(controller:'taskDetailed', |
---|
| 65 | action:'show', |
---|
| 66 | id:taskInstance.id, |
---|
| 67 | params:[showTab:"showProcedureTab"]) |
---|
[133] | 68 | } |
---|
| 69 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
| 70 | flash.message = "TaskProcedure ${params.id} could not be deleted" |
---|
[770] | 71 | redirect(controller:'taskDetailed', |
---|
| 72 | action:'show', |
---|
| 73 | id:taskInstance.id, |
---|
| 74 | params:[showTab:"showProcedureTab"]) |
---|
[133] | 75 | } |
---|
| 76 | } |
---|
| 77 | else { |
---|
| 78 | flash.message = "TaskProcedure not found with id ${params.id}" |
---|
| 79 | redirect(action:list) |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | def edit = { |
---|
[147] | 84 | |
---|
| 85 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 86 | if(params._action_Edit) |
---|
[375] | 87 | params.action='edit' |
---|
[147] | 88 | |
---|
[133] | 89 | def taskProcedureInstance = TaskProcedure.get( params.id ) |
---|
| 90 | |
---|
| 91 | if(!taskProcedureInstance) { |
---|
| 92 | flash.message = "TaskProcedure not found with id ${params.id}" |
---|
| 93 | redirect(action:list) |
---|
| 94 | } |
---|
| 95 | else { |
---|
| 96 | return [ taskProcedureInstance : taskProcedureInstance ] |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | def update = { |
---|
| 101 | def taskProcedureInstance = TaskProcedure.get( params.id ) |
---|
| 102 | if(taskProcedureInstance) { |
---|
| 103 | if(params.version) { |
---|
| 104 | def version = params.version.toLong() |
---|
| 105 | if(taskProcedureInstance.version > version) { |
---|
[147] | 106 | |
---|
[403] | 107 | taskProcedureInstance.errors.rejectValue("version", "default.optimistic.locking.failure") |
---|
[133] | 108 | render(view:'edit',model:[taskProcedureInstance:taskProcedureInstance]) |
---|
| 109 | return |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | taskProcedureInstance.properties = params |
---|
[772] | 113 | taskProcedureInstance.lastUpdatedBy = authService.currentUser |
---|
| 114 | taskProcedureInstance.lastUpdated = new Date() // Required to trigger version increment. |
---|
[760] | 115 | |
---|
[770] | 116 | // Gaps in the html index's can be created by adding 2 items and removing the first one. |
---|
| 117 | // This creates a gap at the missing index where LazyList will return a null. |
---|
| 118 | def nullMaintenanceActions = taskProcedureInstance.maintenanceActions.findAll {!it} |
---|
| 119 | if (nullMaintenanceActions) { |
---|
| 120 | taskProcedureInstance.maintenanceActions.removeAll(nullMaintenanceActions) |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | // Save for restoration if validation fails. |
---|
| 124 | def savedMaintenanceActions = new ArrayList(taskProcedureInstance.maintenanceActions) |
---|
| 125 | |
---|
| 126 | // Remove toBeDeleted before validation. |
---|
| 127 | def toBeDeleted = taskProcedureInstance.maintenanceActions.findAll {it.toBeDeleted} |
---|
| 128 | if (toBeDeleted) { |
---|
[760] | 129 | taskProcedureInstance.maintenanceActions.removeAll(toBeDeleted) |
---|
| 130 | } |
---|
| 131 | |
---|
[178] | 132 | if(!taskProcedureInstance.hasErrors() && taskProcedureInstance.save(flush: true)) { |
---|
[133] | 133 | flash.message = "TaskProcedure ${params.id} updated" |
---|
[770] | 134 | redirect(controller:'taskDetailed', |
---|
| 135 | action:'show', |
---|
| 136 | id:taskProcedureInstance.linkedTask.id, |
---|
| 137 | params:[showTab:"showProcedureTab"]) |
---|
[133] | 138 | } |
---|
| 139 | else { |
---|
[770] | 140 | // Restore the saved items, some of which contain toBeDeleted flags but |
---|
| 141 | // have not been deleted yet since validation failed. |
---|
| 142 | // The toBeDeleted items are hidden in the view. |
---|
| 143 | taskProcedureInstance.maintenanceActions = savedMaintenanceActions |
---|
| 144 | // Populate maintenanceAction errors for display. |
---|
| 145 | taskProcedureInstance.maintenanceActions.each { it.validate() } |
---|
[133] | 146 | render(view:'edit',model:[taskProcedureInstance:taskProcedureInstance]) |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | else { |
---|
| 150 | flash.message = "TaskProcedure not found with id ${params.id}" |
---|
[365] | 151 | redirect(action:list) |
---|
[133] | 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | def create = { |
---|
[147] | 156 | |
---|
[770] | 157 | if(!params.taskInstance?.id) { |
---|
| 158 | flash.errorMessage = "Please select or create a task, then go to the Procedure tab." |
---|
[155] | 159 | redirect(controller:"taskDetailed", action:"search") |
---|
[770] | 160 | return |
---|
[147] | 161 | } |
---|
[770] | 162 | |
---|
| 163 | params.linkedTask = Task.get(params.taskInstance.id) |
---|
| 164 | |
---|
| 165 | if(!params.linkedTask?.primaryAsset) { |
---|
| 166 | flash.errorMessage = "Please set a Primary Asset first, then go to the Procedure tab." |
---|
| 167 | redirect(controller:"taskDetailed", action:"show", id:params.linkedTask?.id) |
---|
| 168 | return |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | def taskProcedureInstance = new TaskProcedure() |
---|
| 172 | taskProcedureInstance.properties = params |
---|
| 173 | return ['taskProcedureInstance':taskProcedureInstance] |
---|
[133] | 174 | } |
---|
| 175 | |
---|
| 176 | def save = { |
---|
| 177 | def taskProcedureInstance = new TaskProcedure(params) |
---|
[772] | 178 | taskProcedureInstance.createdBy = authService.currentUser |
---|
| 179 | taskProcedureInstance.lastUpdatedBy = authService.currentUser |
---|
[762] | 180 | def taskInstance = Task.get(params.linkedTask.id) |
---|
[147] | 181 | |
---|
[770] | 182 | // Gaps in the html index's can be created by adding 2 items and removing the first one. |
---|
| 183 | // This creates a gap at the missing index where LazyList will return a null. |
---|
| 184 | def nullMaintenanceActions = taskProcedureInstance.maintenanceActions.findAll {!it} |
---|
| 185 | if (nullMaintenanceActions) { |
---|
| 186 | taskProcedureInstance.maintenanceActions.removeAll(nullMaintenanceActions) |
---|
| 187 | } |
---|
| 188 | |
---|
[178] | 189 | if(!taskProcedureInstance.hasErrors() && taskProcedureInstance.save(flush: true)) { |
---|
[770] | 190 | // Also sets: taskInstance.taskProcedure = taskProcedureInstance |
---|
| 191 | taskProcedureInstance.addToTasks(taskInstance) |
---|
[147] | 192 | flash.message = "TaskProcedure ${taskProcedureInstance.id} created." |
---|
[770] | 193 | redirect(controller:'taskDetailed', |
---|
| 194 | action:'show', |
---|
| 195 | id:taskProcedureInstance.linkedTask.id, |
---|
| 196 | params:[showTab:"showProcedureTab"]) |
---|
[133] | 197 | } |
---|
| 198 | else { |
---|
[770] | 199 | // Populate maintenanceAction errors for display. |
---|
| 200 | taskProcedureInstance.maintenanceActions.each { it.validate() } |
---|
| 201 | render(view:'create',model:[taskProcedureInstance:taskProcedureInstance]) |
---|
[133] | 202 | } |
---|
| 203 | } |
---|
[770] | 204 | |
---|
[133] | 205 | } |
---|