1 | class AssetService { |
---|
2 | |
---|
3 | boolean transactional = false |
---|
4 | |
---|
5 | def assetSubItemService |
---|
6 | |
---|
7 | /** |
---|
8 | * Determines and returns a possible list of asset sub items. |
---|
9 | * @returns A list of the possible assetSubItems. |
---|
10 | */ |
---|
11 | def possibleAssetSubItems() { |
---|
12 | def criteria = AssetSubItem.createCriteria() |
---|
13 | def possibleAssetSubItems = criteria.list() { |
---|
14 | and { |
---|
15 | eq('isActive', true) |
---|
16 | isNull("parentItem") |
---|
17 | } |
---|
18 | } |
---|
19 | } |
---|
20 | |
---|
21 | def delete(params) { |
---|
22 | Asset.withTransaction { status -> |
---|
23 | def result = [:] |
---|
24 | |
---|
25 | def fail = { Map m -> |
---|
26 | status.setRollbackOnly() |
---|
27 | if(result.assetInstance && m.field) |
---|
28 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
29 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
30 | return result |
---|
31 | } |
---|
32 | |
---|
33 | result.assetInstance = Asset.get(params.id) |
---|
34 | |
---|
35 | if(!result.assetInstance) |
---|
36 | return fail(code:"default.not.found") |
---|
37 | |
---|
38 | if(result.assetInstance.maintenanceActions) |
---|
39 | return fail(code:"maintenanceActions.still.associated") |
---|
40 | |
---|
41 | // Remove orphan assetSubItems. |
---|
42 | def assetSubItems = new ArrayList(result.assetInstance.assetSubItems) // avoid ConcurrentModificationException. |
---|
43 | def r |
---|
44 | for(assetSubItem in assetSubItems) { |
---|
45 | result.assetInstance.removeFromAssetSubItems(assetSubItem) |
---|
46 | if(!assetSubItem.assets && !assetSubItem.parentItem) { |
---|
47 | r = assetSubItemService.delete(id: assetSubItem.id) |
---|
48 | if(r.error) { |
---|
49 | log.debug r.error |
---|
50 | fail(code:"asset.subItems.delete.failure") |
---|
51 | break |
---|
52 | } |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | if(result.error) |
---|
57 | return result |
---|
58 | |
---|
59 | // Success. |
---|
60 | // We have handled all the foreign keys so the delete should go forward. |
---|
61 | // Can't flush here due to cascading from Section and Site. |
---|
62 | // And without flush there is no point it trying to catch the dao.DataIntegrityViolationException |
---|
63 | // since that will only happen after leaving the transaction. |
---|
64 | result.assetInstance.delete() |
---|
65 | return result |
---|
66 | |
---|
67 | } // end withTransaction |
---|
68 | } // end delete() |
---|
69 | |
---|
70 | def create(params) { |
---|
71 | def result = [:] |
---|
72 | def fail = { Map m -> |
---|
73 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
74 | return result |
---|
75 | } |
---|
76 | |
---|
77 | result.assetInstance = new Asset() |
---|
78 | result.assetInstance.properties = params |
---|
79 | |
---|
80 | // success |
---|
81 | return result |
---|
82 | } |
---|
83 | |
---|
84 | def copy(params) { |
---|
85 | def result = [:] |
---|
86 | def fail = { Map m -> |
---|
87 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
88 | return result |
---|
89 | } |
---|
90 | |
---|
91 | result.assetToCopy = Asset.get(params.assetToCopy?.id) |
---|
92 | |
---|
93 | if(!result.assetToCopy) |
---|
94 | return fail(code: "asset.copy.asset.required") |
---|
95 | |
---|
96 | result.assetInstance = new Asset(name: result.assetToCopy.name, |
---|
97 | description: result.assetToCopy.description, |
---|
98 | comment: result.assetToCopy.comment, |
---|
99 | section: result.assetToCopy.section) |
---|
100 | |
---|
101 | result.assetInstance.properties = params |
---|
102 | |
---|
103 | // success |
---|
104 | return result |
---|
105 | } |
---|
106 | |
---|
107 | def save(params) { |
---|
108 | def result = [:] |
---|
109 | def fail = { Map m -> |
---|
110 | if(result.assetInstance && m.field) |
---|
111 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
112 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
113 | return result |
---|
114 | } |
---|
115 | |
---|
116 | result.assetInstance = new Asset(params) |
---|
117 | |
---|
118 | if(result.assetInstance.hasErrors() || !result.assetInstance.save(flush: true)) |
---|
119 | return fail(code:"default.create.failure") |
---|
120 | |
---|
121 | // success |
---|
122 | return result |
---|
123 | } |
---|
124 | |
---|
125 | def saveCopy(params) { |
---|
126 | Asset.withTransaction { status -> |
---|
127 | def result = [:] |
---|
128 | |
---|
129 | def fail = { Map m -> |
---|
130 | status.setRollbackOnly() |
---|
131 | if(result.assetInstance && m.field) |
---|
132 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
133 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
134 | return result |
---|
135 | } |
---|
136 | |
---|
137 | result.assetToCopy = Asset.get(params.assetToCopy?.id) |
---|
138 | if(!result.assetToCopy) |
---|
139 | return fail(code:"default.not.found") |
---|
140 | |
---|
141 | if(!params.copyMethod) |
---|
142 | fail(code:"asset.copy.method.required") |
---|
143 | |
---|
144 | result.assetInstance = new Asset(params) |
---|
145 | |
---|
146 | if(result.assetInstance.hasErrors() || !result.assetInstance.save()) |
---|
147 | return fail(code:"default.create.failure") |
---|
148 | |
---|
149 | def assetSubItemInstance1 |
---|
150 | |
---|
151 | // Copy subItems from level 2 and bellow. |
---|
152 | def copyAssetSubItem = { assetSubItemToCopy, parentItem -> |
---|
153 | def assetSubItemInstance = new AssetSubItem(name: assetSubItemToCopy.name, |
---|
154 | description: assetSubItemToCopy.description, |
---|
155 | parentItem: parentItem) |
---|
156 | |
---|
157 | if(assetSubItemInstance.hasErrors() || !assetSubItemInstance.save()) |
---|
158 | return fail(field:"subItems", code:"default.create.failure") |
---|
159 | |
---|
160 | def i = 0 |
---|
161 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
162 | call(assetSubItem, assetSubItemInstance) |
---|
163 | // Protect against endless recurrsion. |
---|
164 | i++ |
---|
165 | if(i > 100) |
---|
166 | fail(code:"default.create.failure") |
---|
167 | // Stop if an error is flagged. |
---|
168 | if(result.error) |
---|
169 | break |
---|
170 | } |
---|
171 | } //copyAssetSubItem |
---|
172 | |
---|
173 | // Copy the 1st level of subItems. |
---|
174 | def copyAssetSubItem1 = { assetSubItemToCopy -> |
---|
175 | assetSubItemInstance1 = new AssetSubItem(name: assetSubItemToCopy.name, |
---|
176 | description: assetSubItemToCopy.description, |
---|
177 | asset: result.assetInstance) |
---|
178 | |
---|
179 | if(assetSubItemInstance1.hasErrors() || !assetSubItemInstance1.save()) |
---|
180 | return fail(field:"assetSubItems", code:"default.create.failure") |
---|
181 | |
---|
182 | result.assetInstance.addToAssetSubItems(assetSubItemInstance1) |
---|
183 | |
---|
184 | def i = 0 |
---|
185 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
186 | copyAssetSubItem(assetSubItem, assetSubItemInstance1) |
---|
187 | // Protect against endless recurrsion. |
---|
188 | i++ |
---|
189 | if(i > 100) |
---|
190 | fail(code:"default.create.failure") |
---|
191 | // Stop if an error is flagged. |
---|
192 | if(result.error) |
---|
193 | break |
---|
194 | } |
---|
195 | } //copyAssetSubItem1 |
---|
196 | |
---|
197 | def linkAssetSubItem = { assetSubItemToLink -> |
---|
198 | result.assetInstance.addToAssetSubItems(assetSubItemToLink) |
---|
199 | } |
---|
200 | |
---|
201 | def i = 0 |
---|
202 | for(assetSubItem in result.assetToCopy.assetSubItems) { |
---|
203 | |
---|
204 | if(params.copyMethod == "copy") |
---|
205 | copyAssetSubItem1(assetSubItem) |
---|
206 | else |
---|
207 | linkAssetSubItem(assetSubItem) |
---|
208 | // Protect against endless recurrsion. |
---|
209 | i++ |
---|
210 | if(i > 100) |
---|
211 | fail(code:"default.create.failure") |
---|
212 | // Stop if an error is flagged. |
---|
213 | if(result.error) |
---|
214 | break |
---|
215 | } |
---|
216 | |
---|
217 | // Success or not. |
---|
218 | return result |
---|
219 | |
---|
220 | } // end withTransaction |
---|
221 | } // end saveCopySrvce |
---|
222 | |
---|
223 | } // end class |
---|