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

Last change on this file since 589 was 387, checked in by gav, 15 years ago

Capitalise asset and sub items during creation and update.
Small correction to alt menu.

File size: 4.2 KB
RevLine 
[387]1
2import org.apache.commons.lang.WordUtils
3
[285]4class AssetSubItemService {
5
6    boolean transactional = false
7
[352]8    /**
9    * Determines and returns sorted list of possible parent items.
10    * @param assetSubItemInstance The instance to prepare the list for.
11    * @returns A list of the possible parentItems.
12    */
13    def possibleParentItems(assetSubItemInstance) {
14        def criteria = AssetSubItem.createCriteria()
15        def possibleParentItems = criteria.list() {
16            and {
17                eq('isActive', true)
18                ne('id', assetSubItemInstance.id)
19                assetSubItemInstance.subItems.each() { notEqual('id', it.id) }
20                }
21        }
22        possibleParentItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }
23    }
24
[347]25    def delete(params) {
[360]26        AssetSubItem.withTransaction { status ->
27            def result = [:]
[347]28
[360]29            def fail = { Map m ->
30                status.setRollbackOnly()
31                if(result.assetSubItemInstance && m.field)
32                    result.assetSubItemInstance.errors.rejectValue(m.field, m.code)
33                result.error = [ code: m.code, args: ["AssetSubItem", params.id] ]
34                return result
35            }
[347]36
[360]37            result.assetSubItemInstance = AssetSubItem.get(params.id)
[347]38
[360]39            if(!result.assetSubItemInstance)
40                return fail(code:"default.not.found")
[347]41
[360]42            for(subItem in result.assetSubItemInstance.subItems) {
43                if(subItem.maintenanceActions)
44                    fail(code:"maintenanceActions.still.associated.subItem")
45            }
[347]46
[360]47            if(result.error)
48                return result
49
50            if(result.assetSubItemInstance.maintenanceActions)
51                return fail(code:"maintenanceActions.still.associated")
52
53            if(result.assetSubItemInstance.assets)
54                return fail(code:"assetSubItem.assets.associated")
55
56            // We have handled all the foreign keys so the delete should go forward.
57            // Can't flush here due to cascading from Section and Site.
58            // And without flush there is no point it trying to catch the dao.DataIntegrityViolationException
59            // since that will only happen after leaving the transaction.
60            result.assetSubItemInstance.delete()
[347]61            return result //Success.
62
[360]63        } // withTransaction
[347]64    }
65
[285]66    def create(params) {
67        def result = [:]
68        def fail = { Map m ->
69            result.error = [ code: m.code, args: ["AssetSubItem", params.id] ]
70            return result
71        }
72
73        result.assetSubItemInstance = new AssetSubItem()
74        result.assetSubItemInstance.properties = params
75
76        // pass in an asset to link to if provided.
77        if(params.asset?.id)
78            result.assetInstance = Asset.get(params.asset.id)
79
80        // success
81        return result
82    }
83
84    def save(params) {
[290]85        AssetSubItem.withTransaction { status ->
[285]86            def result = [:]
87
88            def fail = { Map m ->
89                status.setRollbackOnly()
90                if(result.assetSubItemInstance && m.field)
91                    result.assetSubItemInstance.errors.rejectValue(m.field, m.code)
92                result.error = [ code: m.code, args: ["AssetSubItem", params.id] ]
93                return result
94            }
95
96            result.assetSubItemInstance = new AssetSubItem(params)
97
[387]98            use(WordUtils) {
99                result.assetSubItemInstance.name = result.assetSubItemInstance.name.capitalize()
100                result.assetSubItemInstance.description = result.assetSubItemInstance.description.capitalize()
101            }
102
[285]103            if(params.asset?.id) {
104                result.assetInstance = Asset.get(params.asset.id)
105
106                if(!result.assetInstance)
107                    return fail(code:"assetSubItem.asset.not.found")
108            }
109
110            if(result.assetSubItemInstance.hasErrors() || !result.assetSubItemInstance.save(flush: true))
111                return fail(code:"default.create.failure")
112
113            if(result.assetInstance)
114                result.assetInstance.addToAssetSubItems(result.assetSubItemInstance)
115
116            // Success.
117            return result
118
119        } //end withTransaction
120    } // save()
121
122} // end class
Note: See TracBrowser for help on using the repository browser.