Ignore:
Timestamp:
Feb 16, 2010, 10:02:34 AM (15 years ago)
Author:
gav
Message:

Configure cascade deletion of Site -> Section -> Asset -> SubItems.

Location:
trunk/grails-app/services
Files:
2 added
2 edited

Legend:

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

    r352 r360  
    4747                    r = assetSubItemService.delete(id: assetSubItem.id)
    4848                    if(r.error) {
     49                        log.debug r.error
    4950                        fail(code:"asset.subItems.delete.failure")
    5051                        break
     
    5657                return result
    5758
    58             try {
    59                 result.assetInstance.delete(flush:true)
    60                 return result //Success.
    61             }
    62             catch(org.springframework.dao.DataIntegrityViolationException e) {
    63                 return fail(code:"default.delete.failure")
    64             }
     59            // Success.
     60            // We have handled all the foreign keys so the delete should go forward.
     61            // Can't flush here due to cascading from Section and Site.
     62            // And without flush there is no point it trying to catch the dao.DataIntegrityViolationException
     63            // since that will only happen after leaving the transaction.
     64            result.assetInstance.delete()
     65            return result
    6566
    6667        } // end withTransaction
  • trunk/grails-app/services/AssetSubItemService.groovy

    r352 r360  
    2121
    2222    def delete(params) {
    23         def result = [:]
    24         def fail = { Map m ->
    25             result.error = [ code: m.code, args: ["AssetSubItem", params.id] ]
    26             return result
    27         }
     23        AssetSubItem.withTransaction { status ->
     24            def result = [:]
    2825
    29         result.assetSubItemInstance = AssetSubItem.get(params.id)
     26            def fail = { Map m ->
     27                status.setRollbackOnly()
     28                if(result.assetSubItemInstance && m.field)
     29                    result.assetSubItemInstance.errors.rejectValue(m.field, m.code)
     30                result.error = [ code: m.code, args: ["AssetSubItem", params.id] ]
     31                return result
     32            }
    3033
    31         if(!result.assetSubItemInstance)
    32             return fail(code:"default.not.found")
     34            result.assetSubItemInstance = AssetSubItem.get(params.id)
    3335
    34         if(result.assetSubItemInstance.assets)
    35             return fail(code:"assetSubItem.assets.associated")
     36            if(!result.assetSubItemInstance)
     37                return fail(code:"default.not.found")
    3638
    37         if(result.assetSubItemInstance.maintenanceActions)
    38             return fail(code:"maintenanceActions.still.associated")
     39            for(subItem in result.assetSubItemInstance.subItems) {
     40                if(subItem.maintenanceActions)
     41                    fail(code:"maintenanceActions.still.associated.subItem")
     42            }
    3943
    40         try {
    41             result.assetSubItemInstance.delete(flush:true)
     44            if(result.error)
     45                return result
     46
     47            if(result.assetSubItemInstance.maintenanceActions)
     48                return fail(code:"maintenanceActions.still.associated")
     49
     50            if(result.assetSubItemInstance.assets)
     51                return fail(code:"assetSubItem.assets.associated")
     52
     53            // We have handled all the foreign keys so the delete should go forward.
     54            // Can't flush here due to cascading from Section and Site.
     55            // And without flush there is no point it trying to catch the dao.DataIntegrityViolationException
     56            // since that will only happen after leaving the transaction.
     57            result.assetSubItemInstance.delete()
    4258            return result //Success.
    43         }
    44         catch(org.springframework.dao.DataIntegrityViolationException e) {
    45             return fail(code:"default.delete.failure")
    46         }
    4759
     60        } // withTransaction
    4861    }
    4962
Note: See TracChangeset for help on using the changeset viewer.