source: trunk/grails-app/services/dbnp/importer/ImporterService.groovy @ 169

Last change on this file since 169 was 169, checked in by tabma, 10 years ago
  • created taglibrary for importer and first implementation detection of (cell) datatypes
  • Property svn:keywords set to Date Author Rev
File size: 3.6 KB
Line 
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: 169 $
13 * $Author: tabma $
14 * $Date: 2010-02-03 14:50:14 +0000 (wo, 03 feb 2010) $
15 */
16
17package dbnp.importer
18import org.apache.poi.hssf.usermodel.*
19import org.apache.poi.poifs.filesystem.POIFSFileSystem
20import org.apache.poi.ss.usermodel.DataFormatter
21
22
23class 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 df = new DataFormatter()
47
48        for (HSSFCell c: sheet.getRow(sheet.getFirstRowNum())) {
49            def datamatrix_celltype = sheet.getRow(datamatrix_start).getCell(c.getColumnIndex()).getCellType()
50
51            // Check for every celltype, currently redundant code, but possibly this will be
52            // a piece of custom code for every cell type like specific formatting
53           
54            switch (c.getCellType()) {
55                    case HSSFCell.CELL_TYPE_STRING:                     
56                        header.add (columnindex:c.getColumnIndex(), value:df.formatCellValue(c), celltype:datamatrix_celltype);
57                        break
58                    case HSSFCell.CELL_TYPE_NUMERIC:
59                        header.add (columnindex:c.getColumnIndex(), value:df.formatCellValue(c), celltype:datamatrix_celltype);
60                        break
61                    case HSSFCell.CELL_TYPE_BLANK:
62                        header.add (columnindex:c.getColumnIndex(), value:"-", celltype:datamatrix_celltype);
63                        break
64                    default:
65                        header.add (columnindex:c.getColumnIndex(), value:df.formatCellValue(c), celltype:datamatrix_celltype);
66                        break
67            }
68        }
69        return header
70    }
71
72    /**
73     * This method is meant to return a matrix of the rows and columns
74     * used in the preview
75     *
76     * @param wb workbook object
77     * @param sheetindex sheet index used
78     * @param rows amount of rows returned
79     * @return two dimensional array (matrix) of HSSFCell objects
80     */
81
82    HSSFCell[][] getDatamatrix(HSSFWorkbook wb, int sheetindex, int count) {
83        def sheet = wb.getSheetAt(sheetindex)
84        def rows  = []
85        def df = new DataFormatter()
86
87        (count <= sheet.getLastRowNum()) ?
88        ((1+sheet.getFirstRowNum())..count).each { rowindex ->
89
90            def row = []
91            for (HSSFCell c: sheet.getRow(rowindex))
92                row.add(c)
93                //row.add(df.formatCellValue(c))
94            rows.add(row)
95        } : 0
96
97        return rows
98    }
99
100    /**
101    * This method will move a file to a new location.
102    *
103    * @param file File object to move
104    * @param folderpath folder to move the file to
105    * @param filename (new) filename to give
106    * @return if file has been moved succesful, the new path and filename will be returned, otherwise an empty string will be returned
107    */
108    def moveFile(File file, String folderpath, String filename) {
109        try {
110                def rnd = ""; //System.currentTimeMillis()
111                file.transferTo(new File(folderpath, rnd+filename))
112                return folderpath + filename
113            } catch(Exception exception) {
114                log.error "File move error, ${exception}"
115                return ""
116                }
117    }
118
119    /**
120    * @return random numeric value
121    */
122    def random = {
123            return System.currentTimeMillis() + Runtime.runtime.freeMemory()
124        }
125}
Note: See TracBrowser for help on using the repository browser.