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

Last change on this file since 449 was 449, checked in by tabma, 14 years ago
  • first page of simple import wizard working with entity-/templatefilter
  • Property svn:keywords set to Date Author Rev
File size: 5.7 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: 449 $
17 * $Author: tabma $
18 * $Date: 2010-05-20 12:56:52 +0000 (do, 20 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
31import grails.converters.JSON
32
33class ImporterController {
34    def ImporterService   
35
36    /**
37     * Default page
38     **/
39    def index = {       
40    }
41
42    def simplewizard = {
43        render(view:"index_simple", model:[studies:Study.list(), entities: grailsApplication.config.gscf.domain.importableEntities])
44    }
45
46    def advancedwizard = {
47        render(view:"index_advanced", model:[templates:Template.list()])
48    }
49
50    /**
51    * This method will move the uploaded file to a temporary path and send the header
52    * and the first n rows to the preview
53    * @param importfile uploaded file to import
54    */
55    def upload = {
56        def downloadedfile = request.getFile('importfile');
57        def tempfile = new File(System.getProperty('java.io.tmpdir') + File.separatorChar + System.currentTimeMillis() + ".nmcdsp")
58        downloadedfile.transferTo(tempfile)
59       
60        def wb = ImporterService.getWorkbook(new FileInputStream(tempfile))
61       
62        def header = ImporterService.getHeader(wb, 0)
63        def datamatrix= ImporterService.getDatamatrix(wb, 0, 5)
64
65        session.importer_header = header
66        session.importer_template_id = params.template_id
67        session.importer_workbook = wb
68
69        render (view:"step1", model:[header:header, datamatrix:datamatrix])
70    }
71
72    /**
73    * User has assigned all entities and templatefieldtypes to the columns and continues to the next step (assigning properties to columns)
74    * All information of the columns is stored in a session as MappingColumn object
75    *
76    * @param entity list of entities and columns it has been assigned to (columnindex:entitytype format)
77    * @param templatefieldtype list of celltypes and columns it has been assigned to (columnindex:templatefieldtype format)
78    * @return properties page
79    *
80    * @see celltype: http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html
81    */
82    def savepreview = {
83        def tft = null 
84        def identifiercolumnindex = (params.identifier!=null) ? params.identifier.toInteger() : -1
85
86        params.templatefieldtype.index.each { columnindex, _templatefieldtype ->
87            switch (_templatefieldtype) {
88                case "STRING"       : tft = TemplateFieldType.STRING
89                                      break
90                case "TEXT"         : tft = TemplateFieldType.TEXT
91                                      break
92                case "INTEGER"      : tft = TemplateFieldType.INTEGER
93                                      break
94                case "FLOAT"        : tft = TemplateFieldType.FLOAT
95                                      break
96                case "DOUBLE"       : tft = TemplateFieldType.DOUBLE
97                                      break
98                case "STRINGLIST"   : tft = TemplateFieldType.STRINGLIST
99                                      break
100                case "ONTOLOGYTERM" : tft = TemplateFieldType.ONTOLOGYTERM
101                                      break
102                case "DATE"         : tft = TemplateFieldType.DATE
103                                      break
104                default: break
105            }
106           
107            session.importer_header[columnindex.toInteger()].templatefieldtype = tft
108        }
109
110        params.entity.index.each { columnindex, entitytype ->
111            Class clazz
112
113            switch (entitytype.toInteger()) {
114                case 0: clazz = Study
115                        break
116                case 1: clazz = Subject
117                        break
118                case 2: clazz = Event
119                        break
120                case 3: clazz = Protocol
121                        break
122                case 4: clazz = Sample
123                        break
124                default: clazz = Object
125                        break
126            }
127
128            session.importer_header[columnindex.toInteger()].identifier = (columnindex.toInteger() == identifiercolumnindex) ? true : false
129            session.importer_header[columnindex.toInteger()].index = columnindex.toInteger()
130            session.importer_header[columnindex.toInteger()].entity = clazz
131        }
132
133        // currently only one template is used for all entities
134        // TODO: show template fields per entity
135       
136        def templates = Template.get(session.importer_template_id)
137
138        render(view:"step2", model:[entities:params.entity, header:session.importer_header, templates:templates])
139    }
140
141    /**
142    * @param columnproperty array of columns containing index and property_id
143    *
144    */
145    def saveproperties = {     
146        session.importer_study = Study.get(params.study.id.toInteger())
147
148        params.columnproperty.index.each { columnindex, property_id ->
149                session.importer_header[columnindex.toInteger()].property = TemplateField.get(property_id.toInteger())
150        }
151
152        //import workbook
153        session.importer_importeddata = ImporterService.importdata(session.importer_template_id, session.importer_workbook, 0, 1, session.importer_header)
154
155        render(view:"step3", model:[datamatrix:session.importer_importeddata])
156    }
157
158    def savepostview = {
159        ImporterService.saveDatamatrix(session.importer_study, session.importer_importeddata)
160        render(view:"step4")
161    }
162
163    /**
164    * Return templates which belong to a certain entity type
165    *
166    * @param entity entity name string (Sample, Subject, Study et cetera)
167    * @return JSON object containing the found templates
168    */
169    def ajaxGetTemplatesByEntity = {
170        def entityClass = grailsApplication.config.gscf.domain.importableEntities.get(params.entity).entity
171
172        // fetch all templates for a specific entity
173        def templates = Template.findAllByEntity(Class.forName(entityClass, true, this.getClass().getClassLoader()))
174
175        println templates
176       
177        // render as JSON
178        render templates as JSON
179    }
180}
Note: See TracBrowser for help on using the repository browser.