Changeset 1411 for trunk/grails-app/services
- Timestamp:
- Jan 18, 2011, 3:51:25 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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) {
Note: See TracChangeset
for help on using the changeset viewer.