Ignore:
Timestamp:
Mar 12, 2010, 11:51:33 AM (14 years ago)
Author:
tabma
Message:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/services/dbnp/importer/ImporterService.groovy

    r255 r259  
    2020import org.apache.poi.ss.usermodel.DataFormatter
    2121import org.apache.poi.hssf.usermodel.HSSFDateUtil
    22 import dbnp.importer.Column
    2322import dbnp.studycapturing.TemplateFieldType
     23import dbnp.studycapturing.Study
     24import dbnp.studycapturing.Subject
     25import dbnp.studycapturing.Event
     26import dbnp.studycapturing.Protocol
     27import dbnp.studycapturing.Sample
     28
    2429
    2530class ImporterService {
     
    3944    /**
    4045     * @param wb high level representation of the workbook
    41      * @return header representation as a string array
     46     * @return header representation as a MappingColumn array
    4247     */
    4348    def getHeader(HSSFWorkbook wb, int sheetindex){
     
    9499        def rows  = []
    95100        def df = new DataFormatter()
    96 
     101        def datamatrix_start = 1
     102
     103        // walk through all rows
    97104        (count <= sheet.getLastRowNum()) ?
    98         ((1+sheet.getFirstRowNum())..count).each { rowindex ->
    99 
     105        ((datamatrix_start+sheet.getFirstRowNum())..count).each { rowindex ->
    100106            def row = []
     107
     108            // walk through every cell
    101109            for (HSSFCell c: sheet.getRow(rowindex))
    102110                row.add(c)
     
    138146    * by using mapping information
    139147    *
    140     *
    141148    * @param wb POI horrible spreadsheet formatted workbook object
    142149    * @param mc array of MappingColumns
     
    146153    * @see dbnp.importer.MappingColumn
    147154    */
    148     def importdata(HSSFWorkbook wb, int sheetindex, int rowindex, MappingColumn[] mc) {
     155    def importdata(HSSFWorkbook wb, int sheetindex, int rowindex, MappingColumn[] mcarray) {
    149156        def sheet = wb.getSheetAt(sheetindex)
    150157        def rows  = []
    151 
    152         (count <= sheet.getLastRowNum()) ?
    153         (rowindex..count).each { i ->
    154 
    155             def row = []
    156             for (HSSFCell c: sheet.getRow(i))
    157                 //row.add(c)
    158                 //row.add(df.formatCellValue(c))
    159                 switch(mc[c.getColumnIndex()].celltype) {
    160                     case 0  : break
    161                     default : break
    162                 }
    163 
    164                 rows.add(row)
    165         } : 0
     158        def df = new DataFormatter()
     159
     160        // walk through all rows       
     161        rowindex..sheet.getLastRowNum().each { i ->
     162            def record = [:]
     163
     164            // get the value of the cells in the row
     165            for (HSSFCell c: sheet.getRow(i))           
     166                mc = mcarray[c.getColumnIndex()]               
     167                record.add(createColumn(c, mc))
     168        }
     169    }
     170
     171    /**
     172    * This function creates a column based on the current cell and mapping
     173    *
     174    * @param cell POI cell read from Excel
     175    * @param mc mapping column
     176    * @return entity object
     177    *
     178    */
     179    def createColumn(HSSFCell cell, MappingColumn mc) {
     180        def df = new DataFormatter()
     181
     182        // check the templatefield type of the cell
     183        switch(mc.entity) {
     184            case Study  :   def st = new Study()
     185                            st.setFieldValue(mc.name, df.formatCellValue(c))
     186                            return st
     187                            break
     188            case Subject:   def su = new Subject()
     189                            su.setFieldValue(mc.name, df.formatCellValue(c))
     190                            return su
     191                            break
     192            case Event  :   def ev = new Event()
     193                            ev.setFieldValue(mc.name, df.formatCellValue(c))
     194                            return ev
     195                            break
     196            case Protocol:  def pr = new Protocol()
     197                            pr.setFieldValue(mc.name, df.formatCellValue(c))
     198                            return pr
     199                            break
     200            case Sample :   def sa = new Sample()
     201                            sa.setFieldValue(mc.name, df.formatCellValue(c))
     202                            return sa
     203                            break
     204            case Object :   break
     205           
     206        }
     207
    166208    }
    167209}
Note: See TracChangeset for help on using the changeset viewer.