Changeset 169


Ignore:
Timestamp:
Feb 3, 2010, 3:50:14 PM (9 years ago)
Author:
tabma
Message:
  • created taglibrary for importer and first implementation detection of (cell) datatypes
Location:
trunk
Files:
9 added
2 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/controllers/dbnp/importer/ImporterController.groovy

    r162 r169  
    2020
    2121package dbnp.importer
     22import org.apache.poi.hssf.usermodel.HSSFCell
     23import org.apache.poi.ss.usermodel.DataFormatter
    2224
    2325class ImporterController {
     
    3840        def wb = ImporterService.getWorkbook(new FileInputStream(tempfile))
    3941       
    40         def header = ImporterService.getHeader(wb)
    41         render (view:"preview", model:[header:header])
     42        def header = ImporterService.getHeader(wb, 0)
     43        def datamatrix= ImporterService.getDatamatrix(wb, 0, 5)
     44
     45        render (view:"step1", model:[header:header, datamatrix:datamatrix])
     46
    4247    }
    4348}
  • trunk/grails-app/services/dbnp/importer/ImporterService.groovy

    r147 r169  
    2525    boolean transactional = true
    2626
    27     def hello() { println "hello "}
    28 
    2927    /**
    3028    * @param is input stream representing the (workbook) resource
     
    4139     * @return header representation as a string array
    4240     */
    43     def getHeader(HSSFWorkbook wb){
    44         def sheet = wb.getSheetAt(0)
    45         def row = 0
    46         def header = []
    47         def cellvalue
     41    def getHeader(HSSFWorkbook wb, int sheetindex){
     42
     43        def sheet = wb.getSheetAt(sheetindex)
     44        def datamatrix_start = sheet.getFirstRowNum() + 1
     45        def header = []       
    4846        def df = new DataFormatter()
    4947
    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             }       
     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            }
    5768        }
    5869        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
    5998    }
    6099
  • trunk/grails-app/views/importer/step1.gsp

    • Property svn:keywords set to Date Author Rev
    r158 r169  
    1 <!--
    2   To change this template, choose Tools | Templates
    3   and open the template in the editor.
    4 -->
     1<%
     2        /**
     3         * First step in the importer, showing a preview of the uploaded data
     4         *
     5         * @author Tjeerd Abma
     6         * @since 20100129
     7         * @package importer
     8         *
     9         * Revision information:
     10         * $Rev$
     11         * $Author$
     12         * $Date$
     13         */
     14%>
    515
    616<%@ page contentType="text/html;charset=UTF-8" %>
     
    919  <head>
    1020    <meta name="layout" content="main"/>
     21    <link rel="stylesheet" href="${resource(dir: 'css', file: 'importer.css')}"/>
    1122    <title>Import wizard preview</title>
    1223  </head>
     
    1425    <h1>Import wizard preview</h1>
    1526    <p>Below you see a preview of your imported file, please correct the automatically detected types.</p>
    16     <table>
    17       <tr>
    18         <g:each var="column" in="${header}">
    19           <td>${column}
    20             <select name="celltype">
    21               <option>String</option>
    22               <option>Numeric</option>
    23               <option>Date</option>
    24               <option>Unknown</option>
    25             </td>
    26         </g:each>
    27       </tr>
    28   </table>
     27  <importer:preview header="${header}" datamatrix="${datamatrix}"/>
    2928  </body>
    3029</html>
Note: See TracChangeset for help on using the changeset viewer.