Ignore:
Timestamp:
Mar 15, 2010, 7:16:02 PM (14 years ago)
Author:
tabma
Message:
  • identifier column added
File:
1 edited

Legend:

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

    r259 r268  
    2121import org.apache.poi.hssf.usermodel.HSSFDateUtil
    2222import dbnp.studycapturing.TemplateFieldType
     23import dbnp.studycapturing.Template
    2324import dbnp.studycapturing.Study
    2425import dbnp.studycapturing.Subject
     
    4445    /**
    4546     * @param wb high level representation of the workbook
    46      * @return header representation as a MappingColumn array
     47     * @return header representation as a MappingColumn hashmap
    4748     */
    4849    def getHeader(HSSFWorkbook wb, int sheetindex){
     
    6869                    case HSSFCell.CELL_TYPE_NUMERIC:                   
    6970                            if (HSSFDateUtil.isCellDateFormatted(c)) {
    70                                 println("DATE")
    7171                                header[c.getColumnIndex()] = new dbnp.importer.MappingColumn(name:df.formatCellValue(headercell), templatefieldtype:TemplateFieldType.DATE)
    7272                            }
     
    146146    * by using mapping information
    147147    *
     148    * @param template_id template identifier to use fields from
    148149    * @param wb POI horrible spreadsheet formatted workbook object
    149     * @param mc array of MappingColumns
     150    * @param mcmap linked hashmap (preserved order) of MappingColumns
    150151    * @param sheetindex sheet to use when using multiple sheets
    151152    * @param rowindex first row to start with reading the actual data (NOT the header)
     
    153154    * @see dbnp.importer.MappingColumn
    154155    */
    155     def importdata(HSSFWorkbook wb, int sheetindex, int rowindex, MappingColumn[] mcarray) {
     156    def importdata(template_id, HSSFWorkbook wb, int sheetindex, int rowindex, mcmap) {
    156157        def sheet = wb.getSheetAt(sheetindex)
    157158        def rows  = []
     
    163164
    164165            // 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         }
     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
    169172    }
    170173
     
    172175    * This function creates a column based on the current cell and mapping
    173176    *
     177    * @param template_id template identifier to use fields from
    174178    * @param cell POI cell read from Excel
    175179    * @param mc mapping column
     
    177181    *
    178182    */
    179     def createColumn(HSSFCell cell, MappingColumn mc) {
     183    def createColumn(template_id, HSSFCell cell, MappingColumn mc) {
    180184        def df = new DataFormatter()
    181 
    182         // check the templatefield type of the cell
     185        def template = Template.get(template_id)
     186
     187        // check the templatefield entity of the cell
    183188        switch(mc.entity) {
    184             case Study  :   def st = new Study()
    185                             st.setFieldValue(mc.name, df.formatCellValue(c))
     189            case Study  :   def st = new Study(template:template)
     190                            st.setFieldValue(mc.name, df.formatCellValue(cell))
    186191                            return st
    187192                            break
    188             case Subject:   def su = new Subject()
    189                             su.setFieldValue(mc.name, df.formatCellValue(c))
     193            case Subject:   def su = new Subject(template:template)
     194                            su.setFieldValue(mc.name, df.formatCellValue(cell))
    190195                            return su
    191196                            break
    192             case Event  :   def ev = new Event()
    193                             ev.setFieldValue(mc.name, df.formatCellValue(c))
     197            case Event  :   def ev = new Event(template:template)
     198                            ev.setFieldValue(mc.name, df.formatCellValue(cell))
    194199                            return ev
    195200                            break
    196             case Protocol:  def pr = new Protocol()
    197                             pr.setFieldValue(mc.name, df.formatCellValue(c))
     201            case Protocol:  def pr = new Protocol(template:template)
     202                            pr.setFieldValue(mc.name, df.formatCellValue(cell))
    198203                            return pr
    199204                            break
    200             case Sample :   def sa = new Sample()
    201                             sa.setFieldValue(mc.name, df.formatCellValue(c))
     205            case Sample :   def sa = new Sample(template:template)
     206                            sa.setFieldValue(mc.name, df.formatCellValue(cell))
    202207                            return sa
    203208                            break
    204             case Object :   break
    205            
    206         }
    207 
     209            case Object :   break           
     210        }
    208211    }
    209212}
Note: See TracChangeset for help on using the changeset viewer.