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