Changeset 259 for trunk/grails-app/services/dbnp
- Timestamp:
- Mar 12, 2010, 11:51:33 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/dbnp/importer/ImporterService.groovy
r255 r259 20 20 import org.apache.poi.ss.usermodel.DataFormatter 21 21 import org.apache.poi.hssf.usermodel.HSSFDateUtil 22 import dbnp.importer.Column23 22 import dbnp.studycapturing.TemplateFieldType 23 import dbnp.studycapturing.Study 24 import dbnp.studycapturing.Subject 25 import dbnp.studycapturing.Event 26 import dbnp.studycapturing.Protocol 27 import dbnp.studycapturing.Sample 28 24 29 25 30 class ImporterService { … … 39 44 /** 40 45 * @param wb high level representation of the workbook 41 * @return header representation as a stringarray46 * @return header representation as a MappingColumn array 42 47 */ 43 48 def getHeader(HSSFWorkbook wb, int sheetindex){ … … 94 99 def rows = [] 95 100 def df = new DataFormatter() 96 101 def datamatrix_start = 1 102 103 // walk through all rows 97 104 (count <= sheet.getLastRowNum()) ? 98 ((1+sheet.getFirstRowNum())..count).each { rowindex -> 99 105 ((datamatrix_start+sheet.getFirstRowNum())..count).each { rowindex -> 100 106 def row = [] 107 108 // walk through every cell 101 109 for (HSSFCell c: sheet.getRow(rowindex)) 102 110 row.add(c) … … 138 146 * by using mapping information 139 147 * 140 *141 148 * @param wb POI horrible spreadsheet formatted workbook object 142 149 * @param mc array of MappingColumns … … 146 153 * @see dbnp.importer.MappingColumn 147 154 */ 148 def importdata(HSSFWorkbook wb, int sheetindex, int rowindex, MappingColumn[] mc ) {155 def importdata(HSSFWorkbook wb, int sheetindex, int rowindex, MappingColumn[] mcarray) { 149 156 def sheet = wb.getSheetAt(sheetindex) 150 157 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 166 208 } 167 209 }
Note: See TracChangeset
for help on using the changeset viewer.