Changeset 1411 for trunk/grails-app
- Timestamp:
- Jan 18, 2011, 3:51:25 PM (10 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/importer/ImporterController.groovy
r1388 r1411 191 191 success() 192 192 } else { 193 println "properties arewrong"193 log.error ".import wizard, properties are set wrong" 194 194 error() 195 195 } … … 273 273 if (pluginManager.getGrailsPlugin('grom')) ".persisting instances to the database...".grom() 274 274 275 if (saveEntities(flow, params)) { 275 if (saveEntities(flow, params)) { 276 276 success() 277 } else { 277 } else { 278 278 log.error ".import wizard imported error, something went wrong showing the imported entities" 279 279 //throw Exception … … 281 281 } catch (Exception e) { 282 282 // put your error handling logic in 283 // here 283 // here 284 log.error ".import wizard, could not save entities:\n" + e.dump() 284 285 flow.page = 4 285 286 error() … … 543 544 } 544 545 545 boolean saveEntities(flow, params) { 546 def (validatedSuccesfully, updatedEntities, failedToPersist) = ImporterService.saveDatamatrix(flow.importer_study, flow.importer_importeddata) 547 548 flow.importer_validatedsuccesfully = validatedSuccesfully 549 flow.importer_failedtopersist = failedToPersist 550 flow.imported_updatedentities = updatedEntities 551 flow.importer_totalrows = flow.importer_importeddata.size 552 flow.importer_referer = "" 546 boolean saveEntities(flow, params) { 547 //def (validatedSuccesfully, updatedEntities, failedToPersist) = 548 try { 549 ImporterService.saveDatamatrix(flow.importer_study, flow.importer_importeddata) 550 551 } 552 catch (Exception e) { 553 log.error ".import wizard saveEntities error\n" + e.dump() 554 } 555 556 //flow.importer_validatedsuccesfully = validatedSuccesfully 557 //flow.importer_failedtopersist = failedToPersist 558 //flow.imported_updatedentities = updatedEntities 559 //flow.importer_totalrows = flow.importer_importeddata.size 560 //flow.importer_referer = "" 553 561 554 562 return true … … 596 604 def errors = [:] 597 605 object.errors.getAllErrors().each() { error -> 598 // error.codes.each() { code -> println code } 599 println "errors is " + error 606 // error.codes.each() { code -> println code } 600 607 601 608 // generally speaking g.message(...) should work, -
trunk/grails-app/services/dbnp/importer/ImporterService.groovy
r1402 r1411 61 61 def datamatrix_celldata = df.formatCellValue(sheet.getRow(datamatrix_start).getCell(columnindex)) 62 62 def datamatrix_cell = sheet.getRow(datamatrix_start).getCell(columnindex) 63 63 def headercell = sheet.getRow(headerrow-1+sheet.getFirstRowNum()).getCell(columnindex) 64 64 def tft = TemplateFieldType.STRING //default templatefield type 65 65 … … 210 210 def importData(template_id, Workbook wb, int sheetindex, int rowindex, mcmap) { 211 211 def sheet = wb.getSheetAt(sheetindex) 212 def template = Template.get(template_id) 212 213 def table = [] 213 214 def failedcells = [] // list of records 214 215 215 216 // walk through all rows and fill the table with records 216 217 (rowindex..sheet.getLastRowNum()).each { i -> 217 218 // Create an entity record based on a row read from Excel and store the cells which failed to be mapped 218 def (record, failed) = createRecord(template _id, sheet.getRow(i), mcmap)219 def (record, failed) = createRecord(template, sheet.getRow(i), mcmap) 219 220 220 221 // Add record with entity and its values to the table … … 276 277 def validatedSuccesfully = 0 277 278 def entitystored = null 278 def failedtopersist = [] 279 def persisterrors = [] 280 def updatedentities = [] 281 279 282 280 // Study passed? Sync data 283 281 if (study!=null) study.refresh() 282 283 //study.subjects.each { it.refresh() } 284 284 285 285 // go through the data matrix, read every record and validate the entity and try to persist it 286 286 datamatrix.each { record -> 287 record.each { entity -> 287 record.each { entity -> 288 288 switch (entity.getClass()) { 289 289 case Study : log.info "Persisting Study `" + entity + "`: " 290 290 entity.owner = AuthenticationService.getLoggedInUser() 291 if (persistEntity(entity)) validatedSuccesfully++; 292 else failedtopersist.add(entity) 291 persistEntity(entity) 293 292 break 294 293 case Subject : log.info "Persisting Subject `" + entity + "`: " 295 294 296 295 // is the current entity not already in the database? 297 entitystored = isEntityStored(entity)296 //entitystored = isEntityStored(entity) 298 297 299 298 // this entity is new, so add it to the study 300 if (entitystored==null) study.addToSubjects(entity)301 else { // existing entity, so update it302 updateEntity(entitystored, entity)303 updatedentities.add(entity)304 }305 306 if (persistEntity(study)) validatedSuccesfully++;307 else failedtopersist.add(entity)299 //if (entitystored==null) 300 study.addToSubjects(entity) 301 302 /*else { // existing entity, so update it 303 updateEntity(entitystored, entity) 304 updatedentities.add(entity) 305 }*/ 306 308 307 break 309 308 case Event : log.info "Persisting Event `" + entity + "`: " 310 309 study.addToEvents(entity) 311 if (persistEntity(entity)) validatedSuccesfully++; 312 else failedtopersist.add(entity) 310 persistEntity(entity) 313 311 break 314 312 case Sample : log.info "Persisting Sample `" + entity +"`: " 315 313 316 // is this sample validatable (sample name unique for example?) 317 if (entity.validate()) { 318 study.addToSamples(entity) 319 if (persistEntity(study)) validatedSuccesfully++; 320 } else { 321 failedtopersist.add(entity) 322 } 314 // is this sample validatable (sample name unique for example?) 315 study.addToSamples(entity) 316 persistEntity(study) 317 323 318 break 324 319 case SamplingEvent: log.info "Persisting SamplingEvent `" + entity + "`: " 325 320 study.addToSamplingEvents(entity) 326 if (persistEntity(entity)) validatedSuccesfully++; 327 else failedtopersist.add(entity) 321 persistEntity(entity) 328 322 break 329 default : log.info "Skipping persisting of `" + entity.getclass() +"`" 330 failedtopersist.add(entity) 323 default : log.info "Skipping persisting of `" + entity.getclass() +"`" 331 324 break 332 325 } // end switch 333 326 } // end record 334 } // end datamatrix 335 return [validatedSuccesfully, updatedentities, failedtopersist] 327 } // end datamatrix 328 329 330 if (study!=null) persistEntity(study) 331 332 //return [validatedSuccesfully, updatedentities, failedtopersist] 333 //return [0,0,0] 336 334 } 337 335 … … 391 389 boolean persistEntity(entity) { 392 390 log.info ".import wizard persisting ${entity}" 393 394 if (entity.save(flush:true)) 395 return true 396 else { // if save was unsuccesful 397 entity.errors.allErrors.each { 398 log.error ".import wizard: " + it 399 } 400 return false 401 } 391 392 try { 393 entity.save(flush:true) 394 return true 395 396 } catch (Exception e) { 397 def session = sessionFactory.currentSession 398 session.setFlushMode(org.hibernate.FlushMode.MANUAL) 399 log.error ".import wizard, failed to save entity:\n" + org.apache.commons.lang.exception.ExceptionUtils.getRootCauseMessage(e) 400 } 401 402 return true 402 403 } 403 404 … … 408 409 * @param excelrow POI based Excel row containing the cells 409 410 * @param mcmap map containing MappingColumn objects 410 411 * @return list of entities and list of failed cells 411 412 */ 412 def createRecord(template_id, Row excelrow, mcmap) { 413 def df = new DataFormatter() 414 def template = Template.get(template_id) 413 def createRecord(template, Row excelrow, mcmap) { 414 def df = new DataFormatter() 415 415 def tft = TemplateFieldType 416 416 def record = [] // list of entities and the read values … … 441 441 // which entity does the current cell (field) belong to? 442 442 switch (mc.entity) { 443 case Study: // does the entity already exist in the record? If not make it so.444 (record.any {it.getClass() == mc.entity}) ? 0 : record.add(study)445 446 447 case Subject: (record.any {it.getClass() == mc.entity}) ? 0 : record.add(subject)448 449 450 case SamplingEvent: (record.any {it.getClass() == mc.entity}) ? 0 : record.add(samplingEvent)451 452 453 case Event: (record.any {it.getClass() == mc.entity}) ? 0 : record.add(event)454 455 456 case Sample: (record.any {it.getClass() == mc.entity}) ? 0 : record.add(sample)457 458 459 case Object: // don't import460 443 case Study: // does the entity already exist in the record? If not make it so. 444 (record.any {it.getClass() == mc.entity}) ? 0 : record.add(study) 445 study.setFieldValue(mc.property, value) 446 break 447 case Subject: (record.any {it.getClass() == mc.entity}) ? 0 : record.add(subject) 448 subject.setFieldValue(mc.property, value) 449 break 450 case SamplingEvent: (record.any {it.getClass() == mc.entity}) ? 0 : record.add(samplingEvent) 451 samplingEvent.setFieldValue(mc.property, value) 452 break 453 case Event: (record.any {it.getClass() == mc.entity}) ? 0 : record.add(event) 454 event.setFieldValue(mc.property, value) 455 break 456 case Sample: (record.any {it.getClass() == mc.entity}) ? 0 : record.add(sample) 457 sample.setFieldValue(mc.property, value) 458 break 459 case Object: // don't import 460 break 461 461 } // end switch 462 462 } catch (Exception iae) { -
trunk/grails-app/views/importer/pages/_final_page.gsp
r1394 r1411 23 23 </p> 24 24 25 ${importer_validatedsuccesfully} of ${importer_totalrows} rows were imported succesfully. 26 27 <g:if test="${importer_failedtopersist}"> 28 <p>The following entities could not be persisted:</p> 29 <table> 30 <g:each var="entity" in="${importer_failedtopersist}"> 31 <tr> 32 <g:each var="field" in="${entity.giveFields()}"> 33 <td> 34 <g:if test="${entity.getFieldValue(field.name)!=null}"> 35 <b>${field.name}</b> ${entity.getFieldValue(field.name)} 36 </g:if> 37 <g:else><b>${field.name}</b> × 38 </g:else> 39 </td> 40 </g:each> 41 <td> 42 <g:each var="error" in="${entity.errors.allErrors}"> 43 <b>error</b>: field `${error.getField()}` rejected value: ${error.getRejectedValue()}</b> 44 </g:each> 45 </td> 46 </tr> 47 </g:each> 48 </table> 49 </g:if> 25 All rows were imported succesfully. 50 26 51 27 <g:if test="${importer_referer}">
Note: See TracChangeset
for help on using the changeset viewer.