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

Last change on this file since 255 was 255, checked in by tabma, 9 years ago
  • rewrite of celltype detection
  • Property svn:keywords set to Author Rev Date
File size: 4.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: 255 $
17 * $Author: tabma $
18 * $Date: 2010-03-10 18:27:57 +0000 (wo, 10 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                case 4 : templatefieldtype = TemplateFieldType.STRING
94                         break
95                default: break
96            }
97        }
98
99        entities.each { e ->
100            def temp = e.split(":")
101            Class clazz
102
103            switch (temp[1].toInteger()) {
104                case 0: clazz = Study
105                        break
106                case 1: clazz = Subject
107                        break
108                case 2: clazz = Event
109                        break
110                case 3: clazz = Protocol
111                        break
112                case 4: clazz = Sample
113                        break
114                default: clazz = Object
115            }
116
117            session.header[temp[0].toInteger()].index = temp[0].toInteger()
118            session.header[temp[0].toInteger()].entity = clazz
119        }
120
121        // currently only one template is used for all entities
122        // TODO: show template fields per entity
123       
124        def templates = Template.get(session.importtemplate_id)
125
126        render(view:"step2", model:[entities:entities, header:session.header, templates:templates])
127    }
128
129    /**
130    * @param columnproperty array of columns and the assigned property in `column:property_id` format
131    *
132    */
133    def saveproperties = {
134
135        def columnproperties  = request.getParameterValues("columnproperty")
136        columnproperties.each { cp ->
137                def temp = cp.split(":")
138                session.header[temp[0].toInteger()].property = temp[1].toInteger()
139        }
140       
141        for (e in session.header) {
142            println e
143        }
144        render ("properties saved")
145    }
146}
Note: See TracBrowser for help on using the repository browser.