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

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

- Importermappings can now be fully retrieved and stored in step 2 of the Importer Wizard

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