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

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

- when trying to set duplicate properties, the user gets warned this is not possible

  • 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.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
115                // map identifier to preferred column
116                def prefcolumn = mc.entity.giveDomainFields().findAll { it.preferredIdentifier == true }
117
118                (mc.identifier) ? out << createPropertySelect(attrs['name'], prefcolumn, matchvalue, mc.index) :
119                        out << createPropertySelect(attrs['name'], templatefields, matchvalue, mc.index)
120        }
121
122        /**
123         * Create the property chooser select element
124         *
125         * @param name name of the HTML select object
126         * @param options list of options (fields) to be used
127         * @param matchvalue value which will be looked up via fuzzy matching against the list of options and will be selected
128         * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
129         * @return HTML select object
130         */
131        def createPropertySelect(String name, options, matchvalue, Integer columnIndex) {
132                // Determine which field in the options list matches the best with the matchvalue
133                def mostsimilar = ImporterService.mostSimilar(matchvalue, options)
134
135                def res = "<select style=\"font-size:10px\" id=\"${name}.index.${columnIndex}\" name=\"${name}.index.${columnIndex}\">"
136
137                res += "<option value=\"dontimport\">Don't import</option>"
138
139                options.each { f ->
140                        res += "<option value=\"${f.name}\""
141
142                        res += (mostsimilar.toString().toLowerCase() == f.name.toLowerCase()) ?
143                                " selected>" :
144                                ">"
145
146                        res += (f.preferredIdentifier) ?
147                                "${f.name} (IDENTIFIER)</option>" :
148                                "${f.name}</option>"
149                }
150
151                res += "</select>"
152                return res
153        }
154
155        /**
156         * @param selected selected TemplateFieldType
157         * @param custval custom value to be combined in the option(s) of the selector
158         * @param name name of the HTML select object
159         * @return HTML select object
160         *
161         * @see org.dbnp.gdt.TemplateFieldType
162         */
163
164        def entitySelect = { attrs ->
165                def sel = (attrs['selected'] == null) ? -1 : attrs['selected']
166                def custval = (attrs['customvalue'] == null) ? "" : attrs['customvalue']
167                def name = (attrs['name'] == null) ? -1 : attrs['name']
168
169                def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
170
171                GdtService.getTemplateEntities().each { e ->
172                        res += "<option value\"${e.name}\""
173                        res += ">${e.name} bla</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 org.dbnp.gdt.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.