source: trunk/grails-app/controllers/dbnp/importer/ImporterController.groovy @ 1444

Last change on this file since 1444 was 1444, checked in by work@…, 13 years ago
  • changed the view to use the static variable from gdt instead of doing a dynamic function call
  • Property svn:keywords set to Rev Author Date
File size: 16.8 KB
Line 
1package dbnp.importer
2
3import dbnp.studycapturing.*
4import nl.grails.plugins.gdt.*
5import org.apache.poi.ss.usermodel.Workbook
6import grails.converters.JSON
7//import cr.co.arquetipos.crypto.Blowfish
8import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib
9import grails.plugins.springsecurity.Secured
10import org.hibernate.SessionFactory
11
12/**
13 * Wizard Controller
14 *
15 * @author Jeroen Wesbeek
16 * @since 20101206
17 *
18 * Revision information:
19 * $Rev: 1444 $
20 * $Author: work@osx.eu $
21 * $Date: 2011-01-26 21:10:21 +0000 (wo, 26 jan 2011) $
22 */
23@Secured(['IS_AUTHENTICATED_REMEMBERED'])
24class ImporterController {
25        // the pluginManager is used to check if the Grom
26        // plugin is available so we can 'Grom' development
27        // notifications to the unified notifications daemon
28        // (see http://www.grails.org/plugin/grom)
29        def pluginManager
30        def authenticationService
31        def fileService
32        def ImporterService
33        def validationTagLib = new ValidationTagLib()
34        def GdtService
35
36        /**
37         * index method, redirect to the webflow
38         * @void
39         */
40        def index = {
41                // Grom a development message
42                if (pluginManager.getGrailsPlugin('grom')) "redirecting into the webflow".grom()
43
44                /**
45                 * Do you believe it in your head?
46                 * I can go with the flow
47                 * Don't say it doesn't matter (with the flow) matter anymore
48                 * I can go with the flow (I can go)
49                 * Do you believe it in your head?
50                 */
51                redirect(action: 'pages')
52        }
53
54        /**
55         * WebFlow definition
56         * @void
57         */
58        def pagesFlow = {
59                // start the flow
60                onStart {
61                        // Grom a development message
62                        if (pluginManager.getGrailsPlugin('grom')) "entering the WebFlow".grom()
63
64                        // define variables in the flow scope which is availabe
65                        // throughout the complete webflow also have a look at
66                        // the Flow Scopes section on http://www.grails.org/WebFlow
67                        //
68                        // The following flow scope variables are used to generate
69                        // wizard tabs. Also see common/_tabs.gsp for more information
70                        flow.page = 0
71                        flow.pages = [
72                                [title: 'Import file'],
73                                [title: 'Assign properties'],
74                                [title: 'Check imported data'],
75                                //[title: 'Imported'],
76                                [title: 'Done']
77                        ]
78                        flow.cancel = true;
79                        flow.quickSave = true;
80
81                        success()
82                }
83
84                // render the main wizard page which immediately
85                // triggers the 'next' action (hence, the main
86                // page dynamically renders the study template
87                // and makes the flow jump to the study logic)
88                mainPage {
89                        render(view: "/importer/index")
90                        onRender {
91                                // Grom a development message
92                                if (pluginManager.getGrailsPlugin('grom')) "rendering the main Ajaxflow page (index.gsp)".grom()
93
94                                // let the view know we're in page 1
95                                flow.page = 1
96                                success()
97                        }
98                        on("next").to "pageOne"
99                }
100
101                // File import and entitie template selection page
102                pageOne {
103                        render(view: "_page_one")
104                        onRender {
105                                log.info ".entering import wizard"
106                                // Grom a development message
107                                if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial: pages/_page_one.gsp".grom()
108
109                                flow.page = 1
110                                flow.studies = Study.findAllWhere(owner: authenticationService.getLoggedInUser())
111
112                                success()
113                        }
114                        on("next") {
115                                flash.wizardErrors = [:]
116
117                                // Study selected?
118                                flow.importer_study = (params.study) ? Study.get(params.study.id.toInteger()) : null
119
120                                // Trying to import data into an existing study?
121                                if (flow.importer_study)
122                                        if (flow.importer_study.canWrite(authenticationService.getLoggedInUser())) {
123                                                if (fileImportPage(flow, params)) {
124                                                        success()
125                                                } else {
126                                                        log.error ".importer wizard not all fields are filled in"
127                                                        this.appendErrorMap(['error': "Not all fields are filled in, please fill in or select all fields"], flash.wizardErrors)
128                                                        error()
129                                                }
130                                        } else {
131                                                log.error ".importer wizard wrong permissions"
132                                                this.appendErrorMap(['error': "You don't have the right permissions"], flash.wizardErrors)
133
134                                                error()
135                                        }
136                                else {
137                                        if (fileImportPage(flow, params)) {
138                                                success()
139                                        } else {
140                                                log.error ".importer wizard not all fields are filled in"
141                                                this.appendErrorMap(['error': "Not all fields are filled in, please fill in or select all fields"], flash.wizardErrors)
142                                                error()
143                                        }
144                                }
145
146                                // put your bussiness logic (if applicable) in here
147                        }.to "pageTwo"
148                }
149
150                // Property to column assignment page
151                pageTwo {
152                        render(view: "_page_two")
153                        onRender {
154                                log.info ".import wizard properties page"
155                                // Grom a development message
156                                if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial: pages/_page_two.gsp".grom()
157
158                                flow.page = 2
159                                success()
160                        }
161                        on("next") {
162                                if (propertiesPage(flow, params)) {
163                                        success()
164                                } else {
165                                        log.error ".import wizard, properties are set wrong"
166                                        error()
167                                }
168                        }.to "pageThree"
169                        on("previous").to "pageOne"
170                }
171
172                // Mapping page
173                pageThree {
174                        render(view: "_page_three")
175                        onRender {
176                                log.info ".import wizard mapping page"
177                                // Grom a development message
178                                if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial pages/_page_three.gsp".grom()
179
180                                flow.page = 3
181                                success()
182                        }
183                        on("refresh") {
184                                success()
185                        }.to "pageThree"
186                        on("next") {
187                                if (mappingsPage(flow, flash, params)) {
188                                        flow.page = 4
189                                        success()
190                                } else {
191                                        log.error ".import wizard mapping error, could not validate all entities"
192                                        error()
193                                }
194                        }.to "save"
195                        on("previous").to "pageTwo"
196                }
197
198                // Imported data overview page
199                pageFour {
200                        render(view: "_page_four")
201                        onRender {
202                                // Grom a development message
203                                if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial pages/_page_four.gsp".grom()
204
205                                flow.page = 4
206                                success()
207                        }
208                        on("next") {
209                                if (importedPage(flow, params)) {
210                                        flow.page = 4
211                                        success()
212                                } else {
213                                        log.error ".import wizard imported error, something went wrong showing the imported entities"
214                                        error()
215                                }
216                        }.to "save"
217                        on("previous").to "pageThree"
218                }
219
220                // Save the imported data
221                save {
222                        action {
223                                // here you can validate and save the
224                                // instances you have created in the
225                                // ajax flow.
226                                // Grom a development message
227                                if (pluginManager.getGrailsPlugin('grom')) ".persisting instances to the database...".grom()
228
229                                if (saveEntities(flow, params)) {
230                                        //if (ImporterService.saveDatamatrix(flow.importer_study, flow.importer_importeddata)) {
231                                        //log.error ".import wizard succesfully persisted all entities"
232                                        //def session = sessionFactory.getCurrentSession()
233                                        //session.clear()
234                                        success()
235                                } else {
236                                        log.error ".import wizard, could not save entities:\n" + e.dump()
237                                        flow.page = 4
238                                        error()
239                                }
240                        }
241                        on("error").to "error"
242                        on(Exception).to "error"
243                        on("success").to "finalPage"
244                }
245
246                // render errors
247                error {
248                        render(view: "_error")
249                        onRender {
250
251                                // Grom a development message
252                                if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial pages/_error.gsp".grom()
253
254                                // set page to 4 so that the navigation
255                                // works (it is disabled on the final page)
256                                flow.page = 4
257                        }
258                        on("next").to "save"
259                        on("previous").to "pageFour"
260                }
261
262                // last wizard page
263                finalPage {
264                        render(view: "_final_page")
265                        onRender {
266                                println "EEN"
267                                // Grom a development message
268                                if (pluginManager.getGrailsPlugin('grom')) ".rendering the partial pages/_final_page.gsp".grom()
269                                println "TWEE"
270
271                                success()
272                                println "DRIE"
273                        }
274                        onEnd {
275                                // clean flow scope
276                                flow.clear()
277                        }
278                }
279        }
280
281        /**
282         * Return templates which belong to a certain entity type
283         *
284         * @param entity entity name string (Sample, Subject, Study et cetera)
285         * @return JSON object containing the found templates
286         */
287        def ajaxGetTemplatesByEntity = {
288                // fetch all templates for a specific entity
289                def templates = Template.findAllByEntity(GdtService.getInstanceByEntity(params.entity.decodeURL()))
290
291
292                // render as JSON
293                render templates as JSON
294        }
295
296        /**
297         * Handle the file import page.
298         *
299         * @param Map LocalAttributeMap (the flow scope)
300         * @param Map GrailsParameterMap (the flow parameters = form data)
301         * @returns boolean true if correctly validated, otherwise false
302         */
303        boolean fileImportPage(flow, params) {
304                def importedfile = fileService.get(params['importfile'])
305                //fileService.delete(YourFile)
306
307                if (params.entity && params.template_id && importedfile.exists()) {
308                        // create a workbook instance of the file
309                        session.importer_workbook = ImporterService.getWorkbook(new FileInputStream(importedfile))
310
311                        def selectedentities = []
312
313                        def entityName = GdtService.decryptEntity(params.entity.decodeURL())
314                        def entityClass = GdtService.getInstanceByEntityName(entityName)
315
316                        // Initialize some session variables
317                        //flow.importer_workbook = wb // workbook object must be serialized for this to work
318
319                        flow.importer_template_id = params.template_id
320                        flow.importer_sheetindex = params.sheetindex.toInteger() - 1 // 0 == first sheet
321                        flow.importer_datamatrix_start = params.datamatrix_start.toInteger() - 1 // 0 == first row
322                        flow.importer_headerrow = params.headerrow.toInteger()
323
324                        // Get the header from the Excel file using the arguments given in the first step of the wizard
325                        flow.importer_header = ImporterService.getHeader(session.importer_workbook,
326                                flow.importer_sheetindex,
327                                flow.importer_headerrow,
328                                flow.importer_datamatrix_start,
329                                entityClass)
330
331                        // Initialize 'selected entities', used to show entities above the columns
332                        flow.importer_header.each {
333                                selectedentities.add([name: entityName, columnindex: it.key.toInteger()])
334                        }
335
336                        flow.importer_selectedentities = selectedentities
337
338                        session.importer_datamatrix = ImporterService.getDatamatrix(
339                                session.importer_workbook, flow.importer_header,
340                                flow.importer_sheetindex,
341                                flow.importer_datamatrix_start,
342                                5)
343
344                        flow.importer_templates = Template.get(flow.importer_template_id)
345                        flow.importer_allfieldtypes = "true"
346                        /*else {
347                                                        render (template:"common/error",
348                                                                model:[error:"Wrong permissions: you are not allowed to write to the study you selected (${flow.importer_study})."])
349                                                }*/
350
351                        return true
352                }
353        }
354
355        /**
356         * Handle the property mapping page.
357         *
358         * @param Map LocalAttributeMap (the flow scope)
359         * @param Map GrailsParameterMap (the flow parameters = form data)
360         * @returns boolean true if correctly validated, otherwise false
361         */
362        boolean propertiesPage(flow, params) {
363                // Find actual Template object from the chosen template name
364                def template = Template.get(flow.importer_template_id)
365
366                params.columnproperty.index.each { columnindex, property ->
367                        // Create an actual class instance of the selected entity with the selected template
368                        // This should be inside the closure because in some cases in the advanced importer, the fields can have different target entities
369                        def entityClass = Class.forName(flow.importer_header[columnindex.toInteger()].entity.getName(), true, this.getClass().getClassLoader())
370                        def entityObj = entityClass.newInstance(template: template)
371
372                        // Store the selected property for this column into the column map for the ImporterService
373                        flow.importer_header[columnindex.toInteger()].property = property
374
375                        // Look up the template field type of the target TemplateField and store it also in the map
376                        flow.importer_header[columnindex.toInteger()].templatefieldtype = entityObj.giveFieldType(property)
377
378                        // Is a "Don't import" property assigned to the column?
379                        flow.importer_header[columnindex.toInteger()].dontimport = (property == "dontimport") ? true : false
380
381                        //if it's an identifier set the mapping column true or false
382                        entityObj.giveFields().each {
383                                (it.preferredIdentifier && (it.name == property)) ? flow.importer_header[columnindex.toInteger()].identifier = true : false
384                        }
385                }
386
387                // Import the workbook and store the table with entity records and store the failed cells
388                def (table, failedcells) = ImporterService.importData(flow.importer_template_id,
389                        session.importer_workbook,
390                        flow.importer_sheetindex,
391                        flow.importer_datamatrix_start,
392                        flow.importer_header)
393
394                flow.importer_importeddata = table
395                flow.importer_failedcells = failedcells
396                return true
397        }
398
399        /**
400         * Handle the mapping page.
401         *
402         * @param Map LocalAttributeMap (the flow scope)
403         * @param Map GrailsParameterMap (the flow parameters = form data)
404         * @returns boolean true if correctly validated, otherwise false
405         */
406        boolean mappingsPage(flow, flash, params) {
407                flash.wizardErrors = [:]
408                flow.importer_invalidentities = 0
409
410                flow.importer_importeddata.each { table ->
411                        table.each { entity ->
412                                def invalidontologies = 0
413
414                                // Set the fields for this entity by retrieving values from the params
415                                entity.giveFields().each { field ->
416                                        // field of type ontology and value "#invalidterm"?
417                                        if (field.type == nl.grails.plugins.gdt.TemplateFieldType.ONTOLOGYTERM &&
418                                                params["entity_" + entity.getIdentifier() + "_" + field.escapedName()] == "#invalidterm"
419                                        ) {
420                                                invalidontologies++
421                                        } else
422                                        if (field.type == nl.grails.plugins.gdt.TemplateFieldType.ONTOLOGYTERM &&
423                                                params["entity_" + entity.getIdentifier() + "_" + field.escapedName()] != "#invalidterm") {
424                                                removeFailedCell(flow.importer_failedcells, entity)
425                                                entity.setFieldValue(field.toString(), params["entity_" + entity.getIdentifier() + "_" + field.escapedName()])
426                                        }
427                                        else
428                                                entity.setFieldValue(field.toString(), params["entity_" + entity.getIdentifier() + "_" + field.escapedName()])
429                                }
430
431                                // Determine entity class and add a parent (defined as Study in first step of wizard)
432                                switch (entity.getClass()) {
433                                        case [Subject, Sample, Event]: entity.parent = flow.importer_study
434                                }
435
436                                // Try to validate the entity now all fields have been set
437                                if (!entity.validate() || invalidontologies) {
438                                        flow.importer_invalidentities++
439
440                                        // add errors to map
441                                        this.appendErrors(entity, flash.wizardErrors, "entity_" + entity.getIdentifier() + "_")
442
443                                        entity.errors.getAllErrors().each() {
444                                                log.error ".import wizard imported validation error:" + it
445                                        }
446                                } else {
447                                        removeFailedCell(flow.importer_failedcells, entity)
448                                } // end else if
449
450                        } // end of record
451                } // end of table
452
453                return (flow.importer_invalidentities == 0) ? true : false
454        } // end of method
455
456        /**
457         * @param failedcell failed ontology cells
458         * @param entity entity to remove from the failedcells list
459         */
460        def removeFailedCell(failedcells, entity) {
461                // Valid entity, remove it from failedcells
462                failedcells.each { record ->
463                        def tempimportcells = []
464
465                        record.importcells.each { cell ->
466                                // remove the cell from the failed cells session
467                                if (cell.entityidentifier != entity.getIdentifier()) {
468                                        //record.removeFromImportcells(cell)
469                                        tempimportcells.add(cell)
470                                }
471                        }
472
473                        record.importcells = tempimportcells
474                        // } // end of importcells
475                } // end of failedcells
476        }
477
478        /**
479         * Handle the imported entities page.
480         *
481         * @param Map LocalAttributeMap (the flow scope)
482         * @param Map GrailsParameterMap (the flow parameters = form data)
483         * @returns boolean true if correctly validated, otherwise false
484         */
485        boolean importedPage(flow, params) {
486                return true
487        }
488
489        boolean saveEntities(flow, params) {
490                //def (validatedSuccesfully, updatedEntities, failedToPersist) =
491                //try {
492                ImporterService.saveDatamatrix(flow.importer_study, flow.importer_importeddata, authenticationService, log)
493
494                //}
495                //catch (Exception e) {
496//                log.error ".import wizard saveEntities error\n" + e.dump()
497//            }
498
499                //flow.importer_validatedsuccesfully = validatedSuccesfully
500                //flow.importer_failedtopersist = failedToPersist
501                //flow.imported_updatedentities = updatedEntities
502                //flow.importer_totalrows = flow.importer_importeddata.size
503                //flow.importer_referer = ""
504
505                return true
506        }
507
508        /**
509         * append errors of a particular object to a map
510         * @param object
511         * @param map linkedHashMap
512         * @void
513         */
514        def appendErrors(object, map) {
515                this.appendErrorMap(getHumanReadableErrors(object), map)
516        }
517
518        def appendErrors(object, map, prepend) {
519                this.appendErrorMap(getHumanReadableErrors(object), map, prepend)
520        }
521
522        /**
523         * append errors of one map to another map
524         * @param map linkedHashMap
525         * @param map linkedHashMap
526         * @void
527         */
528        def appendErrorMap(map, mapToExtend) {
529                map.each() {key, value ->
530                        mapToExtend[key] = ['key': key, 'value': value, 'dynamic': false]
531                }
532        }
533
534        def appendErrorMap(map, mapToExtend, prepend) {
535                map.each() {key, value ->
536                        mapToExtend[prepend + key] = ['key': key, 'value': value, 'dynamic': true]
537                }
538        }
539
540        /**
541         * transform domain class validation errors into a human readable
542         * linked hash map
543         * @param object validated domain class
544         * @return object  linkedHashMap
545         */
546        def getHumanReadableErrors(object) {
547                def errors = [:]
548                object.errors.getAllErrors().each() { error ->
549                        // error.codes.each() { code -> println code }           
550
551                        // generally speaking g.message(...) should work,
552                        // however it fails in some steps of the wizard
553                        // (add event, add assay, etc) so g is not always
554                        // availably. Using our own instance of the
555                        // validationTagLib instead so it is always
556                        // available to us
557                        errors[error.getArguments()[0]] = validationTagLib.message(error: error)
558                }
559
560                return errors
561        }
562}
Note: See TracBrowser for help on using the repository browser.