source: trunk/grails-app/controllers/dbnp/studycapturing/SimpleWizardController.groovy @ 1621

Last change on this file since 1621 was 1621, checked in by robert@…, 13 years ago

Fixed some bugs in simple wizard and study view page

  • Property svn:keywords set to Rev Author Date
File size: 36.9 KB
Line 
1/**
2 * SimpleWizardController Controler
3 *
4 * Description of my controller
5 *
6 * @author  your email (+name?)
7 * @since       2010mmdd
8 * @package     ???
9 *
10 * Revision information:
11 * $Rev: 1621 $
12 * $Author: robert@isdat.nl $
13 * $Date: 2011-03-11 10:02:30 +0000 (vr, 11 mrt 2011) $
14 */
15package dbnp.studycapturing
16
17import org.apache.poi.ss.usermodel.DataFormatter
18import org.dbnp.gdt.*
19import grails.plugins.springsecurity.Secured
20import dbnp.authentication.SecUser
21import dbnp.importer.ImportCell
22import dbnp.importer.ImportRecord
23import dbnp.importer.MappingColumn
24
25@Secured(['IS_AUTHENTICATED_REMEMBERED'])
26class SimpleWizardController extends StudyWizardController {
27        def authenticationService
28        def fileService
29        def importerService
30        def gdtService = new GdtService()
31
32        /**
33         * index closure
34         */
35        def index = {
36                if( params.id )
37                        redirect( action: "simpleWizard", id: params.id );
38                else
39                        redirect( action: "simpleWizard" );
40        }
41
42        def simpleWizardFlow = {
43                entry {
44                        action{
45                                flow.study = getStudyFromRequest( params )
46                                if (!flow.study) retrievalError()
47                        }
48                        on("retrievalError").to "handleError"
49                        on("success").to "study"
50                }
51
52                study {
53                        on("next") {
54                                handleStudy( flow.study, params )
55                                if( !validateObject( flow.study ) )
56                                        error()
57                        }.to "decisionState"
58                        on("refresh") { handleStudy( flow.study, params ); }.to "study"
59                        on( "success" ) { handleStudy( flow.study, params ) }.to "study"
60                }
61
62                decisionState {
63                        action {
64                                // Create data in the flow
65                                flow.templates = [
66                                                        'Sample': Template.findAllByEntity( Sample.class ),
67                                                        'Subject': Template.findAllByEntity( Subject.class ),
68                                                        'Event': Template.findAllByEntity( Event.class ),
69                                                        'SamplingEvent': Template.findAllByEntity( SamplingEvent.class )
70                                ];
71                       
72                                flow.encodedEntity = [
73                                                        'Sample': gdtService.encryptEntity( Sample.class.name ),
74                                                        'Subject': gdtService.encryptEntity( Subject.class.name ),
75                                                        'Event': gdtService.encryptEntity( Event.class.name ),
76                                                        'SamplingEvent': gdtService.encryptEntity( SamplingEvent.class.name )
77                                ]
78
79                                if (flow.study.samples)
80                                        checkStudySimplicity(flow.study) ? existingSamples() : complexStudy()
81                                else
82                                        samples()
83                        }
84                        on ("existingSamples").to "startExistingSamples"
85                        on ("complexStudy").to "complexStudy"
86                        on ("samples").to "samples"
87                }
88               
89                startExistingSamples {
90                        action {
91                                def records = importerService.getRecords( flow.study );
92                                flow.records = records
93                                flow.templateCombinations = records.templateCombination.unique()
94                                success();
95                        }
96                        on( "success" ).to "existingSamples"
97                }
98
99                existingSamples {
100                        on("next") {
101                                handleExistingSamples( flow.study, params, flow ) ? success() : error()
102                        }.to "startAssays"
103                        on("previous").to "study"
104                        on("update") {
105                                handleExistingSamples( flow.study, params, flow ) ? success() : error()
106                        }.to "samples"
107
108                        on("skip").to "startAssays"
109                }
110
111                complexStudy {
112                        on("save").to "save"
113                        on("previous").to "study"
114                }
115
116                samples {
117                        on("next") {
118                                if( !handleSamples( flow.study, params, flow ) )
119                                        return error();
120                               
121                                // Add domain fields for all entities
122                                flow.domainFields = [:]
123                               
124                                flow.templates.each { 
125                                        if( it.value ) {
126                                                flow.domainFields[ it.key ] = it.value[0].entity.giveDomainFields();
127                                        }
128                                }
129                               
130                                println flow.sampleForm.template
131                        }.to "columns"
132                        on("refresh") {
133                                def filename = params.get( 'importfile' );
134               
135                                // Handle 'existing*' in front of the filename. This is put in front to make a distinction between
136                                // an already uploaded file test.txt (maybe moved to some other directory) and a newly uploaded file test.txt
137                                // still being in the temporary directory.
138                                // This import step doesn't have to make that distinction, since all files remain in the temporary directory.
139                                if( filename == 'existing*' )
140                                        filename = '';
141                                else if( filename[0..8] == 'existing*' )
142                                        filename = filename[9..-1]
143                               
144                                // Refresh the templates, since the template editor has been opened
145                                flow.templates = [
146                                                'Sample': Template.findAllByEntity( Sample.class ),
147                                                'Subject': Template.findAllByEntity( Subject.class ),
148                                                'Event': Template.findAllByEntity( Event.class ),
149                                                'SamplingEvent': Template.findAllByEntity( SamplingEvent.class )
150                                ];
151                                                                               
152                                flow.sampleForm = [ importFile: filename ]
153                        }.to "samples"
154                        on("previous").to "returnFromSamples"
155                        on("study").to "study"
156                        on("skip").to "startAssays"
157                }
158
159                returnFromSamples {
160                        action {
161                                flow.study.samples ? existingSamples() : study();
162                        }
163                        on( "existingSamples" ).to "startExistingSamples"
164                        on( "study" ).to "study"
165                }
166               
167                columns {
168                        on( "next" ) {
169                                flow.editImportedData = params.get( 'editAfterwards' ) ? true : false;
170                                handleColumns( flow.study, params, flow ) ? success() : error()
171                        }.to "checkImportedEntities"
172                        on( "previous" ).to "samples" 
173                }
174               
175                checkImportedEntities {
176                        action {
177                                // Only continue to the next page if the information entered is correct
178                                if( flow.editImportedData || flow.imported.numInvalidEntities > 0 ) {
179                                        missingFields();
180                                } else {
181                                        // The import of the excel file has finished. Now delete the excelfile
182                                        if( flow.excel.filename )
183                                                fileService.delete( flow.excel.filename );
184       
185                                        flow.sampleForm = null
186       
187                                        assays();
188                                }
189                        }
190                        on( "missingFields" ).to "missingFields"
191                        on( "assays" ).to "startAssays" 
192                }
193               
194                missingFields {
195                        on( "refresh" ) {
196                                handleMissingFields( flow.study, params, flow );
197                                success();
198                        }.to "missingFields"
199                        on( "next" ) {
200                                if( !handleMissingFields( flow.study, params, flow ) ) {
201                                        return error();
202                                }
203                               
204                                // The import of the excel file has finished. Now delete the excelfile
205                                if( flow.excel.filename )
206                                        fileService.delete( flow.excel.filename );
207
208                                flow.sampleForm = null
209                               
210                                success();
211                        }.to "startAssays"
212                        on( "previous" ) {
213                                // The user goes back to the previous page, so the already imported entities
214                                // (of which some gave an error) should be removed again.
215                                // Add all samples
216                                flow.imported.data.each { record ->
217                                        record.each { entity ->
218                                                if( entity ) {
219                                                        switch( entity.class ) {
220                                                                case Sample:    flow.study.removeFromSamples( entity ); break;
221                                                                case Subject:   flow.study.removeFromSubjects( entity ); break;
222                                                                case Event:             flow.study.removeFromEvents( entity ); break;
223                                                                case SamplingEvent:     flow.study.removeFromSamplingEvents( entity ); break;
224                                                        }
225                                                }
226                                        }
227                                }
228                               
229                                success();
230                        }.to "columns"
231                }
232               
233                startAssays {
234                        action {
235                                if( !flow.assay ) 
236                                        flow.assay = new Assay( parent: flow.study );
237                                       
238                                success();
239                        }
240                        on( "success" ).to "assays"
241                }
242               
243                assays {
244                        on( "next" ) { 
245                                handleAssays( flow.assay, params, flow );
246                                if( !validateObject( flow.assay ) )
247                                        error();
248                         }.to "overview"
249                        on( "skip" ) {
250                                // In case the user has created an assay before he clicked 'skip', it should only be kept if it
251                                // existed before this step
252                                if( flow.assay != null && !flow.assay.id ) {
253                                        flow.remove( "assay" )
254                                }
255
256                         }.to "overview"
257                        on( "previous" ).to "returnFromAssays"
258                        on("refresh") { handleAssays( flow.assay, params, flow ); success() }.to "assays"
259                }
260
261                returnFromAssays {
262                        action {
263                                flow.study.samples ? existingSamples() : samples();
264                        }
265                        on( "existingSamples" ).to "existingSamples"
266                        on( "samples" ).to "samples"
267                }
268               
269                overview { 
270                        on( "save" ).to "saveStudy" 
271                        on( "previous" ).to "startAssays"
272                }
273               
274                saveStudy {
275                        action {
276                                if( flow.assay && !flow.study.assays?.contains( flow.assay ) ) {
277                                        flow.study.addToAssays( flow.assay );
278                                }
279                               
280                                if( flow.study.save( flush: true ) ) {
281                                        // Make sure all samples are attached to all assays
282                                        flow.study.assays.each { assay ->
283                                                def l = []+ assay.samples;
284                                                l.each { sample ->
285                                                        if( sample )
286                                                                assay.removeFromSamples( sample );
287                                                }
288                                                assay.samples?.clear();
289               
290                                                flow.study.samples.each { sample ->
291                                                        assay.addToSamples( sample )
292                                                }
293                                        }
294                       
295                                        flash.message = "Your study is succesfully saved.";
296                                       
297                                        finish();
298                                } else {
299                                        flash.error = "An error occurred while saving your study: <br />"
300                                        flow.study.getErrors().each { flash.error += it.toString() + "<br />"}
301                                       
302                                        // Remove the assay from the study again, since it is still available
303                                        // in the session
304                                        if( flow.assay ) {
305                                                flow.study.removeFromAssays( flow.assay );
306                                                flow.assay.parent = flow.study;
307                                        }
308                                       
309                                        overview();
310                                }
311                        }
312                        on( "finish" ).to "finish"
313                        on( "overview" ).to "overview"
314                }
315               
316                finish()
317               
318                handleError{
319                        redirect action: "errorPage"
320                }
321        }
322
323        /**
324         * Retrieves the required study from the database or return an empty Study object if
325         * no id is given
326         *
327         * @param params        Request parameters with params.id being the ID of the study to be retrieved
328         * @return                      A study from the database or an empty study if no id was given
329         */
330        protected Study getStudyFromRequest( def params ) {
331                int id = params.int( "id" );
332
333                if( !id ) {
334                        return new Study( title: "New study", owner: authenticationService.getLoggedInUser() );
335                }
336
337                Study s = Study.get( id );
338
339                if( !s ) {
340                        flash.error = "No study found with given id";
341                        return null;
342                }
343                if( !s.canWrite( authenticationService.getLoggedInUser() ) ) {
344                        flash.error = "No authorization to edit this study."
345                        return null;
346                }
347
348                return s
349        }
350
351        /**
352         * Handles study input
353         * @param study         Study to update
354         * @param params        Request parameter map
355         * @return                      True if everything went OK, false otherwise. An error message is put in flash.error
356         */
357        def handleStudy( study, params ) {
358                // did the study template change?
359                if (params.get('template') && study.template?.name != params.get('template')) {
360                        // set the template
361                        study.template = Template.findByName(params.remove('template'))
362                }
363
364                // does the study have a template set?
365                if (study.template && study.template instanceof Template) {
366                        // yes, iterate through template fields
367                        study.giveFields().each() {
368                                // and set their values
369                                study.setFieldValue(it.name, params.get(it.escapedName()))
370                        }
371                }
372
373                // handle public checkbox
374                if (params.get("publicstudy")) {
375                        study.publicstudy = params.get("publicstudy")
376                }
377
378                // handle publications
379                handleStudyPublications(study, params)
380
381                // handle contacts
382                handleStudyContacts(study, params)
383
384                // handle users (readers, writers)
385                handleStudyUsers(study, params, 'readers')
386                handleStudyUsers(study, params, 'writers')
387
388                return true
389        }
390       
391        /**
392        * Handles the editing of existing samples
393        * @param study          Study to update
394        * @param params         Request parameter map
395        * @return                       True if everything went OK, false otherwise. An error message is put in flash.error
396        */
397   def handleExistingSamples( study, params, flow ) {
398           flash.validationErrors = [];
399
400           def errors = false;
401           
402           // iterate through objects; set field values and validate the object
403           def eventgroups = study.samples.parentEventGroup.findAll { it }
404           def events;
405           if( !eventgroups )
406                   events = []
407           else
408                   events = eventgroups.events?.getAt(0);
409           
410           def objects = [
411                   'Sample': study.samples,
412                   'Subject': study.samples.parentSubject.findAll { it },
413                   'SamplingEvent': study.samples.parentEvent.findAll { it },
414                   'Event': events.flatten().findAll { it }
415           ];
416           objects.each {
417                   def type = it.key;
418                   def entities = it.value;
419                   
420                   entities.each { entity ->
421                           // iterate through entity fields
422                           entity.giveFields().each() { field ->
423                                   def value = params.get( type.toLowerCase() + '_' + entity.getIdentifier() + '_' + field.escapedName())
424
425                                   // set field value; name cannot be set to an empty value
426                                   if (field.name != 'name' || value) {
427                                           log.info "setting "+field.name+" to "+value
428                                           entity.setFieldValue(field.name, value)
429                                   }
430                           }
431                           
432                           // has the template changed?
433                           def templateName = params.get(type.toLowerCase() + '_' + entity.getIdentifier() + '_template')
434                           if (templateName && entity.template?.name != templateName) {
435                                   entity.template = Template.findByName(templateName)
436                           }
437   
438                           // validate sample
439                           if (!entity.validate()) {
440                                   errors = true;
441                                   
442                                   def entityName = entity.class.name[ entity.class.name.lastIndexOf( "." ) + 1 .. -1 ]
443                                   getHumanReadableErrors( entity ).each {
444                                                flash.validationErrors << [ key: it.key, value: "(" + entityName + ") " + it.value ];
445                                   }
446                           }
447                   }
448           }
449
450           return !errors
451   }
452
453        /**
454         * Handles the upload of sample data
455         * @param study         Study to update
456         * @param params        Request parameter map
457         * @return                      True if everything went OK, false otherwise. An error message is put in flash.error
458         */
459        def handleSamples( study, params, flow ) {
460                def filename = params.get( 'importfile' );
461
462                // Handle 'existing*' in front of the filename. This is put in front to make a distinction between
463                // an already uploaded file test.txt (maybe moved to some other directory) and a newly uploaded file test.txt
464                // still being in the temporary directory.
465                // This import step doesn't have to make that distinction, since all files remain in the temporary directory.
466                if( filename == 'existing*' )
467                        filename = '';
468                else if( filename[0..8] == 'existing*' )
469                        filename = filename[9..-1]
470
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
476                // These fields have been removed from the form, so will always contain
477                // their default value. The code however remains like this for future use.
478                int sheetIndex = (params.int( 'sheetindex' ) ?: 1 )
479                int dataMatrixStart = (params.int( 'datamatrix_start' ) ?: 2 )
480                int headerRow = (params.int( 'headerrow' ) ?: 1 )
481
482                // Save form data in session
483                flow.sampleForm = [
484                                        importFile: filename,
485                                        templateId: [
486                                                'Sample': sampleTemplateId,
487                                                'Subject': subjectTemplateId,
488                                                'Event': eventTemplateId,
489                                                'SamplingEvent': samplingEventTemplateId
490                                        ],
491                                        template: [
492                                                'Sample': sampleTemplateId ? Template.get( sampleTemplateId ) : null,
493                                                'Subject': subjectTemplateId ? Template.get( subjectTemplateId ) : null,
494                                                'Event': eventTemplateId ? Template.get( eventTemplateId ) : null,
495                                                'SamplingEvent': samplingEventTemplateId ? Template.get( samplingEventTemplateId ) : null
496                                        ],
497                                        sheetIndex: sheetIndex,
498                                        dataMatrixStart: dataMatrixStart,
499                                        headerRow: headerRow
500                                ];
501
502                // Check whether the template exists
503                if (!sampleTemplateId || !Template.get( sampleTemplateId ) ){
504                        log.error ".simple study wizard not all fields are filled in: " + sampleTemplateId
505                        flash.error = "No template was chosen. Please choose a template for the samples you provided."
506                        return false
507                }
508               
509                def importedfile = fileService.get( filename )
510                def workbook
511                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: " + e
516                                flash.error = "The given file doesn't seem to be an excel file. Please provide an excel file for entering samples.";
517                                return false
518                        }
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 false
529                }
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 wizard
537                def importerHeader;
538                def importerDataMatrix;
539
540                try {           
541                        importerHeader = importerService.getHeader(workbook,
542                                        sheetIndex - 1,                 // 0 == first sheet
543                                        headerRow,                              // 1 == first row :s
544                                        dataMatrixStart - 1,    // 0 == first row
545                                        Sample.class)
546               
547                        importerDataMatrix = importerService.getDatamatrix(
548                                        workbook,
549                                        importerHeader,
550                                        sheetIndex - 1,                 // 0 == first sheet
551                                        dataMatrixStart - 1,    // 0 == first row
552                                        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 user
559                        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 fields
564                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.entity
571                                               
572                                        fieldNames << field
573                                }
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 list
580                                fieldNames.remove( bestfit );
581                               
582                                mc.entityclass = bestfit.entity
583                                mc.property = bestfit.name
584                        }
585                }
586               
587                // Save read excel data into session
588                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: dataMatrix
602                                        ]
603                                ]
604
605                return true
606        }
607
608       
609        /**
610         * Handles the matching of template fields with excel columns by the user
611         * @param study         Study to update
612         * @param params        Request parameter map
613         * @return                      True if everything went OK, false otherwise. An error message is put in flash.error
614         *                                      The field session.simpleWizard.imported.numInvalidEntities reflects the number of
615         *                                      entities that have errors, and should be fixed before saving. The errors for those entities
616         *                                      are saved into session.simpleWizard.imported.errors
617         */
618        def handleColumns( study, params, flow ) {
619                // Find actual Template object from the chosen template name
620                def templates = [:];
621                flow.sampleForm.templateId.each {
622                        templates[ it.key ] = it.value ? Template.get( it.value ) : null;
623                }
624               
625                def headers = flow.excel.data.header;
626
627                if( !params.matches ) {
628                        log.error( ".simple study wizard no column matches given" );
629                        flash.error = "No column matches given";
630                        return false;
631                }
632
633                // Retrieve the chosen matches from the request parameters and put them into
634                // the headers-structure, for later reference
635                params.matches.index.each { columnindex, value ->
636                        // Determine the entity and property by splitting it
637                        def parts = value.toString().tokenize( "||" );
638                       
639                        def property
640                        def entityName
641                        if( parts.size() > 1 ) {
642                                property = parts[ 1 ];
643                                entityName = "dbnp.studycapturing." + parts[ 0 ];
644                        } else if( parts.size() == 1 ) {
645                                property = parts[ 0 ];
646                                entityName = headers[columnindex.toInteger()].entityclass.getName();
647                        }
648                       
649                        // Create an actual class instance of the selected entity with the selected template
650                        // This should be inside the closure because in some cases in the advanced importer, the fields can have different target entities
651                        def entityClass = Class.forName( entityName, true, this.getClass().getClassLoader())
652                        def entityObj = entityClass.newInstance(template: templates[ entityName[entityName.lastIndexOf( '.' ) + 1..-1] ])
653
654                        headers[ columnindex.toInteger() ].entityclass = entityClass
655                       
656                        // Store the selected property for this column into the column map for the ImporterService
657                        headers[columnindex.toInteger()].property = property
658
659                        // Look up the template field type of the target TemplateField and store it also in the map
660                        headers[columnindex.toInteger()].templatefieldtype = entityObj.giveFieldType(property)
661
662                        // Is a "Don't import" property assigned to the column?
663                        headers[columnindex.toInteger()].dontimport = (property == "dontimport") ? true : false
664
665                        //if it's an identifier set the mapping column true or false
666                        entityClass.giveDomainFields().each {
667                                headers[columnindex.toInteger()].identifier = ( it.preferredIdentifier && (it.name == property) )
668                        }
669                }
670
671                // Import the workbook and store the table with entity records and store the failed cells
672                println "Importing samples for study " + study + " (" + study.id + ")";
673               
674                def importedfile = fileService.get( flow.excel.filename )
675                def workbook
676                if (importedfile.exists()) {
677                        try {
678                                workbook = importerService.getWorkbook(new FileInputStream(importedfile))
679                        } catch (Exception e) {
680                                log.error ".simple study wizard could not load file: " + e
681                                flash.error = "The given file doesn't seem to be an excel file. Please provide an excel file for entering samples.";
682                                return false
683                        }
684                } else {
685                        log.error ".simple study wizard no file given";
686                        flash.error = "No file was given. Please provide an excel file for entering samples.";
687                        return false;
688                }
689
690                if( !workbook ) {
691                        log.error ".simple study wizard could not load file into a workbook"
692                        flash.error = "The given file doesn't seem to be an excel file. Please provide an excel file for entering samples.";
693                        return false
694                }
695                       
696                def imported = importerService.importOrUpdateDataBySampleIdentifier(templates,
697                                workbook,
698                                flow.excel.sheetIndex - 1,
699                                flow.excel.dataMatrixStart - 1,
700                                flow.excel.data.header,
701                                flow.study,
702                                true                    // Also create entities for which no data is imported but where templates were chosen
703                );
704
705                def table = imported.table
706                def failedcells = imported.failedCells
707
708                flow.imported = [
709                        data: table,
710                        failedCells: failedcells
711                ];
712       
713                // loop through all entities to validate them and add them to failedcells if an error occurs
714                def numInvalidEntities = 0;
715                def errors = [];
716
717                // Add all samples
718                table.each { record ->
719                        record.each { entity ->
720                                if( entity ) {
721                                        // Determine entity class and add a parent. Add the entity to the study
722                                        def preferredIdentifier = importerService.givePreferredIdentifier( entity.class );
723                                        def equalClosure = { it.getIdentifier() == entity.getIdentifier() }
724                                        def entityName = entity.class.name[ entity.class.name.lastIndexOf( "." ) + 1 .. -1 ]
725
726                                        entity.parent = study
727                                       
728                                        switch( entity.class ) {
729                                                case Sample:
730                                                        if( !study.samples?.find( equalClosure ) ) {
731                                                                study.addToSamples( entity );
732                                                        }
733                                                       
734                                                        // If an eventgroup is created, add it to the study
735                                                        // The eventgroup must have a unique name, but the user shouldn't be bothered with it
736                                                        // Add 'group ' + samplename and it that is not unique, add a number to it
737                                                        if( entity.parentEventGroup ) {
738                                                                study.addToEventGroups( entity.parentEventGroup )
739
740                                                                entity.parentEventGroup.name = "Group " + entity.name
741                                                                while( !entity.parentEventGroup.validate() ) {
742                                                                        entity.parentEventGroup.getErrors().each { println it }
743                                                                        entity.parentEventGroup.name += "" + Math.floor( Math.random() * 100 )
744                                                                }
745                                                        }
746                                                       
747                                                        break;
748                                                case Subject:
749                                                        if( !study.samples?.find( equalClosure ) ) {
750                                                               
751                                                                if( preferredIdentifier ) {
752                                                                        // Subjects without a name should just be called 'subject'
753                                                                        if( !entity.getFieldValue( preferredIdentifier.name ) )
754                                                                                entity.setFieldValue( preferredIdentifier.name, "Subject" );
755                                                               
756                                                                        // Subjects should have unique names; if the user has entered the same name multiple times,
757                                                                        // the subject will be renamed
758                                                                        def baseName = entity.getFieldValue( preferredIdentifier.name )
759                                                                        def counter = 2;
760                                                                       
761                                                                        while( study.subjects?.find { it.getFieldValue( preferredIdentifier.name ) == entity.getFieldValue( preferredIdentifier.name ) } ) {
762                                                                                entity.setFieldValue( preferredIdentifier.name, baseName + " (" + counter++ + ")" )
763                                                                        }
764                                                                }
765                                                               
766                                                                study.addToSubjects( entity );
767                                                       
768                                                        }
769                                                       
770                                                        break;
771                                                case Event:
772                                                        if( !study.events?.find( equalClosure ) ) {
773                                                                study.addToEvents( entity );
774                                                        }
775                                                        break;
776                                                case SamplingEvent:
777                                                        // Sampling events have a 'sampleTemplate' value, which should be filled by the
778                                                        // template that is chosen for samples.
779                                                        if( !entity.getFieldValue( 'sampleTemplate' ) ) {
780                                                                entity.setFieldValue( 'sampleTemplate', flow.sampleForm.template.Sample.name )
781                                                        } 
782                                               
783                                                        if( !study.samplingEvents?.find( equalClosure ) ) {
784                                                                study.addToSamplingEvents( entity );
785                                                        }
786                                                        break;
787                                        }
788                                       
789                                        if (!entity.validate()) {
790                                                numInvalidEntities++;
791                                               
792                                                // Add this field to the list of failed cells, in order to give the user feedback
793                                                failedcells = addNonValidatingCells( failedcells, entity, flow )
794       
795                                                // Also create a full list of errors
796                                                def currentErrors = getHumanReadableErrors( entity )
797                                                if( currentErrors ) {
798                                                        currentErrors.each {
799                                                                errors += "(" + entityName + ") " + it.value;
800                                                        }
801                                                }
802                                        }
803                                }
804                        }
805                }
806
807                flow.imported.numInvalidEntities = numInvalidEntities + failedcells?.size();
808                flow.imported.errors = errors;
809
810                return true
811        }
812       
813        /**
814         * Handles the update of the edited fields by the user
815         * @param study         Study to update
816         * @param params                Request parameter map
817         * @return                      True if everything went OK, false otherwise. An error message is put in flash.error.
818         *                                      The field session.simpleWizard.imported.numInvalidEntities reflects the number of
819         *                                      entities that still have errors, and should be fixed before saving. The errors for those entities
820         *                                      are saved into session.simpleWizard.imported.errors
821         */
822        def handleMissingFields( study, params, flow ) {
823                def numInvalidEntities = 0;
824                def errors = [];
825
826                // Check which fields failed previously
827                def failedCells = flow.imported.failedCells
828                def newFailedCells = [];
829
830                flow.imported.data.each { table ->
831                        table.each { entity ->
832                                def invalidFields = 0
833                                def failed = new ImportRecord();
834                                def entityName = entity.class.name[ entity.class.name.lastIndexOf( "." ) + 1 .. -1 ]
835                               
836
837                                // Set the fields for this entity by retrieving values from the params
838                                entity.giveFields().each { field ->
839                                        def fieldName = importerService.getFieldNameInTableEditor( entity, field );
840
841                                        if( params[ fieldName ] == "#invalidterm" ) {
842                                                // If the value '#invalidterm' is chosen, the user hasn't fixed anything, so this field is still incorrect
843                                                invalidFields++;
844                                               
845                                                // store the mapping column and value which failed
846                                                def identifier = entityName.toLowerCase() + "_" + entity.getIdentifier() + "_" + fieldName
847                                                def mcInstance = new MappingColumn()
848                                                failed.addToImportcells(new ImportCell(mappingcolumn: mcInstance, value: params[ fieldName ], entityidentifier: identifier))
849                                        } else {
850                                                if( field.type == org.dbnp.gdt.TemplateFieldType.ONTOLOGYTERM || field.type == org.dbnp.gdt.TemplateFieldType.STRINGLIST ) {
851                                                        // If this field is an ontologyterm field or a stringlist field, the value has changed, so remove the field from
852                                                        // the failedCells list
853                                                        importerService.removeFailedCell( failedCells, entity, field )
854                                                }
855
856                                                // Update the field, regardless of the type of field
857                                                entity.setFieldValue(field.name, params[ fieldName ] )
858                                        }
859                                }
860                               
861                                // Try to validate the entity now all fields have been set. If it fails, return an error
862                                if (!entity.validate() || invalidFields) {
863                                        numInvalidEntities++;
864
865                                        // Add this field to the list of failed cells, in order to give the user feedback
866                                        failedCells = addNonValidatingCellsToImportRecord( failed, entity, flow )
867
868                                        // Also create a full list of errors
869                                        def currentErrors = getHumanReadableErrors( entity )
870                                        if( currentErrors ) {
871                                                currentErrors.each {
872                                                        errors += "(" + entityName + ") " + it.value;
873                                                }
874                                        }
875                                       
876                                        newFailedCells << failed;
877                                } else {
878                                        importerService.removeFailedCell( failedCells, entity )
879                                }
880                        } // end of record
881                } // end of table
882
883                flow.imported.failedCells = newFailedCells
884                flow.imported.numInvalidEntities = numInvalidEntities;
885                flow.imported.errors = errors;
886
887                return numInvalidEntities == 0
888        }
889       
890        /**
891        * Handles assay input
892        * @param study          Study to update
893        * @param params         Request parameter map
894        * @return                       True if everything went OK, false otherwise. An error message is put in flash.error
895        */
896   def handleAssays( assay, params, flow ) {
897           // did the study template change?
898           if (params.get('template') && assay.template?.name != params.get('template')) {
899                   // set the template
900                   assay.template = Template.findByName(params.remove('template'))
901           }
902
903           // does the study have a template set?
904           if (assay.template && assay.template instanceof Template) {
905                   // yes, iterate through template fields
906                   assay.giveFields().each() {
907                           // and set their values
908                           assay.setFieldValue(it.name, params.get(it.escapedName()))
909                   }
910           }
911
912           return true
913   }
914       
915       
916        /**
917         * Checks whether the given study is simple enough to be edited using this controller.
918         *
919         * The study is simple enough if the samples, subjects, events and samplingEvents can be
920         * edited as a flat table. That is:
921         *              - Every subject belongs to 0 or 1 eventgroup
922         *              - Every eventgroup belongs to 0 or 1 sample
923         *              - Every eventgroup has 0 or 1 subjects, 0 or 1 event and 0 or 1 samplingEvents
924         *              - If a sample belongs to an eventgroup:
925         *                      - If that eventgroup has a samplingEvent, that same samplingEvent must also be
926         *                              the sampling event that generated this sample
927         *                      - If that eventgroup has a subject, that same subject must also be the subject
928         *                              from whom the sample was taken
929         *
930         * @param study         Study to check
931         * @return                      True if the study can be edited by this controller, false otherwise
932         */
933        def checkStudySimplicity( study ) {
934                def simplicity = true;
935
936                if( !study )
937                        return false
938
939                if( study.eventGroups ) {
940                        study.eventGroups.each { eventGroup ->
941                                // Check for simplicity of eventgroups: only 0 or 1 subject, 0 or 1 event and 0 or 1 samplingEvent
942                                if( eventGroup.subjects?.size() > 1 || eventGroup.events?.size() > 1 || eventGroup.samplingEvents?.size() > 1 ) {
943                                        flash.message = "One or more eventgroups contain multiple subjects or events."
944                                        simplicity = false;
945                                }
946
947                                // Check whether this eventgroup only belongs to (max) 1 sample
948                                def numSamples = 0;
949                                study.samples.each { sample ->
950                                        // If no id is given for the eventGroup, it has been entered in this wizard, but
951                                        // not yet saved. In that case, it is always OK
952                                        if( eventGroup.id && sample.parentEventGroup?.id == eventGroup.id )
953                                                numSamples++;
954                                }
955
956                                if( numSamples > 1 ) {
957                                        flash.message = "One or more eventgroups belong to multiple samples."
958                                        simplicity = false;
959                                }
960                        }
961
962                        if( !simplicity ) return false;
963
964                        // Check whether subject only belong to zero or one event group
965                        if( study.subjects ) {
966                                study.subjects.each { subject ->
967                                        def numEventGroups = 0
968                                        study.eventGroups.each { eventGroup ->
969                                                // If no id is given for the subject, it has been entered in this wizard, but
970                                                // not yet saved. In that case, it is always OK
971                                                if( subject.id && eventGroup.subjects && eventGroup.subjects.toList()[0]?.id == subject.id )
972                                                        numEventGroups++
973                                        }
974
975                                        if( numEventGroups > 1 ) {
976                                                flash.message = "One or more subjects belong to multiple eventgroups."
977                                                simplicity = false;
978                                        }
979                                }
980                        }
981
982                        if( !simplicity ) return false;
983
984                        // Check whether the samples that belong to an eventgroup have the right parentObjects
985                        study.samples.each { sample ->
986                                if( sample.parentEventGroup ) {
987                                        // If no id is given for the subject, it has been entered in this wizard, but
988                                        // not yet saved. In that case, it is always OK
989                                        if( sample.parentSubject && sample.parentSubject.id) {
990                                                if( !sample.parentEventGroup.subjects || sample.parentEventGroup.subjects.toList()[0]?.id != sample.parentSubject.id ) {
991                                                        flash.message = "The structure of the eventgroups of one or more samples is too complex"
992                                                        simplicity = false;
993                                                }
994                                        }
995
996                                        // If no id is given for the sampling event, it has been entered in this wizard, but
997                                        // not yet saved. In that case, it is always OK
998                                        if( sample.parentEvent && sample.parentEvent.id) {
999                                                if( !sample.parentEventGroup.samplingEvents || sample.parentEventGroup.samplingEvents.toList()[0]?.id != sample.parentEvent.id ) {
1000                                                        flash.message = "The structure of the eventgroups of one or more samples is too complex"
1001                                                        simplicity = false;
1002                                                }
1003                                        }
1004                                }
1005                        }
1006
1007                        if( !simplicity ) return false;
1008                }
1009
1010                return simplicity;
1011        }
1012
1013       
1014        /**
1015         * Adds all fields of this entity that have given an error when validating to the failedcells list
1016         * @param failedcells   Current list of ImportRecords
1017         * @param entity                Entity to check. The entity must have been validated before
1018         * @return                              Updated list of ImportRecords
1019         */
1020        protected def addNonValidatingCells( failedcells, entity, flow ) {
1021                // Add this entity and the fields with an error to the failedCells list
1022                ImportRecord failedRecord = addNonValidatingCellsToImportRecord( new ImportRecord(), entity, flow );
1023
1024                failedcells.add( failedRecord );
1025
1026                return failedcells
1027        }
1028       
1029        /**
1030        * Adds all fields of this entity that have given an error when validating to the failedcells list
1031        * @param failedcells    Current list of ImportRecords
1032        * @param entity         Entity to check. The entity must have been validated before
1033        * @return                               Updated list of ImportRecords
1034        */
1035   protected def addNonValidatingCellsToImportRecord( failedRecord, entity, flow ) {
1036           entity.getErrors().getFieldErrors().each { error ->
1037                   String field = error.getField();
1038                   
1039                   def mc = importerService.findMappingColumn( flow.excel.data.header, field );
1040                   def mcInstance = new MappingColumn( name: field, entityClass: Sample.class, index: -1, property: field.toLowerCase(), templateFieldType: entity.giveFieldType( field ) );
1041
1042                   // Create a clone of the mapping column
1043                   if( mc ) {
1044                           mcInstance.properties = mc.properties
1045                   }
1046
1047                   failedRecord.addToImportcells( new ImportCell(mappingcolumn: mcInstance, value: error.getRejectedValue(), entityidentifier: importerService.getFieldNameInTableEditor( entity, field ) ) )
1048           }
1049           
1050           return failedRecord
1051   }
1052
1053       
1054        /**
1055        * Checks an excel workbook whether the given sheetindex and rownumbers are correct
1056        * @param workbook                       Excel workbook to read
1057        * @param sheetIndex             1-based sheet index for the sheet to read (1=first sheet)
1058        * @param headerRow                      1-based row number for the header row (1=first row)
1059        * @param dataMatrixStart        1-based row number for the first data row (1=first row)
1060        * @return                                       True if the sheet index and row numbers are correct.
1061        */
1062   protected boolean excelChecks( def workbook, int sheetIndex, int headerRow, int dataMatrixStart ) {
1063           // Perform some basic checks on the excel file. These checks should be performed by the importerservice
1064           // in a perfect scenario.
1065           if( sheetIndex > workbook.getNumberOfSheets() ) {
1066                   log.error ".simple study wizard Sheet index is too high: " + sheetIndex + " / " + workbook.getNumberOfSheets();
1067                   flash.error = "Your excel sheet contains too few excel sheets. The provided excel sheet has only " + workbook.getNumberOfSheets() + " sheet(s).";
1068                   return false
1069           }
1070
1071           def sheet = workbook.getSheetAt(sheetIndex - 1);
1072           def firstRowNum = sheet.getFirstRowNum();
1073           def lastRowNum = sheet.getLastRowNum();
1074           def numRows = lastRowNum - firstRowNum + 1;
1075
1076           if( headerRow > numRows  ) {
1077                   log.error ".simple study wizard Header row number is incorrect: " + headerRow + " / " + numRows;
1078                   flash.error = "Your excel sheet doesn't contain enough rows (" + numRows + "). Please provide an excel sheet with one header row and data below";
1079                   return false
1080           }
1081
1082           if( dataMatrixStart > numRows  ) {
1083                   log.error ".simple study wizard Data row number is incorrect: " + dataMatrixStart + " / " + numRows;
1084                   flash.error = "Your excel sheet doesn't contain enough rows (" + numRows + "). Please provide an excel sheet with one header row and data below";
1085                   return false
1086           }
1087
1088           return true;
1089   }
1090       
1091        /**
1092         * Validates an object and puts human readable errors in validationErrors variable
1093         * @param entity                Entity to validate
1094         * @return                      True iff the entity validates, false otherwise
1095         */
1096        protected boolean validateObject( def entity ) {
1097                if( !entity.validate() ) {
1098                        flash.validationErrors = getHumanReadableErrors( entity )
1099                        return false;
1100                }
1101                return true;
1102        }
1103
1104        /**
1105         * transform domain class validation errors into a human readable
1106         * linked hash map
1107         * @param object validated domain class
1108         * @return object  linkedHashMap
1109         */
1110        def getHumanReadableErrors(object) {
1111                def errors = [:]
1112                object.errors.getAllErrors().each() { error ->
1113                        // error.codes.each() { code -> println code }
1114
1115                        // generally speaking g.message(...) should work,
1116                        // however it fails in some steps of the wizard
1117                        // (add event, add assay, etc) so g is not always
1118                        // availably. Using our own instance of the
1119                        // validationTagLib instead so it is always
1120                        // available to us
1121                        errors[error.getArguments()[0]] = validationTagLib.message(error: error)
1122                }
1123
1124                return errors
1125        }
1126}
Note: See TracBrowser for help on using the repository browser.