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

Last change on this file since 359 was 359, checked in by duh, 9 years ago
  • set keyword expansion
  • Property svn:keywords set to Date Author Rev
File size: 5.3 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: 359 $
12 * $Author: duh $
13 * $Date: 2010-04-20 12:42:20 +0000 (di, 20 apr 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 in the format of columnindex:entitytype format
56     */
57    def properties = { attrs ->
58        def selectedentities = []
59        def header = attrs['header']
60
61        attrs['entities'].each { se ->
62            def temp = se.split(":")
63            def entity = [type:temp[1],columnindex:temp[0]]
64            selectedentities.add(entity)
65        }
66
67        out << render (template:"common/properties", model:[selectedentities:selectedentities, standardentities:standardentities, header:header])
68    }
69
70    /**
71     * Possibly this will later on return an AJAX-like autocompletion chooser for the fields?
72     *
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     * @param name name of the HTML select object
90     * @param options list of options (fields) to be used
91     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
92     * @return HTML select object
93     */
94    def createPropertySelect(String name, options, Integer columnIndex)
95    {   
96        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
97
98        options.each { f ->         
99            res += "<option value=\"${columnIndex}:${f.id}\""
100            //res += (e.type.toInteger() == selected) ? " selected" : ""
101            res += ">${f}</option>"
102        }
103
104        res += "</select>"
105        return res
106    }
107
108    /**
109    * @param selected selected TemplateFieldType
110    * @param custval custom value to be combined in the option(s) of the selector
111    * @param name name of the HTML select object
112    * @return HTML select object
113    *
114    * @see dbnp.studycapturing.TemplateFieldType
115    */
116
117     def entitySelect = { attrs ->
118        def sel = (attrs['selected']==null) ? -1 : attrs['selected']
119        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
120        def name = (attrs['name']==null) ? -1 : attrs['name']
121
122        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
123
124        standardentities.each { e ->
125            res += "<option value=\"${custval}:${e.type}\""
126            res += (e.type == sel) ? " selected" : ""
127            res += ">${e.name}</option>"
128        }
129
130        res += "</select>"
131        out << res
132    }
133
134    /**
135    * @param selected selected TemplateFieldType
136    * @param customvalue custom value to be combined in the option(s) of the selector
137    * @param name name of the HTML select object
138    * @return HTML select object
139    *
140    * @see dbnp.studycapturing.TemplateFieldType
141    */
142    def templatefieldtypeSelect = { attrs ->
143        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
144        def customvalue = (attrs['customvalue']==null) ? "" : attrs['customvalue']
145        def name = (attrs['name']==null) ? "" : attrs['name']   
146
147        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
148
149        TemplateFieldType.list().each { e ->
150            res += "<option value=\"${customvalue}:${e}\""
151            res += (e == selected) ? " selected" : ""
152            res += ">${e}</option>"
153        }
154
155        res += "</select>"
156
157        out << res
158    }
159
160    /**
161    * @param cell HSSFCell variable
162    * @return good representation of variable (instead of toString())
163    */
164    def displayCell = { attrs ->       
165        def cell = attrs['cell']
166        def df = new DataFormatter()
167
168        switch (cell.getCellType()) {
169            case HSSFCell.CELL_TYPE_STRING      :   out << cell.getStringCellValue()
170                                                    break
171            case HSSFCell.CELL_TYPE_NUMERIC     :   out << df.formatCellValue(cell)
172                                                    break
173        }
174    }
175}
Note: See TracBrowser for help on using the repository browser.