Ignore:
Timestamp:
Feb 16, 2011, 8:10:39 PM (9 years ago)
Author:
gav
Message:

Domain change, first draft of TaskProcedureRevisions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/services/TaskProcedureService.groovy

    r798 r809  
    77
    88    def authService
     9    def dateUtilService
    910
    1011    /**
     
    2324                result.error = [ code: m.code, args: ["TaskProcedure", params.id] ]
    2425                // Fetch to prevent lazy initialization error.
    25                 result.taskProcedureInstance?.linkedTask.primaryAsset
    26                 result.taskProcedureInstance?.createdBy
     26                result.taskProcedureInstance.revisions.each {it.createdBy.toString()}
    2727                return result
    2828            }
     
    4040
    4141            result.taskProcedureInstance.properties = params
    42             result.taskProcedureInstance.lastUpdatedBy = authService.currentUser
    43             result.taskProcedureInstance.lastUpdated = new Date() // Required to trigger version increment.
    4442
    4543            // Gaps in the html index's can be created by adding 2 items and removing the first one.
     
    8078            }
    8179
     80            def r = createRevision(result.taskProcedureInstance)
     81            if(r.error)
     82                return fail(field:'id', code:"default.create.revision.failure")
     83
     84            // Also sets: taskInstance.taskProcedureRevision = taskProcedureRevision
     85            r.taskProcedureRevision.addToTasks(result.taskProcedureInstance.linkedTask)
     86
     87            // Update tasks that are using previousRevision.
     88            // Only those that are not started and due to be started today or in the future.
     89            def previousRevision = result.taskProcedureInstance.getRevision(r.taskProcedureRevision.revision - 1)
     90            if(previousRevision) {
     91                Task.withCriteria {
     92                    eq("taskProcedureRevision", previousRevision)
     93                    ge("targetStartDate", dateUtilService.today)
     94                    taskStatus {
     95                        idEq(1L) // Not Started.
     96                    }
     97                    maxResults(10000)
     98                }.each {
     99                    it.taskProcedureRevision = r.taskProcedureRevision
     100                }
     101            }
     102
    82103            // Success.
    83104            return result
    84105
    85         } //end withTransaction
     106        } // end withTransaction
    86107    }  // end update()
    87108
     
    96117            def fail = { Map m ->
    97118                status.setRollbackOnly()
    98                 if(result.taskProcedureInstance && m.field) 
     119                if(result.taskProcedureInstance && m.field)
    99120                    result.taskProcedureInstance.errors.rejectValue(m.field, m.code)
    100121                result.error = [ code: m.code, args: ["TaskProcedure", params.id] ]
     
    107128
    108129            // Optimistic locking check on linkedTask.
    109             if(result.taskProcedureInstance.linkedTask.taskProcedure)
     130            if(result.taskProcedureInstance.linkedTask.taskProcedureRevision)
    110131                    return fail(field:"version", code:"default.optimistic.locking.failure")
    111 
    112             result.taskProcedureInstance.createdBy = authService.currentUser
    113             result.taskProcedureInstance.lastUpdatedBy = authService.currentUser
    114132
    115133            // Gaps in the html index's can be created by adding 2 items and removing the first one.
     
    124142            }
    125143
    126             // Also sets: taskInstance.taskProcedure = taskProcedureInstance
    127             result.taskProcedureInstance.addToTasks(result.taskProcedureInstance.linkedTask)
    128 
    129144            if(result.taskProcedureInstance.hasErrors() || !result.taskProcedureInstance.save()) {
    130145                // Populate collection errors for display.
     
    134149            }
    135150
    136         } //end withTransaction
    137 
    138         result.taskProcedureInstance.lastUpdated = new Date() // Required to trigger version increment to 1.
    139 
    140         // success
     151            def r = createRevision(result.taskProcedureInstance)
     152            if(r.error)
     153                return fail(field:'id', code:"default.create.revision.failure")
     154
     155            // Also sets: taskInstance.taskProcedureRevision = taskProcedureRevision
     156            r.taskProcedureRevision.addToTasks(result.taskProcedureInstance.linkedTask)
     157
     158        } // end withTransaction
     159
     160        // Success.
    141161        return result
    142162    }  // end save()
    143163
     164    /**
     165    * Creates a new taskProcedureRevision.
     166    * @param taskProcedureInstance The taskProcedure to create the revision for.
     167    * @returns A map containing result.error (if any error) and result.taskProcedureRevision.
     168    */
     169    def createRevision(taskProcedureInstance) {
     170        def result = [:]
     171        TaskProcedureRevision.withTransaction { status ->
     172            def fail = { Map m ->
     173                status.setRollbackOnly()
     174                if(result.taskProcedureRevision && m.field)
     175                    result.taskProcedureRevision.errors.rejectValue(m.field, m.code)
     176                result.error = [ code: m.code, args: ["TaskProcedureRevision"] ]
     177                return result
     178            }
     179
     180            result.taskProcedureRevision = new TaskProcedureRevision()
     181            taskProcedureInstance.addToRevisions(result.taskProcedureRevision)
     182
     183            result.taskProcedureRevision.taskProcedure = taskProcedureInstance
     184            result.taskProcedureRevision.linkedTask = taskProcedureInstance.linkedTask
     185            result.taskProcedureRevision.createdBy =  authService.currentUser
     186            result.taskProcedureRevision.revision = 0 // init to prevent DataIntegrityViolationException
     187            result.taskProcedureRevision.revision = taskProcedureInstance.revision
     188            taskProcedureInstance.maintenanceActions.each {
     189                result.taskProcedureRevision.addToMaintenanceActions(new MaintenanceAction(it.properties))
     190            }
     191            taskProcedureInstance.documentReferences.each {
     192                result.taskProcedureRevision.addToDocumentReferences(new DocumentReference(it.properties))
     193            }
     194
     195            if(result.taskProcedureRevision.hasErrors() || !result.taskProcedureRevision.save()) {
     196                log.error result.taskProcedureRevision.errors
     197                return fail(code:"default.create.failure")
     198            }
     199
     200        } // end withTransaction
     201
     202        // Success.
     203        return result
     204    } // end createRevision()
     205
    144206} // end class
Note: See TracChangeset for help on using the changeset viewer.