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

Last change on this file since 354 was 354, checked in by keesvb, 9 years ago

removed unneccesary imports from importer ;-) which still disabled clean build

  • Property svn:keywords set to Author Rev Date
File size: 5.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: 354 $
17 * $Author: keesvb $
18 * $Date: 2010-04-20 09:30:56 +0000 (di, 20 apr 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
31
32class ImporterController {
33    def ImporterService
34
35    /**
36     * Default page
37     **/
38    def index = { 
39        [templates:Template.list()]
40    }
41
42    /**
43    * This method will move the uploaded file to a temporary path and send the header
44    * and the first n rows to the preview
45    * @param importfile uploaded file to import
46    */
47    def upload = {
48        def downloadedfile = request.getFile('importfile');
49        def tempfile = new File(System.getProperty('java.io.tmpdir') + File.separatorChar + System.currentTimeMillis() + ".nmcdsp")
50        downloadedfile.transferTo(tempfile)
51       
52        def wb = ImporterService.getWorkbook(new FileInputStream(tempfile))
53       
54        def header = ImporterService.getHeader(wb, 0)
55        def datamatrix= ImporterService.getDatamatrix(wb, 0, 5)
56
57        session.importer_header = header
58        session.importer_template_id = params.template_id
59        session.importer_workbook = wb
60
61        render (view:"step1", model:[header:header, datamatrix:datamatrix])
62    }
63
64    /**
65    * User has assigned all entities and templatefieldtypes to the columns and continues to the next step (assigning properties to columns)
66    * All information of the columns is stored in a session as MappingColumn object
67    *
68    * @param entity list of entities and columns it has been assigned to (columnindex:entitytype format)
69    * @param templatefieldtype list of celltypes and columns it has been assigned to (columnindex:templatefieldtype format)
70    * @return properties page
71    *
72    * @see celltype: http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html
73    */
74    def savepreview = {
75        def tft = null
76        def entities  = request.getParameterValues("entity")
77        def templatefieldtypes = request.getParameterValues("templatefieldtype")
78        def identifiercolumnindex = (params.identifier!=null) ? params.identifier.toInteger() : -1
79
80        templatefieldtypes.each { t ->     
81            def columnindex = t.split(":")[0].toInteger()
82            def templatefieldtype = t.split(":")[1]
83           
84            switch (templatefieldtype) {
85                case "STRING"       : tft = TemplateFieldType.STRING
86                                      break
87                case "TEXT"         : tft = TemplateFieldType.TEXT
88                                      break
89                case "INTEGER"      : tft = TemplateFieldType.INTEGER
90                                      break
91                case "FLOAT"        : tft = TemplateFieldType.FLOAT
92                                      break
93                case "DOUBLE"       : tft = TemplateFieldType.DOUBLE
94                                      break
95                case "STRINGLIST"   : tft = TemplateFieldType.STRINGLIST
96                                      break
97                case "ONTOLOGYTERM" : tft = TemplateFieldType.ONTOLOGYTERM
98                                      break
99                case "DATE"         : tft = TemplateFieldType.DATE
100                                      break
101                default: break
102            }
103            session.importer_header[columnindex].templatefieldtype = tft
104        }
105
106        entities.each { e ->               
107            def columnindex = e.split(":")[0].toInteger()
108            def entitytype = e.split(":")[1].toInteger()
109            Class clazz     
110
111            switch (entitytype) {
112                case 0: clazz = Study
113                        break
114                case 1: clazz = Subject
115                        break
116                case 2: clazz = Event
117                        break
118                case 3: clazz = Protocol
119                        break
120                case 4: clazz = Sample
121                        break
122                default: clazz = Object
123                        break
124            }
125
126            session.importer_header[columnindex].identifier = (columnindex == identifiercolumnindex) ? true : false
127            session.importer_header[columnindex].index = columnindex
128            session.importer_header[columnindex].entity = clazz
129        }
130
131        // currently only one template is used for all entities
132        // TODO: show template fields per entity
133       
134        def templates = Template.get(session.importer_template_id)
135
136        render(view:"step2", model:[entities:entities, header:session.importer_header, templates:templates])
137    }
138
139    /**
140    * @param columnproperty array of columns and the assigned property in `column:property_id` format
141    *
142    */
143    def saveproperties = {
144        def columnproperties  = request.getParameterValues("columnproperty")   
145        session.importer_study = Study.get(params.study.id.toInteger())
146
147        columnproperties.each { cp ->           
148                def columnindex = cp.split(":")[0].toInteger()
149                def property_id = cp.split(":")[1].toInteger()
150                session.importer_header[columnindex].property = TemplateField.get(property_id)
151        }
152
153        //import workbook
154        session.importer_importeddata = ImporterService.importdata(session.importer_template_id, session.importer_workbook, 0, 1, session.importer_header)
155
156        render(view:"step3", model:[datamatrix:session.importer_importeddata])
157    }
158
159    def savepostview = {
160        ImporterService.savedata(session.importer_study, session.importer_importeddata)
161        render(view:"step4")
162    }
163}
Note: See TracBrowser for help on using the repository browser.