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

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