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

Last change on this file 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
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
[859]12    static allowedMethods = [delete:'POST', save:'POST', update:'POST', ajaxCreate:'POST', ajaxSave:'POST']
[91]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
[838]69    /// @todo: Refactor to taskService and include moving task to "In Progress" when Entry.duration is updated.
[91]70    def update = {
71        def entryInstance = Entry.get( params.id )
72        if(entryInstance) {
[185]73            // The update method only accepts post requests, so this is just in case.
[291]74            if(entryInstance.enteredBy.loginName == authService.currentUser.loginName) {
[185]75                entryInstance.properties = params
76                if(!entryInstance.hasErrors() && entryInstance.save(flush: true)) {
[838]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                    }
[185]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                }
[91]94            }
95            else {
[185]96                flash.message = "You may only edit your own entries."
97                redirect(action:show,id:entryInstance.id)
[91]98            }
99        }
100        else {
101            flash.message = "Entry not found with id ${params.id}"
[186]102            redirect(controller: "taskDetailed", action:"search")
[91]103        }
104    }
105
[826]106    def ajaxCreate = {
107        if(!params.taskId || !params.entryTypeId) {
[833]108            params.errorMessage = g.message(code: "entry.create.no.params.ajax")
[859]109            render(contentType:"text/json", status: 403, template: "/shared/messages")
[826]110            return
111        }
[822]112
[826]113        def taskInstance = Task.read(params.taskId)
[822]114
[873]115        def entryInstance = new Entry()
116        entryInstance.task = taskInstance
117        entryInstance.entryType = EntryType.read(params.entryTypeId)
[826]118
[873]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)
[859]123            render(contentType:"text/json", status: 403, template: "/shared/messages")
[826]124            return
125        }
126
[833]127        // Success.
[859]128        def model = ['entryInstance': entryInstance]
[822]129
[859]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
[833]138    def ajaxSave = {
139        def result = taskService.saveEntry(params)
140
141        // Success.
142        if(!result.error) {
143            def entryList = Entry.withCriteria {
[859]144                                            eq("entryType", result.entryInstance.entryType)
145                                            task {
146                                                idEq(result.taskId)
147                                            }
148                                    }
149
[865]150            def taskInstance = entryList[0].task
151            def taskModificationList = TaskModification.findAllByTask(taskInstance, [max:100, sort:"id", order:"asc"])
[859]152            def model = ['entryList': entryList]
[865]153            def taskModel = ['taskInstance': taskInstance]
154            def taskModificationModel = ['taskModificationList': taskModificationList ]
[859]155
156            render(contentType:"text/json") {
157                updates = array {
158                    element([ mode: 'replace', target:"$params.target", content: g.render(template: 'list', model:model) ])
[862]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) ])
[865]165                    element([ mode: 'replace',
166                                    target:"#modifications",
167                                    content: g.render(template: '/taskDetailed/showTaskModifications', model:taskModificationModel) ])
[859]168                }
169            }
[833]170            return
[865]171        } // Success.
[833]172
[859]173        if(result.error.code != "default.create.failure")
[833]174            params.errorMessage = g.message(code: result.error.code)
175
[859]176        def model = ['entryInstance': result.entryInstance]
177        render(contentType:"text/json", status: 403, template: "create", model: model)
[833]178    } // ajaxSave
179
[91]180    def create = {
[482]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
[98]185        }
[482]186
187        def taskInstance = Task.read(params.taskInstance.id)
188
189        if(!taskInstance) {
190            flash.message = g.message(code:"task.notFound")
[186]191            redirect(controller:"taskDetailed", action:"search")
[482]192            return
[98]193        }
[91]194
[482]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
[91]208    def save = {
[394]209        def result = taskService.saveEntry(params)
[91]210
[186]211        if(!result.error) {
212            flash.message = "Entry created."
213            redirect(controller: "taskDetailed", action: "show", id: result.taskId)
[482]214            return
[91]215        }
[186]216
[482]217        if(result.error.code != "default.create.failure") {
218            params.errorMessage = g.message(code: result.error.code)
[91]219        }
[186]220
[482]221        render(view:'create',model:[entryInstance: result.entryInstance])
222    } // save
223
224} // end class
Note: See TracBrowser for help on using the repository browser.