Index: trunk/grails-app/controllers/TaskDetailedController.groovy
===================================================================
--- trunk/grails-app/controllers/TaskDetailedController.groovy	(revision 190)
+++ trunk/grails-app/controllers/TaskDetailedController.groovy	(revision 196)
@@ -14,5 +14,5 @@
     static allowedMethods = [save:'POST', update:'POST', restore:'POST', trash:'POST', approve:'POST', renegeApproval:'POST', complete:'POST', reopen:'POST']
 
-    def index = { redirect(action:search,params:params) }
+    def index = { redirect(action: 'search', params: params) }
 
     def list = {
@@ -163,5 +163,5 @@
         if(!taskInstance) {
             flash.message = "Task not found with id ${params.id}"
-            redirect(action:search)
+            redirect(action: 'search')
         }
         else {
@@ -180,6 +180,6 @@
                                                         }
 
-            def subTaskInstanceList = Task.findAllByParentTask(taskInstance, params)
-            def subTaskInstanceTotal = Task.countByParentTask(taskInstance)
+            def subTaskInstanceList = Task.findAllByParentTaskAndTrash(taskInstance, false, params)
+            def subTaskInstanceTotal = Task.countByParentTaskAndTrash(taskInstance, false)
             def showTaskTab = new String("true")
 
@@ -225,5 +225,5 @@
         if(!Task.exists(params.id)) {
             flash.message = "Task not found with id ${params.id}"
-            redirect(action:'search')
+            redirect(action: 'search')
         }
 
@@ -232,13 +232,13 @@
         if(!result.error) {
                 flash.message = "Task ${params.id} has been restored."
-                redirect(action:show,id:result.taskInstance.id)
-        }
-        else {
-            if(result.taskInstance) {
-                render(view:'edit',model:[taskInstance:result.taskInstance])
-            }
-            else {
-                flash.message = "Task could not be updated."
-                redirect(action:'search')
+                redirect(action: 'show', id: result.taskInstance.id)
+        }
+        else {
+            if(result.taskInstance) {
+                render(view:'edit',model:[taskInstance:result.taskInstance])
+            }
+            else {
+                flash.message = "Task could not be updated."
+                redirect(action: 'search')
             }
         }
@@ -250,5 +250,5 @@
         if(!Task.exists(params.id)) {
             flash.message = "Task not found with id ${params.id}."
-            redirect(action:'search')
+            redirect(action: 'search')
         }
 
@@ -257,13 +257,13 @@
         if(!result.error) {
                 flash.message = "Task ${params.id} has been moved to trash."
-                redirect(action:'search')
-        }
-        else {
-            if(result.taskInstance) {
-                render(view:'edit',model:[taskInstance:result.taskInstance])
-            }
-            else {
-                flash.message = "Task could not be updated."
-                redirect(action:'search')
+                redirect(action: 'search')
+        }
+        else {
+            if(result.taskInstance) {
+                render(view:'edit',model:[taskInstance:result.taskInstance])
+            }
+            else {
+                flash.message = "Task could not be updated."
+                redirect(action: 'search')
             }
         }
@@ -275,5 +275,5 @@
         if(!Task.exists(params.id)) {
             flash.message = "Task not found with id ${params.id}."
-            redirect(action:'search')
+            redirect(action: 'search')
         }
 
@@ -282,13 +282,13 @@
         if(!result.error) {
                 flash.message = "Task ${params.id} has been approved."
-                redirect(action:show,id:result.taskInstance.id)
-        }
-        else {
-            if(result.taskInstance) {
-                render(view:'edit',model:[taskInstance:result.taskInstance])
-            }
-            else {
-                flash.message = "Task could not be updated."
-                redirect(action:'search')
+                redirect(action: 'show', id: result.taskInstance.id)
+        }
+        else {
+            if(result.taskInstance) {
+                render(view:'edit',model:[taskInstance:result.taskInstance])
+            }
+            else {
+                flash.message = "Task could not be updated."
+                redirect(action: 'search')
             }
         }
@@ -300,5 +300,5 @@
         if(!Task.exists(params.id)) {
             flash.message = "Task not found with id ${params.id}."
-            redirect(action:'search')
+            redirect(action: 'search')
         }
 
@@ -307,13 +307,13 @@
         if(!result.error) {
                 flash.message = "Task ${params.id} has had approval removed."
-                redirect(action:show,id:result.taskInstance.id)
-        }
-        else {
-            if(result.taskInstance) {
-                render(view:'edit',model:[taskInstance:result.taskInstance])
-            }
-            else {
-                flash.message = "Task could not be updated."
-                redirect(action:'search')
+                redirect(action: 'show', id: result.taskInstance.id)
+        }
+        else {
+            if(result.taskInstance) {
+                render(view:'edit',model:[taskInstance:result.taskInstance])
+            }
+            else {
+                flash.message = "Task could not be updated."
+                redirect(action: 'search')
             }
         }
@@ -325,5 +325,5 @@
         if(!Task.exists(params.id)) {
             flash.message = "Task not found with id ${params.id}."
-            redirect(action:'search')
+            redirect(action: 'search')
         }
 
@@ -332,13 +332,13 @@
         if(!result.error) {
                 flash.message = "Task ${params.id} has been completed."
-                redirect(action:show,id:result.taskInstance.id)
-        }
-        else {
-            if(result.taskInstance) {
-                render(view:'edit',model:[taskInstance:result.taskInstance])
-            }
-            else {
-                flash.message = "Task could not be updated."
-                redirect(action:'search')
+                redirect(action: 'show', id: result.taskInstance.id)
+        }
+        else {
+            if(result.taskInstance) {
+                render(view:'edit',model:[taskInstance:result.taskInstance])
+            }
+            else {
+                flash.message = "Task could not be updated."
+                redirect(action: 'search')
             }
         }
@@ -350,5 +350,5 @@
         if(!Task.exists(params.id)) {
             flash.message = "Task not found with id ${params.id}."
-            redirect(action:'search')
+            redirect(action: 'search')
         }
 
@@ -357,13 +357,13 @@
         if(!result.error) {
                 flash.message = "Task ${params.id} has been reopened."
-                redirect(action:show,id:result.taskInstance.id)
-        }
-        else {
-            if(result.taskInstance) {
-                render(view:'edit',model:[taskInstance:result.taskInstance])
-            }
-            else {
-                flash.message = "Task could not be updated."
-                redirect(action:'search')
+                redirect(action: 'show', id: result.taskInstance.id)
+        }
+        else {
+            if(result.taskInstance) {
+                render(view:'edit',model:[taskInstance:result.taskInstance])
+            }
+            else {
+                flash.message = "Task could not be updated."
+                redirect(action: 'search')
             }
         }
@@ -381,18 +381,13 @@
         if(!taskInstance) {
             flash.message = "Task not found with id ${params.id}"
-            redirect(action:search)
+            redirect(action: 'search')
         }
         else {
             if(taskInstance.trash) {
-                flash.message = "You may not edit items in the trash."
-                redirect(action:show,id:taskInstance.id)
-            }
-            def criteria = taskInstance.createCriteria()
-            def possibleParentList = criteria {
-                and {
-                    notEqual('id', taskInstance.id)
-                    taskInstance.subTasks.each() { notEqual('id', it.id) }
-                    }
-            }
+                flash.message = "You may not edit tasks that are in the trash."
+                redirect(action: 'show', id: taskInstance.id)
+                return
+            }
+            def possibleParentList = taskService.possibleParentList(taskInstance)
             return [ taskInstance : taskInstance, possibleParentList: possibleParentList ]
         }
@@ -403,5 +398,5 @@
         if(!Task.exists(params.id)) {
             flash.message = "Task not found with id ${params.id}"
-            redirect(action:'search')
+            redirect(action: 'search')
         }
 
@@ -410,13 +405,13 @@
         if(!result.error) {
                 flash.message = "Task ${params.id} updated"
-                redirect(action:show,id:result.taskInstance.id)
-        }
-        else {
-            if(result.taskInstance) {
-                render(view:'edit',model:[taskInstance:result.taskInstance])
-            }
-            else {
-                flash.message = "Task could not be updated."
-                redirect(action:'search')
+                redirect(action: 'show', id: result.taskInstance.id)
+        }
+        else {
+            if(result.taskInstance) {
+                render(view:'edit',model:[taskInstance:result.taskInstance])
+            }
+            else {
+                flash.message = "Task could not be updated."
+                redirect(action: 'search')
             }
         }
@@ -453,8 +448,8 @@
     def create = {
         def taskInstance = new Task()
-        // Default leadPerson to current user.
+        // Default leadPerson to current user, unless supplied in params.
         taskInstance.leadPerson = personService.currentUser()
         taskInstance.properties = params
-        return ['taskInstance':taskInstance]
+        return ['taskInstance': taskInstance]
     }
 
@@ -464,13 +459,13 @@
         if(!result.error) {
             flash.message = "Task ${result.taskInstance.id} created."
-            redirect(action: show,id: result.taskInstance.id)
-        }
-        else {
-            if(result.taskInstance) {
-                render(view:'create',model:[taskInstance:result.taskInstance])
+            redirect(action: 'show', id: result.taskInstance.id)
+        }
+        else {
+            if(result.taskInstance) {
+                render(view:'create', model:[taskInstance:result.taskInstance])
             }
             else {
                 flash.message = "Could not create task."
-                redirect(action:"search")
+                redirect(action: 'search')
             }
 
@@ -483,10 +478,10 @@
         if(!parentTaskInstance) {
             flash.message = "Task not found with id ${params.id}"
-            redirect(action:search)
+            redirect(action: 'search')
         }
         else {
         params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
-        def subTaskInstanceList = Task.findAllByParentTask(parentTaskInstance, params)
-        def subTaskInstanceTotal = Task.countByParentTask(parentTaskInstance)
+        def subTaskInstanceList = Task.findAllByParentTaskAndTrash(parentTaskInstance, false, params)
+        def subTaskInstanceTotal = Task.countByParentTaskAndTrash(parentTaskInstance, false)
 
         [ taskInstanceList: subTaskInstanceList,
@@ -496,3 +491,30 @@
     }
 
-}
+    def createSubTask = {
+        def parentTaskInstance = Task.get(params.id)
+
+        if(parentTaskInstance) {
+
+            def result = taskService.createSubTask(parentTaskInstance)
+            if(!result.error) {
+                flash.message = "Sub Task ${result.taskInstance.id} created, please edit and update to your requirements."
+                redirect(action: 'edit', id: result.taskInstance.id)
+            }
+            else {
+                if(result.taskInstance.errors.hasFieldErrors("parentTask")) {
+                    flash.message = g.message(code:"task.operationNotPermittedOnTaskInTrash")
+                    redirect(action: 'show', id:  parentTaskInstance.id)
+                }
+                else {
+                    render(view: 'create', model:[taskInstance: result.taskInstance])
+                }
+            }
+        }
+
+        else {
+            flash.message = "Task not found with id ${params.id}"
+            redirect(action: 'search')
+        }
+    }
+
+} // end of class.
