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
RevLine 
[215]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
[251]25import dbnp.studycapturing.Study
26import dbnp.studycapturing.Subject
27import dbnp.studycapturing.Event
28import dbnp.studycapturing.Protocol
29import dbnp.studycapturing.Sample
30import dbnp.studycapturing.TemplateFieldType
[215]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
[268]59        session.workbook = wb
[215]60
61        render (view:"step1", model:[header:header, datamatrix:datamatrix])
62    }
63
64    /**
[256]65    * User has assigned all entities and templatefieldtypes to the columns and continues to the next step (assigning properties to columns)
[251]66    * All information of the columns is stored in a session as MappingColumn object
[215]67    *
[245]68    * @param entity list of entities and columns it has been assigned to (columnindex:entitytype format)
[256]69    * @param templatefieldtype list of celltypes and columns it has been assigned to (columnindex:templatefieldtype format)
[215]70    * @return properties page
[251]71    *
72    * @see celltype: http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html
[215]73    */
[256]74    def savepreview = {
75        def tft = null
[215]76        def entities  = request.getParameterValues("entity")
[256]77        def templatefieldtypes = request.getParameterValues("templatefieldtype")
[215]78
[256]79        templatefieldtypes.each { t ->
80            def temp = t.split(":")
81            def templatefieldtype = temp[1]         
[245]82           
[256]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
[251]100                default: break
101            }
[256]102            session.header[temp[0].toInteger()].templatefieldtype = tft
[245]103        }
104
105        entities.each { e ->
106            def temp = e.split(":")
[256]107            Class clazz     
[251]108
109            switch (temp[1].toInteger()) {
[245]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
[251]120                default: clazz = Object
[256]121                        break
[245]122            }
[251]123
124            session.header[temp[0].toInteger()].index = temp[0].toInteger()
[245]125            session.header[temp[0].toInteger()].entity = clazz
126        }
127
[215]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    }
[245]135
136    /**
137    * @param columnproperty array of columns and the assigned property in `column:property_id` format
138    *
139    */
140    def saveproperties = {
[256]141        def columnproperties  = request.getParameterValues("columnproperty")
[245]142
143        columnproperties.each { cp ->
144                def temp = cp.split(":")
[251]145                session.header[temp[0].toInteger()].property = temp[1].toInteger()
146        }
147       
[268]148        /*for (e in session.header) {
[245]149            println e
[268]150        }*/
151
152        //import workbook
153        ImporterService.importdata(session.importtemplate_id, session.workbook, 0, 0, session.header)
154        //println session.header.dump()
155
[245]156        render ("properties saved")
157    }
[215]158}
Note: See TracBrowser for help on using the repository browser.