Changeset 350 for trunk


Ignore:
Timestamp:
Feb 10, 2010, 5:09:22 AM (15 years ago)
Author:
gav
Message:

Move Asset delete to a service method and configure cascade deletion checks.

Location:
trunk/grails-app
Files:
4 edited

Legend:

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

    r334 r350  
    232232    */
    233233    def delete = {
    234         def assetInstance = Asset.get( params.id )
    235         if(assetInstance) {
    236             try {
    237                 assetInstance.delete(flush:true)
    238                 flash.message = "Asset ${params.id} deleted"
    239                 redirect(action:search)
    240             }
    241             catch(org.springframework.dao.DataIntegrityViolationException e) {
    242                 flash.message = "Asset ${params.id} could not be deleted"
    243                 redirect(action:show,id:params.id)
    244             }
    245         }
    246         else {
    247             flash.message = "Asset not found with id ${params.id}"
    248             redirect(action:search)
    249         }
     234        def result = assetService.delete(params)
     235
     236        if(!result.error) {
     237            flash.message = g.message(code: "default.delete.success", args: ["Asset", params.id])
     238            redirect(action:search)
     239            return
     240        }
     241
     242        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
     243
     244        if(result.error.code == "default.not.found") {
     245            redirect(action:search)
     246            return
     247        }
     248
     249        redirect(action:show, id: params.id)
    250250    }
    251251
  • trunk/grails-app/i18n/messages.properties

    r347 r350  
    1111asset.copy.asset.required=Please select an asset to copy.
    1212
     13maintenanceActions.still.associated=Could not complete operation as maintenance actions are still associated with this item.
     14
     15asset.subItems.delete.failure=Could not complete operation as orphan sub items failed to delete.
     16
    1317assetSubItem.asset.not.found=Could not complete operation as an asset was supplied but not found.
    1418assetSubItem.assets.associated=Could not complete operation as assets are still associated with this sub item.
    15 assetSubItem.maintenanceActions.associated=Could not complete operation as maintenance actions are still associated with this sub item.
    1619
    1720sub.task.create.confirm=Immediately create and save a new sub task?
  • trunk/grails-app/services/AssetService.groovy

    r329 r350  
    22
    33    boolean transactional = false
     4
     5    def assetSubItemService
    46
    57    /**
     
    1315        }
    1416    }
     17
     18    def delete(params) {
     19        Asset.withTransaction { status ->
     20            def result = [:]
     21
     22            def fail = { Map m ->
     23                status.setRollbackOnly()
     24                if(result.assetInstance && m.field)
     25                    result.assetInstance.errors.rejectValue(m.field, m.code)
     26                result.error = [ code: m.code, args: ["Asset", params.id] ]
     27                return result
     28            }
     29
     30            result.assetInstance = Asset.get(params.id)
     31
     32            if(!result.assetInstance)
     33                return fail(code:"default.not.found")
     34
     35            if(result.assetInstance.maintenanceActions)
     36                return fail(code:"maintenanceActions.still.associated")
     37
     38            // Remove orphan assetSubItems.
     39            def assetSubItems = new ArrayList(result.assetInstance.assetSubItems) // avoid ConcurrentModificationException.
     40            def r
     41            for(assetSubItem in assetSubItems) {
     42                result.assetInstance.removeFromAssetSubItems(assetSubItem)
     43                if(!assetSubItem.assets && !assetSubItem.parentItem) {
     44                    r = assetSubItemService.delete(id: assetSubItem.id)
     45                    if(r.error) {
     46                        fail(code:"asset.subItems.delete.failure")
     47                        break
     48                    }
     49                }
     50            }
     51
     52            if(result.error)
     53                return result
     54
     55            try {
     56                result.assetInstance.delete(flush:true)
     57                return result //Success.
     58            }
     59            catch(org.springframework.dao.DataIntegrityViolationException e) {
     60                return fail(code:"default.delete.failure")
     61            }
     62
     63        } // end withTransaction
     64    } // end delete()
    1565
    1666    def create(params) {
     
    164214            return result
    165215
    166         } //end withTransaction
     216        } // end withTransaction
    167217    } // end saveCopySrvce
    168218
  • trunk/grails-app/services/AssetSubItemService.groovy

    r347 r350  
    1919
    2020        if(result.assetSubItemInstance.maintenanceActions)
    21             return fail(code:"assetSubItem.maintenanceActions.associated")
     21            return fail(code:"maintenanceActions.still.associated")
    2222
    2323        try {
Note: See TracChangeset for help on using the changeset viewer.