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

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