Changeset 186 for trunk


Ignore:
Timestamp:
Nov 15, 2009, 1:50:18 AM (15 years ago)
Author:
gav
Message:

Add createEntry to TaskService, automgically updates the task to "In progress" and creates the "Started" task modification.

Location:
trunk/grails-app
Files:
4 edited

Legend:

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

    r185 r186  
    44
    55    def personService
     6    def taskService
    67
    78    def index = { redirect(action:list,params:params) }
     
    2021        if(!entryInstance) {
    2122            flash.message = "Entry not found with id ${params.id}"
    22             redirect(action:list)
     23            redirect(controller: 'taskDetailed', action: 'search')
    2324        }
    2425        else { return [ entryInstance : entryInstance ] }
     
    4243        else {
    4344            flash.message = "Entry not found with id ${params.id}"
    44             redirect(action:list)
     45            redirect(controller: "taskDetailed", action:"search")
    4546        }
    4647    }
     
    5051        if(!entryInstance) {
    5152                flash.message = "Entry not found with id ${params.id}"
    52                 redirect(action:list)
     53                redirect(controller: "taskDetailed", action:"search")
    5354        }
    5455        else {
     
    8687        else {
    8788            flash.message = "Entry not found with id ${params.id}"
    88             redirect(action:edit,id:params.id)
     89            redirect(controller: "taskDetailed", action:"search")
    8990        }
    9091    }
     
    9899        }
    99100        catch(Exception e) {
    100             flash.message = "Please select a task, then 'Add Entry'"
    101             redirect(controller:"taskDetailed", action:"list")
     101            flash.message = "Please select a task, then 'Add Entry'."
     102            redirect(controller:"taskDetailed", action:"search")
    102103        }
    103104    }
    104105
    105106    def save = {
    106         def entryInstance = new Entry(params)
     107        def result = taskService.createEntry(params)
    107108
    108         entryInstance.enteredBy = personService.currentUser()
    109         if(!entryInstance.hasErrors() && entryInstance.save(flush: true)) {
    110             flash.message = "Entry ${entryInstance.id} created"
    111             redirect(controller:"taskDetailed", action:"show", id: params.task.id)
     109        if(!result.error) {
     110            flash.message = "Entry created."
     111            redirect(controller: "taskDetailed", action: "show", id: result.taskId)
    112112        }
    113113        else {
    114             render(view:'create',model:[entryInstance:entryInstance])
     114            if(result.entryInstance) {
     115                render(view:'create',model:[entryInstance: result.entryInstance])
     116            }
     117            else {
     118                flash.message = "Could not create entry."
     119                redirect(controller: "taskDetailed", action:"search")
     120            }
     121
    115122        }
    116123    }
     124
    117125}
  • trunk/grails-app/i18n/messages.properties

    r181 r186  
    5151inventoryMovement.quantity.insufficientItemsInStock=Could not complete operation, insufficient items in stock.
    5252inventoryMovement.inventoryItem.notFound=Inventory Item not found.
     53
     54entry.task.notFound=Could not complete operation, task not found.
     55entry.task.failedToSave=Could not complete operation, task failed to save.
     56entry.task.failedToSaveTaskModification=Could not complete operation, task modification failed to save.
     57entry.task.taskIsComplete=This operation is not permitted on a complete task.
    5358
    5459default.doesnt.match.message=Property [{0}] of class [{1}] with value [{2}] does not match the required pattern [{3}]
  • trunk/grails-app/services/TaskService.groovy

    r182 r186  
    3636    } // end create()
    3737
    38     def start() {
    39         //TaskModificationType.get(2)
    40     }  // end start()
     38    def createEntry(params) {
     39        Task.withTransaction { status ->
     40            def result = [:]
     41            result.entryInstance = new Entry(params)
     42            result.entryInstance.enteredBy = personService.currentUser()
     43
     44            if(result.entryInstance.validate()) {
     45                def taskInstance = Task.lock(result.entryInstance.task.id)
     46                result.taskId = result.entryInstance.task.id
     47
     48                if(!taskInstance) {
     49                    status.setRollbackOnly()
     50                    result.entryInstance.errors.rejectValue('task', "entry.task.notFound")
     51                    result.error = true
     52                    return result
     53                }
     54
     55                if(taskInstance.taskStatus.id == 3) {
     56                    status.setRollbackOnly()
     57                    result.entryInstance.errors.rejectValue('task', "entry.task.taskIsComplete")
     58                    result.error = true
     59                    return result
     60                }
     61
     62                // If task status is "Not Started" and entry type is "Work Done" then we create the started modification and set the status.
     63                if(taskInstance.taskStatus.id == 1 && result.entryInstance.entryType.id == 2) {
     64
     65                    // Create the "Started" task modification, this provides the "Actual started date".
     66                    def taskModification = new TaskModification(person: personService.currentUser(),
     67                                                            taskModificationType: TaskModificationType.get(2),
     68                                                            task: taskInstance)
     69
     70                    if(!taskModification.save()) {
     71                        status.setRollbackOnly()
     72                        taskInstance.errors.rejectValue("task", "entry.task.failedToSaveTaskModification")
     73                        result.error = true
     74                        return result
     75                    }
     76
     77                    // Set task status to "In progress".
     78                    taskInstance.taskStatus = TaskStatus.get(2)
     79
     80                    if(!taskInstance.save()) {
     81                        status.setRollbackOnly()
     82                        result.entryInstance.errors.rejectValue("task", "entry.task.failedToSave")
     83                        result.error = true
     84                        return result
     85                    }
     86                }
     87
     88                if(!result.entryInstance.save()) {
     89                    status.setRollbackOnly()
     90                    result.error = true
     91                    return result
     92                }
     93
     94                // All went well if we get to here.
     95                return result
     96            }
     97            else {
     98                result.error = true
     99                return result
     100            }
     101
     102        } //end withTransaction
     103    } // end create()
    41104
    42105    def update(params) {
  • trunk/grails-app/views/entryDetailed/create.gsp

    r147 r186  
    3333                                </td>
    3434                                <td valign="top" class="name">
    35                                     ${entryInstance?.task?.description}
     35                                    ${entryInstance?.task}
    3636                                </td>
    3737                            </tr>
Note: See TracChangeset for help on using the changeset viewer.