Changeset 360 for trunk/grails-app


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
Files:
2 added
4 edited

Legend:

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

    r298 r360  
    33@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_AssetManager'])
    44class SiteDetailedController extends BaseController {
     5
     6    def siteService
    57
    68    // the delete, save and update actions only accept POST requests
     
    2931    @Secured(['ROLE_AppAdmin'])
    3032    def delete = {
    31         def siteInstance = Site.get( params.id )
    32         if(siteInstance) {
    33             try {
    34                 siteInstance.delete(flush:true)
    35                 flash.message = "Site ${params.id} deleted"
    36                 redirect(action:list)
    37             }
    38             catch(org.springframework.dao.DataIntegrityViolationException e) {
    39                 flash.message = "Site ${params.id} could not be deleted"
    40                 redirect(action:show,id:params.id)
    41             }
     33        def result = siteService.delete(params)
     34
     35        if(!result.error) {
     36            flash.message = g.message(code: "default.delete.success", args: ["Site", params.id])
     37            redirect(action:list)
     38            return
    4239        }
    43         else {
    44             flash.message = "Site not found with id ${params.id}"
     40
     41        flash.errorMessage = g.message(code: result.error.code, args: result.error.args)
     42
     43        if(result.error.code == "default.not.found") {
    4544            redirect(action:list)
     45            return
    4646        }
     47
     48        redirect(action:show, id: params.id)
    4749    }
    4850
  • trunk/grails-app/i18n/messages.properties

    r350 r360  
    1111asset.copy.asset.required=Please select an asset to copy.
    1212
    13 maintenanceActions.still.associated=Could not complete operation as maintenance actions are still associated with this item.
    14 
     13maintenanceActions.still.associated=Could not complete operation as maintenance actions are still associated \
     14    with this item.
     15maintenanceActions.still.associated.subItem=Could not complete operation as maintenance actions are still associated \
     16    with a sub item.
     17inventoryStores.still.associated=Could not complete operation as inventory stores are still associated with this item.
     18
     19site.section.delete.failure=Could not complete operation as sections failed to delete.
     20section.asset.delete.failure=Could not complete operation as assets failed to delete.
    1521asset.subItems.delete.failure=Could not complete operation as orphan sub items failed to delete.
    1622
  • 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.