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: 417 $ |
---|
17 | * $Author: tabma $ |
---|
18 | * $Date: 2010-05-12 14:09:15 +0000 (wo, 12 mei 2010) $ |
---|
19 | */ |
---|
20 | |
---|
21 | package dbnp.importer |
---|
22 | |
---|
23 | import dbnp.studycapturing.Template |
---|
24 | import dbnp.studycapturing.Study |
---|
25 | import dbnp.studycapturing.Subject |
---|
26 | import dbnp.studycapturing.Event |
---|
27 | |
---|
28 | import dbnp.studycapturing.Sample |
---|
29 | import dbnp.studycapturing.TemplateFieldType |
---|
30 | import dbnp.studycapturing.TemplateField |
---|
31 | |
---|
32 | class ImporterController { |
---|
33 | def ImporterService |
---|
34 | |
---|
35 | /** |
---|
36 | * Default page |
---|
37 | **/ |
---|
38 | def index = { |
---|
39 | } |
---|
40 | |
---|
41 | def simplewizard = { |
---|
42 | def entities = ["Subject", "Event", "Sample"] |
---|
43 | render(view:"index_simple", model:[studies:Study.list(), entities:entities]) |
---|
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 | } |
---|