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

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