Changeset 249 for trunk


Ignore:
Timestamp:
Dec 27, 2009, 9:56:42 PM (15 years ago)
Author:
gav
Message:

Refactor AssignedPerson to use a serviced controller.

Location:
trunk/grails-app
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/AssignedPersonDetailedController.groovy

    r241 r249  
    33class AssignedPersonDetailedController extends BaseController {
    44
    5     def index = { redirect(action:list,params:params) }
     5    def assignedPersonService
     6
     7    def index = {
     8        flash.message = g.message(code: "assignedPerson.task.not.found")
     9        redirect(controller:"taskDetailed", action:"search")
     10    }
    611
    712    // the delete, save and update actions only accept POST requests
     
    914
    1015    def list = {
    11         params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
    12         [ assignedPersonInstanceList: AssignedPerson.list( params ), assignedPersonInstanceTotal: AssignedPerson.count() ]
     16            def result = assignedPersonService.list(params)
     17            if(!result.error) {
     18                return [ assignedPersonInstanceList: result.assignedPersonInstanceList,
     19                                assignedPersonInstanceTotal: result.assignedPersonInstanceTotal ]
     20            }
     21
     22            flash.message = g.message(code: result.error.code, args: result.error.args)
     23            redirect( controller: "appCore", action: "start" )
    1324    }
    1425
    1526    def show = {
    16         def assignedPersonInstance = AssignedPerson.get( params.id )
     27        def result = assignedPersonService.show(params)
    1728
    18         if(!assignedPersonInstance) {
    19             flash.message = "AssignedPerson not found with id ${params.id}"
    20             redirect(action:list)
    21         }
    22         else { return [ assignedPersonInstance : assignedPersonInstance ] }
     29        if(!result.error)
     30            return [ assignedPersonInstance: result.assignedPersonInstance ]
     31
     32        flash.message = g.message(code: result.error.code, args: result.error.args)
     33
     34        if(params.task?.id)
     35            redirect(controller:"taskDetailed", action:"show", id: params.task.id)
     36
     37        redirect(controller:"taskDetailed", action:"search")
    2338    }
    2439
    2540    def delete = {
    26         def assignedPersonInstance = AssignedPerson.get( params.id )
    27         if(assignedPersonInstance) {
    28             try {
    29                 def taskId = assignedPersonInstance.task.id
    30                 assignedPersonInstance.delete()
    31                 flash.message = "AssignedPerson ${params.id} deleted"
    32                 redirect(controller:"taskDetailed", action:"show", id: taskId)
    33             }
    34             catch(org.springframework.dao.DataIntegrityViolationException e) {
    35                 flash.message = "AssignedPerson ${params.id} could not be deleted"
    36                 redirect(action:show,id:params.id)
    37             }
     41        def result = assignedPersonService.delete(params)
     42
     43        if(!result.error) {
     44            flash.message = g.message(code: "default.delete.success", args: ["AssignedPerson", params.id])
     45            redirect(controller:"taskDetailed", action:"show", id: params.task?.id)
     46            return
    3847        }
    39         else {
    40             flash.message = "AssignedPerson not found with id ${params.id}"
    41             redirect(action:list)
     48
     49        flash.message = g.message(code: result.error.code, args: result.error.args)
     50
     51        if(result.error.code == "default.not.found") {
     52            redirect(controller:"taskDetailed", action:"show", id: params.task?.id)
     53            return
    4254        }
     55
     56        redirect(action:show, id: params.id)
    4357    }
    4458
    4559    def edit = {
    46         def assignedPersonInstance = AssignedPerson.get( params.id )
     60        def result = assignedPersonService.edit(params)
    4761
    48         if(!assignedPersonInstance) {
    49             flash.message = "AssignedPerson not found with id ${params.id}"
    50             redirect(action:list)
    51         }
    52         else {
    53             return [ assignedPersonInstance : assignedPersonInstance ]
    54         }
     62        if(!result.error)
     63            return [ assignedPersonInstance : result.assignedPersonInstance ]
     64
     65        flash.message = g.message(code: result.error.code, args: result.error.args)
     66
     67        redirect(controller:"taskDetailed", action:"show", id: params.task?.id)
     68
    5569    }
    5670
    5771    def update = {
    58         def assignedPersonInstance = AssignedPerson.get( params.id )
    59         if(assignedPersonInstance) {
    60             if(params.version) {
    61                 def version = params.version.toLong()
    62                 if(assignedPersonInstance.version > version) {
     72        def result = assignedPersonService.update(params)
    6373
    64                     assignedPersonInstance.errors.rejectValue("version", "assignedPerson.optimistic.locking.failure", "Another user has updated this AssignedPerson while you were editing.")
    65                     render(view:'edit',model:[assignedPersonInstance:assignedPersonInstance])
    66                     return
    67                 }
    68             }
    69             assignedPersonInstance.properties = params
    70             if(!assignedPersonInstance.hasErrors() && assignedPersonInstance.save(flush: true)) {
    71                 flash.message = "AssignedPerson ${params.id} updated"
    72                 redirect(action:show,id:assignedPersonInstance.id)
    73             }
    74             else {
    75                 render(view:'edit',model:[assignedPersonInstance:assignedPersonInstance])
    76             }
     74        if(!result.error) {
     75            flash.message = g.message(code: "default.update.success", args: ["AssignedPerson", params.id])
     76            redirect(action:show, id: params.id)
     77            return
    7778        }
    78         else {
    79             flash.message = "AssignedPerson not found with id ${params.id}"
    80             redirect(action:edit,id:params.id)
     79
     80        if(result.error.code == "default.not.found") {
     81            flash.message = g.message(code: result.error.code, args: result.error.args)
     82            redirect(controller:"taskDetailed", action:"show", id: params.task?.id)
     83            return
    8184        }
     85
     86        render(view:'edit', model:[assignedPersonInstance: result.assignedPersonInstance.attach()])
    8287    }
    8388
    8489    def create = {
    85         if(!params.task?.id) {
    86             flash.message = "Please select a task and then 'Add Assigned Person'"
    87             redirect(controller: "taskDetailed", action: search)
     90        def result = assignedPersonService.create(params)
     91
     92        if(!result.error)
     93            return [assignedPersonInstance: result.assignedPersonInstance]
     94
     95        if(result.error.code == "assignedPerson.task.not.found") {
     96            flash.message = g.message(code: result.error.code, args: result.error.args)
     97            redirect(controller: "taskDetailed", action: "search")
     98            return
    8899        }
    89         else {
    90             def assignedPersonInstance = new AssignedPerson()
    91             assignedPersonInstance.properties = params
    92             return ['assignedPersonInstance':assignedPersonInstance]
    93         }
     100
     101        flash.message = g.message(code: result.error.code, args: result.error.args)
     102        redirect(controller:"taskDetailed", action:"show", id: params.task?.id)
    94103    }
    95104
    96105    def save = {
    97         def assignedPersonInstance = new AssignedPerson(params)
     106        def result = assignedPersonService.save(params)
    98107
    99         if(!assignedPersonInstance.hasErrors() && assignedPersonInstance.save(flush: true)) {
    100             flash.message = "AssignedPerson ${assignedPersonInstance.id} created"
    101             redirect(controller:"taskDetailed", action:"show", id: params.task.id)
     108        if(!result.error) {
     109            flash.message = g.message(code: "default.create.success", args: ["AssignedPerson", result.assignedPersonInstance.id])
     110            redirect(controller:"taskDetailed", action:"show", id: result.assignedPersonInstance.task.id)
     111            return
    102112        }
    103         else {
    104             render(view:'create',model:[assignedPersonInstance:assignedPersonInstance])
    105         }
     113
     114        render(view:'edit', model:[assignedPersonInstance: result.assignedPersonInstance])
    106115    }
     116
    107117}
  • trunk/grails-app/i18n/messages.properties

    r247 r249  
    9595
    9696assignedGroup.task.not.found=Please select a task and then ''Add Assigned Group''.
     97assignedPerson.task.not.found=Please select a task and then ''Add Assigned Person''.
    9798
    9899default.list.failure=Could not generate list for class {0}.
  • trunk/grails-app/views/assignedPersonDetailed/create.gsp

    r242 r249  
    2121            </g:hasErrors>
    2222            <g:form action="save" method="post" >
    23                 <input type="hidden" name="task.id" value="${assignedPersonInstance?.task?.id}" />
     23                <g:hiddenField name="task.id" value="${assignedPersonInstance?.task?.id}" />
    2424                <div class="dialog">
    2525                    <table>
  • trunk/grails-app/views/assignedPersonDetailed/edit.gsp

    r242 r249  
    2121            </g:hasErrors>
    2222            <g:form method="post" >
    23                 <input type="hidden" name="id" value="${assignedPersonInstance?.id}" />
    24                 <input type="hidden" name="version" value="${assignedPersonInstance?.version}" />
     23                <g:hiddenField name="id" value="${assignedPersonInstance?.id}" />
     24                <g:hiddenField name="task.id" value="${assignedPersonInstance?.task?.id}" />
     25                <g:hiddenField name="version" value="${assignedPersonInstance?.version}" />
    2526                <div class="dialog">
    2627                    <table>
  • trunk/grails-app/views/assignedPersonDetailed/show.gsp

    r147 r249  
    5151            <div class="buttons">
    5252                <g:form>
    53                     <input type="hidden" name="id" value="${assignedPersonInstance?.id}" />
     53                    <g:hiddenField name="id" value="${assignedPersonInstance?.id}" />
     54                    <g:hiddenField name="task.id" value="${assignedPersonInstance?.task?.id}" />
    5455                    <span class="button"><g:actionSubmit class="edit" value="Edit" /></span>
    5556                    <span class="button"><g:actionSubmit class="delete" onclick="return confirm('Are you sure?');" value="Delete" /></span>
  • trunk/grails-app/views/taskDetailed/show.gsp

    r247 r249  
    204204                                            <ul>
    205205                                            <g:each var="a" in="${taskInstance.assignedPersons}">
    206                                                 <li><g:link controller="assignedPersonDetailed" action="edit" id="${a.id}">${a?.encodeAsHTML()}</g:link></li>
     206                                                <li><g:link controller="assignedPersonDetailed" action="edit" id="${a.id}" params="['task.id':taskInstance.id]">${a?.encodeAsHTML()}</g:link></li>
    207207                                            </g:each>
    208208                                            </ul>
    209                                             <g:link controller="assignedPersonDetailed" params="['task.id':taskInstance?.id]" action="create">Add Assigned Person</g:link>
     209                                            <g:link controller="assignedPersonDetailed" action="create" params="['task.id':taskInstance?.id]">Add Assigned Person</g:link>
    210210                                        </td>
    211211
Note: See TracChangeset for help on using the changeset viewer.