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

Last change on this file since 191 was 191, checked in by gav, 14 years ago

Improve entry creation so that entryType is passed in with the associated submitButton.
Refactor InventoryMovementService and prevent create/delete of invenotryMovements on complete task.

File size: 4.2 KB
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3class EntryDetailedController extends BaseController {
4
5    def personService
6    def taskService
7
8    def index = { redirect(action:list,params:params) }
9
10    // the delete, save and update actions only accept POST requests
11    static allowedMethods = [delete:'POST', save:'POST', update:'POST']
12
13    def list = {
14        if(!params.max) params.max = 10
15        [ entryInstanceList: Entry.list( params ) ]
16    }
17
18    def show = {
19        def entryInstance = Entry.get( params.id )
20
21        if(!entryInstance) {
22            flash.message = "Entry not found with id ${params.id}"
23            redirect(controller: 'taskDetailed', action: 'search')
24        }
25        else { return [ entryInstance : entryInstance ] }
26    }
27
28    def delete = {
29        def entryInstance = Entry.get( params.id )
30        if(entryInstance) {
31            if(entryInstance.enteredBy.loginName == personService.currentUser().loginName) {
32                def taskID = entryInstance.task.id
33                entryInstance.delete(flush:true)
34                flash.message = "Entry ${params.id} deleted"
35                redirect(controller: 'taskDetailed', action: 'show', id: taskID)
36            }
37            else {
38                flash.message = "You may only delete your own entries."
39                redirect(action:show,id:entryInstance.id)
40            }
41
42        }
43        else {
44            flash.message = "Entry not found with id ${params.id}"
45            redirect(controller: "taskDetailed", action:"search")
46        }
47    }
48
49    def edit = {
50        def entryInstance = Entry.get( params.id )
51        if(!entryInstance) {
52                flash.message = "Entry not found with id ${params.id}"
53                redirect(controller: "taskDetailed", action:"search")
54        }
55        else {
56
57            if(entryInstance.enteredBy.loginName == personService.currentUser().loginName) {
58                return [ entryInstance : entryInstance ]
59            }
60            else {
61                flash.message = "You may only edit your own entries."
62                redirect(action:show,id:entryInstance.id)
63            }
64
65        }
66    }
67
68    def update = {
69        def entryInstance = Entry.get( params.id )
70        if(entryInstance) {
71            // The update method only accepts post requests, so this is just in case.
72            if(entryInstance.enteredBy.loginName == personService.currentUser().loginName) {
73                entryInstance.properties = params
74                if(!entryInstance.hasErrors() && entryInstance.save(flush: true)) {
75                    flash.message = "Entry ${params.id} updated"
76                    redirect(action:show,id:entryInstance.id)
77                }
78                else {
79                    render(view:'edit',model:[entryInstance:entryInstance])
80                }
81            }
82            else {
83                flash.message = "You may only edit your own entries."
84                redirect(action:show,id:entryInstance.id)
85            }
86        }
87        else {
88            flash.message = "Entry not found with id ${params.id}"
89            redirect(controller: "taskDetailed", action:"search")
90        }
91    }
92
93    def create = {
94        try {
95            def taskInstance = Task.get(params.taskInstance.id)
96            def entryInstance = new Entry()
97            entryInstance.task = taskInstance
98            entryInstance.entryType = EntryType.get(params.entryType.id)
99            return ['entryInstance':entryInstance]
100        }
101        catch(Exception e) {
102            flash.message = "Please select a task, then 'Add Entry'."
103            redirect(controller:"taskDetailed", action:"search")
104        }
105    }
106
107    def save = {
108        def result = taskService.createEntry(params)
109
110        if(!result.error) {
111            flash.message = "Entry created."
112            redirect(controller: "taskDetailed", action: "show", id: result.taskId)
113        }
114        else {
115            if(result.entryInstance) {
116                render(view:'create',model:[entryInstance: result.entryInstance])
117            }
118            else {
119                flash.message = "Could not create entry."
120                redirect(controller: "taskDetailed", action:"search")
121            }
122
123        }
124    }
125
126}
Note: See TracBrowser for help on using the repository browser.