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

Revision 1047, 11.3 KB (checked in by t.w.abma@…, 3 years ago)

- referer for now obtained via redirectTo param in the URL

  • Property svn:keywords set to Author Date 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
33import grails.plugins.springsecurity.Secured
34
35@Secured(['IS_AUTHENTICATED_REMEMBERED'])
36class ImporterController {
37    def ImporterService   
38
39    /**
40     * Default page
41     **/
42
43    def index = {
44        //session.import_referer = request.forwardURI
45        // should do a check what is in the url, strip it?
46        session.import_referer = params.redirectTo
47    }
48
49    def simpleWizard = {
50        render(view:"index_simple", model:[studies:Study.list(), entities: grailsApplication.config.gscf.domain.importableEntities])
51    }
52
53    def advancedWizard = {
54        render(view:"index_advanced", model:[templates:Template.list()])
55    }
56
57    /**
58    * This method will move the uploaded file to a temporary path and send the header
59    * and the first n rows to the preview
60    * @param importfile uploaded file to import
61    * @param study.id study identifier
62    */
63    def upload_advanced = {
64        def wb = handleUpload('importfile')
65
66        session.importer_header = ImporterService.getHeader(wb, 0)
67        session.importer_study = Study.get(params.study.id.toInteger())
68        session.importer_template_id = params.template_id
69        session.importer_workbook = wb
70
71        render (view:"step1_advanced", model:[header:session.importer_header, datamatrix:ImporterService.getDatamatrix(wb, session.importer_header, 0, 5)])
72    }
73
74    /**
75    * This method will move the uploaded file to a temporary path and send the header
76    * and the rows to the postview
77    *
78    * @param importfile uploaded file to import
79    * @param entity string representation of the entity chosen
80    */
81    def upload_simple = {
82        def wb = handleUpload('importfile')
83        def selectedentities = []
84        def entity = grailsApplication.config.gscf.domain.importableEntities.get(params.entity).entity
85        def entityClass = Class.forName(entity, true, this.getClass().getClassLoader())
86
87        // Initialize some session variables
88        session.importer_workbook = wb
89        session.importer_study = Study.get(params.study.id.toInteger())
90        session.importer_template_id = params.template_id
91        session.importer_sheetindex = params.sheetindex.toInteger() -1 // 0 == first sheet
92        session.importer_datamatrix_start = params.datamatrix_start.toInteger() -1 // 0 == first row
93        session.importer_headerrow = params.headerrow.toInteger()
94
95        // Get the header from the Excel file using the arguments given in the first step of the wizard
96        session.importer_header = ImporterService.getHeader(wb,
97                                                            session.importer_sheetindex,
98                                                            session.importer_headerrow,
99                                                            session.importer_datamatrix_start,
100                                                            entityClass)
101       
102        // Initialize 'selected entities', used to show entities above the columns
103        session.importer_header.each {
104            selectedentities.add([name:params.entity, columnindex:it.key.toInteger()])
105        }
106
107        def templates = Template.get(session.importer_template_id)
108       
109        render(view:"step2_simple", model:[entities: selectedentities,
110                                           header:session.importer_header,
111                                           datamatrix:ImporterService.getDatamatrix(
112                                               wb, session.importer_header,
113                                               session.importer_sheetindex,
114                                               session.importer_datamatrix_start,
115                                               5),
116                                           templates:templates])
117    }
118
119    /**
120     * This method handles a file being uploaded and storing it in a temporary directory
121     * and returning a workbook
122     *
123     * @param formfilename name used for the file field in the form
124     * @return workbook object reference
125     */
126    private HSSFWorkbook handleUpload(formfilename) {
127
128        def downloadedfile = request.getFile(formfilename);
129        def tempfile = new File(System.getProperty('java.io.tmpdir') + File.separatorChar + System.currentTimeMillis() + ".nmcdsp")
130        downloadedfile.transferTo(tempfile)
131
132        return ImporterService.getWorkbook(new FileInputStream(tempfile))
133    }
134
135    /**
136     * Method to save the missing properties
137     * @param entity entity class we are using (dbnp.studycapturing.Subject etc.)
138     */
139
140    def saveMissingProperties = {
141
142        session.importer_importeddata.each { table ->
143            table.each { entity ->
144                entity.giveFields().each { field ->
145                    //print ":" + params["entity_" + entity.hashCode() + "_" + field.escapedName()]
146                    entity.setFieldValue (field.toString(), params["entity_" + entity.hashCode() + "_" + field.escapedName()])
147                }               
148            }
149        }
150
151        render(view:"step3", model:[datamatrix:session.importer_importeddata]) 
152    }
153
154    /**
155    * User has assigned all entities and templatefieldtypes to the columns and continues to the next step (assigning properties to columns)
156    * All information of the columns is stored in a session as MappingColumn object
157    *
158    * @param entities list of entities and columns it has been assigned to (columnindex.entitytype)
159    * @param templatefieldtype list of celltypes and columns it has been assigned to (columnindex:templatefieldtype format)
160    * @return properties page
161    *
162    * @see celltype: http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html
163    */
164    def savePreview = {
165        def tft = null 
166        def identifiercolumnindex = (params.identifier!=null) ? params.identifier.toInteger() : -1
167        def selectedentities = []
168
169        // loop all entities and see which column has been assigned which entitytype
170        // and build an array containing the selected entities
171        params.entity.index.each { columnindex, entityname ->
172            def _entity = [name:entityname,columnindex:columnindex.toInteger()]
173            selectedentities.add(_entity)
174        }
175
176        params.templatefieldtype.index.each { columnindex, _templatefieldtype ->
177            switch (_templatefieldtype) {
178                case "STRING"       : tft = TemplateFieldType.STRING
179                                      break
180                case "TEXT"         : tft = TemplateFieldType.TEXT
181                                      break
182                case "INTEGER"      : tft = TemplateFieldType.INTEGER
183                                      break
184                case "FLOAT"        : tft = TemplateFieldType.FLOAT
185                                      break
186                case "DOUBLE"       : tft = TemplateFieldType.DOUBLE
187                                      break
188                case "STRINGLIST"   : tft = TemplateFieldType.STRINGLIST
189                                      break
190                case "ONTOLOGYTERM" : tft = TemplateFieldType.ONTOLOGYTERM
191                                      break
192                case "DATE"         : tft = TemplateFieldType.DATE
193                                      break
194                default: break
195            }
196           
197            // Set the templatefield type for this column
198            session.importer_header[columnindex.toInteger()].templatefieldtype = tft
199        }
200
201        // Detect the entity type
202        params.entity.index.each { columnindex, entityname ->
203            Class clazz = null
204
205            switch (entityname) {
206                case "Study"    : clazz = dbnp.studycapturing.Study
207                        break
208                case "Subject"  : clazz = dbnp.studycapturing.Subject
209                        break
210                case "Event"    : clazz = dbnp.studycapturing.Event
211                        break
212                case "Protocol" : clazz = dbnp.studycapturing.Protocol
213                        break
214                case "Sample"   : clazz = dbnp.studycapturing.Sample
215                        break
216                default: clazz = Object
217                        break
218            }
219
220            // Store properties for this column
221            session.importer_header[columnindex.toInteger()].identifier = (columnindex.toInteger() == identifiercolumnindex) ? true : false
222            session.importer_header[columnindex.toInteger()].index = columnindex.toInteger()
223            session.importer_header[columnindex.toInteger()].entity = clazz
224        }
225
226        // currently only one template is used for all entities
227        // TODO: show template fields per entity
228       
229        def templates = Template.get(session.importer_template_id)
230
231        render(view:"step2", model:[entities:selectedentities, header:session.importer_header, templates:templates])
232    }
233   
234    /**
235     * Method which stores the properties set per column and then imports the data.
236     *
237     * @param columnproperty array of columns containing index and property (represented as a String)
238    *
239    */
240    def saveProperties = {       
241
242        // Find actual Template object from the chosen template name
243        def template = Template.get(session.importer_template_id)
244
245        params.columnproperty.index.each { columnindex, property ->
246
247                // Create an actual class instance of the selected entity with the selected template
248                // This should be inside the closure because in some cases in the advanced importer, the fields can have different target entities
249                def entityClass = Class.forName(session.importer_header[columnindex.toInteger()].entity.getName(), true, this.getClass().getClassLoader())
250                def entityObj = entityClass.newInstance(template:template)
251
252                // Store the selected property for this column into the column map for the ImporterService
253                session.importer_header[columnindex.toInteger()].property = property
254
255                // Look up the template field type of the target TemplateField and store it also in the map
256                session.importer_header[columnindex.toInteger()].templatefieldtype = entityObj.giveFieldType(property)
257
258                // Is a "Don't import" property assigned to the column?
259                session.importer_header[columnindex.toInteger()].dontimport = (property=="dontimport") ? true : false
260
261                //if it's an identifier set the mapping column true or false
262                entityObj.giveFields().each {
263                    (it.preferredIdentifier && (it.name==property)) ? session.importer_header[columnindex.toInteger()].identifier = true : false
264                }
265        }
266
267        //import workbook and store it in a session
268        session.importer_importeddata = ImporterService.importData(session.importer_template_id, session.importer_workbook, session.importer_sheetindex, session.importer_datamatrix_start, session.importer_header)
269
270        // First handle the "failed cells"
271        //ImporterService.getFailedCells(session.importer_importeddata)
272       
273        //render(view:"step2a_simple", model:[datamatrix:session.importer_importeddata])
274
275        if (params.layout=="horizontal")
276            render(view:"step3_simple", model:[datamatrix:session.importer_importeddata])
277        else if (params.layout=="vertical")
278            render(view:"step3", model:[datamatrix:session.importer_importeddata])
279    }
280
281    /**
282     * Method which saves the data matrix to the database
283     */
284    def savePostview = {
285        def validatedSuccesfully = ImporterService.saveDatamatrix(session.importer_study, session.importer_importeddata)
286        render(view:"step4", model:[validatedSuccesfully:validatedSuccesfully, totalrows:session.importer_importeddata.size, referer: session.import_referer])
287    }
288
289    /**
290    * Return templates which belong to a certain entity type
291    *
292    * @param entity entity name string (Sample, Subject, Study et cetera)
293    * @return JSON object containing the found templates
294    */
295    def ajaxGetTemplatesByEntity = {
296        def entityClass = grailsApplication.config.gscf.domain.importableEntities.get(params.entity).entity
297       
298
299        // fetch all templates for a specific entity
300        def templates = Template.findAllByEntity(Class.forName(entityClass, true, this.getClass().getClassLoader()))   
301
302        // render as JSON
303        render templates as JSON
304    }
305}
Note: See TracBrowser for help on using the browser.