[271] | 1 | import au.com.bytecode.opencsv.CSVWriter |
---|
| 2 | import au.com.bytecode.opencsv.CSVReader |
---|
| 3 | |
---|
| 4 | /** |
---|
| 5 | * Provides some csv import/export methods. |
---|
| 6 | * Requires the opencsv jar to be available which is included in the grails-export plugin. |
---|
| 7 | */ |
---|
| 8 | class CsvService { |
---|
| 9 | |
---|
| 10 | boolean transactional = false |
---|
| 11 | |
---|
| 12 | /** |
---|
[287] | 13 | * Import an asset tree creating items as required. |
---|
| 14 | * @param request The http request to run getFile against. |
---|
| 15 | * Get file should return a csv format file containing the asset tree as per template. |
---|
[271] | 16 | */ |
---|
| 17 | def importAssetTree(request) { |
---|
| 18 | def result = [:] |
---|
| 19 | |
---|
| 20 | def megaByteMultiplier = 1000 * 1000 |
---|
| 21 | def fileMaxSize = 10 * megaByteMultiplier //Mb |
---|
| 22 | |
---|
[287] | 23 | def multiPartFile = request.getFile('file') |
---|
| 24 | |
---|
| 25 | InputStreamReader sr = new InputStreamReader(multiPartFile.inputStream) |
---|
| 26 | CSVReader reader = new CSVReader(sr) |
---|
| 27 | |
---|
[271] | 28 | def fail = { Map m -> |
---|
| 29 | //status.setRollbackOnly() |
---|
[287] | 30 | reader.close() |
---|
| 31 | result.error = [ code: m.code, args: m.args ] |
---|
[271] | 32 | return result |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | if(!multiPartFile || multiPartFile.isEmpty()) |
---|
| 36 | return fail(code: "asset.tree.import.file.not.supplied") |
---|
| 37 | |
---|
| 38 | if (multiPartFile.getSize() > fileMaxSize) |
---|
[287] | 39 | return fail(code: "asset.tree.import.file.over.max.size", args: [fileMaxSize/megaByteMultiplier]) |
---|
[271] | 40 | |
---|
[287] | 41 | def line = reader.readNext() |
---|
[271] | 42 | def lineNumber = 1 |
---|
| 43 | |
---|
| 44 | def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"] |
---|
[287] | 45 | |
---|
| 46 | if(line != header) |
---|
[271] | 47 | return fail(code: "asset.tree.import.no.header") |
---|
| 48 | |
---|
[287] | 49 | log.info "Import checks passed, start processing asset file." |
---|
| 50 | |
---|
| 51 | // Prepare the first body line. |
---|
| 52 | line = reader.readNext() |
---|
[271] | 53 | lineNumber ++ |
---|
| 54 | |
---|
[287] | 55 | while(line) { |
---|
| 56 | def lineSize = line.size() |
---|
| 57 | // log.info lineNumber+ "(" + lineSize + ")" + " : " + line |
---|
[271] | 58 | |
---|
[287] | 59 | if(line[0]) { |
---|
| 60 | if( !Site.findByName(line[0]) ) |
---|
| 61 | new Site(name: line[0]).save() |
---|
| 62 | } |
---|
[271] | 63 | |
---|
[287] | 64 | line = reader.readNext() |
---|
| 65 | lineNumber ++ |
---|
| 66 | } //while(line) |
---|
[271] | 67 | |
---|
| 68 | // Success. |
---|
[287] | 69 | reader.close() |
---|
[271] | 70 | return result |
---|
| 71 | |
---|
| 72 | } // end importAssetTree() |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Build an asset tree template csv file. |
---|
| 76 | * This template can then be populated for import. |
---|
| 77 | * @returns The template as a String in csv format. |
---|
| 78 | */ |
---|
| 79 | def buildAssetTreeTemplate() { |
---|
| 80 | |
---|
| 81 | StringWriter sw = new StringWriter() |
---|
| 82 | CSVWriter writer = new CSVWriter(sw) |
---|
| 83 | |
---|
| 84 | //Header |
---|
| 85 | def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"] |
---|
[287] | 86 | def blankLine = [] |
---|
| 87 | def noteLine = ["Note: the header is required, start by replacing this line."] |
---|
| 88 | |
---|
[271] | 89 | writer.writeNext(header as String[]) |
---|
[287] | 90 | writer.writeNext(blankLine as String[]) |
---|
| 91 | writer.writeNext(noteLine as String[]) |
---|
[271] | 92 | |
---|
| 93 | writer.close() |
---|
| 94 | return sw.toString() |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Build complete asset trees for export. |
---|
| 99 | * @param assetList The list of assets to build and export trees for. |
---|
| 100 | * @returns The tree as a String in csv format. |
---|
| 101 | */ |
---|
| 102 | def buildAssetTree(List assetList) { |
---|
| 103 | |
---|
| 104 | StringWriter sw = new StringWriter() |
---|
| 105 | CSVWriter writer = new CSVWriter(sw) |
---|
| 106 | |
---|
| 107 | //Header |
---|
| 108 | def header = ["Site", "Section", "Asset", "Sub Asset", "Functional Assembly", "Sub Assembly Group"] |
---|
| 109 | writer.writeNext(header as String[]) |
---|
| 110 | |
---|
| 111 | //Rows |
---|
| 112 | def row |
---|
| 113 | |
---|
| 114 | def writeAssetSubItem4 = { assetSubItem -> |
---|
| 115 | row.add(assetSubItem) |
---|
| 116 | writer.writeNext(row as String[]) |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | def writeAssetSubItem3 = { assetSubItem -> |
---|
| 120 | row.add(assetSubItem) |
---|
| 121 | |
---|
| 122 | if(assetSubItem.subItems.size() > 0) { |
---|
| 123 | assetSubItem.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }.each() { assetSubItem4 -> |
---|
| 124 | writeAssetSubItem4(assetSubItem4) |
---|
| 125 | row.remove(row.last()) |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | else { |
---|
| 129 | writer.writeNext(row as String[]) |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | def writeAssetSubItem2 = { assetSubItem -> |
---|
| 135 | row.add(assetSubItem) |
---|
| 136 | |
---|
| 137 | if(assetSubItem.subItems.size() > 0) { |
---|
| 138 | assetSubItem.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }.each() { assetSubItem3 -> |
---|
| 139 | writeAssetSubItem3(assetSubItem3) |
---|
| 140 | row.remove(row.last()) |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | else { |
---|
| 144 | writer.writeNext(row as String[]) |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | def writeAssetSubItem1 = { assetSubItem -> |
---|
| 150 | row.add(assetSubItem) |
---|
| 151 | |
---|
| 152 | if(assetSubItem.subItems.size() > 0) { |
---|
| 153 | assetSubItem.subItems.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }.each() { assetSubItem2 -> |
---|
| 154 | writeAssetSubItem2(assetSubItem2) |
---|
| 155 | row.remove(row.last()) |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | else { |
---|
| 159 | writer.writeNext(row as String[]) |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | assetList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }.each() { asset -> |
---|
| 165 | row = [] |
---|
| 166 | writer.writeNext(row as String[]) //blank row between assets. |
---|
| 167 | row.add(asset.section.site) |
---|
| 168 | row.add(asset.section) |
---|
| 169 | row.add(asset.name) |
---|
| 170 | |
---|
| 171 | if(asset.assetSubItems.size() > 0) { |
---|
| 172 | asset.assetSubItems.each() { assetSubItem1 -> |
---|
| 173 | writeAssetSubItem1(assetSubItem1) |
---|
| 174 | row.remove(row.last()) |
---|
| 175 | } |
---|
| 176 | } |
---|
| 177 | else { |
---|
| 178 | writer.writeNext(row as String[]) |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | writer.close() |
---|
| 184 | return sw.toString() |
---|
| 185 | } // end buildAssetTree |
---|
| 186 | |
---|
| 187 | } // end class |
---|