- Timestamp:
- Mar 29, 2011, 2:30:12 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/studycapturing/SimpleWizardController.groovy
r1621 r1678 22 22 import dbnp.importer.ImportRecord 23 23 import dbnp.importer.MappingColumn 24 import org.hibernate.SessionFactory; 24 25 25 26 @Secured(['IS_AUTHENTICATED_REMEMBERED']) … … 29 30 def importerService 30 31 def gdtService = new GdtService() 32 def sessionFactory 31 33 32 34 /** … … 133 135 def filename = params.get( 'importfile' ); 134 136 137 handleSampleForm( flow.study, params, flow ) 138 135 139 // Handle 'existing*' in front of the filename. This is put in front to make a distinction between 136 140 // an already uploaded file test.txt (maybe moved to some other directory) and a newly uploaded file test.txt … … 142 146 filename = filename[9..-1] 143 147 144 // Refresh the templates, since the template editor has been opened 148 flow.sampleForm.importFile = filename 149 150 // Refresh the templates, since the template editor has been opened. To make this happen 151 // the hibernate session has to be cleared first 152 sessionFactory.getCurrentSession().clear(); 153 145 154 flow.templates = [ 146 155 'Sample': Template.findAllByEntity( Sample.class ), … … 149 158 'SamplingEvent': Template.findAllByEntity( SamplingEvent.class ) 150 159 ]; 151 152 flow.sampleForm = [ importFile: filename ] 160 153 161 }.to "samples" 154 162 on("previous").to "returnFromSamples" … … 256 264 }.to "overview" 257 265 on( "previous" ).to "returnFromAssays" 258 on("refresh") { handleAssays( flow.assay, params, flow ); success() }.to "assays" 266 on("refresh") { 267 // Refresh the templates, since the template editor has been opened. To make this happen 268 // the hibernate session has to be cleared first 269 sessionFactory.getCurrentSession().clear(); 270 271 handleAssays( flow.assay, params, flow ); 272 273 flow.assay?.template?.refresh() 274 success() 275 }.to "assays" 259 276 } 260 277 … … 469 486 filename = filename[9..-1] 470 487 471 def sampleTemplateId = params.long( 'sample_template_id' ) 472 def subjectTemplateId = params.long( 'subject_template_id' ) 473 def eventTemplateId = params.long( 'event_template_id' ) 474 def samplingEventTemplateId = params.long( 'samplingEvent_template_id' ) 475 488 handleSampleForm( study, params, flow ); 489 490 // Check whether the template exists 491 if (!flow.sampleForm.template.Sample ){ 492 log.error ".simple study wizard not all fields are filled in (sample template) " 493 flash.error = "No template was chosen. Please choose a template for the samples you provided." 494 return false 495 } 496 476 497 // These fields have been removed from the form, so will always contain 477 498 // their default value. The code however remains like this for future use. … … 479 500 int dataMatrixStart = (params.int( 'datamatrix_start' ) ?: 2 ) 480 501 int headerRow = (params.int( 'headerrow' ) ?: 1 ) 502 503 flow.sampleForm.sheetIndex = sheetIndex; 504 flow.sampleForm.dataMatrixStart = dataMatrixStart 505 flow.sampleForm.headerRow = headerRow 506 507 def importedfile = fileService.get( filename ) 508 def workbook 509 if (importedfile.exists()) { 510 try { 511 workbook = importerService.getWorkbook(new FileInputStream(importedfile)) 512 } catch (Exception e) { 513 log.error ".simple study wizard could not load file: " + e 514 flash.error = "The given file doesn't seem to be an excel file. Please provide an excel file for entering samples."; 515 return false 516 } 517 } else { 518 log.error ".simple study wizard no file given"; 519 flash.error = "No file was given. Please provide an excel file for entering samples."; 520 return false; 521 } 522 523 if( !workbook ) { 524 log.error ".simple study wizard could not load file into a workbook" 525 flash.error = "The given file doesn't seem to be an excel file. Please provide an excel file for entering samples."; 526 return false 527 } 528 529 def selectedentities = [] 530 531 if( !excelChecks( workbook, sheetIndex, headerRow, dataMatrixStart ) ) 532 return false; 533 534 // Get the header from the Excel file using the arguments given in the first step of the wizard 535 def importerHeader; 536 def importerDataMatrix; 537 538 try { 539 importerHeader = importerService.getHeader(workbook, 540 sheetIndex - 1, // 0 == first sheet 541 headerRow, // 1 == first row :s 542 dataMatrixStart - 1, // 0 == first row 543 Sample.class) 544 545 importerDataMatrix = importerService.getDatamatrix( 546 workbook, 547 importerHeader, 548 sheetIndex - 1, // 0 == first sheet 549 dataMatrixStart - 1, // 0 == first row 550 5) 551 } catch( Exception e ) { 552 // An error occurred while reading the excel file. 553 log.error ".simple study wizard error while reading the excel file"; 554 e.printStackTrace(); 555 556 // Show a message to the user 557 flash.error = "An error occurred while reading the excel file. Have you provided the right sheet number and row numbers. Contact your system administrator if this problem persists."; 558 return false; 559 } 560 561 // Match excel columns with template fields 562 def fieldNames = []; 563 flow.sampleForm.template.each { template -> 564 if( template.value ) { 565 def fields = template.value.entity.giveDomainFields() + template.value.getFields(); 566 fields.each { field -> 567 if( !field.entity ) 568 field.entity = template.value.entity 569 570 fieldNames << field 571 } 572 } 573 } 574 importerHeader.each { mc -> 575 def bestfit = importerService.mostSimilar( mc.name, fieldNames, 0.8); 576 if( bestfit ) { 577 // Remove this fit from the list 578 fieldNames.remove( bestfit ); 579 580 mc.entityclass = bestfit.entity 581 mc.property = bestfit.name 582 } 583 } 584 585 // Save read excel data into session 586 def dataMatrix = []; 587 def df = new DataFormatter(); 588 importerDataMatrix.each { 589 dataMatrix << it.collect{ it ? df.formatCellValue(it) : "" } 590 } 591 592 flow.excel = [ 593 filename: filename, 594 sheetIndex: sheetIndex, 595 dataMatrixStart: dataMatrixStart, 596 headerRow: headerRow, 597 data: [ 598 header: importerHeader, 599 dataMatrix: dataMatrix 600 ] 601 ] 602 603 return true 604 } 605 606 /** 607 * Copies data from the submitted sample form to the flow 608 * @param study 609 * @param params 610 * @param flow 611 * @return 612 */ 613 protected def handleSampleForm( study, params, flow ) { 614 def sampleTemplateId = params.long( 'sample_template_id' ) 615 def subjectTemplateId = params.long( 'subject_template_id' ) 616 def eventTemplateId = params.long( 'event_template_id' ) 617 def samplingEventTemplateId = params.long( 'samplingEvent_template_id' ) 481 618 482 619 // Save form data in session 483 620 flow.sampleForm = [ 484 importFile: filename,485 621 templateId: [ 486 622 'Sample': sampleTemplateId, … … 495 631 'SamplingEvent': samplingEventTemplateId ? Template.get( samplingEventTemplateId ) : null 496 632 ], 497 sheetIndex: sheetIndex,498 dataMatrixStart: dataMatrixStart,499 headerRow: headerRow500 633 ]; 501 502 // Check whether the template exists503 if (!sampleTemplateId || !Template.get( sampleTemplateId ) ){504 log.error ".simple study wizard not all fields are filled in: " + sampleTemplateId505 flash.error = "No template was chosen. Please choose a template for the samples you provided."506 return false507 }508 509 def importedfile = fileService.get( filename )510 def workbook511 if (importedfile.exists()) {512 try {513 workbook = importerService.getWorkbook(new FileInputStream(importedfile))514 } catch (Exception e) {515 log.error ".simple study wizard could not load file: " + e516 flash.error = "The given file doesn't seem to be an excel file. Please provide an excel file for entering samples.";517 return false518 }519 } else {520 log.error ".simple study wizard no file given";521 flash.error = "No file was given. Please provide an excel file for entering samples.";522 return false;523 }524 525 if( !workbook ) {526 log.error ".simple study wizard could not load file into a workbook"527 flash.error = "The given file doesn't seem to be an excel file. Please provide an excel file for entering samples.";528 return false529 }530 531 def selectedentities = []532 533 if( !excelChecks( workbook, sheetIndex, headerRow, dataMatrixStart ) )534 return false;535 536 // Get the header from the Excel file using the arguments given in the first step of the wizard537 def importerHeader;538 def importerDataMatrix;539 540 try {541 importerHeader = importerService.getHeader(workbook,542 sheetIndex - 1, // 0 == first sheet543 headerRow, // 1 == first row :s544 dataMatrixStart - 1, // 0 == first row545 Sample.class)546 547 importerDataMatrix = importerService.getDatamatrix(548 workbook,549 importerHeader,550 sheetIndex - 1, // 0 == first sheet551 dataMatrixStart - 1, // 0 == first row552 5)553 } catch( Exception e ) {554 // An error occurred while reading the excel file.555 log.error ".simple study wizard error while reading the excel file";556 e.printStackTrace();557 558 // Show a message to the user559 flash.error = "An error occurred while reading the excel file. Have you provided the right sheet number and row numbers. Contact your system administrator if this problem persists.";560 return false;561 }562 563 // Match excel columns with template fields564 def fieldNames = [];565 flow.sampleForm.template.each { template ->566 if( template.value ) {567 def fields = template.value.entity.giveDomainFields() + template.value.getFields();568 fields.each { field ->569 if( !field.entity )570 field.entity = template.value.entity571 572 fieldNames << field573 }574 }575 }576 importerHeader.each { mc ->577 def bestfit = importerService.mostSimilar( mc.name, fieldNames, 0.8);578 if( bestfit ) {579 // Remove this fit from the list580 fieldNames.remove( bestfit );581 582 mc.entityclass = bestfit.entity583 mc.property = bestfit.name584 }585 }586 587 // Save read excel data into session588 def dataMatrix = [];589 def df = new DataFormatter();590 importerDataMatrix.each {591 dataMatrix << it.collect{ it ? df.formatCellValue(it) : "" }592 }593 594 flow.excel = [595 filename: filename,596 sheetIndex: sheetIndex,597 dataMatrixStart: dataMatrixStart,598 headerRow: headerRow,599 data: [600 header: importerHeader,601 dataMatrix: dataMatrix602 ]603 ]604 605 return true606 634 } 607 608 635 609 636 /**
Note: See TracChangeset
for help on using the changeset viewer.