Changeset 1093 for trunk/grails-app/services/dbnp
- Timestamp:
- Nov 6, 2010, 4:01:11 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/dbnp/importer/ImporterService.groovy
r1087 r1093 219 219 def sheet = wb.getSheetAt(sheetindex) 220 220 def table = [] 221 def failedcells = [:] // map [recordhash, [mappingcolumnlist]] values 221 222 222 223 // walk through all rows and fill the table with records 223 224 (rowindex..sheet.getLastRowNum()).each { i -> 224 table.add(createRecord(template_id, sheet.getRow(i), mcmap)) 225 //table.add(createRecord(template_id, sheet.getRow(i), mcmap)) 226 // Create an entity record based on a row read from Excel and store the cells which failed to be mapped 227 def (record, failed) = createRecord(template_id, sheet.getRow(i), mcmap) 228 229 // Add record with entity and its values to the table 230 table.add(record) 231 232 // If failed cells have been found, add them to the failed cells map 233 // the record hashcode is later on used to put the failed data back 234 // in the data matrix 235 if (failed.size()!=0) failedcells.put(record.hashCode(), failed) 225 236 } 226 return table 227 } 228 229 /** Method to extract failed cells from the datamatrix. Failed cells are cell values 237 238 failedcells.each { record -> 239 record.each { cell -> 240 cell.each { 241 println it.value.dump() 242 } 243 } 244 } 245 246 247 return [table,failedcells] 248 } 249 250 /** Method to put failed cells back into the datamatrix. Failed cells are cell values 230 251 * which could not be stored in an entity (e.g. Humu Supiuns in an ontology field). 252 * Empty corrections should not be stored 231 253 * 232 254 * @param datamatrix two dimensional array containing entities and possibly also failed cells 233 * @return array of failed cells in [mappingcolumn, cell] format 234 * */ 235 def getFailedCells(datamatrix) { 236 def failedcells = [] 237 238 datamatrix.each { record -> 239 record.each { column -> 240 column.each { 241 if (it.getClass().getName().equals('java.util.LinkedHashMap$Entry')) { 242 failedcells.add(it) 243 } 244 } 245 } 246 } 247 248 return failedcells 249 } 250 251 /** Method to put failed cells back into the datamatrix. Failed cells are cell values 252 * which could not be stored in an entity (e.g. Humu Supiuns in an ontology field). 253 * 254 * @param datamatrix two dimensional array containing entities and possibly also failed cells 255 * @param failedcells map of failed cells in [mappingcolumn, cell] format 256 * @param correctedcells map of corrected cells 255 * @param failedcells list with maps of failed cells in [mappingcolumn, cell] format 256 * @param correctedcells map of corrected cells in [cellhashcode, value] format 257 257 **/ 258 def saveCorrectedCells(datamatrix, failedcells, correctedcells) { 259 /*failedcells.each { 260 println it 261 }*/ 262 def newdatamatrix = [] 263 264 // Remove failed cells 'entity' / clean up datamatrix 265 datamatrix.each { record -> 266 def newrecord = [] 267 268 record.each { entity -> 269 // LinkedHashMap means a "mappingcolumn:cell" object is inside the record (= failed to map to entity) 270 if (!entity.getClass().getName().equals('java.util.LinkedHashMap')) 271 newrecord.add(entity) 272 } 273 274 newdatamatrix.add(newrecord) 275 } 276 277 newdatamatrix.each { record -> 278 record.each { entity -> 279 entity.giveFields().each { field -> 280 println "das"+ field 281 } 282 } 283 } 284 285 println "-----" 286 correctedcells.each { 287 println it.dump() 288 } 258 def saveCorrectedCells(datamatrix, failedcells, correctedcells) { 259 // Loop through all failed cells 260 failedcells.each { mcrecord -> 261 mcrecord.each { mappingcolumn -> 262 // Get the corrected value 263 def correctedvalue = correctedcells.find { it.key.toInteger() == mappingcolumn.hashCode()}.value 264 265 // Find the record in the table which the mappingcolumn belongs to 266 def tablerecord = datamatrix.find { it.hashCode() == mcrecord.key } 267 268 // Loop through all entities in the record 269 tablerecord.each { rec -> 270 rec.each { entity -> 271 try { 272 // Update the entity field 273 entity.setFieldValue(mappingcolumn.value.property[0], correctedvalue) 274 println "Adjusted " + mappingcolumn.value.property[0] + " to " + correctedvalue 275 } 276 catch (Exception e) { 277 println "Could not map corrected ontology" 278 } 279 } 280 } // end of table record 281 } // end of mapping record 282 } // end of failed cells loop 289 283 } 290 284 … … 347 341 348 342 /** 349 * Check whether an entity already exists within a study. A combination of 350 * the study where the entity will be stored and a unique field in the entity is 343 * Check whether an entity already exist. A unique field in the entity is 351 344 * used to check whether the instantiated entity (read from Excel) is new. 352 * If the entity is found in the database it will be returned as so and 353 * it should update the entity-values (read from Ecel) into the record in the database. 345 * If the entity is found in the database it will be returned as is. 354 346 * 355 347 * @param entity entity object like a Study, Subject, Sample et cetera … … 428 420 * @param excelrow POI based Excel row containing the cells 429 421 * @param mcmap map containing MappingColumn objects 422 * @return list of entities and list of failed cells 430 423 */ 431 424 def createRecord(template_id, Row excelrow, mcmap) { 432 425 def df = new DataFormatter() 433 426 def template = Template.get(template_id) 434 def record = [] 435 def failed = [ :]427 def record = [] // list of entities and the read values 428 def failed = [] // list of failed columns [mappingcolumn] with the value which couldn't be mapped into the entity 436 429 437 430 // Initialize all possible entities with the chosen template … … 480 473 } // end switch 481 474 } catch (IllegalArgumentException iae) { 482 // store the mapping column and the cell in an array 483 failed.put(mc, cell) 484 //println "failed ("+mc.templatefieldtype+"`" + value + "`" 475 // store the mapping column and value which failed 476 def temp = new MappingColumn() 477 temp.properties = mc.properties 478 temp.value = value 479 failed.add(temp) 485 480 } 486 481 } // end 487 482 } // end for 488 489 // add the failed columns to the record (might also be just an empty map if nothing failed) 490 // a failed column means that using the entity.setFieldValue() threw an exception 491 //record.add(failed) 492 return record 483 484 // a failed column means that using the entity.setFieldValue() threw an exception 485 return [record, failed] 493 486 } 494 487
Note: See TracChangeset
for help on using the changeset viewer.