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

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

Domain change, improved draft of creating taskProcedure dynamic form.

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