[91] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
| 2 | |
---|
| 3 | class EntryDetailedController extends BaseController { |
---|
| 4 | |
---|
[185] | 5 | def personService |
---|
[186] | 6 | def taskService |
---|
[147] | 7 | |
---|
[91] | 8 | def index = { redirect(action:list,params:params) } |
---|
| 9 | |
---|
| 10 | // the delete, save and update actions only accept POST requests |
---|
| 11 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
| 12 | |
---|
| 13 | def list = { |
---|
| 14 | if(!params.max) params.max = 10 |
---|
| 15 | [ entryInstanceList: Entry.list( params ) ] |
---|
| 16 | } |
---|
| 17 | |
---|
| 18 | def show = { |
---|
| 19 | def entryInstance = Entry.get( params.id ) |
---|
| 20 | |
---|
| 21 | if(!entryInstance) { |
---|
| 22 | flash.message = "Entry not found with id ${params.id}" |
---|
[186] | 23 | redirect(controller: 'taskDetailed', action: 'search') |
---|
[91] | 24 | } |
---|
| 25 | else { return [ entryInstance : entryInstance ] } |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | def delete = { |
---|
| 29 | def entryInstance = Entry.get( params.id ) |
---|
| 30 | if(entryInstance) { |
---|
[216] | 31 | if(entryInstance.enteredBy.loginName == personService.currentUser.loginName) { |
---|
[185] | 32 | def taskID = entryInstance.task.id |
---|
| 33 | entryInstance.delete(flush:true) |
---|
[98] | 34 | flash.message = "Entry ${params.id} deleted" |
---|
[185] | 35 | redirect(controller: 'taskDetailed', action: 'show', id: taskID) |
---|
[98] | 36 | } |
---|
| 37 | else { |
---|
| 38 | flash.message = "You may only delete your own entries." |
---|
| 39 | redirect(action:show,id:entryInstance.id) |
---|
| 40 | } |
---|
| 41 | |
---|
[91] | 42 | } |
---|
| 43 | else { |
---|
| 44 | flash.message = "Entry not found with id ${params.id}" |
---|
[186] | 45 | redirect(controller: "taskDetailed", action:"search") |
---|
[91] | 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | def edit = { |
---|
| 50 | def entryInstance = Entry.get( params.id ) |
---|
| 51 | if(!entryInstance) { |
---|
[98] | 52 | flash.message = "Entry not found with id ${params.id}" |
---|
[186] | 53 | redirect(controller: "taskDetailed", action:"search") |
---|
[91] | 54 | } |
---|
| 55 | else { |
---|
[98] | 56 | |
---|
[216] | 57 | if(entryInstance.enteredBy.loginName == personService.currentUser.loginName) { |
---|
[98] | 58 | return [ entryInstance : entryInstance ] |
---|
| 59 | } |
---|
| 60 | else { |
---|
| 61 | flash.message = "You may only edit your own entries." |
---|
| 62 | redirect(action:show,id:entryInstance.id) |
---|
| 63 | } |
---|
| 64 | |
---|
[91] | 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | def update = { |
---|
| 69 | def entryInstance = Entry.get( params.id ) |
---|
| 70 | if(entryInstance) { |
---|
[185] | 71 | // The update method only accepts post requests, so this is just in case. |
---|
[216] | 72 | if(entryInstance.enteredBy.loginName == personService.currentUser.loginName) { |
---|
[185] | 73 | entryInstance.properties = params |
---|
| 74 | if(!entryInstance.hasErrors() && entryInstance.save(flush: true)) { |
---|
| 75 | flash.message = "Entry ${params.id} updated" |
---|
| 76 | redirect(action:show,id:entryInstance.id) |
---|
| 77 | } |
---|
| 78 | else { |
---|
| 79 | render(view:'edit',model:[entryInstance:entryInstance]) |
---|
| 80 | } |
---|
[91] | 81 | } |
---|
| 82 | else { |
---|
[185] | 83 | flash.message = "You may only edit your own entries." |
---|
| 84 | redirect(action:show,id:entryInstance.id) |
---|
[91] | 85 | } |
---|
| 86 | } |
---|
| 87 | else { |
---|
| 88 | flash.message = "Entry not found with id ${params.id}" |
---|
[186] | 89 | redirect(controller: "taskDetailed", action:"search") |
---|
[91] | 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | def create = { |
---|
[98] | 94 | try { |
---|
| 95 | def taskInstance = Task.get(params.taskInstance.id) |
---|
| 96 | def entryInstance = new Entry() |
---|
| 97 | entryInstance.task = taskInstance |
---|
[191] | 98 | entryInstance.entryType = EntryType.get(params.entryType.id) |
---|
[98] | 99 | return ['entryInstance':entryInstance] |
---|
| 100 | } |
---|
| 101 | catch(Exception e) { |
---|
[186] | 102 | flash.message = "Please select a task, then 'Add Entry'." |
---|
| 103 | redirect(controller:"taskDetailed", action:"search") |
---|
[98] | 104 | } |
---|
[91] | 105 | } |
---|
| 106 | |
---|
| 107 | def save = { |
---|
[186] | 108 | def result = taskService.createEntry(params) |
---|
[91] | 109 | |
---|
[186] | 110 | if(!result.error) { |
---|
| 111 | flash.message = "Entry created." |
---|
| 112 | redirect(controller: "taskDetailed", action: "show", id: result.taskId) |
---|
[91] | 113 | } |
---|
| 114 | else { |
---|
[186] | 115 | if(result.entryInstance) { |
---|
| 116 | render(view:'create',model:[entryInstance: result.entryInstance]) |
---|
| 117 | } |
---|
| 118 | else { |
---|
| 119 | flash.message = "Could not create entry." |
---|
| 120 | redirect(controller: "taskDetailed", action:"search") |
---|
| 121 | } |
---|
| 122 | |
---|
[91] | 123 | } |
---|
| 124 | } |
---|
[186] | 125 | |
---|
[91] | 126 | } |
---|