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

Last change on this file since 283 was 283, checked in by tabma, 9 years ago
  • final step in the wizard, implemented method in the ImporterService? to persist the entities into the database
  • Property svn:keywords set to Author Rev Date
File size: 4.8 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: 283 $
17 * $Author: tabma $
18 * $Date: 2010-03-18 09:17:02 +0000 (do, 18 mrt 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.header = header
59        session.importtemplate_id = params.template_id
60        session.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
80        templatefieldtypes.each { t ->
81            def temp = t.split(":")
82            def templatefieldtype = temp[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.header[temp[0].toInteger()].templatefieldtype = tft
104        }
105
106        entities.each { e ->
107            def temp = e.split(":")
108            Class clazz     
109
110            switch (temp[1].toInteger()) {
111                case 0: clazz = Study
112                        break
113                case 1: clazz = Subject
114                        break
115                case 2: clazz = Event
116                        break
117                case 3: clazz = Protocol
118                        break
119                case 4: clazz = Sample
120                        break
121                default: clazz = Object
122                        break
123            }
124
125            session.header[temp[0].toInteger()].index = temp[0].toInteger()
126            session.header[temp[0].toInteger()].entity = clazz
127        }
128
129        // currently only one template is used for all entities
130        // TODO: show template fields per entity
131       
132        def templates = Template.get(session.importtemplate_id)
133
134        render(view:"step2", model:[entities:entities, header:session.header, templates:templates])
135    }
136
137    /**
138    * @param columnproperty array of columns and the assigned property in `column:property_id` format
139    *
140    */
141    def saveproperties = {
142        def columnproperties  = request.getParameterValues("columnproperty")
143
144        columnproperties.each { cp ->
145                def temp = cp.split(":")
146                session.header[temp[0].toInteger()].property = TemplateField.get(temp[1].toInteger())
147        }
148
149        //import workbook
150        session.importeddata = ImporterService.importdata(session.importtemplate_id, session.workbook, 0, 1, session.header)   
151
152        render(view:"step3", model:[datamatrix:session.importeddata])
153    }
154
155    def savepostview = {
156        ImporterService.savedata(session.importeddata)
157        render(view:"step4")
158    }
159}
Note: See TracBrowser for help on using the repository browser.