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 | /// @todo: Refactor to taskService and include moving task to "In Progress" when Entry.duration is updated. |
---|
70 | def update = { |
---|
71 | def entryInstance = Entry.get( params.id ) |
---|
72 | if(entryInstance) { |
---|
73 | // The update method only accepts post requests, so this is just in case. |
---|
74 | if(entryInstance.enteredBy.loginName == authService.currentUser.loginName) { |
---|
75 | entryInstance.properties = params |
---|
76 | if(!entryInstance.hasErrors() && entryInstance.save(flush: true)) { |
---|
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 | } |
---|
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 | } |
---|
94 | } |
---|
95 | else { |
---|
96 | flash.message = "You may only edit your own entries." |
---|
97 | redirect(action:show,id:entryInstance.id) |
---|
98 | } |
---|
99 | } |
---|
100 | else { |
---|
101 | flash.message = "Entry not found with id ${params.id}" |
---|
102 | redirect(controller: "taskDetailed", action:"search") |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | def ajaxCreate = { |
---|
107 | if(!params.taskId || !params.entryTypeId) { |
---|
108 | response.status = 403 |
---|
109 | params.errorMessage = g.message(code: "entry.create.no.params.ajax") |
---|
110 | render(template: "/shared/messages") |
---|
111 | return |
---|
112 | } |
---|
113 | |
---|
114 | def taskInstance = Task.read(params.taskId) |
---|
115 | |
---|
116 | if(!taskInstance) { |
---|
117 | response.status = 403 |
---|
118 | params.errorMessage = g.message(code:"default.not.found", args:['Task',params.taskId]) |
---|
119 | render(template: "/shared/messages") |
---|
120 | return |
---|
121 | } |
---|
122 | |
---|
123 | // Check for Complete task. |
---|
124 | if(taskInstance.taskStatus.id == 3) { |
---|
125 | response.status = 403 |
---|
126 | params.errorMessage = g.message(code:"task.operationNotPermittedOnCompleteTask") |
---|
127 | render(template: "/shared/messages") |
---|
128 | return |
---|
129 | } |
---|
130 | |
---|
131 | // Success. |
---|
132 | def entryInstance = new Entry() |
---|
133 | entryInstance.task = taskInstance |
---|
134 | entryInstance.entryType = EntryType.read(params.entryTypeId) |
---|
135 | render(template: "create", model: ['entryInstance': entryInstance]) |
---|
136 | } |
---|
137 | |
---|
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 | |
---|
164 | def create = { |
---|
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 |
---|
169 | } |
---|
170 | |
---|
171 | def taskInstance = Task.read(params.taskInstance.id) |
---|
172 | |
---|
173 | if(!taskInstance) { |
---|
174 | flash.message = g.message(code:"task.notFound") |
---|
175 | redirect(controller:"taskDetailed", action:"search") |
---|
176 | return |
---|
177 | } |
---|
178 | |
---|
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 | |
---|
192 | def save = { |
---|
193 | def result = taskService.saveEntry(params) |
---|
194 | |
---|
195 | if(!result.error) { |
---|
196 | flash.message = "Entry created." |
---|
197 | redirect(controller: "taskDetailed", action: "show", id: result.taskId) |
---|
198 | return |
---|
199 | } |
---|
200 | |
---|
201 | if(result.error.code != "default.create.failure") { |
---|
202 | params.errorMessage = g.message(code: result.error.code) |
---|
203 | } |
---|
204 | |
---|
205 | render(view:'create',model:[entryInstance: result.entryInstance]) |
---|
206 | } // save |
---|
207 | |
---|
208 | } // end class |
---|