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