| 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured | 
|---|
| 2 |  | 
|---|
| 3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser']) | 
|---|
| 4 | class EntryDetailedController extends BaseController { | 
|---|
| 5 |  | 
|---|
| 6 |     def authService | 
|---|
| 7 |     def taskService | 
|---|
| 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 |         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}" | 
|---|
| 24 |             redirect(controller: 'taskDetailed', action: 'search') | 
|---|
| 25 |         } | 
|---|
| 26 |         else { return [ entryInstance : entryInstance ] } | 
|---|
| 27 |     } | 
|---|
| 28 |  | 
|---|
| 29 |     def delete = { | 
|---|
| 30 |         def entryInstance = Entry.get( params.id ) | 
|---|
| 31 |         if(entryInstance) { | 
|---|
| 32 |             if(entryInstance.enteredBy.loginName == authService.currentUser.loginName) { | 
|---|
| 33 |                 def taskID = entryInstance.task.id | 
|---|
| 34 |                 entryInstance.delete(flush:true) | 
|---|
| 35 |                 flash.message = "Entry ${params.id} deleted" | 
|---|
| 36 |                 redirect(controller: 'taskDetailed', action: 'show', id: taskID) | 
|---|
| 37 |             } | 
|---|
| 38 |             else { | 
|---|
| 39 |                 flash.message = "You may only delete your own entries." | 
|---|
| 40 |                 redirect(action:show,id:entryInstance.id) | 
|---|
| 41 |             } | 
|---|
| 42 |  | 
|---|
| 43 |         } | 
|---|
| 44 |         else { | 
|---|
| 45 |             flash.message = "Entry not found with id ${params.id}" | 
|---|
| 46 |             redirect(controller: "taskDetailed", action:"search") | 
|---|
| 47 |         } | 
|---|
| 48 |     } | 
|---|
| 49 |  | 
|---|
| 50 |     def edit = { | 
|---|
| 51 |         def entryInstance = Entry.get( params.id ) | 
|---|
| 52 |         if(!entryInstance) { | 
|---|
| 53 |                 flash.message = "Entry not found with id ${params.id}" | 
|---|
| 54 |                 redirect(controller: "taskDetailed", action:"search") | 
|---|
| 55 |         } | 
|---|
| 56 |         else { | 
|---|
| 57 |  | 
|---|
| 58 |             if(entryInstance.enteredBy.loginName == authService.currentUser.loginName) { | 
|---|
| 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 |  | 
|---|
| 66 |         } | 
|---|
| 67 |     } | 
|---|
| 68 |  | 
|---|
| 69 |     def update = { | 
|---|
| 70 |         def entryInstance = Entry.get( params.id ) | 
|---|
| 71 |         if(entryInstance) { | 
|---|
| 72 |             // The update method only accepts post requests, so this is just in case. | 
|---|
| 73 |             if(entryInstance.enteredBy.loginName == authService.currentUser.loginName) { | 
|---|
| 74 |                 entryInstance.properties = params | 
|---|
| 75 |                 if(!entryInstance.hasErrors() && entryInstance.save(flush: true)) { | 
|---|
| 76 |                     flash.message = "Entry ${params.id} updated" | 
|---|
| 77 |                     redirect(action:show,id:entryInstance.id) | 
|---|
| 78 |                 } | 
|---|
| 79 |                 else { | 
|---|
| 80 |                     render(view:'edit',model:[entryInstance:entryInstance]) | 
|---|
| 81 |                 } | 
|---|
| 82 |             } | 
|---|
| 83 |             else { | 
|---|
| 84 |                 flash.message = "You may only edit your own entries." | 
|---|
| 85 |                 redirect(action:show,id:entryInstance.id) | 
|---|
| 86 |             } | 
|---|
| 87 |         } | 
|---|
| 88 |         else { | 
|---|
| 89 |             flash.message = "Entry not found with id ${params.id}" | 
|---|
| 90 |             redirect(controller: "taskDetailed", action:"search") | 
|---|
| 91 |         } | 
|---|
| 92 |     } | 
|---|
| 93 |  | 
|---|
| 94 |     def create = { | 
|---|
| 95 |         if(!params.taskInstance?.id || !params.entryType?.id) { | 
|---|
| 96 |             flash.message = g.message(code:"entry.create.no.params") | 
|---|
| 97 |             redirect(controller:"taskDetailed", action:"search") | 
|---|
| 98 |             return | 
|---|
| 99 |         } | 
|---|
| 100 |  | 
|---|
| 101 |         def taskInstance = Task.read(params.taskInstance.id) | 
|---|
| 102 |  | 
|---|
| 103 |         if(!taskInstance) { | 
|---|
| 104 |             flash.message = g.message(code:"task.notFound") | 
|---|
| 105 |             redirect(controller:"taskDetailed", action:"search") | 
|---|
| 106 |             return | 
|---|
| 107 |         } | 
|---|
| 108 |  | 
|---|
| 109 |         // Check for Complete task. | 
|---|
| 110 |         if(taskInstance.taskStatus.id == 3) { | 
|---|
| 111 |             flash.errorMessage = g.message(code:"task.operationNotPermittedOnCompleteTask") | 
|---|
| 112 |             redirect(controller:"taskDetailed", action:"show", id: taskInstance.id) | 
|---|
| 113 |             return | 
|---|
| 114 |         } | 
|---|
| 115 |  | 
|---|
| 116 |         def entryInstance = new Entry() | 
|---|
| 117 |         entryInstance.task = taskInstance | 
|---|
| 118 |         entryInstance.entryType = EntryType.read(params.entryType.id) | 
|---|
| 119 |         return ['entryInstance': entryInstance] | 
|---|
| 120 |     } // create | 
|---|
| 121 |  | 
|---|
| 122 |     def save = { | 
|---|
| 123 |         def result = taskService.saveEntry(params) | 
|---|
| 124 |  | 
|---|
| 125 |         if(!result.error) { | 
|---|
| 126 |             flash.message = "Entry created." | 
|---|
| 127 |             redirect(controller: "taskDetailed", action: "show", id: result.taskId) | 
|---|
| 128 |             return | 
|---|
| 129 |         } | 
|---|
| 130 |  | 
|---|
| 131 |         if(result.error.code != "default.create.failure") { | 
|---|
| 132 |             params.errorMessage = g.message(code: result.error.code) | 
|---|
| 133 |         } | 
|---|
| 134 |  | 
|---|
| 135 |         render(view:'create',model:[entryInstance: result.entryInstance]) | 
|---|
| 136 |     } // save | 
|---|
| 137 |  | 
|---|
| 138 | } // end class | 
|---|