[271] | 1 | import au.com.bytecode.opencsv.CSVWriter |
---|
| 2 | import au.com.bytecode.opencsv.CSVReader |
---|
[315] | 3 | import org.apache.commons.lang.WordUtils |
---|
[271] | 4 | |
---|
| 5 | /** |
---|
| 6 | * Provides some csv import/export methods. |
---|
| 7 | * Requires the opencsv jar to be available which is included in the grails-export plugin. |
---|
| 8 | */ |
---|
[414] | 9 | class AssetCsvService { |
---|
[271] | 10 | |
---|
| 11 | boolean transactional = false |
---|
| 12 | |
---|
[421] | 13 | def g = new org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib() |
---|
| 14 | |
---|
[271] | 15 | /** |
---|
[287] | 16 | * Import an asset tree creating items as required. |
---|
| 17 | * @param request The http request to run getFile against. |
---|
| 18 | * Get file should return a csv format file containing the asset tree as per template. |
---|
[271] | 19 | */ |
---|
| 20 | def importAssetTree(request) { |
---|
[290] | 21 | Asset.withTransaction { status -> |
---|
| 22 | def result = [:] |
---|
[271] | 23 | |
---|
[300] | 24 | def kByteMultiplier = 1000 |
---|
| 25 | def fileMaxSize = 500 * kByteMultiplier |
---|
[421] | 26 | def logFileLink = g.link(controller: "appCore", action: "appLog") {"log"} |
---|
[271] | 27 | |
---|
[290] | 28 | def multiPartFile = request.getFile('file') |
---|
[287] | 29 | |
---|
[290] | 30 | InputStreamReader sr = new InputStreamReader(multiPartFile.inputStream) |
---|
| 31 | CSVReader reader = new CSVReader(sr) |
---|
[287] | 32 | |
---|
[290] | 33 | def fail = { Map m -> |
---|
| 34 | status.setRollbackOnly() |
---|
| 35 | reader.close() |
---|
| 36 | result.error = [ code: m.code, args: m.args ] |
---|
| 37 | return result |
---|
| 38 | } |
---|
[271] | 39 | |
---|
[290] | 40 | if(!multiPartFile || multiPartFile.isEmpty()) |
---|
[414] | 41 | return fail(code: "default.file.not.supplied") |
---|
[271] | 42 | |
---|
[290] | 43 | if (multiPartFile.getSize() > fileMaxSize) |
---|
[414] | 44 | return fail(code: "default.file.over.max.size", args: [fileMaxSize/kByteMultiplier, "kB"]) |
---|
[271] | 45 | |
---|
[300] | 46 | def columnIndex = 0 |
---|
[340] | 47 | def nameColumnIndex = 0 |
---|
[300] | 48 | def numberOfColumns = 0 |
---|
| 49 | def maxNumberOfColumns = 20 |
---|
| 50 | |
---|
| 51 | // Get first line. |
---|
[290] | 52 | def line = reader.readNext() |
---|
| 53 | def lineNumber = 1 |
---|
[271] | 54 | |
---|
[300] | 55 | // Check for header line 1. |
---|
| 56 | if(line != templateHeaderLine1) { |
---|
| 57 | log.error "Failed to find header line 1. " |
---|
| 58 | log.error "Required: " + templateHeaderLine1.toString() |
---|
| 59 | log.error "Supplied: " + line.toString() |
---|
[414] | 60 | return fail(code: "default.file.no.header") |
---|
[300] | 61 | } |
---|
[287] | 62 | |
---|
[300] | 63 | // Get second line. |
---|
| 64 | line = reader.readNext() |
---|
| 65 | lineNumber ++ |
---|
| 66 | |
---|
| 67 | // Check for header line 2. |
---|
| 68 | if(line != templateHeaderLine2) { |
---|
| 69 | log.error "Failed to find header line 2. " |
---|
| 70 | log.error "Required: " + templateHeaderLine2.toString() |
---|
| 71 | log.error "Supplied: " + line.toString() |
---|
[414] | 72 | return fail(code: "default.file.no.header") |
---|
[300] | 73 | } |
---|
[271] | 74 | |
---|
[290] | 75 | log.info "Import checks passed, start processing asset file." |
---|
[287] | 76 | |
---|
[290] | 77 | // Prepare the first body line. |
---|
| 78 | line = reader.readNext() |
---|
| 79 | lineNumber ++ |
---|
[271] | 80 | |
---|
[290] | 81 | def siteInstance |
---|
[300] | 82 | def departmentInstance |
---|
[290] | 83 | def sectionInstance |
---|
| 84 | def assetInstance |
---|
[300] | 85 | def assetSubItemInstance |
---|
| 86 | def parentItem |
---|
[271] | 87 | |
---|
[301] | 88 | def column = [:] |
---|
| 89 | |
---|
| 90 | def nextLine = { |
---|
| 91 | line = reader.readNext() |
---|
| 92 | lineNumber ++ |
---|
| 93 | log.info "Processing line: " + lineNumber |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | def nextColumn = { |
---|
[340] | 97 | nameColumnIndex = columnIndex |
---|
| 98 | |
---|
| 99 | if( (columnIndex+1) < numberOfColumns ) { |
---|
| 100 | // Capitalise and assign the name and description columns. |
---|
| 101 | use(WordUtils) { |
---|
| 102 | column.name = line[columnIndex].trim().capitalize() |
---|
| 103 | column.description = line[++columnIndex].trim().capitalize() |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | else if( columnIndex < numberOfColumns ) { |
---|
| 107 | // Capitalise and assign the name column with a blank description. |
---|
| 108 | use(WordUtils) { |
---|
| 109 | column.name = line[columnIndex].trim().capitalize() |
---|
| 110 | column.description = '' |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | else { |
---|
[301] | 114 | log.info "No more columns on line: " + lineNumber |
---|
| 115 | return false |
---|
| 116 | } |
---|
[340] | 117 | |
---|
| 118 | if(!column.name) { |
---|
| 119 | log.info "No name at " + "line: " + lineNumber + " col: " + nameColumnIndex |
---|
[301] | 120 | return false |
---|
| 121 | } |
---|
[340] | 122 | |
---|
[301] | 123 | columnIndex++ |
---|
| 124 | // Success. |
---|
| 125 | return column |
---|
| 126 | } |
---|
| 127 | |
---|
[315] | 128 | // Primary loop. |
---|
[290] | 129 | while(line) { |
---|
[301] | 130 | numberOfColumns = Math.min( line.size(), maxNumberOfColumns ) |
---|
[300] | 131 | columnIndex = 0 |
---|
[271] | 132 | |
---|
[301] | 133 | if(!nextColumn()) { |
---|
| 134 | nextLine() |
---|
[300] | 135 | continue |
---|
[290] | 136 | } |
---|
[271] | 137 | |
---|
[302] | 138 | // Ignore comment lines. |
---|
[315] | 139 | if(line.toString().toLowerCase().contains("comment")) { |
---|
[302] | 140 | log.info "Comment line found." |
---|
| 141 | nextLine() |
---|
| 142 | continue |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | // Ignore example lines. |
---|
[315] | 146 | if(line.toString().toLowerCase().contains("example")) { |
---|
[302] | 147 | log.info "Example line found." |
---|
| 148 | nextLine() |
---|
| 149 | continue |
---|
| 150 | } |
---|
| 151 | |
---|
[315] | 152 | // Site. |
---|
[301] | 153 | siteInstance = Site.findByName(column.name) |
---|
| 154 | if(!siteInstance) { |
---|
| 155 | log.info "Creating site: " + column.name |
---|
| 156 | siteInstance = new Site(name: column.name, |
---|
| 157 | description: column.description) |
---|
| 158 | if(!siteInstance.save()) { |
---|
| 159 | log.error "Failed to create site on line: " + column.name + "(" + lineNumber + ")" |
---|
[421] | 160 | return fail(code: "asset.tree.import.failure", args: [lineNumber, logFileLink]) |
---|
[300] | 161 | } |
---|
[301] | 162 | } |
---|
| 163 | else log.info "Existing site: " + siteInstance |
---|
[271] | 164 | |
---|
[301] | 165 | if(!nextColumn()) { |
---|
| 166 | nextLine() |
---|
| 167 | continue |
---|
| 168 | } |
---|
| 169 | |
---|
[315] | 170 | // Department. |
---|
[301] | 171 | departmentInstance = Department.findByName(column.name) |
---|
| 172 | if(!departmentInstance) { |
---|
| 173 | log.info "Creating department: " + column.name |
---|
| 174 | departmentInstance = new Department(name: column.name, |
---|
| 175 | description: column.description, |
---|
| 176 | site: siteInstance) |
---|
| 177 | if(!departmentInstance.save()) { |
---|
| 178 | log.error "Failed to create department on line: " + column.name + "(" + lineNumber + ")" |
---|
[421] | 179 | return fail(code: "asset.tree.import.failure", args: [lineNumber, logFileLink]) |
---|
[300] | 180 | } |
---|
[301] | 181 | } |
---|
| 182 | else log.info "Existing department: " + departmentInstance |
---|
[300] | 183 | |
---|
[301] | 184 | if(!nextColumn()) { |
---|
| 185 | nextLine() |
---|
| 186 | continue |
---|
| 187 | } |
---|
[290] | 188 | |
---|
[315] | 189 | // Section. |
---|
[301] | 190 | sectionInstance = Section.findByName(column.name) |
---|
| 191 | if(!sectionInstance) { |
---|
| 192 | log.info "Creating section: " + column.name |
---|
| 193 | sectionInstance = new Section(name: column.name, |
---|
| 194 | description: column.description, |
---|
| 195 | site: siteInstance, |
---|
| 196 | department: departmentInstance) |
---|
| 197 | if(!sectionInstance.save()) { |
---|
| 198 | log.error "Failed to create section on line: " + column.name + "(" + lineNumber + ")" |
---|
[421] | 199 | return fail(code: "asset.tree.import.failure", args: [lineNumber, logFileLink]) |
---|
[301] | 200 | } |
---|
| 201 | } |
---|
| 202 | else log.info "Existing section: " + sectionInstance |
---|
[300] | 203 | |
---|
[301] | 204 | if(!nextColumn()) { |
---|
| 205 | nextLine() |
---|
| 206 | continue |
---|
| 207 | } |
---|
[300] | 208 | |
---|
[315] | 209 | // Asset. |
---|
[301] | 210 | assetInstance = Asset.findByName(column.name) |
---|
| 211 | if(!assetInstance) { |
---|
| 212 | log.info "Creating asset: " + column.name |
---|
| 213 | assetInstance = new Asset(name: column.name, |
---|
| 214 | description: column.description, |
---|
| 215 | section: sectionInstance) |
---|
| 216 | if(!assetInstance.save()) { |
---|
| 217 | log.error "Failed to create asset on line: " + column.name + "(" + lineNumber + ")" |
---|
[421] | 218 | return fail(code: "asset.tree.import.failure", args: [lineNumber, logFileLink]) |
---|
[301] | 219 | } |
---|
| 220 | } |
---|
| 221 | else log.info "Existing asset: " + assetInstance |
---|
[300] | 222 | |
---|
[301] | 223 | if(!nextColumn()) { |
---|
| 224 | nextLine() |
---|
| 225 | continue |
---|
| 226 | } |
---|
[300] | 227 | |
---|
[315] | 228 | // AssetSubItem Level 1. |
---|
[301] | 229 | assetSubItemInstance = AssetSubItem.findByName(column.name) |
---|
| 230 | if(!assetSubItemInstance) { |
---|
| 231 | log.info "Creating asset sub item: " + column.name |
---|
| 232 | assetSubItemInstance = new AssetSubItem(name: column.name, |
---|
| 233 | description: column.description) |
---|
| 234 | if(!assetInstance.save()) { |
---|
| 235 | log.error "Failed to create assetSubItem on line: " + column.name + "(" + lineNumber + ")" |
---|
[421] | 236 | return fail(code: "asset.tree.import.failure", args: [lineNumber, logFileLink]) |
---|
[301] | 237 | } |
---|
| 238 | } |
---|
| 239 | else log.info "Existing asset sub item: " + assetSubItemInstance |
---|
[300] | 240 | |
---|
[301] | 241 | assetInstance.addToAssetSubItems(assetSubItemInstance) |
---|
[300] | 242 | |
---|
[315] | 243 | // Remaining AssetSubItems. |
---|
[301] | 244 | while( nextColumn() ) { |
---|
[300] | 245 | |
---|
[301] | 246 | parentItem = assetSubItemInstance |
---|
| 247 | assetSubItemInstance = AssetSubItem.findByName(column.name) |
---|
| 248 | if(!assetSubItemInstance) { |
---|
| 249 | log.info "Creating asset sub item: " + column.name |
---|
| 250 | assetSubItemInstance = new AssetSubItem(name: column.name, |
---|
| 251 | description: column.description, |
---|
| 252 | parentItem: parentItem) |
---|
| 253 | if(!assetSubItemInstance.save()) { |
---|
| 254 | log.error "Failed to create assetSubItem on line: " + column.name + "(" + lineNumber + ")" |
---|
[421] | 255 | return fail(code: "asset.tree.import.failure", args: [lineNumber, logFileLink]) |
---|
[301] | 256 | } |
---|
| 257 | } |
---|
[315] | 258 | else log.info "Existing asset sub item: " + assetSubItemInstance |
---|
[300] | 259 | |
---|
[301] | 260 | } // while( nextColumn() ) |
---|
[300] | 261 | |
---|
[301] | 262 | nextLine() |
---|
[290] | 263 | } //while(line) |
---|
| 264 | |
---|
| 265 | // Success. |
---|
[302] | 266 | log.info "End of file." |
---|
[290] | 267 | reader.close() |
---|
| 268 | return result |
---|
| 269 | |
---|
| 270 | } //end withTransaction |
---|
[271] | 271 | } // end importAssetTree() |
---|
| 272 | |
---|
| 273 | /** |
---|
| 274 | * Build an asset tree template csv file. |
---|
| 275 | * This template can then be populated for import. |
---|
| 276 | * @returns The template as a String in csv format. |
---|
| 277 | */ |
---|
| 278 | def buildAssetTreeTemplate() { |
---|
| 279 | |
---|
| 280 | StringWriter sw = new StringWriter() |
---|
| 281 | CSVWriter writer = new CSVWriter(sw) |
---|
| 282 | |
---|
[302] | 283 | writeTemplateLines(writer) |
---|
| 284 | |
---|
| 285 | writer.close() |
---|
| 286 | return sw.toString() |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | private writeTemplateLines(writer) { |
---|
[300] | 290 | writer.writeNext(templateHeaderLine1 as String[]) |
---|
| 291 | writer.writeNext(templateHeaderLine2 as String[]) |
---|
| 292 | writer.writeNext() |
---|
[302] | 293 | writer.writeNext(["Example: Site1", "", "Department 1", "", "Section 1", "", "Asset 1", ""] as String[]) |
---|
| 294 | writer.writeNext() |
---|
| 295 | writer.writeNext(["Example: Site1", "", "Department 1", "", "Section 1", "", "Asset 2", ""] as String[]) |
---|
| 296 | writer.writeNext() |
---|
[315] | 297 | writer.writeNext("Comment: The first two header lines are required.") |
---|
| 298 | writer.writeNext("Comment: Leave a blank line between assets.") |
---|
| 299 | writer.writeNext("Comment: Names are required, descriptions are optional.") |
---|
| 300 | writer.writeNext("Comment: Identical and existing names will be considered as the same item.") |
---|
| 301 | writer.writeNext("Comment: An asset may share the first level of asset sub items with other assets.") |
---|
| 302 | writer.writeNext("Comment: Lower levels of asset sub items are only parented once but may have many children.") |
---|
| 303 | writer.writeNext("Comment: Lines containing 'comment' will be ignored.") |
---|
| 304 | writer.writeNext("Comment: Lines containing 'example' will be ignored.") |
---|
| 305 | writer.writeNext("Comment: This file must be saved as a CSV file before import.") |
---|
[302] | 306 | writer.writeNext() |
---|
| 307 | } |
---|
[287] | 308 | |
---|
[302] | 309 | /** |
---|
| 310 | * Build an asset tree test file. |
---|
| 311 | * This test file can be imported to test the import and template methods. |
---|
| 312 | * @returns The test file as a String in csv format. |
---|
| 313 | */ |
---|
| 314 | def buildAssetTreeTest() { |
---|
| 315 | |
---|
| 316 | StringWriter sw = new StringWriter() |
---|
| 317 | CSVWriter writer = new CSVWriter(sw) |
---|
| 318 | |
---|
| 319 | writeTemplateLines(writer) |
---|
| 320 | |
---|
| 321 | writer.writeNext(["Lake Press", "Lake Press Site", |
---|
| 322 | "Print Department", "The printing department", |
---|
| 323 | "Press Section", "Contains all printing units", |
---|
| 324 | "Print Unit 1", "Printing Unit Number 1", |
---|
| 325 | "Print Unit", "Print Unit (Common to all units)", |
---|
| 326 | "Print Couple", "Includes blanket cylinder", |
---|
| 327 | "Motor", "Main Drive Motor", |
---|
| 328 | "NDS Bearing", "Non Drive Side Main Bearing" |
---|
| 329 | ] as String[]) |
---|
| 330 | |
---|
| 331 | writer.writeNext(["Lake Press", "Lake Press Site", |
---|
| 332 | "Print Department", "The printing department", |
---|
| 333 | "Press Section", "Contains all printing units", |
---|
| 334 | "Print Unit 1", "Printing Unit Number 1", |
---|
| 335 | "Print Unit", "Print Unit (Common to all units)", |
---|
| 336 | "Print Couple", "Includes blanket cylinder", |
---|
| 337 | "Motor", "Main Drive Motor", |
---|
| 338 | "DS Bearing", "Drive Side Main Bearing" |
---|
| 339 | ] as String[]) |
---|
| 340 | |
---|
[271] | 341 | writer.close() |
---|
| 342 | return sw.toString() |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | /** |
---|
| 346 | * Build complete asset trees for export. |
---|
| 347 | * @param assetList The list of assets to build and export trees for. |
---|
| 348 | * @returns The tree as a String in csv format. |
---|
| 349 | */ |
---|
| 350 | def buildAssetTree(List assetList) { |
---|
| 351 | |
---|
| 352 | StringWriter sw = new StringWriter() |
---|
| 353 | CSVWriter writer = new CSVWriter(sw) |
---|
| 354 | |
---|
[315] | 355 | writeTemplateLines(writer) |
---|
[271] | 356 | |
---|
| 357 | //Rows |
---|
| 358 | def row |
---|
| 359 | |
---|
| 360 | def writeAssetSubItem4 = { assetSubItem -> |
---|
[315] | 361 | row.add(assetSubItem.name) |
---|
| 362 | row.add(assetSubItem.description) |
---|
[271] | 363 | writer.writeNext(row as String[]) |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | def writeAssetSubItem3 = { assetSubItem -> |
---|
[315] | 367 | row.add(assetSubItem.name) |
---|
| 368 | row.add(assetSubItem.description) |
---|
[271] | 369 | |
---|
| 370 | if(assetSubItem.subItems.size() > 0) { |
---|
| 371 | assetSubItem.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }.each() { assetSubItem4 -> |
---|
| 372 | writeAssetSubItem4(assetSubItem4) |
---|
[327] | 373 | row.removeRange(row.size()-2, row.size()) |
---|
[271] | 374 | } |
---|
| 375 | } |
---|
| 376 | else { |
---|
| 377 | writer.writeNext(row as String[]) |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | def writeAssetSubItem2 = { assetSubItem -> |
---|
[315] | 383 | row.add(assetSubItem.name) |
---|
| 384 | row.add(assetSubItem.description) |
---|
[271] | 385 | |
---|
| 386 | if(assetSubItem.subItems.size() > 0) { |
---|
| 387 | assetSubItem.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }.each() { assetSubItem3 -> |
---|
| 388 | writeAssetSubItem3(assetSubItem3) |
---|
[327] | 389 | row.removeRange(row.size()-2, row.size()) |
---|
[271] | 390 | } |
---|
| 391 | } |
---|
| 392 | else { |
---|
| 393 | writer.writeNext(row as String[]) |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | def writeAssetSubItem1 = { assetSubItem -> |
---|
[315] | 399 | row.add(assetSubItem.name) |
---|
| 400 | row.add(assetSubItem.description) |
---|
[271] | 401 | |
---|
| 402 | if(assetSubItem.subItems.size() > 0) { |
---|
| 403 | assetSubItem.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }.each() { assetSubItem2 -> |
---|
| 404 | writeAssetSubItem2(assetSubItem2) |
---|
[327] | 405 | row.removeRange(row.size()-2, row.size()) |
---|
[271] | 406 | } |
---|
| 407 | } |
---|
| 408 | else { |
---|
| 409 | writer.writeNext(row as String[]) |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | assetList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }.each() { asset -> |
---|
| 415 | row = [] |
---|
| 416 | writer.writeNext(row as String[]) //blank row between assets. |
---|
[315] | 417 | row.add(asset.section.site.name) |
---|
| 418 | row.add(asset.section.site.description) |
---|
| 419 | row.add(asset.section.department.name) |
---|
| 420 | row.add(asset.section.department.description) |
---|
| 421 | row.add(asset.section.name) |
---|
| 422 | row.add(asset.section.description) |
---|
[271] | 423 | row.add(asset.name) |
---|
[315] | 424 | row.add(asset.description) |
---|
[271] | 425 | |
---|
| 426 | if(asset.assetSubItems.size() > 0) { |
---|
[409] | 427 | asset.assetSubItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }.each() { assetSubItem1 -> |
---|
[271] | 428 | writeAssetSubItem1(assetSubItem1) |
---|
[327] | 429 | row.removeRange(row.size()-2, row.size()) |
---|
[271] | 430 | } |
---|
| 431 | } |
---|
| 432 | else { |
---|
| 433 | writer.writeNext(row as String[]) |
---|
| 434 | } |
---|
| 435 | |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | writer.close() |
---|
| 439 | return sw.toString() |
---|
| 440 | } // end buildAssetTree |
---|
| 441 | |
---|
[300] | 442 | private getTemplateHeaderLine1() { |
---|
| 443 | ["Site", "", "Department", "", "Section", "","Asset", "", "Sub Asset", "", "Functional Assembly", "", "Sub Assembly Group", "", "SubItem", ""] |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | private getTemplateHeaderLine2() { |
---|
| 447 | (["Name", "Description"])*8 |
---|
| 448 | } |
---|
| 449 | |
---|
[271] | 450 | } // end class |
---|