source: trunk/grails-app/taglib/dbnp/importer/ImporterTagLib.groovy @ 1481

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