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

Last change on this file since 492 was 492, checked in by tabma, 11 years ago
  • properties in simple import wizard now shows all template fields
  • moved study chooser in advanced wizard to first page
  • Property svn:keywords set to Date Author Rev
File size: 7.7 KB
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: 492 $
17 * $Author: tabma $
18 * $Date: 2010-05-28 09:08:19 +0000 (vr, 28 mei 2010) $
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       
61        session.importer_header = ImporterService.getHeader(wb, 0)
62        session.importer_template_id = params.template_id
63        session.importer_study = Study.get(params.study.id.toInteger())
64        session.importer_workbook = wb
65
66        render (view:"step1_advanced", model:[header:session.importer_header, datamatrix:ImporterService.getDatamatrix(wb, 0, 5)])
67    }
68
69    /**
70    * This method will move the uploaded file to a temporary path and send the header
71    * and the rows to the postview
72    *
73    * @param importfile uploaded file to import
74    * @param entity string representation of the entity chosen
75    */
76    def upload_simple = {
77        def wb = handleUpload('importfile')
78        def selectedentities = []
79        def entity = grailsApplication.config.gscf.domain.importableEntities.get(params.entity).entity
80        def entityClass = Class.forName(entity, true, this.getClass().getClassLoader())
81
82        session.importer_header = ImporterService.getHeader(wb, 0, entityClass)
83        session.importer_template_id = params.template_id
84        session.importer_workbook = wb
85
86        session.importer_header.each {     
87            selectedentities.add([name:params.entity, columnindex:it.key.toInteger()])
88        }
89
90        //import workbook
91        //session.importer_importeddata = ImporterService.importdata(session.importer_template_id, session.importer_workbook, 0, 1, session.importer_header)
92
93        //println "DAS" + session.importer_header
94
95        //render(view:"step2_simple", model:[datamatrix:session.importer_importeddata])
96        def templates = Template.get(session.importer_template_id)
97       
98        render(view:"step2_simple", model:[entities: selectedentities, header:session.importer_header, templates:templates])
99    }
100
101    /**
102     * This method handles a file being uploaded and storing it in a temporary directory
103     * and returning a workbook
104     *
105     * @param formfilename name used for the file field in the form
106     * @return workbook object reference
107     */
108    private HSSFWorkbook handleUpload(formfilename) {
109
110        def downloadedfile = request.getFile(formfilename);
111        def tempfile = new File(System.getProperty('java.io.tmpdir') + File.separatorChar + System.currentTimeMillis() + ".nmcdsp")
112        downloadedfile.transferTo(tempfile)
113
114        return ImporterService.getWorkbook(new FileInputStream(tempfile))
115    }
116
117    /**
118    * User has assigned all entities and templatefieldtypes to the columns and continues to the next step (assigning properties to columns)
119    * All information of the columns is stored in a session as MappingColumn object
120    *
121    * @param entities list of entities and columns it has been assigned to (columnindex.entitytype)
122    * @param templatefieldtype list of celltypes and columns it has been assigned to (columnindex:templatefieldtype format)
123    * @return properties page
124    *
125    * @see celltype: http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html
126    */
127    def savepreview = {
128        def tft = null 
129        def identifiercolumnindex = (params.identifier!=null) ? params.identifier.toInteger() : -1
130        def selectedentities = []
131
132        // loop all entities and see which column has been assigned which entitytype
133        // and build an array containing the selected entities
134        params.entity.index.each { columnindex, entityname ->
135            def _entity = [name:entityname,columnindex:columnindex.toInteger()]
136            selectedentities.add(_entity)
137        }
138
139        params.templatefieldtype.index.each { columnindex, _templatefieldtype ->
140            switch (_templatefieldtype) {
141                case "STRING"       : tft = TemplateFieldType.STRING
142                                      break
143                case "TEXT"         : tft = TemplateFieldType.TEXT
144                                      break
145                case "INTEGER"      : tft = TemplateFieldType.INTEGER
146                                      break
147                case "FLOAT"        : tft = TemplateFieldType.FLOAT
148                                      break
149                case "DOUBLE"       : tft = TemplateFieldType.DOUBLE
150                                      break
151                case "STRINGLIST"   : tft = TemplateFieldType.STRINGLIST
152                                      break
153                case "ONTOLOGYTERM" : tft = TemplateFieldType.ONTOLOGYTERM
154                                      break
155                case "DATE"         : tft = TemplateFieldType.DATE
156                                      break
157                default: break
158            }
159           
160            session.importer_header[columnindex.toInteger()].templatefieldtype = tft
161        }
162
163        params.entity.index.each { columnindex, entityname ->
164            Class clazz
165
166            switch (entityname) {
167                case "Study"    : clazz = Study
168                        break
169                case "Subject"  : clazz = Subject
170                        break
171                case "Event"    : clazz = Event
172                        break
173                case "Protocol" : clazz = Protocol
174                        break
175                case "Sample"   : clazz = Sample
176                        break
177                default: clazz = Object
178                        break
179            }
180
181            session.importer_header[columnindex.toInteger()].identifier = (columnindex.toInteger() == identifiercolumnindex) ? true : false
182            session.importer_header[columnindex.toInteger()].index = columnindex.toInteger()
183            session.importer_header[columnindex.toInteger()].entity = clazz
184        }
185
186        // currently only one template is used for all entities
187        // TODO: show template fields per entity
188       
189        def templates = Template.get(session.importer_template_id)
190
191        render(view:"step2", model:[entities:selectedentities, header:session.importer_header, templates:templates])
192    }
193
194    /**
195    * @param columnproperty array of columns containing index and property_id
196    *
197    */
198    def saveproperties = {     
199
200        params.columnproperty.index.each { columnindex, property_id ->
201                session.importer_header[columnindex.toInteger()].property = TemplateField.get(property_id.toInteger())
202        }
203
204        //import workbook
205        session.importer_importeddata = ImporterService.importdata(session.importer_template_id, session.importer_workbook, 0, 1, session.importer_header)
206
207        render(view:"step3", model:[datamatrix:session.importer_importeddata])
208    }
209
210    def savepostview = {
211        ImporterService.saveDatamatrix(session.importer_study, session.importer_importeddata)
212        render(view:"step4")
213    }
214
215    /**
216    * Return templates which belong to a certain entity type
217    *
218    * @param entity entity name string (Sample, Subject, Study et cetera)
219    * @return JSON object containing the found templates
220    */
221    def ajaxGetTemplatesByEntity = {
222        def entityClass = grailsApplication.config.gscf.domain.importableEntities.get(params.entity).entity
223
224        // fetch all templates for a specific entity
225        def templates = Template.findAllByEntity(Class.forName(entityClass, true, this.getClass().getClassLoader()))
226
227        // render as JSON
228        render templates as JSON
229    }
230}
Note: See TracBrowser for help on using the repository browser.