1 | |
---|
2 | import org.apache.commons.lang.WordUtils |
---|
3 | |
---|
4 | class AssetService { |
---|
5 | |
---|
6 | boolean transactional = false |
---|
7 | |
---|
8 | def sessionFactory |
---|
9 | |
---|
10 | def assetSubItemService |
---|
11 | |
---|
12 | /** |
---|
13 | * Determines and returns a possible list of asset sub items. |
---|
14 | * @returns A list of the possible assetSubItems. |
---|
15 | */ |
---|
16 | def possibleAssetSubItems() { |
---|
17 | def criteria = AssetSubItem.createCriteria() |
---|
18 | def possibleAssetSubItems = criteria.list() { |
---|
19 | and { |
---|
20 | eq('isActive', true) |
---|
21 | isNull("parentItem") |
---|
22 | } |
---|
23 | } |
---|
24 | } |
---|
25 | |
---|
26 | def delete(params) { |
---|
27 | Asset.withTransaction { status -> |
---|
28 | def result = [:] |
---|
29 | |
---|
30 | def fail = { Map m -> |
---|
31 | status.setRollbackOnly() |
---|
32 | if(result.assetInstance && m.field) |
---|
33 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
34 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
35 | return result |
---|
36 | } |
---|
37 | |
---|
38 | result.assetInstance = Asset.get(params.id) |
---|
39 | |
---|
40 | if(!result.assetInstance) |
---|
41 | return fail(code:"default.not.found") |
---|
42 | |
---|
43 | if(result.assetInstance.maintenanceActions) |
---|
44 | return fail(code:"maintenanceActions.still.associated") |
---|
45 | |
---|
46 | // Remove orphan assetSubItems. |
---|
47 | def assetSubItems = new ArrayList(result.assetInstance.assetSubItems) // avoid ConcurrentModificationException. |
---|
48 | def r |
---|
49 | for(assetSubItem in assetSubItems) { |
---|
50 | result.assetInstance.removeFromAssetSubItems(assetSubItem) |
---|
51 | if(!assetSubItem.assets && !assetSubItem.parentItem) { |
---|
52 | r = assetSubItemService.delete(id: assetSubItem.id) |
---|
53 | if(r.error) { |
---|
54 | log.debug r.error |
---|
55 | fail(code:"asset.subItems.delete.failure") |
---|
56 | break |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | if(result.error) |
---|
62 | return result |
---|
63 | |
---|
64 | // Success. |
---|
65 | // We have handled all the foreign keys so the delete should go forward. |
---|
66 | // Can't flush here due to cascading from Section and Site. |
---|
67 | // And without flush there is no point it trying to catch the dao.DataIntegrityViolationException |
---|
68 | // since that will only happen after leaving the transaction. |
---|
69 | result.assetInstance.delete() |
---|
70 | return result |
---|
71 | |
---|
72 | } // end withTransaction |
---|
73 | } // end delete() |
---|
74 | |
---|
75 | def create(params) { |
---|
76 | def result = [:] |
---|
77 | def fail = { Map m -> |
---|
78 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
79 | return result |
---|
80 | } |
---|
81 | |
---|
82 | result.assetInstance = new Asset() |
---|
83 | result.assetInstance.properties = params |
---|
84 | |
---|
85 | // success |
---|
86 | return result |
---|
87 | } |
---|
88 | |
---|
89 | def copy(params) { |
---|
90 | def result = [:] |
---|
91 | def fail = { Map m -> |
---|
92 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
93 | return result |
---|
94 | } |
---|
95 | |
---|
96 | result.assetToCopy = Asset.get(params.assetToCopy?.id) |
---|
97 | |
---|
98 | if(!result.assetToCopy) |
---|
99 | return fail(code: "asset.copy.asset.required") |
---|
100 | |
---|
101 | result.assetInstance = new Asset(name: result.assetToCopy.name, |
---|
102 | description: result.assetToCopy.description, |
---|
103 | comment: result.assetToCopy.comment, |
---|
104 | section: result.assetToCopy.section) |
---|
105 | |
---|
106 | result.assetInstance.properties = params |
---|
107 | |
---|
108 | // success |
---|
109 | return result |
---|
110 | } |
---|
111 | |
---|
112 | def save(params) { |
---|
113 | def result = [:] |
---|
114 | def fail = { Map m -> |
---|
115 | if(result.assetInstance && m.field) |
---|
116 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
117 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
118 | return result |
---|
119 | } |
---|
120 | |
---|
121 | result.assetInstance = new Asset(params) |
---|
122 | |
---|
123 | use(WordUtils) { |
---|
124 | result.assetInstance.name = result.assetInstance.name.capitalize() |
---|
125 | result.assetInstance.description = result.assetInstance.description.capitalize() |
---|
126 | } |
---|
127 | |
---|
128 | if(result.assetInstance.hasErrors() || !result.assetInstance.save(flush: true)) |
---|
129 | return fail(code:"default.create.failure") |
---|
130 | |
---|
131 | // success |
---|
132 | return result |
---|
133 | } |
---|
134 | |
---|
135 | def saveCopy(params) { |
---|
136 | Asset.withTransaction { status -> |
---|
137 | def result = [:] |
---|
138 | |
---|
139 | def fail = { Map m -> |
---|
140 | status.setRollbackOnly() |
---|
141 | if(result.assetInstance && m.field) |
---|
142 | result.assetInstance.errors.rejectValue(m.field, m.code) |
---|
143 | result.error = [ code: m.code, args: ["Asset", params.id] ] |
---|
144 | return result |
---|
145 | } |
---|
146 | |
---|
147 | result.assetToCopy = Asset.get(params.assetToCopy?.id) |
---|
148 | if(!result.assetToCopy) |
---|
149 | return fail(code:"default.not.found") |
---|
150 | |
---|
151 | if(!params.copyMethod) |
---|
152 | fail(code:"asset.copy.method.required") |
---|
153 | |
---|
154 | result.assetInstance = new Asset(params) |
---|
155 | |
---|
156 | if(result.assetInstance.hasErrors() || !result.assetInstance.save()) |
---|
157 | return fail(code:"default.create.failure") |
---|
158 | |
---|
159 | def assetSubItemInstance1 |
---|
160 | |
---|
161 | // Copy subItems from level 2 and bellow. |
---|
162 | def copyAssetSubItem = { assetSubItemToCopy, parentItem -> |
---|
163 | def nextCount = AssetSubItem.count() + 1 |
---|
164 | def baseName = assetSubItemToCopy.name.split('\\(id:')[0] |
---|
165 | def name = baseName +'(id:'+nextCount+')' |
---|
166 | def assetSubItemInstance = new AssetSubItem(name: name, |
---|
167 | description: assetSubItemToCopy.description, |
---|
168 | parentItem: parentItem) |
---|
169 | |
---|
170 | if(assetSubItemInstance.hasErrors() || !assetSubItemInstance.save()) |
---|
171 | return fail(code:"asset.copy.subItem.create.failure") |
---|
172 | |
---|
173 | def i = 0 |
---|
174 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
175 | call(assetSubItem, assetSubItemInstance) |
---|
176 | // Protect against endless recurrsion. |
---|
177 | i++ |
---|
178 | if(i > 100) |
---|
179 | fail(code:"asset.copy.subItem.too.many.failure") |
---|
180 | // Stop if an error is flagged. |
---|
181 | if(result.error) |
---|
182 | break |
---|
183 | } |
---|
184 | } //copyAssetSubItem |
---|
185 | |
---|
186 | // Copy the 1st level of subItems. |
---|
187 | def copyAssetSubItem1 = { assetSubItemToCopy -> |
---|
188 | def nextCount = AssetSubItem.count() + 1 |
---|
189 | def baseName = assetSubItemToCopy.name.split('\\(id:')[0] |
---|
190 | def name = baseName +'(id:'+nextCount+')' |
---|
191 | assetSubItemInstance1 = new AssetSubItem(name: name, |
---|
192 | description: assetSubItemToCopy.description, |
---|
193 | asset: result.assetInstance) |
---|
194 | |
---|
195 | if(assetSubItemInstance1.hasErrors() || !assetSubItemInstance1.save()) |
---|
196 | return fail(code:"asset.copy.subItem.create.failure") |
---|
197 | |
---|
198 | result.assetInstance.addToAssetSubItems(assetSubItemInstance1) |
---|
199 | |
---|
200 | def i = 0 |
---|
201 | for(assetSubItem in assetSubItemToCopy.subItems) { |
---|
202 | copyAssetSubItem(assetSubItem, assetSubItemInstance1) |
---|
203 | // Protect against endless recurrsion. |
---|
204 | i++ |
---|
205 | if(i > 100) |
---|
206 | fail(code:"asset.copy.subItem.too.many.failure") |
---|
207 | // Stop if an error is flagged. |
---|
208 | if(result.error) |
---|
209 | break |
---|
210 | } |
---|
211 | } //copyAssetSubItem1 |
---|
212 | |
---|
213 | def linkAssetSubItem = { assetSubItemToLink -> |
---|
214 | result.assetInstance.addToAssetSubItems(assetSubItemToLink) |
---|
215 | } |
---|
216 | |
---|
217 | def i = 0 |
---|
218 | for(assetSubItem in result.assetToCopy.assetSubItems) { |
---|
219 | |
---|
220 | if(params.copyMethod == "copy") |
---|
221 | copyAssetSubItem1(assetSubItem) |
---|
222 | else |
---|
223 | linkAssetSubItem(assetSubItem) |
---|
224 | // Protect against endless recurrsion. |
---|
225 | i++ |
---|
226 | if(i > 100) |
---|
227 | fail(code:"asset.copy.subItem.too.many.failure") |
---|
228 | // Stop if an error is flagged. |
---|
229 | if(result.error) |
---|
230 | break |
---|
231 | } |
---|
232 | |
---|
233 | // Success or not. |
---|
234 | return result |
---|
235 | |
---|
236 | } // end withTransaction |
---|
237 | } // end saveCopySrvce |
---|
238 | |
---|
239 | /** |
---|
240 | * Create recommended extended attributes for all assets. |
---|
241 | */ |
---|
242 | def createRecommendedExtendedAttributes() { |
---|
243 | def result = [:] |
---|
244 | |
---|
245 | def hibernateSession = sessionFactory.currentSession |
---|
246 | |
---|
247 | def assets = Asset.list() |
---|
248 | def locationDescription = ExtendedAttributeType.findByName("Location Description") |
---|
249 | def ecr = ExtendedAttributeType.findByName("ecr") |
---|
250 | def assetNumber = ExtendedAttributeType.findByName("Asset Number") |
---|
251 | def assetCondition = ExtendedAttributeType.findByName("Asset Condition") |
---|
252 | def maintenancePercentComplete = ExtendedAttributeType.findByName("Maintenance % Completion") |
---|
253 | def registrationRequired = ExtendedAttributeType.findByName("Registration Required") |
---|
254 | def registrationExpiryDate = ExtendedAttributeType.findByName("Registration Expiry Date") |
---|
255 | def regulatoryRequirement = ExtendedAttributeType.findByName("Regulatory Requirement") |
---|
256 | def riskLevel = ExtendedAttributeType.findByName("Risk Level") |
---|
257 | def safeWorkProcedure = ExtendedAttributeType.findByName("Safe Work Procedure") |
---|
258 | |
---|
259 | for(asset in assets) { |
---|
260 | |
---|
261 | def attributeTypes = asset.assetExtendedAttributes.collect {it.extendedAttributeType} |
---|
262 | |
---|
263 | //AssetExtendedAttribute |
---|
264 | def assetExtendedAttributeInstance |
---|
265 | |
---|
266 | if(!attributeTypes.contains(locationDescription)) { |
---|
267 | //AssetExtendedAttribute #1 |
---|
268 | assetExtendedAttributeInstance = new AssetExtendedAttribute(value: "Not Specified", |
---|
269 | asset: asset, |
---|
270 | extendedAttributeType: locationDescription) |
---|
271 | assetExtendedAttributeInstance.save() |
---|
272 | } |
---|
273 | |
---|
274 | if(!attributeTypes.contains(ecr)) { |
---|
275 | //AssetExtendedAttribute #2 |
---|
276 | assetExtendedAttributeInstance = new AssetExtendedAttribute(value: "Not Specified", |
---|
277 | asset: asset, |
---|
278 | extendedAttributeType: ecr) |
---|
279 | assetExtendedAttributeInstance.save() |
---|
280 | } |
---|
281 | |
---|
282 | if(!attributeTypes.contains(assetNumber)) { |
---|
283 | //AssetExtendedAttribute #3 |
---|
284 | assetExtendedAttributeInstance = new AssetExtendedAttribute(value: "Not Specified", |
---|
285 | asset: asset, |
---|
286 | extendedAttributeType: assetNumber) |
---|
287 | assetExtendedAttributeInstance.save() |
---|
288 | } |
---|
289 | |
---|
290 | if(!attributeTypes.contains(assetCondition)) { |
---|
291 | //AssetExtendedAttribute #4 |
---|
292 | assetExtendedAttributeInstance = new AssetExtendedAttribute(value: "Not Specified", |
---|
293 | asset: asset, |
---|
294 | extendedAttributeType: assetCondition) |
---|
295 | assetExtendedAttributeInstance.save() |
---|
296 | } |
---|
297 | |
---|
298 | if(!attributeTypes.contains(maintenancePercentComplete)) { |
---|
299 | //AssetExtendedAttribute #5 |
---|
300 | assetExtendedAttributeInstance = new AssetExtendedAttribute(value: "TBA", |
---|
301 | asset: asset, |
---|
302 | extendedAttributeType: maintenancePercentComplete) |
---|
303 | assetExtendedAttributeInstance.save() |
---|
304 | } |
---|
305 | |
---|
306 | if(!attributeTypes.contains(registrationRequired)) { |
---|
307 | //AssetExtendedAttribute #6 |
---|
308 | assetExtendedAttributeInstance = new AssetExtendedAttribute(value: "Not Specified", |
---|
309 | asset: asset, |
---|
310 | extendedAttributeType: registrationRequired) |
---|
311 | assetExtendedAttributeInstance.save() |
---|
312 | } |
---|
313 | |
---|
314 | if(!attributeTypes.contains(registrationExpiryDate)) { |
---|
315 | //AssetExtendedAttribute #7 |
---|
316 | assetExtendedAttributeInstance = new AssetExtendedAttribute(value: "Not Specified", |
---|
317 | asset: asset, |
---|
318 | extendedAttributeType:registrationExpiryDate) |
---|
319 | assetExtendedAttributeInstance.save() |
---|
320 | } |
---|
321 | |
---|
322 | if(!attributeTypes.contains(regulatoryRequirement)) { |
---|
323 | //AssetExtendedAttribute #8 |
---|
324 | assetExtendedAttributeInstance = new AssetExtendedAttribute(value: "Not Specified", |
---|
325 | asset: asset, |
---|
326 | extendedAttributeType: regulatoryRequirement) |
---|
327 | assetExtendedAttributeInstance.save() |
---|
328 | } |
---|
329 | |
---|
330 | if(!attributeTypes.contains(riskLevel)) { |
---|
331 | //AssetExtendedAttribute #9 |
---|
332 | assetExtendedAttributeInstance = new AssetExtendedAttribute(value: "Not Specified", |
---|
333 | asset: asset, |
---|
334 | extendedAttributeType: riskLevel) |
---|
335 | assetExtendedAttributeInstance.save() |
---|
336 | } |
---|
337 | |
---|
338 | if(!attributeTypes.contains(safeWorkProcedure)) { |
---|
339 | //AssetExtendedAttribute #10 |
---|
340 | assetExtendedAttributeInstance = new AssetExtendedAttribute(value: "Not Specified", |
---|
341 | asset: asset, |
---|
342 | extendedAttributeType: safeWorkProcedure) |
---|
343 | assetExtendedAttributeInstance.save() |
---|
344 | } |
---|
345 | |
---|
346 | hibernateSession.flush() |
---|
347 | |
---|
348 | } // for |
---|
349 | |
---|
350 | // Success. |
---|
351 | return result |
---|
352 | |
---|
353 | } // createRecommendedExtendedAttributes() |
---|
354 | |
---|
355 | } // end class |
---|