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

Last change on this file since 1202 was 1202, checked in by t.w.abma@…, 11 years ago
  • removed extra step and implemented a failed cells check in step 3 (missing mappings) which shows the failed ontologies in the table editor (might have to tune appearance)
  • Property svn:keywords set to Author Date Rev
File size: 7.8 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: 1202 $
12 * $Author: t.w.abma@umcutrecht.nl $
13 * $Date: 2010-11-25 14:51:14 +0000 (do, 25 nov 2010) $
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        def failedcells = attrs['failedcells']
64        out << render (template:"common/missingproperties", model:[datamatrix:datamatrix, failedcells:failedcells])
65    }
66
67    /**
68     * Show failed cells
69     */
70    def failedCells = { attrs ->
71        def failedcells = attrs['failedcells']
72        out << render (template:"common/failedcells", model:[failedcells:failedcells])
73    }
74
75    /**
76     * @param entities array containing selected entities
77     * @param header array containing mappingcolumn objects
78     * @param allfieldtypes if set, show all fields
79     * @param layout constant value: "horizontal" or "vertical"
80     */
81    def properties = { attrs ->
82        def header = attrs['header']
83        def entities = attrs['entities']
84        def allfieldtypes = (attrs['allfieldtypes']==null) ? "false" : "true"
85        def layout = (attrs['layout']==null) ? "vertical" : attrs['layout']
86
87        //choose template for vertical layout (default) or horizontal layout
88        def template = (layout == "vertical") ? "common/properties_vertical" : "common/properties_horizontal"
89       
90        out << render ( template:template,
91                        model:[selectedentities:entities,
92                        standardentities:grailsApplication.config.gscf.domain.importableEntities,
93                        header:header,
94                        allfieldtypes:allfieldtypes,
95                        layout:layout]
96                        )
97    }
98
99    /**
100     * Possibly this will later on return an AJAX-like autocompletion chooser for the fields?
101     *
102     * @param name name for the property chooser element
103     * @param importtemplate_id template identifier where fields are retrieved from
104     * @param matchvalue value which will be looked up via fuzzy matching against the list of options and will be selected
105     * @param MappingColumn object containing all required information
106     * @param allfieldtypes boolean true if all templatefields should be listed, otherwise only show filtered templatefields
107     * @return chooser object
108     * */
109    def propertyChooser = { attrs ->
110        // TODO: this should be changed to retrieving fields per entity instead of from one template
111        //       and session variables should not be used inside the service, migrate to controller
112
113        def t = Template.get(session.importer_template_id)
114        def mc = attrs['mappingcolumn']
115        def allfieldtypes = attrs['allfieldtypes']
116        def matchvalue = attrs['matchvalue']
117        def domainfields = mc.entity.giveDomainFields().findAll { it.type == mc.templatefieldtype }
118            domainfields = domainfields.findAll { it.preferredIdentifier != mc.identifier}
119
120        //def templatefields = (allfieldtypes=="true") ? t.fields : t.fields.findAll { it.type == mc.templatefieldtype }
121        def templatefields = (allfieldtypes=="true") ? 
122            t.fields + mc.entity.giveDomainFields() :
123            t.fields.findAll { it.type == mc.templatefieldtype } + domainfields
124
125        // map identifier to preferred column
126        def prefcolumn = mc.entity.giveDomainFields().findAll { it.preferredIdentifier == true }
127
128        (mc.identifier) ? out << createPropertySelect(attrs['name'], prefcolumn, matchvalue, mc.index) :
129            out << createPropertySelect(attrs['name'], templatefields, matchvalue, mc.index)
130    }
131
132    /**
133     * Create the property chooser select element
134     *
135     * @param name name of the HTML select object
136     * @param options list of options (fields) to be used
137     * @param matchvalue value which will be looked up via fuzzy matching against the list of options and will be selected
138     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
139     * @return HTML select object
140     */
141    def createPropertySelect(String name, options, matchvalue, Integer columnIndex)
142    {
143        // Determine which field in the options list matches the best with the matchvalue
144        def mostsimilar = ImporterService.mostSimilar(matchvalue, options)
145
146        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${columnIndex}\">"
147
148        res += "<option value=\"dontimport\">Don't import</option>"
149
150        options.each { f ->
151            res+= "<option value=\"${f.name}\""
152
153            res+= (mostsimilar.toString().toLowerCase()==f.name.toLowerCase()) ?
154                    " selected>" :
155                    ">"
156           
157            res+= (f.preferredIdentifier) ? 
158                    "${f.name} (IDENTIFIER)</option>" :
159                    "${f.name}</option>"
160        }
161
162        res += "</select>"
163        return res
164    }
165
166    /**
167    * @param selected selected TemplateFieldType
168    * @param custval custom value to be combined in the option(s) of the selector
169    * @param name name of the HTML select object
170    * @return HTML select object
171    *
172    * @see dbnp.studycapturing.TemplateFieldType
173    */
174
175     def entitySelect = { attrs ->
176        def sel = (attrs['selected']==null) ? -1 : attrs['selected']
177        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
178        def name = (attrs['name']==null) ? -1 : attrs['name']
179
180        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
181
182        grailsApplication.config.gscf.domain.importableEntities.each { e ->
183            res += "<option value=\"${e.value.name}\""
184            res += (e.value.type == sel) ? " selected" : ""
185            res += ">${e.value.name}</option>"
186        }
187
188        res += "</select>"
189        out << res
190    }
191
192    /**
193     * Create a templatefieldtype selector
194     *
195    * @param selected selected TemplateFieldType
196    * @param customvalue custom value to be combined in the option(s) of the selector
197    * @param name name of the HTML select object
198    * @return HTML select object
199    *
200    * @see dbnp.studycapturing.TemplateFieldType
201    */
202    def templatefieldtypeSelect = { attrs ->
203        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
204        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
205        def name = (attrs['name']==null) ? "" : attrs['name']   
206
207        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
208
209        TemplateFieldType.list().each { e ->
210            res += "<option value=\"${e}\""
211            res += (e == selected) ? " selected" : ""
212            res += ">${e}</option>"
213        }
214
215        res += "</select>"
216
217        out << res
218    }
219
220    /**
221    * @param cell Cell variable
222    * @return good representation of variable (instead of toString())
223    */
224    def displayCell = { attrs ->       
225        def cell = attrs['cell']
226        def df = new DataFormatter()
227
228        switch (cell.getCellType()) {
229            case Cell.CELL_TYPE_STRING  :   out << cell.getStringCellValue()
230                                                    break
231            case Cell.CELL_TYPE_NUMERIC :   out << df.formatCellValue(cell)
232                                                    break
233        }
234    }
235}
Note: See TracBrowser for help on using the repository browser.