/**
* Provides a service class for the TaskProcedure domain class.
*/
class TaskProcedureService {

    boolean transactional = false

    def authService

    /**
    * Updates an existing taskProcedure.
    * @param params The params to update for taskProcedure with id of params.id.
    * @returns A map containing result.error (if any error) and result.taskProcedureInstance (if available).
    */
    def update(params) {
        TaskProcedure.withTransaction { status ->
            def result = [:]

            def fail = { Map m ->
                status.setRollbackOnly()
                if(result.taskProcedureInstance && m.field)
                    result.taskProcedureInstance.errors.rejectValue(m.field, m.code)
                result.error = [ code: m.code, args: ["TaskProcedure", params.id] ]
                return result
            }

            result.taskProcedureInstance = TaskProcedure.get(params.id)

            if(!result.taskProcedureInstance)
                return fail(code:"default.not.found")

            // Optimistic locking check.
            if(params.version) {
                if(result.taskProcedureInstance.version > params.version.toLong())
                    return fail(field:"version", code:"default.optimistic.locking.failure")
            }

            result.taskProcedureInstance.properties = params
            result.taskProcedureInstance.lastUpdatedBy = authService.currentUser
            result.taskProcedureInstance.lastUpdated = new Date() // Required to trigger version increment.

            // Gaps in the html index's can be created by adding 2 items and removing the first one.
            // This creates a gap at the missing index where LazyList will return a null.
            def nullMaintenanceActions = result.taskProcedureInstance.maintenanceActions.findAll {!it}
            if (nullMaintenanceActions) {
                result.taskProcedureInstance.maintenanceActions.removeAll(nullMaintenanceActions)
            }

            // Save for restoration if validation fails.
            def savedMaintenanceActions = new ArrayList(result.taskProcedureInstance.maintenanceActions)

            // Remove toBeDeleted before validation.
            def toBeDeleted = result.taskProcedureInstance.maintenanceActions.findAll {it.toBeDeleted}
            if (toBeDeleted) {
                result.taskProcedureInstance.maintenanceActions.removeAll(toBeDeleted)
            }

            if(result.taskProcedureInstance.hasErrors() || !result.taskProcedureInstance.save()) {
                // Restore the saved items, some of which contain toBeDeleted flags but
                // have not been deleted yet since validation failed.
                // The toBeDeleted items are hidden in the view.
                result.taskProcedureInstance.maintenanceActions = savedMaintenanceActions
                // Populate maintenanceAction errors for display.
                result.taskProcedureInstance.maintenanceActions.each { it.validate() }
                // Fetch to prevent lazy initialization error.
                result.taskProcedureInstance.linkedTask.primaryAsset
                result.taskProcedureInstance.createdBy
                return fail(code:"default.update.failure")
            }

            // Success.
            return result

        } //end withTransaction
    }  // end update()

} // end class
