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: 231 $ |
---|
13 | * $Author: tabma $ |
---|
14 | * $Date: 2010-03-03 14:50:32 +0000 (wo, 03 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 dbnp.importer.Column |
---|
22 | |
---|
23 | class ImporterService { |
---|
24 | |
---|
25 | boolean transactional = true |
---|
26 | |
---|
27 | /** |
---|
28 | * @param is input stream representing the (workbook) resource |
---|
29 | * @return high level representation of the workbook |
---|
30 | */ |
---|
31 | HSSFWorkbook getWorkbook(InputStream is) { |
---|
32 | POIFSFileSystem fs = new POIFSFileSystem(is) |
---|
33 | HSSFWorkbook wb = new HSSFWorkbook(fs); |
---|
34 | return wb; |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * @param wb high level representation of the workbook |
---|
39 | * @return header representation as a string array |
---|
40 | */ |
---|
41 | def getHeader(HSSFWorkbook wb, int sheetindex){ |
---|
42 | |
---|
43 | def sheet = wb.getSheetAt(sheetindex) |
---|
44 | def datamatrix_start = sheet.getFirstRowNum() + 1 |
---|
45 | //def header = [] |
---|
46 | def header = [:] |
---|
47 | def df = new DataFormatter() |
---|
48 | |
---|
49 | |
---|
50 | for (HSSFCell c: sheet.getRow(sheet.getFirstRowNum())) { |
---|
51 | def datamatrix_celltype = sheet.getRow(datamatrix_start).getCell(c.getColumnIndex()).getCellType() |
---|
52 | |
---|
53 | // Check for every celltype, currently redundant code, but possibly this will be |
---|
54 | // a piece of custom code for every cell type like specific formatting |
---|
55 | |
---|
56 | switch (c.getCellType()) { |
---|
57 | case HSSFCell.CELL_TYPE_STRING: |
---|
58 | header[c.getColumnIndex()] = new dbnp.importer.Column(value:df.formatCellValue(c), type:datamatrix_celltype); |
---|
59 | break |
---|
60 | case HSSFCell.CELL_TYPE_NUMERIC: |
---|
61 | header[c.getColumnIndex()] = new dbnp.importer.Column(value:df.formatCellValue(c), type:datamatrix_celltype); |
---|
62 | break |
---|
63 | case HSSFCell.CELL_TYPE_BLANK: |
---|
64 | header[c.getColumnIndex()] = new dbnp.importer.Column(value:df.formatCellValue(c), type:datamatrix_celltype); |
---|
65 | break |
---|
66 | default: |
---|
67 | header[c.getColumnIndex()] = new dbnp.importer.Column(value:df.formatCellValue(c), type:datamatrix_celltype); |
---|
68 | break |
---|
69 | } |
---|
70 | } |
---|
71 | return header |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * This method is meant to return a matrix of the rows and columns |
---|
76 | * used in the preview |
---|
77 | * |
---|
78 | * @param wb workbook object |
---|
79 | * @param sheetindex sheet index used |
---|
80 | * @param rows amount of rows returned |
---|
81 | * @return two dimensional array (matrix) of HSSFCell objects |
---|
82 | */ |
---|
83 | |
---|
84 | HSSFCell[][] getDatamatrix(HSSFWorkbook wb, int sheetindex, int count) { |
---|
85 | def sheet = wb.getSheetAt(sheetindex) |
---|
86 | def rows = [] |
---|
87 | def df = new DataFormatter() |
---|
88 | |
---|
89 | (count <= sheet.getLastRowNum()) ? |
---|
90 | ((1+sheet.getFirstRowNum())..count).each { rowindex -> |
---|
91 | |
---|
92 | def row = [] |
---|
93 | for (HSSFCell c: sheet.getRow(rowindex)) |
---|
94 | row.add(c) |
---|
95 | //row.add(df.formatCellValue(c)) |
---|
96 | rows.add(row) |
---|
97 | } : 0 |
---|
98 | |
---|
99 | return rows |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * This method will move a file to a new location. |
---|
104 | * |
---|
105 | * @param file File object to move |
---|
106 | * @param folderpath folder to move the file to |
---|
107 | * @param filename (new) filename to give |
---|
108 | * @return if file has been moved succesful, the new path and filename will be returned, otherwise an empty string will be returned |
---|
109 | */ |
---|
110 | def moveFile(File file, String folderpath, String filename) { |
---|
111 | try { |
---|
112 | def rnd = ""; //System.currentTimeMillis() |
---|
113 | file.transferTo(new File(folderpath, rnd+filename)) |
---|
114 | return folderpath + filename |
---|
115 | } catch(Exception exception) { |
---|
116 | log.error "File move error, ${exception}" |
---|
117 | return "" |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * @return random numeric value |
---|
123 | */ |
---|
124 | def random = { |
---|
125 | return System.currentTimeMillis() + Runtime.runtime.freeMemory() |
---|
126 | } |
---|
127 | |
---|
128 | def importdata = { |
---|
129 | |
---|
130 | } |
---|
131 | } |
---|