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

Last change on this file since 415 was 415, checked in by tabma, 9 years ago
  • refactored code and templates of the Importer Wizard, now makes use of indexed formparameters
  • Property svn:keywords set to Date Author Rev
File size: 4.9 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: 415 $
17 * $Author: tabma $
18 * $Date: 2010-05-12 11:23:35 +0000 (wo, 12 mei 2010) $
19 */
20
21package dbnp.importer
22
23import dbnp.studycapturing.Template
24import dbnp.studycapturing.Study
25import dbnp.studycapturing.Subject
26import dbnp.studycapturing.Event
27
28import dbnp.studycapturing.Sample
29import dbnp.studycapturing.TemplateFieldType
30import dbnp.studycapturing.TemplateField
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.importer_header = header
58        session.importer_template_id = params.template_id
59        session.importer_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 identifiercolumnindex = (params.identifier!=null) ? params.identifier.toInteger() : -1
77
78        params.templatefieldtype.index.each { columnindex, _templatefieldtype ->
79            switch (_templatefieldtype) {
80                case "STRING"       : tft = TemplateFieldType.STRING
81                                      break
82                case "TEXT"         : tft = TemplateFieldType.TEXT
83                                      break
84                case "INTEGER"      : tft = TemplateFieldType.INTEGER
85                                      break
86                case "FLOAT"        : tft = TemplateFieldType.FLOAT
87                                      break
88                case "DOUBLE"       : tft = TemplateFieldType.DOUBLE
89                                      break
90                case "STRINGLIST"   : tft = TemplateFieldType.STRINGLIST
91                                      break
92                case "ONTOLOGYTERM" : tft = TemplateFieldType.ONTOLOGYTERM
93                                      break
94                case "DATE"         : tft = TemplateFieldType.DATE
95                                      break
96                default: break
97            }
98           
99            session.importer_header[columnindex.toInteger()].templatefieldtype = tft
100        }
101
102        params.entity.index.each { columnindex, entitytype ->
103            Class clazz
104
105            switch (entitytype.toInteger()) {
106                case 0: clazz = Study
107                        break
108                case 1: clazz = Subject
109                        break
110                case 2: clazz = Event
111                        break
112                case 3: clazz = Protocol
113                        break
114                case 4: clazz = Sample
115                        break
116                default: clazz = Object
117                        break
118            }
119
120            session.importer_header[columnindex.toInteger()].identifier = (columnindex.toInteger() == identifiercolumnindex) ? true : false
121            session.importer_header[columnindex.toInteger()].index = columnindex.toInteger()
122            session.importer_header[columnindex.toInteger()].entity = clazz
123        }
124
125        // currently only one template is used for all entities
126        // TODO: show template fields per entity
127       
128        def templates = Template.get(session.importer_template_id)
129
130        render(view:"step2", model:[entities:params.entity, header:session.importer_header, templates:templates])
131    }
132
133    /**
134    * @param columnproperty array of columns containing index and property_id
135    *
136    */
137    def saveproperties = {     
138        session.importer_study = Study.get(params.study.id.toInteger())
139
140        params.columnproperty.index.each { columnindex, property_id ->
141                session.importer_header[columnindex.toInteger()].property = TemplateField.get(property_id.toInteger())
142        }
143
144        //import workbook
145        session.importer_importeddata = ImporterService.importdata(session.importer_template_id, session.importer_workbook, 0, 1, session.importer_header)
146
147        render(view:"step3", model:[datamatrix:session.importer_importeddata])
148    }
149
150    def savepostview = {
151        ImporterService.saveDatamatrix(session.importer_study, session.importer_importeddata)
152        render(view:"step4")
153    }
154}
Note: See TracBrowser for help on using the repository browser.