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

Revision 1141, 7.6 KB (checked in by t.w.abma@…, 3 years ago)

- added fuzzy matching to importer tag library

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