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

Last change on this file since 415 was 415, checked in by tabma, 11 years ago
  • refactored code and templates of the Importer Wizard, now makes use of indexed formparameters
  • Property svn:keywords set to Date Author Rev
File size: 5.5 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: 415 $
12 * $Author: tabma $
13 * $Date: 2010-05-12 11:23:35 +0000 (wo, 12 mei 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    def standardentities = [[type:-1, name:"Don't import"], [type:0, name:"Study"], [type:1, name:"Subject"], [type:2, name:"Event"],
25                        [type:3, name:"Protocol"], [type:4, name:"Sample"]]
26
27    /**
28    * @param header string array containing header
29    * @param datamatrix two dimensional array containing actual data
30    * @return preview of the data with the ability to adjust the datatypes
31    */
32    def preview = { attrs ->
33       
34        def header = attrs['header']
35        def datamatrix = attrs['datamatrix']
36
37        out << render (template:"common/preview", model:[header:header, datamatrix:datamatrix])
38    }
39
40    /**
41     * @param datamatrix two dimensional array containing entities with read values
42     * @return postview of the imported data
43     */
44    def postview = { attrs ->
45        def datamatrix = attrs['datamatrix']
46
47        out << render (template:"common/postview", model:[datamatrix:datamatrix])
48    }
49
50    def entity = { attrs ->
51        out << entities[attrs['index']].name
52    }
53
54    /**
55     * @param entities array
56     */
57    def properties = { attrs ->
58        def selectedentities = []
59        def header = attrs['header']
60
61        attrs['entities'].index.each { columnindex, entitytype ->
62            def entity = [type:entitytype,columnindex:columnindex.toInteger()]
63            selectedentities.add(entity)
64        }
65
66        out << render (template:"common/properties", model:[selectedentities:selectedentities, standardentities:standardentities, header:header])
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     * @return chooser object
76     * */
77    def propertyChooser = { attrs ->
78        // TODO: this should be changed to retrieving fields per entity instead of from one template
79        //       and session variables should not be used inside the service, migrate to controller
80
81        def t = Template.get(session.importer_template_id)
82        def mc = attrs['mappingcolumn']
83
84        (mc.identifier) ? out << "<select style=\"font-size:10px\" name=\"\" disabled><option>Identifier</option></select>":
85            out << createPropertySelect(attrs['name'], t.fields.findAll { it.type == mc.templatefieldtype }, mc.index)
86    }
87
88    /**
89     * Create the property chooser select element
90     *
91     * @param name name of the HTML select object
92     * @param options list of options (fields) to be used
93     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
94     * @return HTML select object
95     */
96    def createPropertySelect(String name, options, Integer columnIndex)
97    {   
98        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${columnIndex}\">"
99
100        options.each { f ->         
101            res += "<option value=\"${f.id}\""
102            //res += (e.type.toInteger() == selected) ? " selected" : ""
103            res += ">${f}</option>"
104        }
105
106        res += "</select>"
107        return res
108    }
109
110    /**
111    * @param selected selected TemplateFieldType
112    * @param custval custom value to be combined in the option(s) of the selector
113    * @param name name of the HTML select object
114    * @return HTML select object
115    *
116    * @see dbnp.studycapturing.TemplateFieldType
117    */
118
119     def entitySelect = { attrs ->
120        def sel = (attrs['selected']==null) ? -1 : attrs['selected']
121        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
122        def name = (attrs['name']==null) ? -1 : attrs['name']
123
124        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
125
126        standardentities.each { e ->
127            res += "<option value=\"${e.type}\""
128            res += (e.type == sel) ? " selected" : ""
129            res += ">${e.name}</option>"
130        }
131
132        res += "</select>"
133        out << res
134    }
135
136    /**
137     * Create a templatefieldtype selector
138     *
139    * @param selected selected TemplateFieldType
140    * @param customvalue custom value to be combined in the option(s) of the selector
141    * @param name name of the HTML select object
142    * @return HTML select object
143    *
144    * @see dbnp.studycapturing.TemplateFieldType
145    */
146    def templatefieldtypeSelect = { attrs ->
147        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
148        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
149        def name = (attrs['name']==null) ? "" : attrs['name']   
150
151        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
152
153        TemplateFieldType.list().each { e ->
154            res += "<option value=\"${e}\""
155            res += (e == selected) ? " selected" : ""
156            res += ">${e}</option>"
157        }
158
159        res += "</select>"
160
161        out << res
162    }
163
164    /**
165    * @param cell HSSFCell variable
166    * @return good representation of variable (instead of toString())
167    */
168    def displayCell = { attrs ->       
169        def cell = attrs['cell']
170        def df = new DataFormatter()
171
172        switch (cell.getCellType()) {
173            case HSSFCell.CELL_TYPE_STRING      :   out << cell.getStringCellValue()
174                                                    break
175            case HSSFCell.CELL_TYPE_NUMERIC     :   out << df.formatCellValue(cell)
176                                                    break
177        }
178    }
179}
Note: See TracBrowser for help on using the repository browser.