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

Last change on this file since 534 was 534, checked in by tabma, 11 years ago
  • rewrote MappingColumn? (and underlying methods) to have a String property instead of TemplateField?
  • improved Excel-column detection to importer service
  • Property svn:keywords set to Date Author Rev
File size: 5.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: 534 $
12 * $Author: tabma $
13 * $Date: 2010-06-04 14:00:31 +0000 (vr, 04 jun 2010) $
14 */
15
16package dbnp.importer
17import dbnp.studycapturing.Template
18import dbnp.studycapturing.TemplateFieldType
19import org.apache.poi.hssf.usermodel.HSSFCell
20import org.apache.poi.ss.usermodel.DataFormatter
21
22class ImporterTagLib {
23    static namespace = 'importer'
24
25    /**
26    * @param header string array containing header
27    * @param datamatrix two dimensional array containing actual data
28    * @return preview of the data with the ability to adjust the datatypes
29    */
30    def preview = { attrs ->
31       
32        def header = attrs['header']
33        def datamatrix = attrs['datamatrix']
34
35        out << render (template:"common/preview", model:[header:header, datamatrix:datamatrix])
36    }
37
38    /**
39     * @param datamatrix two dimensional array containing entities with read values
40     * @return postview of the imported data
41     */
42    def postview = { attrs ->
43        def datamatrix = attrs['datamatrix']
44
45        out << render (template:"common/postview", model:[datamatrix:datamatrix])
46    }
47
48    def entity = { attrs ->
49        out << entities[attrs['index']].name
50    }
51
52    /**
53     * @param entities array containing selected entities
54     */
55    def properties = { attrs ->
56        def header = attrs['header']
57        def entities = attrs['entities']
58        def allfieldtypes = (attrs['allfieldtypes']==null) ? "false" : "true"
59       
60
61        out << render ( template:"common/properties",
62                        model:[selectedentities:entities,
63                        standardentities:grailsApplication.config.gscf.domain.importableEntities,
64                        header:header,
65                        allfieldtypes:allfieldtypes]
66                        )
67    }
68
69    /**
70     * Possibly this will later on return an AJAX-like autocompletion chooser for the fields?
71     *
72     * @param name name for the property chooser element
73     * @param importtemplate_id template identifier where fields are retrieved from
74     * @param MappingColumn object containing all required information
75     * @param allfieldtypes boolean true if all templatefields should be listed, otherwise only show filtered templatefields
76     * @return chooser object
77     * */
78    def propertyChooser = { attrs ->
79        // TODO: this should be changed to retrieving fields per entity instead of from one template
80        //       and session variables should not be used inside the service, migrate to controller
81
82        def t = Template.get(session.importer_template_id)
83        def mc = attrs['mappingcolumn']
84        def allfieldtypes = attrs['allfieldtypes']
85
86        //def templatefields = (allfieldtypes=="true") ? t.fields : t.fields.findAll { it.type == mc.templatefieldtype }
87        def templatefields = (allfieldtypes=="true") ? 
88            t.fields + mc.entity.giveDomainFields() :
89            t.fields.findAll { it.type == mc.templatefieldtype } + mc.entity.giveDomainFields()
90
91        (mc.identifier) ? out << "<select style=\"font-size:10px\" name=\"\" disabled><option>Identifier</option></select>":
92            out << createPropertySelect(attrs['name'], templatefields, mc.index)
93    }
94
95    /**
96     * Create the property chooser select element
97     *
98     * @param name name of the HTML select object
99     * @param options list of options (fields) to be used
100     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
101     * @return HTML select object
102     */
103    def createPropertySelect(String name, options, Integer columnIndex)
104    {   
105        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${columnIndex}\">"
106
107        options.each { f ->
108            res+= "<option value=\"${f.name}\">"
109           
110            res+= (f.preferredIdentifier) ? 
111                    "${f.name} (IDENTIFIER)</option>" :
112                    "${f.name}</option>"
113        }
114
115        res += "</select>"
116        return res
117    }
118
119    /**
120    * @param selected selected TemplateFieldType
121    * @param custval custom value to be combined in the option(s) of the selector
122    * @param name name of the HTML select object
123    * @return HTML select object
124    *
125    * @see dbnp.studycapturing.TemplateFieldType
126    */
127
128     def entitySelect = { attrs ->
129        def sel = (attrs['selected']==null) ? -1 : attrs['selected']
130        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
131        def name = (attrs['name']==null) ? -1 : attrs['name']
132
133        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
134
135        grailsApplication.config.gscf.domain.importableEntities.each { e ->
136            res += "<option value=\"${e.value.name}\""
137            res += (e.value.type == sel) ? " selected" : ""
138            res += ">${e.value.name}</option>"
139        }
140
141        res += "</select>"
142        out << res
143    }
144
145    /**
146     * Create a templatefieldtype selector
147     *
148    * @param selected selected TemplateFieldType
149    * @param customvalue custom value to be combined in the option(s) of the selector
150    * @param name name of the HTML select object
151    * @return HTML select object
152    *
153    * @see dbnp.studycapturing.TemplateFieldType
154    */
155    def templatefieldtypeSelect = { attrs ->
156        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
157        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
158        def name = (attrs['name']==null) ? "" : attrs['name']   
159
160        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
161
162        TemplateFieldType.list().each { e ->
163            res += "<option value=\"${e}\""
164            res += (e == selected) ? " selected" : ""
165            res += ">${e}</option>"
166        }
167
168        res += "</select>"
169
170        out << res
171    }
172
173    /**
174    * @param cell HSSFCell variable
175    * @return good representation of variable (instead of toString())
176    */
177    def displayCell = { attrs ->       
178        def cell = attrs['cell']
179        def df = new DataFormatter()
180
181        switch (cell.getCellType()) {
182            case HSSFCell.CELL_TYPE_STRING      :   out << cell.getStringCellValue()
183                                                    break
184            case HSSFCell.CELL_TYPE_NUMERIC     :   out << df.formatCellValue(cell)
185                                                    break
186        }
187    }
188}
Note: See TracBrowser for help on using the repository browser.