[91] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
| 2 | |
---|
[298] | 3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser']) |
---|
[91] | 4 | class EntryDetailedController extends BaseController { |
---|
| 5 | |
---|
[291] | 6 | def authService |
---|
[186] | 7 | def taskService |
---|
[147] | 8 | |
---|
[91] | 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 | if(!params.max) params.max = 10 |
---|
| 16 | [ entryInstanceList: Entry.list( params ) ] |
---|
| 17 | } |
---|
| 18 | |
---|
| 19 | def show = { |
---|
| 20 | def entryInstance = Entry.get( params.id ) |
---|
| 21 | |
---|
| 22 | if(!entryInstance) { |
---|
| 23 | flash.message = "Entry not found with id ${params.id}" |
---|
[186] | 24 | redirect(controller: 'taskDetailed', action: 'search') |
---|
[91] | 25 | } |
---|
| 26 | else { return [ entryInstance : entryInstance ] } |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | def delete = { |
---|
| 30 | def entryInstance = Entry.get( params.id ) |
---|
| 31 | if(entryInstance) { |
---|
[291] | 32 | if(entryInstance.enteredBy.loginName == authService.currentUser.loginName) { |
---|
[833] | 33 | def taskId = entryInstance.task.id |
---|
[185] | 34 | entryInstance.delete(flush:true) |
---|
[98] | 35 | flash.message = "Entry ${params.id} deleted" |
---|
[833] | 36 | redirect(controller: 'taskDetailed', action: 'show', id: taskId) |
---|
[98] | 37 | } |
---|
| 38 | else { |
---|
| 39 | flash.message = "You may only delete your own entries." |
---|
| 40 | redirect(action:show,id:entryInstance.id) |
---|
| 41 | } |
---|
| 42 | |
---|
[91] | 43 | } |
---|
| 44 | else { |
---|
| 45 | flash.message = "Entry not found with id ${params.id}" |
---|
[186] | 46 | redirect(controller: "taskDetailed", action:"search") |
---|
[91] | 47 | } |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | def edit = { |
---|
| 51 | def entryInstance = Entry.get( params.id ) |
---|
| 52 | if(!entryInstance) { |
---|
[98] | 53 | flash.message = "Entry not found with id ${params.id}" |
---|
[186] | 54 | redirect(controller: "taskDetailed", action:"search") |
---|
[91] | 55 | } |
---|
| 56 | else { |
---|
[98] | 57 | |
---|
[291] | 58 | if(entryInstance.enteredBy.loginName == authService.currentUser.loginName) { |
---|
[98] | 59 | return [ entryInstance : entryInstance ] |
---|
| 60 | } |
---|
| 61 | else { |
---|
| 62 | flash.message = "You may only edit your own entries." |
---|
| 63 | redirect(action:show,id:entryInstance.id) |
---|
| 64 | } |
---|
| 65 | |
---|
[91] | 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
[838] | 69 | /// @todo: Refactor to taskService and include moving task to "In Progress" when Entry.duration is updated. |
---|
[91] | 70 | def update = { |
---|
| 71 | def entryInstance = Entry.get( params.id ) |
---|
| 72 | if(entryInstance) { |
---|
[185] | 73 | // The update method only accepts post requests, so this is just in case. |
---|
[291] | 74 | if(entryInstance.enteredBy.loginName == authService.currentUser.loginName) { |
---|
[185] | 75 | entryInstance.properties = params |
---|
| 76 | if(!entryInstance.hasErrors() && entryInstance.save(flush: true)) { |
---|
[838] | 77 | // If PM Entry update task.highestSeverity |
---|
| 78 | if(entryInstance.entryType.id == 6) { |
---|
| 79 | def clist = [] |
---|
| 80 | entryInstance.task.entries.each { entry -> |
---|
| 81 | if(entry.entryType.id == 6) |
---|
| 82 | clist << entry.highestSeverity |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | if(clist) |
---|
| 86 | entryInstance.task.highestSeverity = clist.sort{p1,p2 -> p2.id <=> p1.id}[0] |
---|
| 87 | } |
---|
[185] | 88 | flash.message = "Entry ${params.id} updated" |
---|
| 89 | redirect(action:show,id:entryInstance.id) |
---|
| 90 | } |
---|
| 91 | else { |
---|
| 92 | render(view:'edit',model:[entryInstance:entryInstance]) |
---|
| 93 | } |
---|
[91] | 94 | } |
---|
| 95 | else { |
---|
[185] | 96 | flash.message = "You may only edit your own entries." |
---|
| 97 | redirect(action:show,id:entryInstance.id) |
---|
[91] | 98 | } |
---|
| 99 | } |
---|
| 100 | else { |
---|
| 101 | flash.message = "Entry not found with id ${params.id}" |
---|
[186] | 102 | redirect(controller: "taskDetailed", action:"search") |
---|
[91] | 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
[826] | 106 | def ajaxCreate = { |
---|
| 107 | if(!params.taskId || !params.entryTypeId) { |
---|
[833] | 108 | response.status = 403 |
---|
| 109 | params.errorMessage = g.message(code: "entry.create.no.params.ajax") |
---|
| 110 | render(template: "/shared/messages") |
---|
[826] | 111 | return |
---|
| 112 | } |
---|
[822] | 113 | |
---|
[826] | 114 | def taskInstance = Task.read(params.taskId) |
---|
[822] | 115 | |
---|
[826] | 116 | if(!taskInstance) { |
---|
[833] | 117 | response.status = 403 |
---|
| 118 | params.errorMessage = g.message(code:"default.not.found", args:['Task',params.taskId]) |
---|
| 119 | render(template: "/shared/messages") |
---|
[826] | 120 | return |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | // Check for Complete task. |
---|
| 124 | if(taskInstance.taskStatus.id == 3) { |
---|
[833] | 125 | response.status = 403 |
---|
| 126 | params.errorMessage = g.message(code:"task.operationNotPermittedOnCompleteTask") |
---|
| 127 | render(template: "/shared/messages") |
---|
[826] | 128 | return |
---|
| 129 | } |
---|
| 130 | |
---|
[833] | 131 | // Success. |
---|
[826] | 132 | def entryInstance = new Entry() |
---|
| 133 | entryInstance.task = taskInstance |
---|
| 134 | entryInstance.entryType = EntryType.read(params.entryTypeId) |
---|
| 135 | render(template: "create", model: ['entryInstance': entryInstance]) |
---|
[822] | 136 | } |
---|
| 137 | |
---|
[833] | 138 | def ajaxSave = { |
---|
| 139 | def result = taskService.saveEntry(params) |
---|
| 140 | |
---|
| 141 | // Success. |
---|
| 142 | if(!result.error) { |
---|
| 143 | def entryList = Entry.withCriteria { |
---|
| 144 | eq("entryType", result.entryInstance.entryType) |
---|
| 145 | task { |
---|
| 146 | idEq(result.taskId) |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | render(template: "list", model: ['entryList': entryList]) |
---|
| 150 | return |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | if(result.error.code != "default.create.failure") { |
---|
| 154 | response.status = 403 |
---|
| 155 | params.errorMessage = g.message(code: result.error.code) |
---|
| 156 | render(template: "/shared/messages") |
---|
| 157 | return |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | response.status = 403 |
---|
| 161 | render(template: "create", model: ['entryInstance': result.entryInstance]) |
---|
| 162 | } // ajaxSave |
---|
| 163 | |
---|
[91] | 164 | def create = { |
---|
[482] | 165 | if(!params.taskInstance?.id || !params.entryType?.id) { |
---|
| 166 | flash.message = g.message(code:"entry.create.no.params") |
---|
| 167 | redirect(controller:"taskDetailed", action:"search") |
---|
| 168 | return |
---|
[98] | 169 | } |
---|
[482] | 170 | |
---|
| 171 | def taskInstance = Task.read(params.taskInstance.id) |
---|
| 172 | |
---|
| 173 | if(!taskInstance) { |
---|
| 174 | flash.message = g.message(code:"task.notFound") |
---|
[186] | 175 | redirect(controller:"taskDetailed", action:"search") |
---|
[482] | 176 | return |
---|
[98] | 177 | } |
---|
[91] | 178 | |
---|
[482] | 179 | // Check for Complete task. |
---|
| 180 | if(taskInstance.taskStatus.id == 3) { |
---|
| 181 | flash.errorMessage = g.message(code:"task.operationNotPermittedOnCompleteTask") |
---|
| 182 | redirect(controller:"taskDetailed", action:"show", id: taskInstance.id) |
---|
| 183 | return |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | def entryInstance = new Entry() |
---|
| 187 | entryInstance.task = taskInstance |
---|
| 188 | entryInstance.entryType = EntryType.read(params.entryType.id) |
---|
| 189 | return ['entryInstance': entryInstance] |
---|
| 190 | } // create |
---|
| 191 | |
---|
[91] | 192 | def save = { |
---|
[394] | 193 | def result = taskService.saveEntry(params) |
---|
[91] | 194 | |
---|
[186] | 195 | if(!result.error) { |
---|
| 196 | flash.message = "Entry created." |
---|
| 197 | redirect(controller: "taskDetailed", action: "show", id: result.taskId) |
---|
[482] | 198 | return |
---|
[91] | 199 | } |
---|
[186] | 200 | |
---|
[482] | 201 | if(result.error.code != "default.create.failure") { |
---|
| 202 | params.errorMessage = g.message(code: result.error.code) |
---|
[91] | 203 | } |
---|
[186] | 204 | |
---|
[482] | 205 | render(view:'create',model:[entryInstance: result.entryInstance]) |
---|
| 206 | } // save |
---|
| 207 | |
---|
| 208 | } // end class |
---|