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

Last change on this file since 147 was 147, checked in by tabma, 10 years ago
  • basic skeleton for the importer/previewer
  • Property svn:keywords set to Date Author Rev
File size: 2.3 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: 147 $
13 * $Author: tabma $
14 * $Date: 2010-01-28 20:22:59 +0000 (do, 28 jan 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    def hello() { println "hello "}
28
29    /**
30    * @param is input stream representing the (workbook) resource
31    * @return high level representation of the workbook
32    */
33    HSSFWorkbook getWorkbook(InputStream is) {
34        POIFSFileSystem fs = new POIFSFileSystem(is)
35        HSSFWorkbook    wb = new HSSFWorkbook(fs);
36        return wb;
37    }
38
39    /**
40     * @param wb high level representation of the workbook
41     * @return header representation as a string array
42     */
43    def getHeader(HSSFWorkbook wb){
44        def sheet = wb.getSheetAt(0)
45        def row = 0
46        def header = []
47        def cellvalue
48        def df = new DataFormatter()
49
50        for (HSSFCell c: sheet.getRow(row)) {
51            switch (c.getCellType()) {
52                    case HSSFCell.CELL_TYPE_STRING: header.add (df.formatCellValue(c)); break
53                    case HSSFCell.CELL_TYPE_NUMERIC: header.add (df.formatCellValue(c)); break
54                    case HSSFCell.CELL_TYPE_BLANK: header.add(""); break
55                    default: header.add("")
56            }       
57        }
58        return header
59    }
60
61    /**
62    * This method will move a file to a new location.
63    *
64    * @param file File object to move
65    * @param folderpath folder to move the file to
66    * @param filename (new) filename to give
67    * @return if file has been moved succesful, the new path and filename will be returned, otherwise an empty string will be returned
68    */
69    def moveFile(File file, String folderpath, String filename) {
70        try {
71                def rnd = ""; //System.currentTimeMillis()
72                file.transferTo(new File(folderpath, rnd+filename))
73                return folderpath + filename
74            } catch(Exception exception) {
75                log.error "File move error, ${exception}"
76                return ""
77                }
78    }
79
80    /**
81    * @return random numeric value
82    */
83    def random = {
84            return System.currentTimeMillis() + Runtime.runtime.freeMemory()
85        }
86}
Note: See TracBrowser for help on using the repository browser.