Changeset 1603
- Timestamp:
- Mar 8, 2011, 5:10:09 PM (10 years ago)
- Location:
- trunk/grails-app
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/controllers/dbnp/importer/ImporterController.groovy
r1599 r1603 106 106 flow.page = 1 107 107 flow.studies = Study.findAllWhere(owner: authenticationService.getLoggedInUser()) 108 flow.importer_fuzzymatching="false"109 110 success() 111 } 112 113 on("refresh") { 114 115 116 117 118 119 flash.importer_params = params 120 121 122 123 124 108 flow.importer_fuzzymatching = "false" 109 110 success() 111 } 112 113 on("refresh") { 114 115 if (params.entity) { 116 flash.importer_datatemplates = Template.findAllByEntity(gdtService.getInstanceByEntity(params.entity.decodeURL())) 117 } 118 119 flash.importer_params = params 120 121 // If the file already exists an "existing*" string is added, but we don't 122 // want that after a refresh of the first step in the import wizard, so remove 123 // that string 124 flash.importer_params.importfile = params.importfile.replace('existing*', '') 125 125 126 126 success() … … 129 129 on("next") { 130 130 flash.wizardErrors = [:] 131 flash.importer_params = params 132 flash.importer_params.importfile = params.importfile.replace('existing*', '') 133 134 if (params.entity) { 135 flash.importer_datatemplates = Template.findAllByEntity(gdtService.getInstanceByEntity(params.entity.decodeURL())) 136 } 131 flash.importer_params = params 132 flash.importer_params.importfile = params.importfile.replace('existing*', '') 133 134 if (params.entity) { 135 flash.importer_datatemplates = Template.findAllByEntity(gdtService.getInstanceByEntity(params.entity.decodeURL())) 136 def importer_entity_type = gdtService.decryptEntity(params.entity.decodeURL()).toString().split(/\./) 137 flow.importer_entity_type = importer_entity_type[importer_entity_type.size()-1] 138 } 137 139 138 140 // Study selected? … … 141 143 // Trying to import data into an existing study? 142 144 if (flow.importer_study) 143 if (flow.importer_study.canWrite(authenticationService.getLoggedInUser())) 144 fileImportPage(flow, flash, params) ? success() : error() 145 if (flow.importer_study.canWrite(authenticationService.getLoggedInUser())) 146 fileImportPage(flow, flash, params) ? success() : error() 145 147 else { 146 148 log.error ".importer wizard wrong permissions" … … 161 163 onRender { 162 164 log.info ".import wizard properties page" 163 164 165 166 def template = Template.get(flow.importer_template_id) 165 167 166 168 // Grom a development message 167 169 if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial: pages/_page_two.gsp".grom() 168 170 169 171 flow.importer_importmappings = ImportMapping.findAllByTemplate(template) 170 172 171 173 flow.page = 2 172 174 success() 173 175 } 174 175 176 177 178 179 180 181 propertiesSaveImportMappingPage(flow, flash, params) 182 183 184 185 186 187 188 flow.importer_fuzzymatching="true" else189 flow.importer_fuzzymatching="false"190 191 176 on("refresh") { 177 def template = Template.get(flow.importer_template_id) 178 flow.importer_importmappings = ImportMapping.findAllByTemplate(template) 179 180 // a name was given to the current property mapping, try to store it 181 if (params.mappingname) { 182 flash.importer_columnproperty = params.columnproperty 183 propertiesSaveImportMappingPage(flow, flash, params) 184 } else // trying to load an existing import mapping 185 if (params.importmapping_id) { 186 propertiesLoadImportMappingPage(flow, flash, params) 187 } 188 189 if (params.fuzzymatching == "true") 190 flow.importer_fuzzymatching = "true" else 191 flow.importer_fuzzymatching = "false" 192 193 success() 192 194 }.to "pageTwo" 193 195 194 196 on("next") { 195 flow.importer_fuzzymatching="false"196 if (propertiesPage(flow, flash, params)) { 197 flow.importer_fuzzymatching = "false" 198 if (propertiesPage(flow, flash, params)) { 197 199 success() 198 200 } else { … … 260 262 // Grom a development message 261 263 if (pluginManager.getGrailsPlugin('grom')) ".persisting instances to the database...".grom() 262 263 264 264 265 // Always delete the uploaded file in the save step to be sure it doesn't reside there anymore 266 fileService.delete(flow.importer_importedfile) 265 267 266 268 // Save all entities 267 if (saveEntities(flow, params)) { 269 if (saveEntities(flow, params)) { 268 270 success() 269 271 } else { … … 308 310 } 309 311 310 311 render (view:"common/_propertiesManager")312 312 def propertiesManager = { 313 render(view: "common/_propertiesManager") 314 } 313 315 314 316 /** … … 321 323 // fetch all templates for a specific entity 322 324 def templates = Template.findAllByEntity(gdtService.getInstanceByEntity(params.entity.decodeURL())) 323 324 325 325 326 // render as JSON … … 334 335 * @returns boolean true if correctly validated, otherwise false 335 336 */ 336 boolean fileImportPage(flow, flash, params) { 337 boolean fileImportPage(flow, flash, params) { 337 338 def importedfile = fileService.get(params['importfile']) 338 flow.importer_importedfile = params['importfile'] 339 340 341 342 343 344 345 346 347 348 349 350 if (params.entity && params.template_id) { 351 352 353 354 355 356 357 358 339 flow.importer_importedfile = params['importfile'] 340 341 if (importedfile.exists()) { 342 try { 343 session.importer_workbook = importerService.getWorkbook(new FileInputStream(importedfile)) 344 } catch (Exception e) { 345 log.error ".importer wizard could not load file: " + e 346 this.appendErrorMap(['error': "Wrong file (format), the importer requires an Excel file as input"], flash.wizardErrors) 347 return false 348 } 349 } 350 351 if (params.entity && params.template_id) { 352 353 try { 354 session.importer_workbook = importerService.getWorkbook(new FileInputStream(importedfile)) 355 } catch (Exception e) { 356 log.error ".importer wizard could not load file: " + e 357 this.appendErrorMap(['error': "Excel file required as input"], flash.wizardErrors) 358 return false 359 } 359 360 360 361 def selectedentities = [] … … 370 371 flow.importer_datamatrix_start = params.datamatrix_start.toInteger() - 1 // 0 == first row 371 372 flow.importer_headerrow = params.headerrow.toInteger() 372 373 flow.importer_entity = gdtService.cachedEntities.find{ it.entity==entityName }374 373 flow.importer_entityclass = entityClass 374 flow.importer_entity = gdtService.cachedEntities.find { it.entity == entityName } 375 375 376 // Get the header from the Excel file using the arguments given in the first step of the wizard 376 377 flow.importer_header = importerService.getHeader(session.importer_workbook, … … 393 394 394 395 395 396 397 398 } 399 400 401 402 403 396 log.error ".importer wizard not all fields are filled in" 397 this.appendErrorMap(['error': "Not all fields are filled in, please fill in or select all fields"], flash.wizardErrors) 398 return false 399 } 400 401 /** 402 * Load an existing import mapping 403 * 404 * @param Map LocalAttributeMap (the flow scope) 404 405 * @param Map GrailsParameterMap (the flow parameters = form data) 405 406 * @returns boolean true if correctly validated, otherwise false 406 407 */ 407 408 409 410 411 412 413 414 415 flow.importer_header[mappingcolumn.index.toInteger()] = mappingcolumn 416 417 418 419 420 421 422 408 boolean propertiesLoadImportMappingPage(flow, flash, params) { 409 def im = ImportMapping.get(params.importmapping_id.toInteger()) 410 im.refresh() 411 412 im.mappingcolumns.each { mappingcolumn -> 413 //def mc = new MappingColumn() 414 //mc.properties = mappingcolumn.properties 415 416 flow.importer_header[mappingcolumn.index.toInteger()] = mappingcolumn 417 } 418 } 419 420 /** 421 * Save the properties as an import mapping. 422 * 423 * @param Map LocalAttributeMap (the flow scope) 423 424 * @param Map GrailsParameterMap (the flow parameters = form data) 424 425 * @returns boolean true if correctly validated, otherwise false 425 426 */ 426 427 428 427 boolean propertiesSaveImportMappingPage(flow, flash, params) { 428 flash.wizardErrors = [:] 429 def isPreferredIdentifier = false 429 430 430 431 // Find actual Template object from the chosen template name 431 432 def template = Template.get(flow.importer_template_id) 432 433 434 def im = new ImportMapping(name:params.mappingname, entity: flow.importer_entityclass, template:template).save() 433 434 // Create new ImportMapping instance and persist it 435 def im = new ImportMapping(name: params.mappingname, entity: flow.importer_entityclass, template: template).save() 435 436 436 437 params.columnproperty.index.each { columnindex, property -> … … 438 439 // This should be inside the closure because in some cases in the advanced importer, the fields can have different target entities 439 440 //def entityClass = gdtService.getInstanceByEntityName(flow.importer_header[columnindex.toInteger()].entity.getName()) 440 def entityObj = flow.importer_entityclass.newInstance(template:template)441 442 443 444 445 entityObj.giveFields().each { 441 def entityObj = flow.importer_entityclass.newInstance(template: template) 442 443 def dontimport = (property == "dontimport") ? true : false 444 445 // Loop through all fields and find the preferred identifier 446 entityObj.giveFields().each { 446 447 isPreferredIdentifier = (it.preferredIdentifier && (it.name == property)) ? true : false 447 448 } 448 449 449 450 def mc = new MappingColumn (importmapping:im,451 452 property:property,453 index:columnindex,454 entityclass:flow.importer_entityclass,455 templatefieldtype:entityObj.giveFieldType(property),456 457 identifier:isPreferredIdentifier)458 459 460 461 im.addToMappingcolumns(mc) 462 463 464 465 466 467 468 469 470 471 472 im.save(flush:true)473 474 475 476 477 478 479 480 481 482 483 484 } 485 486 487 450 // Create new MappingColumn instance 451 def mc = new MappingColumn(importmapping: im, 452 name: flow.importer_header[columnindex.toInteger()].name, 453 property: property, 454 index: columnindex, 455 entityclass: flow.importer_entityclass, 456 templatefieldtype: entityObj.giveFieldType(property), 457 dontimport: dontimport, 458 identifier: isPreferredIdentifier) 459 460 // Save mappingcolumn 461 if (mc.validate()) { 462 im.addToMappingcolumns(mc) 463 } 464 else { 465 mc.errors.allErrors.each { 466 println it 467 } 468 } 469 470 // Save importmapping 471 if (im.validate()) { 472 try { 473 im.save(flush: true) 474 } catch (Exception e) { 475 //getNextException 476 log.error "importer wizard save importmapping error: " + e 477 } 478 } 479 else { 480 im.errors.allErrors.each { 481 println it 482 } 483 } 484 485 } 486 } 487 488 /** 488 489 * Handle the property mapping page. 489 490 * … … 493 494 */ 494 495 boolean propertiesPage(flow, flash, params) { 495 496 496 flash.wizardErrors = [:] 497 497 498 // Find actual Template object from the chosen template name 498 499 def template = Template.get(flow.importer_template_id) … … 528 529 flow.importer_importeddata = table 529 530 530 531 532 533 534 this.appendErrors(entity, flash.wizardErrors, 'entity_' + entity.getIdentifier() + '_')535 536 537 538 539 531 // loop through all entities to validate them and add them to wizardErrors flash when invalid 532 /*table.each { record -> 533 record.each { entity -> 534 if (!entity.validate()) { 535 this.appendErrors(entity, flash.wizardErrors, 'entity_' + entity.getIdentifier() + '_') 536 } 537 } 538 }*/ 539 540 flow.importer_failedcells = failedcells 540 541 541 542 return true … … 560 561 entity.giveFields().each { field -> 561 562 562 // field is a date field, try to set it with the value, if someone enters a non-date value it throws 563 // an error, this should be caught to prevent a complete breakdown 564 if (field.type == org.dbnp.gdt.TemplateFieldType.DATE) { 565 try { 566 entity.setFieldValue(field.toString(), params["entity_" + entity.getIdentifier() + "_" + field.escapedName()]) 567 } catch (Exception e) { log.error ".importer wizard could not set date field with value: " + 568 params["entity_" + entity.getIdentifier() + "_" + field.escapedName()] 569 } 570 } else 563 // field is a date field, try to set it with the value, if someone enters a non-date value it throws 564 // an error, this should be caught to prevent a complete breakdown 565 if (field.type == org.dbnp.gdt.TemplateFieldType.DATE) { 566 try { 567 entity.setFieldValue(field.toString(), params["entity_" + entity.getIdentifier() + "_" + field.escapedName()]) 568 } catch (Exception e) { 569 log.error ".importer wizard could not set date field with value: " + 570 params["entity_" + entity.getIdentifier() + "_" + field.escapedName()] 571 } 572 } else 571 573 572 574 // field of type ontology and value "#invalidterm"? … … 578 580 if (field.type == org.dbnp.gdt.TemplateFieldType.ONTOLOGYTERM && 579 581 params["entity_" + entity.getIdentifier() + "_" + field.escapedName()] != "#invalidterm") { 580 if (entity) removeFailedCell(flow.importer_failedcells, entity, field) 582 if (entity) removeFailedCell(flow.importer_failedcells, entity, field) 581 583 entity.setFieldValue(field.toString(), params["entity_" + entity.getIdentifier() + "_" + field.escapedName()]) 582 584 } 583 585 else 584 586 585 587 if (field.type == org.dbnp.gdt.TemplateFieldType.STRINGLIST && 586 588 params["entity_" + entity.getIdentifier() + "_" + field.escapedName()] != "#invalidterm") { 587 if (entity) removeFailedCell(flow.importer_failedcells, entity, field) 589 if (entity) removeFailedCell(flow.importer_failedcells, entity, field) 588 590 entity.setFieldValue(field.toString(), params["entity_" + entity.getIdentifier() + "_" + field.escapedName()]) 589 590 591 } else 592 if (field.type == org.dbnp.gdt.TemplateFieldType.STRINGLIST && 591 593 params["entity_" + entity.getIdentifier() + "_" + field.escapedName()] == "#invalidterm" 592 594 ) { … … 628 630 def removeFailedCell(failedcells, entity, field) { 629 631 // Valid entity, remove it from failedcells 630 632 def entityidfield = "entity_" + entity.getIdentifier() + "_" + field.name.toLowerCase() 631 633 632 634 failedcells.each { record -> … … 634 636 635 637 636 record.importcells.each { cell -> 638 record.importcells.each { cell -> 637 639 // remove the cell from the failed cells session 638 640 if (cell.entityidentifier != entityidfield) { … … 661 663 //def (validatedSuccesfully, updatedEntities, failedToPersist) = 662 664 try { 663 importerService.saveDatamatrix(flow.importer_study, flow.importer_ importeddata, authenticationService, log)665 importerService.saveDatamatrix(flow.importer_study, flow.importer_entity_type, flow.importer_importeddata, authenticationService, log) 664 666 } catch (Exception e) { 665 667 log.error ".import wizard saveEntities error\n" + e.dump() -
trunk/grails-app/services/dbnp/importer/ImporterService.groovy
r1600 r1603 727 727 * @param datamatrix two dimensional array containing entities with values read from Excel file 728 728 */ 729 static saveDatamatrix(Study study, datamatrix, authenticationService, log) {729 static saveDatamatrix(Study study, importerEntityType, datamatrix, authenticationService, log) { 730 730 def validatedSuccesfully = 0 731 731 def entitystored = null 732 732 733 733 // Study passed? Sync data 734 if (study != null ) study.refresh()734 if (study != null && importerEntityType != 'Study') study.refresh() 735 735 736 736 // go through the data matrix, read every record and validate the entity and try to persist it … … 741 741 entity.owner = authenticationService.getLoggedInUser() 742 742 743 if ( study.validate()) {743 if (entity.validate()) { 744 744 if (!entity.save(flush:true)) { 745 745 log.error ".importer wizard, study could not be saved: " + entity … … 747 747 } 748 748 } else { 749 println "---"750 study.errors.getAllErrors().each {751 println it.dump()752 }753 println "---//"754 749 log.error ".importer wizard, study could not be validated: " + entity 755 750 throw new Exception('.importer wizard, study could not be validated: ' + entity) … … 787 782 788 783 // validate study 789 if (study.validate()) { 790 if (!study.save(flush: true)) { 791 //this.appendErrors(flow.study, flash.wizardErrors) 792 throw new Exception('.importer wizard [saveDatamatrix] error while saving study') 784 if (importerEntityType != 'Study') { 785 if (study.validate()) { 786 if (!study.save(flush: true)) { 787 //this.appendErrors(flow.study, flash.wizardErrors) 788 throw new Exception('.importer wizard [saveDatamatrix] error while saving study') 789 } 790 } else { 791 throw new Exception('.importer wizard [saveDatamatrix] study does not validate') 793 792 } 794 } else {795 throw new Exception('.importer wizard [saveDatamatrix] study does not validate')796 793 } 797 794
Note: See TracChangeset
for help on using the changeset viewer.