source: trunk/grails-app/services/AssetSubItemService.groovy @ 385

Last change on this file since 385 was 360, checked in by gav, 15 years ago

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

File size: 3.9 KB
Line 
1class AssetSubItemService {
2
3    boolean transactional = false
4
5    /**
6    * Determines and returns sorted list of possible parent items.
7    * @param assetSubItemInstance The instance to prepare the list for.
8    * @returns A list of the possible parentItems.
9    */
10    def possibleParentItems(assetSubItemInstance) {
11        def criteria = AssetSubItem.createCriteria()
12        def possibleParentItems = criteria.list() {
13            and {
14                eq('isActive', true)
15                ne('id', assetSubItemInstance.id)
16                assetSubItemInstance.subItems.each() { notEqual('id', it.id) }
17                }
18        }
19        possibleParentItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }
20    }
21
22    def delete(params) {
23        AssetSubItem.withTransaction { status ->
24            def result = [:]
25
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            }
33
34            result.assetSubItemInstance = AssetSubItem.get(params.id)
35
36            if(!result.assetSubItemInstance)
37                return fail(code:"default.not.found")
38
39            for(subItem in result.assetSubItemInstance.subItems) {
40                if(subItem.maintenanceActions)
41                    fail(code:"maintenanceActions.still.associated.subItem")
42            }
43
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()
58            return result //Success.
59
60        } // withTransaction
61    }
62
63    def create(params) {
64        def result = [:]
65        def fail = { Map m ->
66            result.error = [ code: m.code, args: ["AssetSubItem", params.id] ]
67            return result
68        }
69
70        result.assetSubItemInstance = new AssetSubItem()
71        result.assetSubItemInstance.properties = params
72
73        // pass in an asset to link to if provided.
74        if(params.asset?.id)
75            result.assetInstance = Asset.get(params.asset.id)
76
77        // success
78        return result
79    }
80
81    def save(params) {
82        AssetSubItem.withTransaction { status ->
83            def result = [:]
84
85            def fail = { Map m ->
86                status.setRollbackOnly()
87                if(result.assetSubItemInstance && m.field)
88                    result.assetSubItemInstance.errors.rejectValue(m.field, m.code)
89                result.error = [ code: m.code, args: ["AssetSubItem", params.id] ]
90                return result
91            }
92
93            result.assetSubItemInstance = new AssetSubItem(params)
94
95            if(params.asset?.id) {
96                result.assetInstance = Asset.get(params.asset.id)
97
98                if(!result.assetInstance)
99                    return fail(code:"assetSubItem.asset.not.found")
100            }
101
102            if(result.assetSubItemInstance.hasErrors() || !result.assetSubItemInstance.save(flush: true))
103                return fail(code:"default.create.failure")
104
105            if(result.assetInstance)
106                result.assetInstance.addToAssetSubItems(result.assetSubItemInstance)
107
108            // Success.
109            return result
110
111        } //end withTransaction
112    } // save()
113
114} // end class
Note: See TracBrowser for help on using the repository browser.