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

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

AJAX Work Done and PM Entry, second draft.

File size: 6.7 KB
RevLine 
[91]1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
[298]3@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
[91]4class EntryDetailedController extends BaseController {
5
[291]6    def authService
[186]7    def taskService
[147]8
[91]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}"
[186]24            redirect(controller: 'taskDetailed', action: 'search')
[91]25        }
26        else { return [ entryInstance : entryInstance ] }
27    }
28
29    def delete = {
30        def entryInstance = Entry.get( params.id )
31        if(entryInstance) {
[291]32            if(entryInstance.enteredBy.loginName == authService.currentUser.loginName) {
[833]33                def taskId = entryInstance.task.id
[185]34                entryInstance.delete(flush:true)
[98]35                flash.message = "Entry ${params.id} deleted"
[833]36                redirect(controller: 'taskDetailed', action: 'show', id: taskId)
[98]37            }
38            else {
39                flash.message = "You may only delete your own entries."
40                redirect(action:show,id:entryInstance.id)
41            }
42
[91]43        }
44        else {
45            flash.message = "Entry not found with id ${params.id}"
[186]46            redirect(controller: "taskDetailed", action:"search")
[91]47        }
48    }
49
50    def edit = {
51        def entryInstance = Entry.get( params.id )
52        if(!entryInstance) {
[98]53                flash.message = "Entry not found with id ${params.id}"
[186]54                redirect(controller: "taskDetailed", action:"search")
[91]55        }
56        else {
[98]57
[291]58            if(entryInstance.enteredBy.loginName == authService.currentUser.loginName) {
[98]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
[91]66        }
67    }
68
69    def update = {
70        def entryInstance = Entry.get( params.id )
71        if(entryInstance) {
[185]72            // The update method only accepts post requests, so this is just in case.
[291]73            if(entryInstance.enteredBy.loginName == authService.currentUser.loginName) {
[185]74                entryInstance.properties = params
75                if(!entryInstance.hasErrors() && entryInstance.save(flush: true)) {
76                    flash.message = "Entry ${params.id} updated"
77                    redirect(action:show,id:entryInstance.id)
78                }
79                else {
80                    render(view:'edit',model:[entryInstance:entryInstance])
81                }
[91]82            }
83            else {
[185]84                flash.message = "You may only edit your own entries."
85                redirect(action:show,id:entryInstance.id)
[91]86            }
87        }
88        else {
89            flash.message = "Entry not found with id ${params.id}"
[186]90            redirect(controller: "taskDetailed", action:"search")
[91]91        }
92    }
93
[826]94    def ajaxCreate = {
95        if(!params.taskId || !params.entryTypeId) {
[833]96            response.status = 403
97            params.errorMessage = g.message(code: "entry.create.no.params.ajax")
98            render(template: "/shared/messages")
[826]99            return
100        }
[822]101
[826]102        def taskInstance = Task.read(params.taskId)
[822]103
[826]104        if(!taskInstance) {
[833]105            response.status = 403
106            params.errorMessage = g.message(code:"default.not.found", args:['Task',params.taskId])
107            render(template: "/shared/messages")
[826]108            return
109        }
110
111        // Check for Complete task.
112        if(taskInstance.taskStatus.id == 3) {
[833]113            response.status = 403
114            params.errorMessage = g.message(code:"task.operationNotPermittedOnCompleteTask")
115            render(template: "/shared/messages")
[826]116            return
117        }
118
[833]119        // Success.
[826]120        def entryInstance = new Entry()
121        entryInstance.task = taskInstance
122        entryInstance.entryType = EntryType.read(params.entryTypeId)
123        render(template: "create", model: ['entryInstance': entryInstance])
[822]124    }
125
[833]126    def ajaxSave = {
127        def result = taskService.saveEntry(params)
128
129        // Success.
130        if(!result.error) {
131            def entryList = Entry.withCriteria {
132                                                                eq("entryType", result.entryInstance.entryType)
133                                                                task {
134                                                                    idEq(result.taskId)
135                                                                }
136                                                        }
137            render(template: "list", model: ['entryList': entryList])
138            return
139        }
140
141        if(result.error.code != "default.create.failure") {
142            response.status = 403
143            params.errorMessage = g.message(code: result.error.code)
144            render(template: "/shared/messages")
145            return
146        }
147
148        response.status = 403
149        render(template: "create", model: ['entryInstance': result.entryInstance])
150    } // ajaxSave
151
[91]152    def create = {
[482]153        if(!params.taskInstance?.id || !params.entryType?.id) {
154            flash.message = g.message(code:"entry.create.no.params")
155            redirect(controller:"taskDetailed", action:"search")
156            return
[98]157        }
[482]158
159        def taskInstance = Task.read(params.taskInstance.id)
160
161        if(!taskInstance) {
162            flash.message = g.message(code:"task.notFound")
[186]163            redirect(controller:"taskDetailed", action:"search")
[482]164            return
[98]165        }
[91]166
[482]167        // Check for Complete task.
168        if(taskInstance.taskStatus.id == 3) {
169            flash.errorMessage = g.message(code:"task.operationNotPermittedOnCompleteTask")
170            redirect(controller:"taskDetailed", action:"show", id: taskInstance.id)
171            return
172        }
173
174        def entryInstance = new Entry()
175        entryInstance.task = taskInstance
176        entryInstance.entryType = EntryType.read(params.entryType.id)
177        return ['entryInstance': entryInstance]
178    } // create
179
[91]180    def save = {
[394]181        def result = taskService.saveEntry(params)
[91]182
[186]183        if(!result.error) {
184            flash.message = "Entry created."
185            redirect(controller: "taskDetailed", action: "show", id: result.taskId)
[482]186            return
[91]187        }
[186]188
[482]189        if(result.error.code != "default.create.failure") {
190            params.errorMessage = g.message(code: result.error.code)
[91]191        }
[186]192
[482]193        render(view:'create',model:[entryInstance: result.entryInstance])
194    } // save
195
196} // end class
Note: See TracBrowser for help on using the repository browser.