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

Last change on this file since 251 was 251, checked in by tabma, 9 years ago
  • Property svn:keywords set to Author Rev Date
File size: 4.1 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: 251 $
17 * $Author: tabma $
18 * $Date: 2010-03-09 14:53:46 +0000 (di, 09 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
60        render (view:"step1", model:[header:header, datamatrix:datamatrix])
61    }
62
63    /**
64    * User has assigned all entities and celltypes to the columns and continues to the next step (assigning properties to columns)
65    * All information of the columns is stored in a session as MappingColumn object
66    *
67    * @param entity list of entities and columns it has been assigned to (columnindex:entitytype format)
68    * @param celltype list of celltypes and columns it has been assigned to (columnindex:celltype format)
69    * @return properties page
70    *
71    * @see celltype: http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html
72    */
73    def savepreview = { 
74        def entities  = request.getParameterValues("entity")
75        def celltypes = request.getParameterValues("celltype")
76
77        celltypes.each { c ->
78            def temp = c.split(":")
79            def celltype = temp[1].toInteger()
80            def templatefieldtype = TemplateFieldType.STRING
81           
82            session.header[temp[0].toInteger()].celltype = celltype
83
84            switch (celltype) {
85                case 0 : templatefieldtype = TemplateFieldType.INTEGER
86                         break
87                case 1 : templatefieldtype = TemplateFieldType.STRING
88                         break
89                case 2 :  // formula cell type, cannot handle this
90                         break
91                case 3 : templatefieldtype = TemplateFieldType.STRING
92                         break
93                default: break
94            }
95        }
96
97        entities.each { e ->
98            def temp = e.split(":")
99            Class clazz
100
101            switch (temp[1].toInteger()) {
102                case 0: clazz = Study
103                        break
104                case 1: clazz = Subject
105                        break
106                case 2: clazz = Event
107                        break
108                case 3: clazz = Protocol
109                        break
110                case 4: clazz = Sample
111                        break
112                default: clazz = Object
113            }
114
115            session.header[temp[0].toInteger()].index = temp[0].toInteger()
116            session.header[temp[0].toInteger()].entity = clazz
117        }
118
119        // currently only one template is used for all entities
120        // TODO: show template fields per entity
121       
122        def templates = Template.get(session.importtemplate_id)
123
124        render(view:"step2", model:[entities:entities, header:session.header, templates:templates])
125    }
126
127    /**
128    * @param columnproperty array of columns and the assigned property in `column:property_id` format
129    *
130    */
131    def saveproperties = {
132
133        def columnproperties  = request.getParameterValues("columnproperty")
134        columnproperties.each { cp ->
135                def temp = cp.split(":")
136                session.header[temp[0].toInteger()].property = temp[1].toInteger()
137        }
138       
139        for (e in session.header) {
140            println e
141        }
142        render ("properties saved")
143    }
144}
Note: See TracBrowser for help on using the repository browser.