1 | /** |
---|
2 | * Importer service |
---|
3 | * |
---|
4 | * The importer service handles the import of tabular, comma delimited and Excel format |
---|
5 | * based files. |
---|
6 | * |
---|
7 | * @package importer |
---|
8 | * @author t.w.abma@umcutrecht.nl |
---|
9 | * @since 20100126 |
---|
10 | * |
---|
11 | * Revision information: |
---|
12 | * $Rev: 274 $ |
---|
13 | * $Author: tabma $ |
---|
14 | * $Date: 2010-03-17 10:39:58 +0000 (wo, 17 mrt 2010) $ |
---|
15 | */ |
---|
16 | |
---|
17 | package dbnp.importer |
---|
18 | import org.apache.poi.hssf.usermodel.* |
---|
19 | import org.apache.poi.poifs.filesystem.POIFSFileSystem |
---|
20 | import org.apache.poi.ss.usermodel.DataFormatter |
---|
21 | import org.apache.poi.hssf.usermodel.HSSFDateUtil |
---|
22 | import dbnp.studycapturing.TemplateFieldType |
---|
23 | import dbnp.studycapturing.Template |
---|
24 | import dbnp.studycapturing.Study |
---|
25 | import dbnp.studycapturing.Subject |
---|
26 | import dbnp.studycapturing.Event |
---|
27 | import dbnp.studycapturing.Protocol |
---|
28 | import dbnp.studycapturing.Sample |
---|
29 | |
---|
30 | |
---|
31 | class ImporterService { |
---|
32 | |
---|
33 | boolean transactional = true |
---|
34 | |
---|
35 | /** |
---|
36 | * @param is input stream representing the (workbook) resource |
---|
37 | * @return high level representation of the workbook |
---|
38 | */ |
---|
39 | HSSFWorkbook getWorkbook(InputStream is) { |
---|
40 | POIFSFileSystem fs = new POIFSFileSystem(is) |
---|
41 | HSSFWorkbook wb = new HSSFWorkbook(fs); |
---|
42 | return wb; |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * @param wb high level representation of the workbook |
---|
47 | * @return header representation as a MappingColumn hashmap |
---|
48 | */ |
---|
49 | def getHeader(HSSFWorkbook wb, int sheetindex){ |
---|
50 | |
---|
51 | def sheet = wb.getSheetAt(sheetindex) |
---|
52 | def datamatrix_start = sheet.getFirstRowNum() + 1 |
---|
53 | //def header = [] |
---|
54 | def header = [:] |
---|
55 | def df = new DataFormatter() |
---|
56 | |
---|
57 | |
---|
58 | for (HSSFCell c: sheet.getRow(datamatrix_start)) { |
---|
59 | def datamatrix_celltype = sheet.getRow(datamatrix_start).getCell(c.getColumnIndex()).getCellType() |
---|
60 | def headercell = sheet.getRow(sheet.getFirstRowNum()).getCell(c.getColumnIndex()) |
---|
61 | |
---|
62 | // Check for every celltype, currently redundant code, but possibly this will be |
---|
63 | // a piece of custom code for every cell type like specific formatting |
---|
64 | |
---|
65 | switch (datamatrix_celltype) { |
---|
66 | case HSSFCell.CELL_TYPE_STRING: |
---|
67 | header[c.getColumnIndex()] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.STRING); |
---|
68 | break |
---|
69 | case HSSFCell.CELL_TYPE_NUMERIC: |
---|
70 | if (HSSFDateUtil.isCellDateFormatted(c)) { |
---|
71 | header[c.getColumnIndex()] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.DATE) |
---|
72 | } |
---|
73 | else |
---|
74 | header[c.getColumnIndex()] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.INTEGER); |
---|
75 | break |
---|
76 | case HSSFCell.CELL_TYPE_BLANK: |
---|
77 | header[c.getColumnIndex()] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.STRING); |
---|
78 | break |
---|
79 | default: |
---|
80 | header[c.getColumnIndex()] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.STRING); |
---|
81 | break |
---|
82 | } |
---|
83 | } |
---|
84 | return header |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * This method is meant to return a matrix of the rows and columns |
---|
89 | * used in the preview |
---|
90 | * |
---|
91 | * @param wb workbook object |
---|
92 | * @param sheetindex sheet index used |
---|
93 | * @param rows amount of rows returned |
---|
94 | * @return two dimensional array (matrix) of HSSFCell objects |
---|
95 | */ |
---|
96 | |
---|
97 | HSSFCell[][] getDatamatrix(HSSFWorkbook wb, int sheetindex, int count) { |
---|
98 | def sheet = wb.getSheetAt(sheetindex) |
---|
99 | def rows = [] |
---|
100 | def df = new DataFormatter() |
---|
101 | def datamatrix_start = 1 |
---|
102 | |
---|
103 | // walk through all rows |
---|
104 | (count <= sheet.getLastRowNum()) ? |
---|
105 | ((datamatrix_start+sheet.getFirstRowNum())..count).each { rowindex -> |
---|
106 | def row = [] |
---|
107 | |
---|
108 | // walk through every cell |
---|
109 | for (HSSFCell c: sheet.getRow(rowindex)) |
---|
110 | row.add(c) |
---|
111 | //row.add(df.formatCellValue(c)) |
---|
112 | rows.add(row) |
---|
113 | } : 0 |
---|
114 | |
---|
115 | return rows |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * This method will move a file to a new location. |
---|
120 | * |
---|
121 | * @param file File object to move |
---|
122 | * @param folderpath folder to move the file to |
---|
123 | * @param filename (new) filename to give |
---|
124 | * @return if file has been moved succesful, the new path and filename will be returned, otherwise an empty string will be returned |
---|
125 | */ |
---|
126 | def moveFile(File file, String folderpath, String filename) { |
---|
127 | try { |
---|
128 | def rnd = ""; //System.currentTimeMillis() |
---|
129 | file.transferTo(new File(folderpath, rnd+filename)) |
---|
130 | return folderpath + filename |
---|
131 | } catch(Exception exception) { |
---|
132 | log.error "File move error, ${exception}" |
---|
133 | return "" |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * @return random numeric value |
---|
139 | */ |
---|
140 | def random = { |
---|
141 | return System.currentTimeMillis() + Runtime.runtime.freeMemory() |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * Method to read data from a workbook and to import data into the database |
---|
146 | * by using mapping information |
---|
147 | * |
---|
148 | * @param template_id template identifier to use fields from |
---|
149 | * @param wb POI horrible spreadsheet formatted workbook object |
---|
150 | * @param mcmap linked hashmap (preserved order) of MappingColumns |
---|
151 | * @param sheetindex sheet to use when using multiple sheets |
---|
152 | * @param rowindex first row to start with reading the actual data (NOT the header) |
---|
153 | * |
---|
154 | * @see dbnp.importer.MappingColumn |
---|
155 | */ |
---|
156 | def importdata(template_id, HSSFWorkbook wb, int sheetindex, int rowindex, mcmap) { |
---|
157 | def sheet = wb.getSheetAt(sheetindex) |
---|
158 | def table = [] |
---|
159 | |
---|
160 | // walk through all rows |
---|
161 | (rowindex..sheet.getLastRowNum()).each { i -> |
---|
162 | table.add(createRecord(template_id, sheet.getRow(i), mcmap)) |
---|
163 | } |
---|
164 | |
---|
165 | table.each { |
---|
166 | it.each { entity -> |
---|
167 | entity.giveFields().each { field -> |
---|
168 | print field.name + ":" + entity.getFieldValue(field.name) + "/" |
---|
169 | } |
---|
170 | println |
---|
171 | } |
---|
172 | } |
---|
173 | } |
---|
174 | /** |
---|
175 | * This method created a record (array) containing entities with values |
---|
176 | */ |
---|
177 | def createRecord(template_id, HSSFRow excelrow, mcmap) { |
---|
178 | def df = new DataFormatter() |
---|
179 | def template = Template.get(template_id) |
---|
180 | def record = [] |
---|
181 | |
---|
182 | def study = new Study(title:"New study", template:template) |
---|
183 | def subject = new Subject(title:"New subject", template:template) |
---|
184 | def event = new Event(title:"New event", template:template) |
---|
185 | def protocol = new Protocol(title:"New protocol", template:template) |
---|
186 | def sample = new Sample(title:"New sample", template:template) |
---|
187 | |
---|
188 | /*record.add(study) |
---|
189 | record.add(subject) |
---|
190 | record.add(event) |
---|
191 | record.add(protocol) |
---|
192 | record.add(sample)*/ |
---|
193 | |
---|
194 | for (HSSFCell cell: excelrow) { |
---|
195 | def mc = mcmap[cell.getColumnIndex()] |
---|
196 | |
---|
197 | switch(mc.entity) { |
---|
198 | case Study : (record.any {it.getClass()==mc.entity}) ? 0 : record.add(study) |
---|
199 | study.setFieldValue(mc.property.name, df.formatCellValue(cell)) |
---|
200 | break |
---|
201 | case Subject : (record.any {it.getClass()==mc.entity}) ? 0 : record.add(subject) |
---|
202 | subject.setFieldValue(mc.property.name, df.formatCellValue(cell)) |
---|
203 | break |
---|
204 | case Event : (record.any {it.getClass()==mc.entity}) ? 0 : record.add(event) |
---|
205 | event.setFieldValue(mc.property.name, df.formatCellValue(cell)) |
---|
206 | break |
---|
207 | case Protocol : (record.any {it.getClass()==mc.entity}) ? 0 : record.add(protocol) |
---|
208 | protocol.setFieldValue(mc.property.name, df.formatCellValue(cell)) |
---|
209 | break |
---|
210 | case Sample : (record.any {it.getClass()==mc.entity}) ? record.add(sample) : 0 |
---|
211 | sample.setFieldValue(mc.property.name, df.formatCellValue(cell)) |
---|
212 | break |
---|
213 | case Object : // don't import |
---|
214 | break |
---|
215 | } // end switch |
---|
216 | } // end for |
---|
217 | |
---|
218 | return record |
---|
219 | } |
---|
220 | } |
---|