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', ajaxCreate:'POST', ajaxSave:'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 | params.errorMessage = g.message(code: "entry.create.no.params.ajax") |
---|
109 | render(contentType:"text/json", status: 403, template: "/shared/messages") |
---|
110 | return |
---|
111 | } |
---|
112 | |
---|
113 | def taskInstance = Task.read(params.taskId) |
---|
114 | |
---|
115 | if(!taskInstance) { |
---|
116 | params.errorMessage = g.message(code:"default.not.found", args:['Task',params.taskId]) |
---|
117 | render(contentType:"text/json", status: 403, template: "/shared/messages") |
---|
118 | return |
---|
119 | } |
---|
120 | |
---|
121 | // Check for Complete task. |
---|
122 | if(taskInstance.taskStatus.id == 3) { |
---|
123 | params.errorMessage = g.message(code:"task.operationNotPermittedOnCompleteTask") |
---|
124 | render(contentType:"text/json", status: 403, template: "/shared/messages") |
---|
125 | return |
---|
126 | } |
---|
127 | |
---|
128 | // Success. |
---|
129 | def entryInstance = new Entry() |
---|
130 | entryInstance.task = taskInstance |
---|
131 | entryInstance.entryType = EntryType.read(params.entryTypeId) |
---|
132 | def model = ['entryInstance': entryInstance] |
---|
133 | |
---|
134 | render(contentType:"text/json") { |
---|
135 | updates = array { |
---|
136 | element([ mode: 'replace', target:"$params.target", content: g.render(template: 'create', model:model) ]) |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | } // ajaxCreate |
---|
141 | |
---|
142 | def ajaxSave = { |
---|
143 | def result = taskService.saveEntry(params) |
---|
144 | |
---|
145 | // Success. |
---|
146 | if(!result.error) { |
---|
147 | def entryList = Entry.withCriteria { |
---|
148 | eq("entryType", result.entryInstance.entryType) |
---|
149 | task { |
---|
150 | idEq(result.taskId) |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | def taskInstance = entryList[0].task |
---|
155 | def taskModificationList = TaskModification.findAllByTask(taskInstance, [max:100, sort:"id", order:"asc"]) |
---|
156 | def model = ['entryList': entryList] |
---|
157 | def taskModel = ['taskInstance': taskInstance] |
---|
158 | def taskModificationModel = ['taskModificationList': taskModificationList ] |
---|
159 | |
---|
160 | render(contentType:"text/json") { |
---|
161 | updates = array { |
---|
162 | element([ mode: 'replace', target:"$params.target", content: g.render(template: 'list', model:model) ]) |
---|
163 | element([ mode: 'replace', |
---|
164 | target:".tabHeader", |
---|
165 | content: g.render(template: '/taskDetailed/showTabHeader', model:taskModel) ]) |
---|
166 | element([ mode: 'replace', |
---|
167 | target:"#taskControlButtons", |
---|
168 | content: g.render(template: '/taskDetailed/showTaskControlButtons', model:taskModel) ]) |
---|
169 | element([ mode: 'replace', |
---|
170 | target:"#modifications", |
---|
171 | content: g.render(template: '/taskDetailed/showTaskModifications', model:taskModificationModel) ]) |
---|
172 | } |
---|
173 | } |
---|
174 | return |
---|
175 | } // Success. |
---|
176 | |
---|
177 | if(result.error.code != "default.create.failure") |
---|
178 | params.errorMessage = g.message(code: result.error.code) |
---|
179 | |
---|
180 | def model = ['entryInstance': result.entryInstance] |
---|
181 | render(contentType:"text/json", status: 403, template: "create", model: model) |
---|
182 | } // ajaxSave |
---|
183 | |
---|
184 | def create = { |
---|
185 | if(!params.taskInstance?.id || !params.entryType?.id) { |
---|
186 | flash.message = g.message(code:"entry.create.no.params") |
---|
187 | redirect(controller:"taskDetailed", action:"search") |
---|
188 | return |
---|
189 | } |
---|
190 | |
---|
191 | def taskInstance = Task.read(params.taskInstance.id) |
---|
192 | |
---|
193 | if(!taskInstance) { |
---|
194 | flash.message = g.message(code:"task.notFound") |
---|
195 | redirect(controller:"taskDetailed", action:"search") |
---|
196 | return |
---|
197 | } |
---|
198 | |
---|
199 | // Check for Complete task. |
---|
200 | if(taskInstance.taskStatus.id == 3) { |
---|
201 | flash.errorMessage = g.message(code:"task.operationNotPermittedOnCompleteTask") |
---|
202 | redirect(controller:"taskDetailed", action:"show", id: taskInstance.id) |
---|
203 | return |
---|
204 | } |
---|
205 | |
---|
206 | def entryInstance = new Entry() |
---|
207 | entryInstance.task = taskInstance |
---|
208 | entryInstance.entryType = EntryType.read(params.entryType.id) |
---|
209 | return ['entryInstance': entryInstance] |
---|
210 | } // create |
---|
211 | |
---|
212 | def save = { |
---|
213 | def result = taskService.saveEntry(params) |
---|
214 | |
---|
215 | if(!result.error) { |
---|
216 | flash.message = "Entry created." |
---|
217 | redirect(controller: "taskDetailed", action: "show", id: result.taskId) |
---|
218 | return |
---|
219 | } |
---|
220 | |
---|
221 | if(result.error.code != "default.create.failure") { |
---|
222 | params.errorMessage = g.message(code: result.error.code) |
---|
223 | } |
---|
224 | |
---|
225 | render(view:'create',model:[entryInstance: result.entryInstance]) |
---|
226 | } // save |
---|
227 | |
---|
228 | } // end class |
---|