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