source: trunk/grails-app/controllers/TaskDetailedController.groovy @ 226

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

Complete inventory movement implementation as per ticket #24.
Refactor showTab function in both Task and Inventory (which now has tabs).

File size: 19.9 KB
RevLine 
[69]1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
[165]2import org.codehaus.groovy.grails.commons.ConfigurationHolder
[209]3import com.zeddware.grails.plugins.filterpane.FilterUtils
[69]4
[85]5class TaskDetailedController extends BaseController {
[66]6
[185]7    def personService
[180]8    def taskService
[143]9    def taskSearchService
[140]10    def filterService
[165]11    def exportService
[214]12    def dateUtilService
[139]13
[181]14    // these actions only accept POST requests
15    static allowedMethods = [save:'POST', update:'POST', restore:'POST', trash:'POST', approve:'POST', renegeApproval:'POST', complete:'POST', reopen:'POST']
[66]16
[196]17    def index = { redirect(action: 'search', params: params) }
[140]18
[66]19    def list = {
[143]20        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100 )
[124]21        [ taskInstanceList: Task.list( params ), taskInstanceTotal: Task.count() ]
[66]22    }
[143]23
[139]24    def search = {
[143]25        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100 )
26
[155]27        // Quick Search:
[209]28        if(!FilterUtils.isFilterApplied(params)) {
[144]29            def taskInstanceList = []
[216]30            def personInstance = personService.currentUser
[143]31
[155]32            if(params.quickSearch == "searchMyTodays") {
[144]33                taskInstanceList = taskSearchService.getMyTodays(params)
34                if(taskInstanceList.totalCount > 0) { params.message = "Today's tasks for ${personInstance.firstName} ${personInstance.lastName}." }
35                else { params.message = "No tasks found for today." }
36            }
[155]37            else if(params.quickSearch == "searchInTheLastWeek") {
[144]38                taskInstanceList = taskSearchService.getInTheLastWeek(params)
39                if(taskInstanceList.totalCount > 0) { params.message = "Tasks with Target Start Date in the last week." }
[185]40                else { params.message = "No tasks found for the last week." }
[144]41            }
[155]42            else if(params.quickSearch == "searchMyInTheLastWeek") {
[144]43                taskInstanceList = taskSearchService.getMyInTheLastWeek(params)
44                if(taskInstanceList.totalCount > 0) { params.message = "Tasks with Target Start Date in the last week for ${personInstance.firstName} ${personInstance.lastName}." }
[185]45                else { params.message = "No tasks found for the last week." }
[144]46            }
47            else {
48                //Default:
49                taskInstanceList = taskSearchService.getTodays(params)
50                if(taskInstanceList.totalCount > 0) { params.message = "Today's tasks." }
51                else { params.message = "No tasks found for today." }
[155]52                params.quickSearch = "searchTodays"
[144]53            }
[155]54            return[taskInstanceList: taskInstanceList, taskInstanceTotal: taskInstanceList.totalCount, filterParams: params]
[139]55        }
[143]56        // filterPane:
57        return[ taskInstanceList: filterService.filter( params, Task ),
58            taskInstanceTotal: filterService.count( params, Task ),
59            filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params),
60            params:params ]
[140]61    }
[143]62
[155]63    def searchCalendar = {
64        params.max = 30
[140]65
[155]66        // Quick Search:
[209]67        if(!FilterUtils.isFilterApplied(params)) {
[155]68            def taskInstanceList = []
[216]69            def personInstance = personService.currentUser
[155]70
71            if(params.quickSearch == "searchMyTodays") {
72                taskInstanceList = taskSearchService.getMyTodays(params)
73                if(taskInstanceList.totalCount > 0) { params.message = "Today's tasks for ${personInstance.firstName} ${personInstance.lastName}." }
74                else { params.message = "No tasks found for today." }
75                if(taskInstanceList.totalCount > params.max) { params.message = "Too many results, only the first ${params.max} shown" }
76            }
77            else if(params.quickSearch == "searchInTheLastWeek") {
78                taskInstanceList = taskSearchService.getInTheLastWeek(params)
79                if(taskInstanceList.totalCount > 0) { params.message = "Tasks with Target Start Date in the last week." }
[185]80                else { params.message = "No tasks found for the last week." }
[155]81                if(taskInstanceList.totalCount > params.max) { params.message = "Too many results, only the first ${params.max} shown" }
82            }
83            else if(params.quickSearch == "searchMyInTheLastWeek") {
84                taskInstanceList = taskSearchService.getMyInTheLastWeek(params)
85                if(taskInstanceList.totalCount > 0) { params.message = "Tasks with Target Start Date in the last week for ${personInstance.firstName} ${personInstance.lastName}." }
[185]86                else { params.message = "No tasks found for the last week." }
[155]87                if(taskInstanceList.totalCount > params.max) { params.message = "Too many results, only the first ${params.max} shown" }
88            }
89            else {
90                //Default:
91                taskInstanceList = taskSearchService.getTodays(params)
92                if(taskInstanceList.totalCount > 0) { params.message = "Today's tasks." }
93                else { params.message = "No tasks found for today." }
94                if(taskInstanceList.totalCount > params.max) { params.message = "Too many results, only the first ${params.max} shown" }
95                params.quickSearch = "searchTodays"
96            }
97            return[taskInstanceList: taskInstanceList, taskInstanceTotal: taskInstanceList.totalCount, filterParams: params]
[139]98        }
[155]99        // filterPane:
100        def taskInstanceTotal = filterService.count( params, Task )
101        if(taskInstanceTotal > params.max) { params.message = "Too many results, only the first ${params.max} shown" }
102        return[ taskInstanceList: filterService.filter( params, Task ),
103            taskInstanceTotal: taskInstanceTotal,
104            filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params),
105            params:params ]
[139]106    }
[140]107
[165]108    def budget = {
109        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100 )
110
111        // Quick Search:
[209]112        if(!FilterUtils.isFilterApplied(params)) {
[165]113            def taskInstanceList = []
[216]114            def personInstance = personService.currentUser
[165]115
116            if(params.quickSearch == "budgetUnplanned") {
117                taskInstanceList = taskSearchService.getBudgetUnplanned(params)
118                if(taskInstanceList.totalCount > 0) { params.message = "Budget unplanned tasks in the last week." }
119                else { params.message = "No tasks found." }
120            }
121            //else if(params.quickSearch == "budgetPlanned") {
122            else {
123                //Default:
124                taskInstanceList = taskSearchService.getBudgetPlanned(params)
125                if(taskInstanceList.totalCount > 0) { params.message = "Budget planned Tasks in the last week." }
126                else { params.message = "No tasks found.." }
127            }
128            // export plugin:
129            if(params?.format && params.format != "html") {
130                response.contentType = ConfigurationHolder.config.grails.mime.types[params.format]
131                response.setHeader("Content-disposition", "attachment; filename=tasks.${params.extension}")
132                List fields = ["id", "targetStartDate", "description", "leadPerson", "taskStatus", "taskType"]
133                Map labels = ["id": "ID", "targetStartDate": "Target Start Date", "description": "Description",
134                                        "leadPerson": "Lead Person", "taskStatus": "Task Status", "taskType": "Task Type"]
135                Map formatters = [:]
136                String title = "${params.quickSearch} tasks in the last week."
137                Map parameters = [title: title]
138
139                exportService.export(params.format, response.outputStream, taskInstanceList, fields, labels, formatters, parameters) 
140            }
141            return[taskInstanceList: taskInstanceList, taskInstanceTotal: taskInstanceList.totalCount, filterParams: params]
142        }
143        // filterPane:
144        return[ taskInstanceList: filterService.filter( params, Task ),
145            taskInstanceTotal: filterService.count( params, Task ),
146            filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params),
147            params:params ]
148    }
149
[66]150    def show = {
[147]151
[139]152        // In the case of an actionSubmit button, rewrite action name from 'index'.
153        if(params._action_Show)
154        { params.action='show' }
155
[225]156        def showTab = [:]
157        switch (params.showTab) {
158            case "showProcedureTab":
159                showTab.procedure =  new String("true")
160                break
161            case "showRecurrenceTab":
162                showTab.recurrence =  new String("true")
163                break
164            case "showInventoryTab":
165                showTab.inventory = new String("true")
166                break
167            case "showSubTasksTab":
168                showTab.subTasks = new String("true")
169                break
170            default:
171                showTab.task = new String("true")
172        }
173
[66]174        def taskInstance = Task.get( params.id )
175
176        if(!taskInstance) {
177            flash.message = "Task not found with id ${params.id}"
[196]178            redirect(action: 'search')
[66]179        }
[133]180        else {
[179]181            params.max = 10
182            params.order = "desc"
183            params.sort = "id"
[134]184
[179]185            def entryWorkDoneList = Entry.withCriteria {
[190]186                                                                eq("entryType", EntryType.get(2))
[179]187                                                                eq("task", taskInstance)
188                                                        }
189
190            def entryFaultList = Entry.withCriteria {
[190]191                                                                eq("entryType", EntryType.get(1))
[179]192                                                                eq("task", taskInstance)
193                                                        }
194
[196]195            def subTaskInstanceList = Task.findAllByParentTaskAndTrash(taskInstance, false, params)
196            def subTaskInstanceTotal = Task.countByParentTaskAndTrash(taskInstance, false)
[134]197
[175]198            def inventoryMovementList = InventoryMovement.findAllByTask(taskInstance, [max:100, sort:"id", order:"desc", offset:0])
199
[180]200            def taskModificationList = TaskModification.findAllByTask(taskInstance, [max:100, sort:"id", order:"asc", offset:0])
201
[133]202            def taskProcedureInstance = TaskProcedure.get(taskInstance.taskProcedure?.id)
203            def taskProcedureExits = new Boolean("true")
204            if(!taskProcedureInstance) {
205                taskProcedureExits = false
206            }
[175]207
208            params.order = "asc"
209            params.sort = "procedureStepNumber"
210            def maintenanceActionList = MaintenanceAction.findAllByTaskProcedure(taskProcedureInstance, params)
211
[134]212            def taskRecurringScheduleInstance = TaskRecurringSchedule.get(taskInstance.taskRecurringSchedule?.id)
213            def taskRecurringScheduleExits= new Boolean("true")
[175]214            if(!taskRecurringScheduleInstance) {
[134]215                taskRecurringScheduleExits = false
216            }
[179]217
[137]218            return [ taskInstance: taskInstance,
[179]219                            entryWorkDoneList: entryWorkDoneList,
220                            entryFaultList: entryFaultList,
[133]221                            taskProcedureInstance: taskProcedureInstance,
222                            taskProcedureExits: taskProcedureExits,
[225]223                            showTab: showTab,
[179]224                            subTaskInstanceList: subTaskInstanceList,
225                            subTaskInstanceTotal: subTaskInstanceTotal,
226                            subTaskInstanceMax: params.max,
227                            maintenanceActionList: maintenanceActionList,
228                            taskRecurringScheduleInstance: taskRecurringScheduleInstance,
229                            taskRecurringScheduleExits: taskRecurringScheduleExits,
[180]230                            inventoryMovementList: inventoryMovementList,
231                            taskModificationList: taskModificationList]
[131]232        }
[66]233    }
234
[181]235    def restore = {
236
237        if(!Task.exists(params.id)) {
238            flash.message = "Task not found with id ${params.id}"
[196]239            redirect(action: 'search')
[181]240        }
241
242        def result = taskService.restore(params)
243
244        if(!result.error) {
245                flash.message = "Task ${params.id} has been restored."
[196]246                redirect(action: 'show', id: result.taskInstance.id)
[181]247        }
248        else {
249            if(result.taskInstance) {
250                render(view:'edit',model:[taskInstance:result.taskInstance])
[96]251            }
[181]252            else {
253                flash.message = "Task could not be updated."
[196]254                redirect(action: 'search')
[96]255            }
[66]256        }
[181]257
258    }
259
260    def trash = {
261
262        if(!Task.exists(params.id)) {
263            flash.message = "Task not found with id ${params.id}."
[196]264            redirect(action: 'search')
[181]265        }
266
267        def result = taskService.trash(params)
268
269        if(!result.error) {
270                flash.message = "Task ${params.id} has been moved to trash."
[196]271                redirect(action: 'search')
[181]272        }
[66]273        else {
[181]274            if(result.taskInstance) {
275                render(view:'edit',model:[taskInstance:result.taskInstance])
276            }
277            else {
278                flash.message = "Task could not be updated."
[196]279                redirect(action: 'search')
[181]280            }
[66]281        }
[181]282
[66]283    }
284
[181]285    def approve = {
286
287        if(!Task.exists(params.id)) {
288            flash.message = "Task not found with id ${params.id}."
[196]289            redirect(action: 'search')
[181]290        }
291
292        def result = taskService.approve(params)
293
294        if(!result.error) {
295                flash.message = "Task ${params.id} has been approved."
[196]296                redirect(action: 'show', id: result.taskInstance.id)
[181]297        }
298        else {
299            if(result.taskInstance) {
300                render(view:'edit',model:[taskInstance:result.taskInstance])
301            }
302            else {
303                flash.message = "Task could not be updated."
[196]304                redirect(action: 'search')
[181]305            }
306        }
307
308    }
309
310    def renegeApproval = {
311
312        if(!Task.exists(params.id)) {
313            flash.message = "Task not found with id ${params.id}."
[196]314            redirect(action: 'search')
[181]315        }
316
317        def result = taskService.renegeApproval(params)
318
319        if(!result.error) {
320                flash.message = "Task ${params.id} has had approval removed."
[196]321                redirect(action: 'show', id: result.taskInstance.id)
[181]322        }
323        else {
324            if(result.taskInstance) {
325                render(view:'edit',model:[taskInstance:result.taskInstance])
326            }
327            else {
328                flash.message = "Task could not be updated."
[196]329                redirect(action: 'search')
[181]330            }
331        }
332
333    }
334
335    def complete = {
336
337        if(!Task.exists(params.id)) {
338            flash.message = "Task not found with id ${params.id}."
[196]339            redirect(action: 'search')
[181]340        }
341
342        def result = taskService.complete(params)
343
344        if(!result.error) {
345                flash.message = "Task ${params.id} has been completed."
[196]346                redirect(action: 'show', id: result.taskInstance.id)
[181]347        }
348        else {
349            if(result.taskInstance) {
350                render(view:'edit',model:[taskInstance:result.taskInstance])
351            }
352            else {
353                flash.message = "Task could not be updated."
[196]354                redirect(action: 'search')
[181]355            }
356        }
357
358    }
359
360    def reopen = {
361
362        if(!Task.exists(params.id)) {
363            flash.message = "Task not found with id ${params.id}."
[196]364            redirect(action: 'search')
[181]365        }
366
367        def result = taskService.reopen(params)
368
369        if(!result.error) {
370                flash.message = "Task ${params.id} has been reopened."
[196]371                redirect(action: 'show', id: result.taskInstance.id)
[181]372        }
373        else {
374            if(result.taskInstance) {
375                render(view:'edit',model:[taskInstance:result.taskInstance])
376            }
377            else {
378                flash.message = "Task could not be updated."
[196]379                redirect(action: 'search')
[181]380            }
381        }
382
383    }
384
[66]385    def edit = {
[147]386
[139]387        // In the case of an actionSubmit button, rewrite action name from 'index'.
388        if(params._action_Edit)
389        { params.action='edit' }
[169]390
[66]391        def taskInstance = Task.get( params.id )
392
393        if(!taskInstance) {
394            flash.message = "Task not found with id ${params.id}"
[196]395            redirect(action: 'search')
[66]396        }
397        else {
[181]398            if(taskInstance.trash) {
[196]399                flash.message = "You may not edit tasks that are in the trash."
400                redirect(action: 'show', id: taskInstance.id)
401                return
[181]402            }
[196]403            def possibleParentList = taskService.possibleParentList(taskInstance)
[169]404            return [ taskInstance : taskInstance, possibleParentList: possibleParentList ]
[84]405        }
406    }
407
[66]408    def update = {
[179]409
[180]410        if(!Task.exists(params.id)) {
411            flash.message = "Task not found with id ${params.id}"
[196]412            redirect(action: 'search')
[180]413        }
414
415        def result = taskService.update(params)
416
417        if(!result.error) {
[66]418                flash.message = "Task ${params.id} updated"
[196]419                redirect(action: 'show', id: result.taskInstance.id)
[180]420        }
421        else {
[206]422            render(view:'edit',model:[taskInstance:result.taskInstance.attach()])
[66]423        }
[180]424
[66]425    }
426
427    def create = {
428        def taskInstance = new Task()
[214]429
430        // Set the targetStartDate if specified, used by searchCalendar view.
431        if(params.year && params.month && params.day)
432            taskInstance.targetStartDate = dateUtilService.makeDate(params.year, params.month, params.day)
433
[196]434        // Default leadPerson to current user, unless supplied in params.
[216]435        taskInstance.leadPerson = personService.currentUser
[66]436        taskInstance.properties = params
[196]437        return ['taskInstance': taskInstance]
[66]438    }
439
440    def save = {
[180]441        def result = taskService.create(params)
442
443        if(!result.error) {
444            flash.message = "Task ${result.taskInstance.id} created."
[196]445            redirect(action: 'show', id: result.taskInstance.id)
[66]446        }
447        else {
[180]448            if(result.taskInstance) {
[196]449                render(view:'create', model:[taskInstance:result.taskInstance])
[180]450            }
451            else {
452                flash.message = "Could not create task."
[196]453                redirect(action: 'search')
[180]454            }
455
[66]456        }
457    }
[179]458
459    def listSubTasks = {
460        def parentTaskInstance = Task.get(params.id)
461
[134]462        if(!parentTaskInstance) {
463            flash.message = "Task not found with id ${params.id}"
[196]464            redirect(action: 'search')
[133]465        }
466        else {
[179]467        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
[196]468        def subTaskInstanceList = Task.findAllByParentTaskAndTrash(parentTaskInstance, false, params)
469        def subTaskInstanceTotal = Task.countByParentTaskAndTrash(parentTaskInstance, false)
[179]470
[134]471        [ taskInstanceList: subTaskInstanceList,
[179]472            taskInstanceTotal:  subTaskInstanceTotal,
473            parentTaskInstance: parentTaskInstance]
474        }
475    }
476
[196]477    def createSubTask = {
478        def parentTaskInstance = Task.get(params.id)
479
480        if(parentTaskInstance) {
481
482            def result = taskService.createSubTask(parentTaskInstance)
483            if(!result.error) {
484                flash.message = "Sub Task ${result.taskInstance.id} created, please edit and update to your requirements."
485                redirect(action: 'edit', id: result.taskInstance.id)
486            }
487            else {
488                if(result.taskInstance.errors.hasFieldErrors("parentTask")) {
489                    flash.message = g.message(code:"task.operationNotPermittedOnTaskInTrash")
490                    redirect(action: 'show', id:  parentTaskInstance.id)
491                }
492                else {
493                    render(view: 'create', model:[taskInstance: result.taskInstance])
494                }
495            }
496        }
497
498        else {
499            flash.message = "Task not found with id ${params.id}"
500            redirect(action: 'search')
501        }
502    }
503
504} // end of class.
Note: See TracBrowser for help on using the repository browser.