1 | class AssetSubItemService { |
---|
2 | |
---|
3 | boolean transactional = false |
---|
4 | |
---|
5 | def delete(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 = AssetSubItem.get(params.id) |
---|
13 | |
---|
14 | if(!result.assetSubItemInstance) |
---|
15 | return fail(code:"default.not.found") |
---|
16 | |
---|
17 | if(result.assetSubItemInstance.assets) |
---|
18 | return fail(code:"assetSubItem.assets.associated") |
---|
19 | |
---|
20 | if(result.assetSubItemInstance.maintenanceActions) |
---|
21 | return fail(code:"maintenanceActions.still.associated") |
---|
22 | |
---|
23 | try { |
---|
24 | result.assetSubItemInstance.delete(flush:true) |
---|
25 | return result //Success. |
---|
26 | } |
---|
27 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
28 | return fail(code:"default.delete.failure") |
---|
29 | } |
---|
30 | |
---|
31 | } |
---|
32 | |
---|
33 | def create(params) { |
---|
34 | def result = [:] |
---|
35 | def fail = { Map m -> |
---|
36 | result.error = [ code: m.code, args: ["AssetSubItem", params.id] ] |
---|
37 | return result |
---|
38 | } |
---|
39 | |
---|
40 | result.assetSubItemInstance = new AssetSubItem() |
---|
41 | result.assetSubItemInstance.properties = params |
---|
42 | |
---|
43 | // pass in an asset to link to if provided. |
---|
44 | if(params.asset?.id) |
---|
45 | result.assetInstance = Asset.get(params.asset.id) |
---|
46 | |
---|
47 | // success |
---|
48 | return result |
---|
49 | } |
---|
50 | |
---|
51 | def save(params) { |
---|
52 | AssetSubItem.withTransaction { status -> |
---|
53 | def result = [:] |
---|
54 | |
---|
55 | def fail = { Map m -> |
---|
56 | status.setRollbackOnly() |
---|
57 | if(result.assetSubItemInstance && m.field) |
---|
58 | result.assetSubItemInstance.errors.rejectValue(m.field, m.code) |
---|
59 | result.error = [ code: m.code, args: ["AssetSubItem", params.id] ] |
---|
60 | return result |
---|
61 | } |
---|
62 | |
---|
63 | result.assetSubItemInstance = new AssetSubItem(params) |
---|
64 | |
---|
65 | if(params.asset?.id) { |
---|
66 | result.assetInstance = Asset.get(params.asset.id) |
---|
67 | |
---|
68 | if(!result.assetInstance) |
---|
69 | return fail(code:"assetSubItem.asset.not.found") |
---|
70 | } |
---|
71 | |
---|
72 | if(result.assetSubItemInstance.hasErrors() || !result.assetSubItemInstance.save(flush: true)) |
---|
73 | return fail(code:"default.create.failure") |
---|
74 | |
---|
75 | if(result.assetInstance) |
---|
76 | result.assetInstance.addToAssetSubItems(result.assetSubItemInstance) |
---|
77 | |
---|
78 | // Success. |
---|
79 | return result |
---|
80 | |
---|
81 | } //end withTransaction |
---|
82 | } // save() |
---|
83 | |
---|
84 | } // end class |
---|