Last change
on this file since 288 was
285,
checked in by gav, 15 years ago
|
Add AssetSubItemService and start moving methods to it.
|
File size:
1.7 KB
|
Rev | Line | |
---|
[285] | 1 | class AssetSubItemService { |
---|
| 2 | |
---|
| 3 | boolean transactional = false |
---|
| 4 | |
---|
| 5 | def create(params) { |
---|
| 6 | def result = [:] |
---|
| 7 | def fail = { Map m -> |
---|
| 8 | result.error = [ code: m.code, args: ["AssetSubItem", params.id] ] |
---|
| 9 | return result |
---|
| 10 | } |
---|
| 11 | |
---|
| 12 | result.assetSubItemInstance = new AssetSubItem() |
---|
| 13 | result.assetSubItemInstance.properties = params |
---|
| 14 | |
---|
| 15 | // pass in an asset to link to if provided. |
---|
| 16 | if(params.asset?.id) |
---|
| 17 | result.assetInstance = Asset.get(params.asset.id) |
---|
| 18 | |
---|
| 19 | // success |
---|
| 20 | return result |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | def save(params) { |
---|
| 24 | Asset.withTransaction { status -> |
---|
| 25 | def result = [:] |
---|
| 26 | |
---|
| 27 | def fail = { Map m -> |
---|
| 28 | status.setRollbackOnly() |
---|
| 29 | if(result.assetSubItemInstance && m.field) |
---|
| 30 | result.assetSubItemInstance.errors.rejectValue(m.field, m.code) |
---|
| 31 | result.error = [ code: m.code, args: ["AssetSubItem", params.id] ] |
---|
| 32 | return result |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | result.assetSubItemInstance = new AssetSubItem(params) |
---|
| 36 | |
---|
| 37 | if(params.asset?.id) { |
---|
| 38 | result.assetInstance = Asset.get(params.asset.id) |
---|
| 39 | |
---|
| 40 | if(!result.assetInstance) |
---|
| 41 | return fail(code:"assetSubItem.asset.not.found") |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | if(result.assetSubItemInstance.hasErrors() || !result.assetSubItemInstance.save(flush: true)) |
---|
| 45 | return fail(code:"default.create.failure") |
---|
| 46 | |
---|
| 47 | if(result.assetInstance) |
---|
| 48 | result.assetInstance.addToAssetSubItems(result.assetSubItemInstance) |
---|
| 49 | |
---|
| 50 | // Success. |
---|
| 51 | return result |
---|
| 52 | |
---|
| 53 | } //end withTransaction |
---|
| 54 | } // save() |
---|
| 55 | |
---|
| 56 | } // end class |
---|
Note: See
TracBrowser
for help on using the repository browser.