source: branches/features/taskProcedureRework/grails-app/controllers/TaskProcedureDetailedController.groovy @ 772

Last change on this file since 772 was 772, checked in by gav, 9 years ago

Domain change, add TaskProcedure updated and created fields.

File size: 8.5 KB
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager'])
4class TaskProcedureDetailedController extends BaseController {
5
6    def filterService
7    def authService
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']
13
14    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
15    def list = {
16        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
17
18        if(!params.filter)
19        { return [taskProcedureInstanceList: TaskProcedure.list(params), taskProcedureInstanceTotal: TaskProcedure.count()] }
20
21        // filterPane:
22        return[ taskProcedureInstanceList: filterService.filter( params, TaskProcedure ),
23            taskProcedureInstanceTotal: filterService.count( params, TaskProcedure ),
24            filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params),
25            params:params ]
26    }
27
28    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
29    def search = {
30        redirect(action:list)
31    }
32
33    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_TaskManager', 'ROLE_TaskUser'])
34    def show = {
35
36        // In the case of an actionSubmit button, rewrite action name from 'index'.
37        if(params._action_Show)
38            params.action='show'
39
40        def taskProcedureInstance = TaskProcedure.get( params.id )
41
42        if(!taskProcedureInstance) {
43            flash.errorMessage = "TaskProcedure not found with id ${params.id}"
44            redirect(controller:'taskDetailed', action:'search')
45            return
46        }
47
48        redirect(controller:'taskDetailed',
49                        action:'show',
50                        id:taskProcedureInstance.linkedTask?.id,
51                        params:[showTab:"showProcedureTab"])
52    }
53
54    def delete = {
55        def taskProcedureInstance = TaskProcedure.get( params.id )
56        if(taskProcedureInstance) {
57            def taskInstance = taskProcedureInstance.linkedTask
58            try {
59                taskProcedureInstance.tasks.each {
60                    it.taskProcedure = null
61                }
62                taskProcedureInstance.delete(flush:true)
63                flash.message = "TaskProcedure ${params.id} deleted"
64                redirect(controller:'taskDetailed',
65                                action:'show',
66                                id:taskInstance.id,
67                                params:[showTab:"showProcedureTab"])
68            }
69            catch(org.springframework.dao.DataIntegrityViolationException e) {
70                flash.message = "TaskProcedure ${params.id} could not be deleted"
71                redirect(controller:'taskDetailed',
72                                action:'show',
73                                id:taskInstance.id,
74                                params:[showTab:"showProcedureTab"])
75            }
76        }
77        else {
78            flash.message = "TaskProcedure not found with id ${params.id}"
79            redirect(action:list)
80        }
81    }
82
83    def edit = {
84
85        // In the case of an actionSubmit button, rewrite action name from 'index'.
86        if(params._action_Edit)
87            params.action='edit'
88
89        def taskProcedureInstance = TaskProcedure.get( params.id )
90
91        if(!taskProcedureInstance) {
92            flash.message = "TaskProcedure not found with id ${params.id}"
93            redirect(action:list)
94        }
95        else {
96            return [ taskProcedureInstance : taskProcedureInstance ]
97        }
98    }
99
100    def update = {
101        def taskProcedureInstance = TaskProcedure.get( params.id )
102        if(taskProcedureInstance) {
103            if(params.version) {
104                def version = params.version.toLong()
105                if(taskProcedureInstance.version > version) {
106
107                    taskProcedureInstance.errors.rejectValue("version", "default.optimistic.locking.failure")
108                    render(view:'edit',model:[taskProcedureInstance:taskProcedureInstance])
109                    return
110                }
111            }
112            taskProcedureInstance.properties = params
113            taskProcedureInstance.lastUpdatedBy = authService.currentUser
114            taskProcedureInstance.lastUpdated = new Date() // Required to trigger version increment.
115
116            // Gaps in the html index's can be created by adding 2 items and removing the first one.
117            // This creates a gap at the missing index where LazyList will return a null.
118            def nullMaintenanceActions = taskProcedureInstance.maintenanceActions.findAll {!it}
119            if (nullMaintenanceActions) {
120                taskProcedureInstance.maintenanceActions.removeAll(nullMaintenanceActions)
121            }
122
123            // Save for restoration if validation fails.
124            def savedMaintenanceActions = new ArrayList(taskProcedureInstance.maintenanceActions)
125
126            // Remove toBeDeleted before validation.
127            def toBeDeleted = taskProcedureInstance.maintenanceActions.findAll {it.toBeDeleted}
128            if (toBeDeleted) {
129                taskProcedureInstance.maintenanceActions.removeAll(toBeDeleted)
130            }
131
132            if(!taskProcedureInstance.hasErrors() && taskProcedureInstance.save(flush: true)) {
133                flash.message = "TaskProcedure ${params.id} updated"
134                redirect(controller:'taskDetailed',
135                                action:'show',
136                                id:taskProcedureInstance.linkedTask.id,
137                                params:[showTab:"showProcedureTab"])
138            }
139            else {
140                // Restore the saved items, some of which contain toBeDeleted flags but
141                // have not been deleted yet since validation failed.
142                // The toBeDeleted items are hidden in the view.
143                taskProcedureInstance.maintenanceActions = savedMaintenanceActions
144                // Populate maintenanceAction errors for display.
145                taskProcedureInstance.maintenanceActions.each { it.validate() }
146                render(view:'edit',model:[taskProcedureInstance:taskProcedureInstance])
147            }
148        }
149        else {
150            flash.message = "TaskProcedure not found with id ${params.id}"
151            redirect(action:list)
152        }
153    }
154
155    def create = {
156
157        if(!params.taskInstance?.id) {
158            flash.errorMessage = "Please select or create a task, then go to the Procedure tab."
159            redirect(controller:"taskDetailed", action:"search")
160            return
161        }
162
163        params.linkedTask = Task.get(params.taskInstance.id)
164
165        if(!params.linkedTask?.primaryAsset) {
166            flash.errorMessage = "Please set a Primary Asset first, then go to the Procedure tab."
167            redirect(controller:"taskDetailed", action:"show", id:params.linkedTask?.id)
168            return
169        }
170
171        def taskProcedureInstance = new TaskProcedure()
172        taskProcedureInstance.properties = params
173        return ['taskProcedureInstance':taskProcedureInstance]
174    }
175
176    def save = {
177        def taskProcedureInstance = new TaskProcedure(params)
178        taskProcedureInstance.createdBy = authService.currentUser
179        taskProcedureInstance.lastUpdatedBy = authService.currentUser
180        def taskInstance = Task.get(params.linkedTask.id)
181
182        // Gaps in the html index's can be created by adding 2 items and removing the first one.
183        // This creates a gap at the missing index where LazyList will return a null.
184        def nullMaintenanceActions = taskProcedureInstance.maintenanceActions.findAll {!it}
185        if (nullMaintenanceActions) {
186            taskProcedureInstance.maintenanceActions.removeAll(nullMaintenanceActions)
187        }
188
189        if(!taskProcedureInstance.hasErrors() && taskProcedureInstance.save(flush: true)) {
190             // Also sets: taskInstance.taskProcedure = taskProcedureInstance
191            taskProcedureInstance.addToTasks(taskInstance)
192            flash.message = "TaskProcedure ${taskProcedureInstance.id} created."
193            redirect(controller:'taskDetailed',
194                            action:'show',
195                            id:taskProcedureInstance.linkedTask.id,
196                            params:[showTab:"showProcedureTab"])
197        }
198        else {
199            // Populate maintenanceAction errors for display.
200            taskProcedureInstance.maintenanceActions.each { it.validate() }
201            render(view:'create',model:[taskProcedureInstance:taskProcedureInstance])
202        }
203    }
204
205}
Note: See TracBrowser for help on using the repository browser.