root/trunk/grails-app/taglib/dbnp/importer/ImporterTagLib.groovy @ 1087

Revision 1087, 7.1 KB (checked in by s.h.sikkema@…, 3 years ago)

Added xlsx import functionality

  • Property svn:keywords set to Author Date Rev
Line 
1/**
2 * Importer tag library
3 *
4 * The importer tag library gives support for automating several 'components'
5 *
6 * @package     importer
7 * @author      t.w.abma@umcutrecht.nl
8 * @since       20100202
9 *
10 * Revision information:
11 * $Rev$
12 * $Author$
13 * $Date$
14 */
15
16package dbnp.importer
17import dbnp.studycapturing.Template
18import dbnp.studycapturing.TemplateFieldType
19import org.apache.poi.ss.usermodel.Cell
20import org.apache.poi.ss.usermodel.DataFormatter
21
22class ImporterTagLib {
23    static namespace = 'importer'
24
25    /**
26    * @param header string array containing header
27    * @param datamatrix two dimensional array containing actual data
28    * @return preview of the data with the ability to adjust the datatypes
29    */
30    def preview = { attrs ->
31       
32        def header = attrs['header']
33        def datamatrix = attrs['datamatrix']
34
35        out << render (template:"common/preview", model:[header:header, datamatrix:datamatrix])
36    }
37
38    /**
39     * @param datamatrix two dimensional array containing entities with read values
40     * @return postview of the imported data
41     */
42    def postview = { attrs ->
43        def datamatrix = attrs['datamatrix']
44
45        out << render (template:"common/postview", model:[datamatrix:datamatrix])
46    }
47
48    def entity = { attrs ->
49        out << entities[attrs['index']].name
50    }
51
52    def datapreview = { attrs ->
53        def datamatrix = attrs['datamatrix']
54        out << render (template:"common/datapreview", model:[datamatrix:datamatrix])
55    }
56
57    /**
58     * Show missing properties
59     */
60    def missingProperties = { attrs ->
61        def datamatrix = attrs['datamatrix']   
62        out << render (template:"common/missingproperties", model:[datamatrix:datamatrix])
63    }
64
65    /**
66     * Show failed cells
67     */
68    def failedCells = { attrs ->
69        def failedcells = attrs['failedcells']
70        out << render (template:"common/failedcells", model:[failedcells:failedcells])
71    }
72
73    /**
74     * @param entities array containing selected entities
75     * @param header array containing mappingcolumn objects
76     * @param allfieldtypes if set, show all fields
77     * @param layout constant value: "horizontal" or "vertical"
78     */
79    def properties = { attrs ->
80        def header = attrs['header']
81        def entities = attrs['entities']
82        def allfieldtypes = (attrs['allfieldtypes']==null) ? "false" : "true"
83        def layout = (attrs['layout']==null) ? "vertical" : attrs['layout']
84
85        //choose template for vertical layout (default) or horizontal layout
86        def template = (layout == "vertical") ? "common/properties_vertical" : "common/properties_horizontal"
87       
88        out << render ( template:template,
89                        model:[selectedentities:entities,
90                        standardentities:grailsApplication.config.gscf.domain.importableEntities,
91                        header:header,
92                        allfieldtypes:allfieldtypes,
93                        layout:layout]
94                        )
95    }
96
97    /**
98     * Possibly this will later on return an AJAX-like autocompletion chooser for the fields?
99     *
100     * @param name name for the property chooser element
101     * @param importtemplate_id template identifier where fields are retrieved from
102     * @param MappingColumn object containing all required information
103     * @param allfieldtypes boolean true if all templatefields should be listed, otherwise only show filtered templatefields
104     * @return chooser object
105     * */
106    def propertyChooser = { attrs ->
107        // TODO: this should be changed to retrieving fields per entity instead of from one template
108        //       and session variables should not be used inside the service, migrate to controller
109
110        def t = Template.get(session.importer_template_id)
111        def mc = attrs['mappingcolumn']
112        def allfieldtypes = attrs['allfieldtypes']
113        def domainfields = mc.entity.giveDomainFields().findAll { it.type == mc.templatefieldtype }
114            domainfields = domainfields.findAll { it.preferredIdentifier != mc.identifier}
115
116        //def templatefields = (allfieldtypes=="true") ? t.fields : t.fields.findAll { it.type == mc.templatefieldtype }
117        def templatefields = (allfieldtypes=="true") ?
118            t.fields + mc.entity.giveDomainFields() :
119            t.fields.findAll { it.type == mc.templatefieldtype } + domainfields
120
121        // map identifier to preferred column
122        def prefcolumn = mc.entity.giveDomainFields().findAll { it.preferredIdentifier == true }
123
124        (mc.identifier) ? out << createPropertySelect(attrs['name'], prefcolumn, mc.index) :
125            out << createPropertySelect(attrs['name'], templatefields, mc.index)
126    }
127
128    /**
129     * Create the property chooser select element
130     *
131     * @param name name of the HTML select object
132     * @param options list of options (fields) to be used
133     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
134     * @return HTML select object
135     */
136    def createPropertySelect(String name, options, Integer columnIndex)
137    {   
138        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${columnIndex}\">"
139
140        res += "<option value=\"dontimport\" selected>Don't import</option>"
141
142        options.each { f ->
143            res+= "<option value=\"${f.name}\">"
144           
145            res+= (f.preferredIdentifier) ?
146                    "${f.name} (IDENTIFIER)</option>" :
147                    "${f.name}</option>"
148        }
149
150        res += "</select>"
151        return res
152    }
153
154    /**
155    * @param selected selected TemplateFieldType
156    * @param custval custom value to be combined in the option(s) of the selector
157    * @param name name of the HTML select object
158    * @return HTML select object
159    *
160    * @see dbnp.studycapturing.TemplateFieldType
161    */
162
163     def entitySelect = { attrs ->
164        def sel = (attrs['selected']==null) ? -1 : attrs['selected']
165        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
166        def name = (attrs['name']==null) ? -1 : attrs['name']
167
168        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
169
170        grailsApplication.config.gscf.domain.importableEntities.each { e ->
171            res += "<option value=\"${e.value.name}\""
172            res += (e.value.type == sel) ? " selected" : ""
173            res += ">${e.value.name}</option>"
174        }
175
176        res += "</select>"
177        out << res
178    }
179
180    /**
181     * Create a templatefieldtype selector
182     *
183    * @param selected selected TemplateFieldType
184    * @param customvalue custom value to be combined in the option(s) of the selector
185    * @param name name of the HTML select object
186    * @return HTML select object
187    *
188    * @see dbnp.studycapturing.TemplateFieldType
189    */
190    def templatefieldtypeSelect = { attrs ->
191        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
192        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
193        def name = (attrs['name']==null) ? "" : attrs['name']   
194
195        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
196
197        TemplateFieldType.list().each { e ->
198            res += "<option value=\"${e}\""
199            res += (e == selected) ? " selected" : ""
200            res += ">${e}</option>"
201        }
202
203        res += "</select>"
204
205        out << res
206    }
207
208    /**
209    * @param cell Cell variable
210    * @return good representation of variable (instead of toString())
211    */
212    def displayCell = { attrs ->       
213        def cell = attrs['cell']
214        def df = new DataFormatter()
215
216        switch (cell.getCellType()) {
217            case Cell.CELL_TYPE_STRING  :   out << cell.getStringCellValue()
218                                                    break
219            case Cell.CELL_TYPE_NUMERIC :   out << df.formatCellValue(cell)
220                                                    break
221        }
222    }
223}
Note: See TracBrowser for help on using the browser.