Ignore:
Timestamp:
Feb 2, 2011, 8:38:20 PM (14 years ago)
Author:
gav
Message:

Domain change, improved draft of creating taskProcedure dynamic form.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/features/taskProcedureRework/grails-app/controllers/TaskProcedureDetailedController.groovy

    r762 r770  
    4040
    4141        if(!taskProcedureInstance) {
    42             flash.message = "TaskProcedure not found with id ${params.id}"
    43             redirect(action:list)
    44         }
    45         else { return [ taskProcedureInstance : 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"])
    4651    }
    4752
     
    4954        def taskProcedureInstance = TaskProcedure.get( params.id )
    5055        if(taskProcedureInstance) {
     56            def taskInstance = taskProcedureInstance.linkedTask
    5157            try {
    5258                taskProcedureInstance.tasks.each {
     
    5561                taskProcedureInstance.delete(flush:true)
    5662                flash.message = "TaskProcedure ${params.id} deleted"
    57                 redirect(action:list)
     63                redirect(controller:'taskDetailed',
     64                                action:'show',
     65                                id:taskInstance.id,
     66                                params:[showTab:"showProcedureTab"])
    5867            }
    5968            catch(org.springframework.dao.DataIntegrityViolationException e) {
    6069                flash.message = "TaskProcedure ${params.id} could not be deleted"
    61                 redirect(action:show,id:params.id)
     70                redirect(controller:'taskDetailed',
     71                                action:'show',
     72                                id:taskInstance.id,
     73                                params:[showTab:"showProcedureTab"])
    6274            }
    6375        }
     
    99111            taskProcedureInstance.properties = params
    100112
    101             def toBeDeleted = taskProcedureInstance.maintenanceActions.findAll {it.deleted}
    102             if(toBeDeleted) {
     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) {
    103126                taskProcedureInstance.maintenanceActions.removeAll(toBeDeleted)
    104127            }
     
    106129            if(!taskProcedureInstance.hasErrors() && taskProcedureInstance.save(flush: true)) {
    107130                flash.message = "TaskProcedure ${params.id} updated"
    108                 redirect(action:show,id:taskProcedureInstance.id)
     131                redirect(controller:'taskDetailed',
     132                                action:'show',
     133                                id:taskProcedureInstance.linkedTask.id,
     134                                params:[showTab:"showProcedureTab"])
    109135            }
    110136            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() }
    111143                render(view:'edit',model:[taskProcedureInstance:taskProcedureInstance])
    112144            }
     
    119151
    120152    def create = {
    121         if(params.taskInstance?.id) {
    122             def taskInstance = Task.get(params.taskInstance.id)
    123 
    124             def taskProcedureInstance = new TaskProcedure()
    125             taskProcedureInstance.properties = params
    126             return ['taskProcedureInstance':taskProcedureInstance,
    127                             taskInstance: taskInstance]
    128         } else {
    129             flash.message = "Please select or create a task, then go to the Procedure tab."
     153
     154        if(!params.taskInstance?.id) {
     155            flash.errorMessage = "Please select or create a task, then go to the Procedure tab."
    130156            redirect(controller:"taskDetailed", action:"search")
    131         }
     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]
    132171    }
    133172
     
    136175        def taskInstance = Task.get(params.linkedTask.id)
    137176
     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
    138184        if(!taskProcedureInstance.hasErrors() && taskProcedureInstance.save(flush: true)) {
    139             taskProcedureInstance.addToTasks(taskInstance) // also sets: taskInstance.taskProcedure = taskProcedureInstance
     185             // Also sets: taskInstance.taskProcedure = taskProcedureInstance
     186            taskProcedureInstance.addToTasks(taskInstance)
    140187            flash.message = "TaskProcedure ${taskProcedureInstance.id} created."
    141             redirect(action:show,id:taskProcedureInstance.id)
    142         }
    143         else {
    144             render(view:'create',model:[taskProcedureInstance:taskProcedureInstance, taskInstance: taskInstance])
    145         }
    146     }
     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
    147200}
Note: See TracChangeset for help on using the changeset viewer.