Ignore:
Timestamp:
Mar 16, 2010, 3:36:14 PM (14 years ago)
Author:
tabma
Message:
  • Excel file is parsed, per row a record is created containing entities with the values (based on the MappingColumn?)
  • result is an array containing records
File:
1 edited

Legend:

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

    r268 r273  
    155155    */
    156156    def importdata(template_id, HSSFWorkbook wb, int sheetindex, int rowindex, mcmap) {
    157         def sheet = wb.getSheetAt(sheetindex)
    158         def rows  = []
    159         def df = new DataFormatter()
     157        def sheet = wb.getSheetAt(sheetindex)           
    160158
    161159        // walk through all rows       
    162160        rowindex..sheet.getLastRowNum().each { i ->
    163             def record = [:]
    164 
    165             // get the value of the cells in the row
    166             for (HSSFCell cell: sheet.getRow(i)) {
    167                 def mc = mcmap[cell.getColumnIndex()]
    168                 record.add(createColumn(template_id, cell, mc))
    169             }
    170         }
    171         println record
    172     }
    173 
    174     /**
    175     * This function creates a column based on the current cell and mapping
    176     *
    177     * @param template_id template identifier to use fields from
    178     * @param cell POI cell read from Excel
    179     * @param mc mapping column
    180     * @return entity object
    181     *
    182     */
    183     def createColumn(template_id, HSSFCell cell, MappingColumn mc) {
     161            def table = []
     162
     163            table.add(createRecord(template_id, sheet.getRow(i), mcmap))
     164        }       
     165    }
     166    /**
     167     * This method created a record (array) containing entities with values
     168     */
     169    def createRecord(template_id, HSSFRow excelrow, mcmap) {
    184170        def df = new DataFormatter()
    185171        def template = Template.get(template_id)
    186 
    187         // check the templatefield entity of the cell
    188         switch(mc.entity) {
    189             case Study  :   def st = new Study(template:template)
    190                             st.setFieldValue(mc.name, df.formatCellValue(cell))
    191                             return st
    192                             break
    193             case Subject:   def su = new Subject(template:template)
    194                             su.setFieldValue(mc.name, df.formatCellValue(cell))
    195                             return su
    196                             break
    197             case Event  :   def ev = new Event(template:template)
    198                             ev.setFieldValue(mc.name, df.formatCellValue(cell))
    199                             return ev
    200                             break
    201             case Protocol:  def pr = new Protocol(template:template)
    202                             pr.setFieldValue(mc.name, df.formatCellValue(cell))
    203                             return pr
    204                             break
    205             case Sample :   def sa = new Sample(template:template)
    206                             sa.setFieldValue(mc.name, df.formatCellValue(cell))
    207                             return sa
    208                             break
    209             case Object :   break           
    210         }
    211     }
     172        def record = []
     173
     174        def study = new Study(title:"New study", template:template)
     175        def subject = new Subject(title:"New subject", template:template)
     176        def event = new Event(title:"New event", template:template)
     177        def protocol = new Protocol(title:"New protocol", template:template)
     178        def sample = new Sample(title:"New sample", template:template)
     179
     180        record.add(study)
     181        record.add(subject)
     182        record.add(event)
     183        record.add(protocol)
     184        record.add(sample)
     185
     186        for (HSSFCell cell: excelrow) {
     187            def mc = mcmap[cell.getColumnIndex()]
     188
     189            switch(mc.entity) {
     190                case Study      :   study.setFieldValue(mc.property.name, df.formatCellValue(cell))
     191                                    break
     192                case Subject    :   subject.setFieldValue(mc.property.name, df.formatCellValue(cell))
     193                                    break
     194                case Event      :   event.setFieldValue(mc.property.name, df.formatCellValue(cell))
     195                                    break
     196                case Protocol   :   protocol.setFieldValue(mc.property.name, df.formatCellValue(cell))
     197                                    break
     198                case Sample     :   sample.setFieldValue(mc.property.name, df.formatCellValue(cell))
     199                                    break
     200                case Object     :   // don't import
     201                                    break
     202            } // end switch
     203        } // end for
     204       
     205        return record
     206    }
     207
    212208}
Note: See TracChangeset for help on using the changeset viewer.