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

Last change on this file since 1058 was 1058, checked in by t.w.abma@…, 13 years ago
  • added failed cells view/logic
  • fixed Study-owner bug
  • Property svn:keywords set to Author Date Rev
File size: 7.1 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: 1058 $
12 * $Author: t.w.abma@umcutrecht.nl $
13 * $Date: 2010-11-03 13:03:20 +0000 (wo, 03 nov 2010) $
14 */
15
16package dbnp.importer
17import dbnp.studycapturing.Template
18import dbnp.studycapturing.TemplateField
19import dbnp.studycapturing.TemplateFieldType
20import org.apache.poi.hssf.usermodel.HSSFCell
21import org.apache.poi.ss.usermodel.DataFormatter
22
23class ImporterTagLib {
24    static namespace = 'importer'
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 MappingColumn object containing all required information
104     * @param allfieldtypes boolean true if all templatefields should be listed, otherwise only show filtered templatefields
105     * @return chooser object
106     * */
107    def propertyChooser = { attrs ->
108        // TODO: this should be changed to retrieving fields per entity instead of from one template
109        //       and session variables should not be used inside the service, migrate to controller
110
111        def t = Template.get(session.importer_template_id)
112        def mc = attrs['mappingcolumn']
113        def allfieldtypes = attrs['allfieldtypes']
114        def domainfields = mc.entity.giveDomainFields().findAll { it.type == mc.templatefieldtype }
115            domainfields = domainfields.findAll { it.preferredIdentifier != mc.identifier}
116
117        //def templatefields = (allfieldtypes=="true") ? t.fields : t.fields.findAll { it.type == mc.templatefieldtype }
118        def templatefields = (allfieldtypes=="true") ? 
119            t.fields + mc.entity.giveDomainFields() :
120            t.fields.findAll { it.type == mc.templatefieldtype } + domainfields
121
122        // map identifier to preferred column
123        def prefcolumn = mc.entity.giveDomainFields().findAll { it.preferredIdentifier == true }
124
125        (mc.identifier) ? out << createPropertySelect(attrs['name'], prefcolumn, mc.index) :
126            out << createPropertySelect(attrs['name'], templatefields, mc.index)
127    }
128
129    /**
130     * Create the property chooser select element
131     *
132     * @param name name of the HTML select object
133     * @param options list of options (fields) to be used
134     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
135     * @return HTML select object
136     */
137    def createPropertySelect(String name, options, Integer columnIndex)
138    {   
139        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${columnIndex}\">"
140
141        res += "<option value=\"dontimport\" selected>Don't import</option>"
142
143        options.each { f ->
144            res+= "<option value=\"${f.name}\">"
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 dbnp.studycapturing.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        grailsApplication.config.gscf.domain.importableEntities.each { e ->
172            res += "<option value=\"${e.value.name}\""
173            res += (e.value.type == sel) ? " selected" : ""
174            res += ">${e.value.name}</option>"
175        }
176
177        res += "</select>"
178        out << res
179    }
180
181    /**
182     * Create a templatefieldtype selector
183     *
184    * @param selected selected TemplateFieldType
185    * @param customvalue custom value to be combined in the option(s) of the selector
186    * @param name name of the HTML select object
187    * @return HTML select object
188    *
189    * @see dbnp.studycapturing.TemplateFieldType
190    */
191    def templatefieldtypeSelect = { attrs ->
192        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
193        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
194        def name = (attrs['name']==null) ? "" : attrs['name']   
195
196        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
197
198        TemplateFieldType.list().each { e ->
199            res += "<option value=\"${e}\""
200            res += (e == selected) ? " selected" : ""
201            res += ">${e}</option>"
202        }
203
204        res += "</select>"
205
206        out << res
207    }
208
209    /**
210    * @param cell HSSFCell variable
211    * @return good representation of variable (instead of toString())
212    */
213    def displayCell = { attrs ->       
214        def cell = attrs['cell']
215        def df = new DataFormatter()
216
217        switch (cell.getCellType()) {
218            case HSSFCell.CELL_TYPE_STRING      :   out << cell.getStringCellValue()
219                                                    break
220            case HSSFCell.CELL_TYPE_NUMERIC     :   out << df.formatCellValue(cell)
221                                                    break
222        }
223    }
224}
Note: See TracBrowser for help on using the repository browser.