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

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