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

Last change on this file since 225 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
Line 
1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2import org.codehaus.groovy.grails.commons.ConfigurationHolder
3import com.zeddware.grails.plugins.filterpane.FilterUtils
4
5class TaskDetailedController extends BaseController {
6
7    def personService
8    def taskService
9    def taskSearchService
10    def filterService
11    def exportService
12    def dateUtilService
13
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']
16
17    def index = { redirect(action: 'search', params: params) }
18
19    def list = {
20        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100 )
21        [ taskInstanceList: Task.list( params ), taskInstanceTotal: Task.count() ]
22    }
23
24    def search = {
25        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100 )
26
27        // Quick Search:
28        if(!FilterUtils.isFilterApplied(params)) {
29            def taskInstanceList = []
30            def personInstance = personService.currentUser
31
32            if(params.quickSearch == "searchMyTodays") {
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            }
37            else if(params.quickSearch == "searchInTheLastWeek") {
38                taskInstanceList = taskSearchService.getInTheLastWeek(params)
39                if(taskInstanceList.totalCount > 0) { params.message = "Tasks with Target Start Date in the last week." }
40                else { params.message = "No tasks found for the last week." }
41            }
42            else if(params.quickSearch == "searchMyInTheLastWeek") {
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}." }
45                else { params.message = "No tasks found for the last week." }
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." }
52                params.quickSearch = "searchTodays"
53            }
54            return[taskInstanceList: taskInstanceList, taskInstanceTotal: taskInstanceList.totalCount, filterParams: params]
55        }
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 ]
61    }
62
63    def searchCalendar = {
64        params.max = 30
65
66        // Quick Search:
67        if(!FilterUtils.isFilterApplied(params)) {
68            def taskInstanceList = []
69            def personInstance = personService.currentUser
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." }
80                else { params.message = "No tasks found for the last week." }
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}." }
86                else { params.message = "No tasks found for the last week." }
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]
98        }
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 ]
106    }
107
108    def budget = {
109        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100 )
110
111        // Quick Search:
112        if(!FilterUtils.isFilterApplied(params)) {
113            def taskInstanceList = []
114            def personInstance = personService.currentUser
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
150    def show = {
151
152        // In the case of an actionSubmit button, rewrite action name from 'index'.
153        if(params._action_Show)
154        { params.action='show' }
155
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
174        def taskInstance = Task.get( params.id )
175
176        if(!taskInstance) {
177            flash.message = "Task not found with id ${params.id}"
178            redirect(action: 'search')
179        }
180        else {
181            params.max = 10
182            params.order = "desc"
183            params.sort = "id"
184
185            def entryWorkDoneList = Entry.withCriteria {
186                                                                eq("entryType", EntryType.get(2))
187                                                                eq("task", taskInstance)
188                                                        }
189
190            def entryFaultList = Entry.withCriteria {
191                                                                eq("entryType", EntryType.get(1))
192                                                                eq("task", taskInstance)
193                                                        }
194
195            def subTaskInstanceList = Task.findAllByParentTaskAndTrash(taskInstance, false, params)
196            def subTaskInstanceTotal = Task.countByParentTaskAndTrash(taskInstance, false)
197
198            def inventoryMovementList = InventoryMovement.findAllByTask(taskInstance, [max:100, sort:"id", order:"desc", offset:0])
199
200            def taskModificationList = TaskModification.findAllByTask(taskInstance, [max:100, sort:"id", order:"asc", offset:0])
201
202            def taskProcedureInstance = TaskProcedure.get(taskInstance.taskProcedure?.id)
203            def taskProcedureExits = new Boolean("true")
204            if(!taskProcedureInstance) {
205                taskProcedureExits = false
206            }
207
208            params.order = "asc"
209            params.sort = "procedureStepNumber"
210            def maintenanceActionList = MaintenanceAction.findAllByTaskProcedure(taskProcedureInstance, params)
211
212            def taskRecurringScheduleInstance = TaskRecurringSchedule.get(taskInstance.taskRecurringSchedule?.id)
213            def taskRecurringScheduleExits= new Boolean("true")
214            if(!taskRecurringScheduleInstance) {
215                taskRecurringScheduleExits = false
216            }
217
218            return [ taskInstance: taskInstance,
219                            entryWorkDoneList: entryWorkDoneList,
220                            entryFaultList: entryFaultList,
221                            taskProcedureInstance: taskProcedureInstance,
222                            taskProcedureExits: taskProcedureExits,
223                            showTab: showTab,
224                            subTaskInstanceList: subTaskInstanceList,
225                            subTaskInstanceTotal: subTaskInstanceTotal,
226                            subTaskInstanceMax: params.max,
227                            maintenanceActionList: maintenanceActionList,
228                            taskRecurringScheduleInstance: taskRecurringScheduleInstance,
229                            taskRecurringScheduleExits: taskRecurringScheduleExits,
230                            inventoryMovementList: inventoryMovementList,
231                            taskModificationList: taskModificationList]
232        }
233    }
234
235    def restore = {
236
237        if(!Task.exists(params.id)) {
238            flash.message = "Task not found with id ${params.id}"
239            redirect(action: 'search')
240        }
241
242        def result = taskService.restore(params)
243
244        if(!result.error) {
245                flash.message = "Task ${params.id} has been restored."
246                redirect(action: 'show', id: result.taskInstance.id)
247        }
248        else {
249            if(result.taskInstance) {
250                render(view:'edit',model:[taskInstance:result.taskInstance])
251            }
252            else {
253                flash.message = "Task could not be updated."
254                redirect(action: 'search')
255            }
256        }
257
258    }
259
260    def trash = {
261
262        if(!Task.exists(params.id)) {
263            flash.message = "Task not found with id ${params.id}."
264            redirect(action: 'search')
265        }
266
267        def result = taskService.trash(params)
268
269        if(!result.error) {
270                flash.message = "Task ${params.id} has been moved to trash."
271                redirect(action: 'search')
272        }
273        else {
274            if(result.taskInstance) {
275                render(view:'edit',model:[taskInstance:result.taskInstance])
276            }
277            else {
278                flash.message = "Task could not be updated."
279                redirect(action: 'search')
280            }
281        }
282
283    }
284
285    def approve = {
286
287        if(!Task.exists(params.id)) {
288            flash.message = "Task not found with id ${params.id}."
289            redirect(action: 'search')
290        }
291
292        def result = taskService.approve(params)
293
294        if(!result.error) {
295                flash.message = "Task ${params.id} has been approved."
296                redirect(action: 'show', id: result.taskInstance.id)
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."
304                redirect(action: 'search')
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}."
314            redirect(action: 'search')
315        }
316
317        def result = taskService.renegeApproval(params)
318
319        if(!result.error) {
320                flash.message = "Task ${params.id} has had approval removed."
321                redirect(action: 'show', id: result.taskInstance.id)
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."
329                redirect(action: 'search')
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}."
339            redirect(action: 'search')
340        }
341
342        def result = taskService.complete(params)
343
344        if(!result.error) {
345                flash.message = "Task ${params.id} has been completed."
346                redirect(action: 'show', id: result.taskInstance.id)
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."
354                redirect(action: 'search')
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}."
364            redirect(action: 'search')
365        }
366
367        def result = taskService.reopen(params)
368
369        if(!result.error) {
370                flash.message = "Task ${params.id} has been reopened."
371                redirect(action: 'show', id: result.taskInstance.id)
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."
379                redirect(action: 'search')
380            }
381        }
382
383    }
384
385    def edit = {
386
387        // In the case of an actionSubmit button, rewrite action name from 'index'.
388        if(params._action_Edit)
389        { params.action='edit' }
390
391        def taskInstance = Task.get( params.id )
392
393        if(!taskInstance) {
394            flash.message = "Task not found with id ${params.id}"
395            redirect(action: 'search')
396        }
397        else {
398            if(taskInstance.trash) {
399                flash.message = "You may not edit tasks that are in the trash."
400                redirect(action: 'show', id: taskInstance.id)
401                return
402            }
403            def possibleParentList = taskService.possibleParentList(taskInstance)
404            return [ taskInstance : taskInstance, possibleParentList: possibleParentList ]
405        }
406    }
407
408    def update = {
409
410        if(!Task.exists(params.id)) {
411            flash.message = "Task not found with id ${params.id}"
412            redirect(action: 'search')
413        }
414
415        def result = taskService.update(params)
416
417        if(!result.error) {
418                flash.message = "Task ${params.id} updated"
419                redirect(action: 'show', id: result.taskInstance.id)
420        }
421        else {
422            render(view:'edit',model:[taskInstance:result.taskInstance.attach()])
423        }
424
425    }
426
427    def create = {
428        def taskInstance = new Task()
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
434        // Default leadPerson to current user, unless supplied in params.
435        taskInstance.leadPerson = personService.currentUser
436        taskInstance.properties = params
437        return ['taskInstance': taskInstance]
438    }
439
440    def save = {
441        def result = taskService.create(params)
442
443        if(!result.error) {
444            flash.message = "Task ${result.taskInstance.id} created."
445            redirect(action: 'show', id: result.taskInstance.id)
446        }
447        else {
448            if(result.taskInstance) {
449                render(view:'create', model:[taskInstance:result.taskInstance])
450            }
451            else {
452                flash.message = "Could not create task."
453                redirect(action: 'search')
454            }
455
456        }
457    }
458
459    def listSubTasks = {
460        def parentTaskInstance = Task.get(params.id)
461
462        if(!parentTaskInstance) {
463            flash.message = "Task not found with id ${params.id}"
464            redirect(action: 'search')
465        }
466        else {
467        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
468        def subTaskInstanceList = Task.findAllByParentTaskAndTrash(parentTaskInstance, false, params)
469        def subTaskInstanceTotal = Task.countByParentTaskAndTrash(parentTaskInstance, false)
470
471        [ taskInstanceList: subTaskInstanceList,
472            taskInstanceTotal:  subTaskInstanceTotal,
473            parentTaskInstance: parentTaskInstance]
474        }
475    }
476
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.