root/trunk/grails-app/controllers/dbnp/importer/ImporterController.groovy @ 545

Revision 545, 8.3 KB (checked in by tabma, 4 years ago)

- fixed Advanced Import Wizard (preferredIdentifier issues)

  • Property svn:keywords set to Date Author Rev
Line 
1/**
2 * Importer controller
3 *
4 * The importer controller handles the uploading of tabular, comma delimited and Excel format
5 * based files. When uploaded a preview is shown of the data and the user can adjust the column
6 * type. Data in cells which don't correspond to the specified column type will be represented as "#error".
7 *
8 * The importer controller catches the actions and consecutively performs the
9 * logic behind it.
10 *
11 * @package     importer
12 * @author      t.w.abma@umcutrecht.nl
13 * @since       20100126
14 *
15 * Revision information:
16 * $Rev$
17 * $Author$
18 * $Date$
19 */
20
21package dbnp.importer
22
23import dbnp.studycapturing.Template
24import dbnp.studycapturing.Study
25import dbnp.studycapturing.Subject
26import dbnp.studycapturing.Event
27
28import dbnp.studycapturing.Sample
29import dbnp.studycapturing.TemplateFieldType
30import dbnp.studycapturing.TemplateField
31import grails.converters.JSON
32import org.apache.poi.hssf.usermodel.HSSFWorkbook
33
34class ImporterController {
35    def ImporterService   
36
37    /**
38     * Default page
39     **/
40    def index = {       
41    }
42
43    def simplewizard = {
44        render(view:"index_simple", model:[studies:Study.list(), entities: grailsApplication.config.gscf.domain.importableEntities])
45    }
46
47    def advancedwizard = {
48        render(view:"index_advanced", model:[templates:Template.list()])
49    }
50
51    /**
52    * This method will move the uploaded file to a temporary path and send the header
53    * and the first n rows to the preview
54    * @param importfile uploaded file to import
55    * @param study.id study identifier
56    */
57    def upload_advanced = {
58        def wb = handleUpload('importfile')
59
60        session.importer_header = ImporterService.getHeader(wb, 0)
61        session.importer_study = Study.get(params.study.id.toInteger())
62        session.importer_template_id = params.template_id
63        session.importer_workbook = wb
64
65       
66
67        render (view:"step1_advanced", model:[header:session.importer_header, datamatrix:ImporterService.getDatamatrix(wb, 0, 5)])
68    }
69
70    /**
71    * This method will move the uploaded file to a temporary path and send the header
72    * and the rows to the postview
73    *
74    * @param importfile uploaded file to import
75    * @param entity string representation of the entity chosen
76    */
77    def upload_simple = {
78        def wb = handleUpload('importfile')
79        def selectedentities = []
80        def entity = grailsApplication.config.gscf.domain.importableEntities.get(params.entity).entity
81        def entityClass = Class.forName(entity, true, this.getClass().getClassLoader())
82
83        session.importer_header = ImporterService.getHeader(wb, 0, entityClass)
84        session.importer_study = Study.get(params.study.id.toInteger())
85        session.importer_template_id = params.template_id
86        session.importer_workbook = wb
87
88        session.importer_header.each {     
89            selectedentities.add([name:params.entity, columnindex:it.key.toInteger()])
90        }
91
92        //import workbook
93        //session.importer_importeddata = ImporterService.importdata(session.importer_template_id, session.importer_workbook, 0, 1, session.importer_header)
94
95        //println "DAS" + session.importer_header
96
97        //render(view:"step2_simple", model:[datamatrix:session.importer_importeddata])
98        def templates = Template.get(session.importer_template_id)
99       
100        render(view:"step2_simple", model:[entities: selectedentities, header:session.importer_header, templates:templates])
101    }
102
103    /**
104     * This method handles a file being uploaded and storing it in a temporary directory
105     * and returning a workbook
106     *
107     * @param formfilename name used for the file field in the form
108     * @return workbook object reference
109     */
110    private HSSFWorkbook handleUpload(formfilename) {
111
112        def downloadedfile = request.getFile(formfilename);
113        def tempfile = new File(System.getProperty('java.io.tmpdir') + File.separatorChar + System.currentTimeMillis() + ".nmcdsp")
114        downloadedfile.transferTo(tempfile)
115
116        return ImporterService.getWorkbook(new FileInputStream(tempfile))
117    }
118
119    /**
120    * User has assigned all entities and templatefieldtypes to the columns and continues to the next step (assigning properties to columns)
121    * All information of the columns is stored in a session as MappingColumn object
122    *
123    * @param entities list of entities and columns it has been assigned to (columnindex.entitytype)
124    * @param templatefieldtype list of celltypes and columns it has been assigned to (columnindex:templatefieldtype format)
125    * @return properties page
126    *
127    * @see celltype: http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html
128    */
129    def savepreview = {
130        def tft = null 
131        def identifiercolumnindex = (params.identifier!=null) ? params.identifier.toInteger() : -1
132        def selectedentities = []
133
134        // loop all entities and see which column has been assigned which entitytype
135        // and build an array containing the selected entities
136        params.entity.index.each { columnindex, entityname ->
137            def _entity = [name:entityname,columnindex:columnindex.toInteger()]
138            selectedentities.add(_entity)
139        }
140
141        params.templatefieldtype.index.each { columnindex, _templatefieldtype ->
142            switch (_templatefieldtype) {
143                case "STRING"       : tft = TemplateFieldType.STRING
144                                      break
145                case "TEXT"         : tft = TemplateFieldType.TEXT
146                                      break
147                case "INTEGER"      : tft = TemplateFieldType.INTEGER
148                                      break
149                case "FLOAT"        : tft = TemplateFieldType.FLOAT
150                                      break
151                case "DOUBLE"       : tft = TemplateFieldType.DOUBLE
152                                      break
153                case "STRINGLIST"   : tft = TemplateFieldType.STRINGLIST
154                                      break
155                case "ONTOLOGYTERM" : tft = TemplateFieldType.ONTOLOGYTERM
156                                      break
157                case "DATE"         : tft = TemplateFieldType.DATE
158                                      break
159                default: break
160            }
161           
162            session.importer_header[columnindex.toInteger()].templatefieldtype = tft
163        }
164
165        params.entity.index.each { columnindex, entityname ->
166            Class clazz = null
167
168            switch (entityname) {
169                case "Study"    : clazz = dbnp.studycapturing.Study
170                        break
171                case "Subject"  : clazz = dbnp.studycapturing.Subject
172                        break
173                case "Event"    : clazz = dbnp.studycapturing.Event
174                        break
175                case "Protocol" : clazz = dbnp.studycapturing.Protocol
176                        break
177                case "Sample"   : clazz = dbnp.studycapturing.Sample
178                        break
179                default: clazz = Object
180                        break
181            }
182
183            session.importer_header[columnindex.toInteger()].identifier = (columnindex.toInteger() == identifiercolumnindex) ? true : false
184            session.importer_header[columnindex.toInteger()].index = columnindex.toInteger()
185            session.importer_header[columnindex.toInteger()].entity = clazz
186        }
187
188        // currently only one template is used for all entities
189        // TODO: show template fields per entity
190       
191        def templates = Template.get(session.importer_template_id)
192
193        render(view:"step2", model:[entities:selectedentities, header:session.importer_header, templates:templates])
194    }
195
196    /**
197    * @param columnproperty array of columns containing index and property (represented as a String)
198    *
199    */
200    def saveproperties = {     
201
202        params.columnproperty.index.each { columnindex, property ->
203                def template = Template.get(session.imported_template_id)
204
205                def entityClass = Class.forName(session.importer_header[columnindex.toInteger()].entity.getName(), true, this.getClass().getClassLoader())
206                def entityObj = entityClass.newInstance(template:template)             
207
208               
209                session.importer_header[columnindex.toInteger()].property = property
210               
211                //if it's an identifier set the mapping column true or false           
212                entityObj.giveFields().each {
213                    (it.preferredIdentifier && (it.name==property)) ? session.importer_header[columnindex.toInteger()].identifier = true : false
214                }
215        }
216
217        //import workbook
218        session.importer_importeddata = ImporterService.importdata(session.importer_template_id, session.importer_workbook, 0, 1, session.importer_header)
219
220        render(view:"step3", model:[datamatrix:session.importer_importeddata])
221    }
222
223    def savepostview = {
224        ImporterService.saveDatamatrix(session.importer_study, session.importer_importeddata)
225        render(view:"step4")
226    }
227
228    /**
229    * Return templates which belong to a certain entity type
230    *
231    * @param entity entity name string (Sample, Subject, Study et cetera)
232    * @return JSON object containing the found templates
233    */
234    def ajaxGetTemplatesByEntity = {
235        def entityClass = grailsApplication.config.gscf.domain.importableEntities.get(params.entity).entity
236
237        // fetch all templates for a specific entity
238        def templates = Template.findAllByEntity(Class.forName(entityClass, true, this.getClass().getClassLoader()))
239
240        // render as JSON
241        render templates as JSON
242    }
243}
Note: See TracBrowser for help on using the browser.