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

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

Add case insensitive sorting to Task show view, assignedPersons and assignedGroups.

File size: 20.4 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 assignedGroupList = taskInstance.assignedGroups.sort { p1, p2 -> p1.personGroup.name.compareToIgnoreCase(p2.personGroup.name) }
203            def assignedPersonList = taskInstance.assignedPersons.sort { p1, p2 -> p1.person.firstName.compareToIgnoreCase(p2.person.firstName) }
204
205            def taskProcedureInstance = TaskProcedure.get(taskInstance.taskProcedure?.id)
206            def taskProcedureExits = new Boolean("true")
207            if(!taskProcedureInstance) {
208                taskProcedureExits = false
209            }
210
211            params.order = "asc"
212            params.sort = "procedureStepNumber"
213            def maintenanceActionList = MaintenanceAction.findAllByTaskProcedure(taskProcedureInstance, params)
214
215            def taskRecurringScheduleInstance = TaskRecurringSchedule.get(taskInstance.taskRecurringSchedule?.id)
216            def taskRecurringScheduleExits= new Boolean("true")
217            if(!taskRecurringScheduleInstance) {
218                taskRecurringScheduleExits = false
219            }
220
221            return [ taskInstance: taskInstance,
222                            entryWorkDoneList: entryWorkDoneList,
223                            entryFaultList: entryFaultList,
224                            taskProcedureInstance: taskProcedureInstance,
225                            taskProcedureExits: taskProcedureExits,
226                            showTab: showTab,
227                            subTaskInstanceList: subTaskInstanceList,
228                            subTaskInstanceTotal: subTaskInstanceTotal,
229                            subTaskInstanceMax: params.max,
230                            maintenanceActionList: maintenanceActionList,
231                            taskRecurringScheduleInstance: taskRecurringScheduleInstance,
232                            taskRecurringScheduleExits: taskRecurringScheduleExits,
233                            inventoryMovementList: inventoryMovementList,
234                            taskModificationList: taskModificationList,
235                            assignedGroupList: assignedGroupList,
236                            assignedPersonList: assignedPersonList]
237        }
238    }
239
240    def restore = {
241
242        if(!Task.exists(params.id)) {
243            flash.message = "Task not found with id ${params.id}"
244            redirect(action: 'search')
245        }
246
247        def result = taskService.restore(params)
248
249        if(!result.error) {
250                flash.message = "Task ${params.id} has been restored."
251                redirect(action: 'show', id: result.taskInstance.id)
252        }
253        else {
254            if(result.taskInstance) {
255                render(view:'edit',model:[taskInstance:result.taskInstance])
256            }
257            else {
258                flash.message = "Task could not be updated."
259                redirect(action: 'search')
260            }
261        }
262
263    }
264
265    def trash = {
266
267        if(!Task.exists(params.id)) {
268            flash.message = "Task not found with id ${params.id}."
269            redirect(action: 'search')
270        }
271
272        def result = taskService.trash(params)
273
274        if(!result.error) {
275                flash.message = "Task ${params.id} has been moved to trash."
276                redirect(action: 'search')
277        }
278        else {
279            if(result.taskInstance) {
280                render(view:'edit',model:[taskInstance:result.taskInstance])
281            }
282            else {
283                flash.message = "Task could not be updated."
284                redirect(action: 'search')
285            }
286        }
287
288    }
289
290    def approve = {
291
292        if(!Task.exists(params.id)) {
293            flash.message = "Task not found with id ${params.id}."
294            redirect(action: 'search')
295        }
296
297        def result = taskService.approve(params)
298
299        if(!result.error) {
300                flash.message = "Task ${params.id} has been approved."
301                redirect(action: 'show', id: result.taskInstance.id)
302        }
303        else {
304            if(result.taskInstance) {
305                render(view:'edit',model:[taskInstance:result.taskInstance])
306            }
307            else {
308                flash.message = "Task could not be updated."
309                redirect(action: 'search')
310            }
311        }
312
313    }
314
315    def renegeApproval = {
316
317        if(!Task.exists(params.id)) {
318            flash.message = "Task not found with id ${params.id}."
319            redirect(action: 'search')
320        }
321
322        def result = taskService.renegeApproval(params)
323
324        if(!result.error) {
325                flash.message = "Task ${params.id} has had approval removed."
326                redirect(action: 'show', id: result.taskInstance.id)
327        }
328        else {
329            if(result.taskInstance) {
330                render(view:'edit',model:[taskInstance:result.taskInstance])
331            }
332            else {
333                flash.message = "Task could not be updated."
334                redirect(action: 'search')
335            }
336        }
337
338    }
339
340    def complete = {
341
342        if(!Task.exists(params.id)) {
343            flash.message = "Task not found with id ${params.id}."
344            redirect(action: 'search')
345        }
346
347        def result = taskService.complete(params)
348
349        if(!result.error) {
350                flash.message = "Task ${params.id} has been completed."
351                redirect(action: 'show', id: result.taskInstance.id)
352        }
353        else {
354            if(result.taskInstance) {
355                render(view:'edit',model:[taskInstance:result.taskInstance])
356            }
357            else {
358                flash.message = "Task could not be updated."
359                redirect(action: 'search')
360            }
361        }
362
363    }
364
365    def reopen = {
366
367        if(!Task.exists(params.id)) {
368            flash.message = "Task not found with id ${params.id}."
369            redirect(action: 'search')
370        }
371
372        def result = taskService.reopen(params)
373
374        if(!result.error) {
375                flash.message = "Task ${params.id} has been reopened."
376                redirect(action: 'show', id: result.taskInstance.id)
377        }
378        else {
379            if(result.taskInstance) {
380                render(view:'edit',model:[taskInstance:result.taskInstance])
381            }
382            else {
383                flash.message = "Task could not be updated."
384                redirect(action: 'search')
385            }
386        }
387
388    }
389
390    def edit = {
391
392        // In the case of an actionSubmit button, rewrite action name from 'index'.
393        if(params._action_Edit)
394        { params.action='edit' }
395
396        def taskInstance = Task.get( params.id )
397
398        if(!taskInstance) {
399            flash.message = "Task not found with id ${params.id}"
400            redirect(action: 'search')
401        }
402        else {
403            if(taskInstance.trash) {
404                flash.message = "You may not edit tasks that are in the trash."
405                redirect(action: 'show', id: taskInstance.id)
406                return
407            }
408//             def possibleParentList = taskService.possibleParentList(taskInstance)
409//             return [ taskInstance : taskInstance, possibleParentList: possibleParentList ]
410            return [ taskInstance : taskInstance ]
411        }
412    }
413
414    def update = {
415
416        if(!Task.exists(params.id)) {
417            flash.message = "Task not found with id ${params.id}"
418            redirect(action: 'search')
419        }
420
421        def result = taskService.update(params)
422
423        if(!result.error) {
424                flash.message = "Task ${params.id} updated"
425                redirect(action: 'show', id: result.taskInstance.id)
426        }
427        else {
428            render(view:'edit',model:[taskInstance:result.taskInstance.attach()])
429        }
430
431    }
432
433    def create = {
434        def taskInstance = new Task()
435
436        // Set the targetStartDate if specified, used by searchCalendar view.
437        if(params.year && params.month && params.day)
438            taskInstance.targetStartDate = dateUtilService.makeDate(params.year, params.month, params.day)
439
440        // Default leadPerson to current user, unless supplied in params.
441        taskInstance.leadPerson = personService.currentUser
442        taskInstance.properties = params
443        return ['taskInstance': taskInstance]
444    }
445
446    def save = {
447        def result = taskService.create(params)
448
449        if(!result.error) {
450            flash.message = "Task ${result.taskInstance.id} created."
451            redirect(action: 'show', id: result.taskInstance.id)
452        }
453        else {
454            if(result.taskInstance) {
455                render(view:'create', model:[taskInstance:result.taskInstance])
456            }
457            else {
458                flash.message = "Could not create task."
459                redirect(action: 'search')
460            }
461
462        }
463    }
464
465    def listSubTasks = {
466        def parentTaskInstance = Task.get(params.id)
467
468        if(!parentTaskInstance) {
469            flash.message = "Task not found with id ${params.id}"
470            redirect(action: 'search')
471        }
472        else {
473        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
474        def subTaskInstanceList = Task.findAllByParentTaskAndTrash(parentTaskInstance, false, params)
475        def subTaskInstanceTotal = Task.countByParentTaskAndTrash(parentTaskInstance, false)
476
477        [ taskInstanceList: subTaskInstanceList,
478            taskInstanceTotal:  subTaskInstanceTotal,
479            parentTaskInstance: parentTaskInstance]
480        }
481    }
482
483    def createSubTask = {
484        def parentTaskInstance = Task.get(params.id)
485
486        if(parentTaskInstance) {
487
488            def result = taskService.createSubTask(parentTaskInstance)
489            if(!result.error) {
490                flash.message = "Sub Task ${result.taskInstance.id} created, please edit and update to your requirements."
491                redirect(action: 'edit', id: result.taskInstance.id)
492            }
493            else {
494                if(result.taskInstance.errors.hasFieldErrors("parentTask")) {
495                    flash.message = g.message(code:"task.operationNotPermittedOnTaskInTrash")
496                    redirect(action: 'show', id:  parentTaskInstance.id)
497                }
498                else {
499                    render(view: 'create', model:[taskInstance: result.taskInstance])
500                }
501            }
502        }
503
504        else {
505            flash.message = "Task not found with id ${params.id}"
506            redirect(action: 'search')
507        }
508    }
509
510} // end of class.
Note: See TracBrowser for help on using the repository browser.