source: trunk/grails-app/controllers/EntryDetailedController.groovy @ 873

Last change on this file since 873 was 873, checked in by gav, 13 years ago

Refactor logic into TaskService.checkCreateEntry().
Prevent entry creation on recurring tasks altogether.
Prevent entry creation other than PM Entries on tasks with a procedure.

File size: 8.5 KB
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
4class 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        def entryInstance = new Entry()
116        entryInstance.task = taskInstance
117        entryInstance.entryType = EntryType.read(params.entryTypeId)
118
119        // Check if we should create this entry.
120        def checkResult = taskService.checkCreateEntry(entryInstance)
121        if(checkResult.error) {
122            params.errorMessage = g.message(code: checkResult.error.code)
123            render(contentType:"text/json", status: 403, template: "/shared/messages")
124            return
125        }
126
127        // Success.
128        def model = ['entryInstance': entryInstance]
129
130        render(contentType:"text/json") {
131            updates = array {
132                element([ mode: 'replace', target:"$params.target", content: g.render(template: 'create', model:model) ])
133            }
134        }
135
136    } // ajaxCreate
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
150            def taskInstance = entryList[0].task
151            def taskModificationList = TaskModification.findAllByTask(taskInstance, [max:100, sort:"id", order:"asc"])
152            def model = ['entryList': entryList]
153            def taskModel = ['taskInstance': taskInstance]
154            def taskModificationModel = ['taskModificationList': taskModificationList ]
155
156            render(contentType:"text/json") {
157                updates = array {
158                    element([ mode: 'replace', target:"$params.target", content: g.render(template: 'list', model:model) ])
159                    element([ mode: 'replace',
160                                    target:".tabHeader",
161                                    content: g.render(template: '/taskDetailed/showTabHeader', model:taskModel) ])
162                    element([ mode: 'replace',
163                                    target:"#taskControlButtons",
164                                    content: g.render(template: '/taskDetailed/showTaskControlButtons', model:taskModel) ])
165                    element([ mode: 'replace',
166                                    target:"#modifications",
167                                    content: g.render(template: '/taskDetailed/showTaskModifications', model:taskModificationModel) ])
168                }
169            }
170            return
171        } // Success.
172
173        if(result.error.code != "default.create.failure")
174            params.errorMessage = g.message(code: result.error.code)
175
176        def model = ['entryInstance': result.entryInstance]
177        render(contentType:"text/json", status: 403, template: "create", model: model)
178    } // ajaxSave
179
180    def create = {
181        if(!params.taskInstance?.id || !params.entryType?.id) {
182            flash.message = g.message(code:"entry.create.no.params")
183            redirect(controller:"taskDetailed", action:"search")
184            return
185        }
186
187        def taskInstance = Task.read(params.taskInstance.id)
188
189        if(!taskInstance) {
190            flash.message = g.message(code:"task.notFound")
191            redirect(controller:"taskDetailed", action:"search")
192            return
193        }
194
195        // Check for Complete task.
196        if(taskInstance.taskStatus.id == 3) {
197            flash.errorMessage = g.message(code:"task.operationNotPermittedOnCompleteTask")
198            redirect(controller:"taskDetailed", action:"show", id: taskInstance.id)
199            return
200        }
201
202        def entryInstance = new Entry()
203        entryInstance.task = taskInstance
204        entryInstance.entryType = EntryType.read(params.entryType.id)
205        return ['entryInstance': entryInstance]
206    } // create
207
208    def save = {
209        def result = taskService.saveEntry(params)
210
211        if(!result.error) {
212            flash.message = "Entry created."
213            redirect(controller: "taskDetailed", action: "show", id: result.taskId)
214            return
215        }
216
217        if(result.error.code != "default.create.failure") {
218            params.errorMessage = g.message(code: result.error.code)
219        }
220
221        render(view:'create',model:[entryInstance: result.entryInstance])
222    } // save
223
224} // end class
Note: See TracBrowser for help on using the repository browser.