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