1 | class AssetService { |
---|
2 | |
---|
3 | boolean transactional = false |
---|
4 | |
---|
5 | /** |
---|
6 | * Determines and returns a possible list of asset sub items. |
---|
7 | * @returns A list of the possible assetSubItems. |
---|
8 | */ |
---|
9 | def possibleAssetSubItems() { |
---|
10 | def criteria = AssetSubItem.createCriteria() |
---|
11 | def possibleAssetSubItems = criteria.list() { |
---|
12 | isNull("parentItem") |
---|
13 | } |
---|
14 | } |
---|
15 | |
---|
16 | def create(params) { |
---|
17 | def result = [:] |
---|
18 | def fail = { Map m -> |
---|
19 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
20 | return result |
---|
21 | } |
---|
22 | |
---|
23 | result.assetInstance = new Asset() |
---|
24 | result.assetInstance.properties = params |
---|
25 | |
---|
26 | // success |
---|
27 | return result |
---|
28 | } |
---|
29 | |
---|
30 | def copy(params) { |
---|
31 | def result = [:] |
---|
32 | def fail = { Map m -> |
---|
33 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
34 | return result |
---|
35 | } |
---|
36 | |
---|
37 | result.assetToCopy = Asset.get(params.assetToCopy?.id) |
---|
38 | |
---|
39 | if(!result.assetToCopy) |
---|
40 | return fail(code: "asset.copy.asset.required") |
---|
41 | |
---|
42 | result.assetInstance = new Asset(name: result.assetToCopy.name, |
---|
43 | description: result.assetToCopy.description, |
---|
44 | section: result.assetToCopy.section) |
---|
45 | |
---|
46 | result.assetInstance.properties = params |
---|
47 | |
---|
48 | // success |
---|
49 | return result |
---|
50 | } |
---|
51 | |
---|
52 | def save(params) { |
---|
53 | def result = [:] |
---|
54 | def fail = { Map m -> |
---|
55 | if(result.assetInstance && m.field) |
---|
56 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
57 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
58 | return result |
---|
59 | } |
---|
60 | |
---|
61 | result.assetInstance = new Asset(params) |
---|
62 | |
---|
63 | if(result.assetInstance.hasErrors() || !result.assetInstance.save(flush: true)) |
---|
64 | return fail(code:"default.create.failure") |
---|
65 | |
---|
66 | // success |
---|
67 | return result |
---|
68 | } |
---|
69 | |
---|
70 | def saveCopy(params) { |
---|
71 | Asset.withTransaction { status -> |
---|
72 | def result = [:] |
---|
73 | |
---|
74 | def fail = { Map m -> |
---|
75 | status.setRollbackOnly() |
---|
76 | if(result.assetInstance && m.field) |
---|
77 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
78 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
79 | return result |
---|
80 | } |
---|
81 | |
---|
82 | result.assetToCopy = Asset.get(params.assetToCopy?.id) |
---|
83 | if(!result.assetToCopy) |
---|
84 | return fail(code:"default.not.found") |
---|
85 | |
---|
86 | if(!params.copyMethod) |
---|
87 | fail(code:"asset.copy.method.required") |
---|
88 | |
---|
89 | result.assetInstance = new Asset(params) |
---|
90 | |
---|
91 | if(result.assetInstance.hasErrors() || !result.assetInstance.save()) |
---|
92 | return fail(code:"default.create.failure") |
---|
93 | |
---|
94 | def assetSubItemInstance1 |
---|
95 | |
---|
96 | // Copy subItems from level 2 and bellow. |
---|
97 | def copyAssetSubItem = { assetSubItemToCopy, parentItem -> |
---|
98 | def assetSubItemInstance = new AssetSubItem(name: assetSubItemToCopy.name, |
---|
99 | description: assetSubItemToCopy.description, |
---|
100 | parentItem: parentItem) |
---|
101 | |
---|
102 | if(assetSubItemInstance.hasErrors() || !assetSubItemInstance.save()) |
---|
103 | return fail(field:"subItems", code:"default.create.failure") |
---|
104 | |
---|
105 | def i = 0 |
---|
106 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
107 | call(assetSubItem, assetSubItemInstance) |
---|
108 | // Protect against endless recurrsion. |
---|
109 | i++ |
---|
110 | if(i > 100) |
---|
111 | fail(code:"default.create.failure") |
---|
112 | // Stop if an error is flagged. |
---|
113 | if(result.error) |
---|
114 | break |
---|
115 | } |
---|
116 | } //copyAssetSubItem |
---|
117 | |
---|
118 | // Copy the 1st level of subItems. |
---|
119 | def copyAssetSubItem1 = { assetSubItemToCopy -> |
---|
120 | assetSubItemInstance1 = new AssetSubItem(name: assetSubItemToCopy.name, |
---|
121 | description: assetSubItemToCopy.description, |
---|
122 | asset: result.assetInstance) |
---|
123 | |
---|
124 | if(assetSubItemInstance1.hasErrors() || !assetSubItemInstance1.save()) |
---|
125 | return fail(field:"assetSubItems", code:"default.create.failure") |
---|
126 | |
---|
127 | result.assetInstance.addToAssetSubItems(assetSubItemInstance1) |
---|
128 | |
---|
129 | def i = 0 |
---|
130 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
131 | copyAssetSubItem(assetSubItem, assetSubItemInstance1) |
---|
132 | // Protect against endless recurrsion. |
---|
133 | i++ |
---|
134 | if(i > 100) |
---|
135 | fail(code:"default.create.failure") |
---|
136 | // Stop if an error is flagged. |
---|
137 | if(result.error) |
---|
138 | break |
---|
139 | } |
---|
140 | } //copyAssetSubItem1 |
---|
141 | |
---|
142 | def linkAssetSubItem = { assetSubItemToLink -> |
---|
143 | result.assetInstance.addToAssetSubItems(assetSubItemToLink) |
---|
144 | } |
---|
145 | |
---|
146 | def i = 0 |
---|
147 | for(assetSubItem in result.assetToCopy.assetSubItems) { |
---|
148 | |
---|
149 | if(params.copyMethod == "copy") |
---|
150 | copyAssetSubItem1(assetSubItem) |
---|
151 | else |
---|
152 | linkAssetSubItem(assetSubItem) |
---|
153 | // Protect against endless recurrsion. |
---|
154 | i++ |
---|
155 | if(i > 100) |
---|
156 | fail(code:"default.create.failure") |
---|
157 | // Stop if an error is flagged. |
---|
158 | if(result.error) |
---|
159 | break |
---|
160 | } |
---|
161 | |
---|
162 | // Success or not. |
---|
163 | return result |
---|
164 | |
---|
165 | } //end withTransaction |
---|
166 | } // end saveCopySrvce |
---|
167 | |
---|
168 | } // end class |
---|