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

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

- initial fuzzy logic incorporated in Importer service

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