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

Last change on this file since 268 was 268, checked in by tabma, 12 years ago
  • identifier column added
  • Property svn:keywords set to Author Rev Date
File size: 4.6 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: 268 $
17 * $Author: tabma $
18 * $Date: 2010-03-15 18:16:02 +0000 (ma, 15 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
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.header = header
58        session.importtemplate_id = params.template_id
59        session.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
79        templatefieldtypes.each { t ->
80            def temp = t.split(":")
81            def templatefieldtype = temp[1]         
82           
83            switch (templatefieldtype) {
84                case "STRING"       : tft = TemplateFieldType.STRING
85                                      break
86                case "TEXT"         : tft = TemplateFieldType.TEXT
87                                      break
88                case "INTEGER"      : tft = TemplateFieldType.INTEGER
89                                      break
90                case "FLOAT"        : tft = TemplateFieldType.FLOAT
91                                      break
92                case "DOUBLE"       : tft = TemplateFieldType.DOUBLE
93                                      break
94                case "STRINGLIST"   : tft = TemplateFieldType.STRINGLIST
95                                      break
96                case "ONTOLOGYTERM" : tft = TemplateFieldType.ONTOLOGYTERM
97                                      break
98                case "DATE"         : tft = TemplateFieldType.DATE
99                                      break
100                default: break
101            }
102            session.header[temp[0].toInteger()].templatefieldtype = tft
103        }
104
105        entities.each { e ->
106            def temp = e.split(":")
107            Class clazz     
108
109            switch (temp[1].toInteger()) {
110                case 0: clazz = Study
111                        break
112                case 1: clazz = Subject
113                        break
114                case 2: clazz = Event
115                        break
116                case 3: clazz = Protocol
117                        break
118                case 4: clazz = Sample
119                        break
120                default: clazz = Object
121                        break
122            }
123
124            session.header[temp[0].toInteger()].index = temp[0].toInteger()
125            session.header[temp[0].toInteger()].entity = clazz
126        }
127
128        // currently only one template is used for all entities
129        // TODO: show template fields per entity
130       
131        def templates = Template.get(session.importtemplate_id)
132
133        render(view:"step2", model:[entities:entities, header:session.header, templates:templates])
134    }
135
136    /**
137    * @param columnproperty array of columns and the assigned property in `column:property_id` format
138    *
139    */
140    def saveproperties = {
141        def columnproperties  = request.getParameterValues("columnproperty")
142
143        columnproperties.each { cp ->
144                def temp = cp.split(":")
145                session.header[temp[0].toInteger()].property = temp[1].toInteger()
146        }
147       
148        /*for (e in session.header) {
149            println e
150        }*/
151
152        //import workbook
153        ImporterService.importdata(session.importtemplate_id, session.workbook, 0, 0, session.header)
154        //println session.header.dump()
155
156        render ("properties saved")
157    }
158}
Note: See TracBrowser for help on using the repository browser.