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