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

Last change on this file since 299 was 299, checked in by duh, 14 years ago
  • set keyword expansion
  • Property svn:keywords set to Date Rev Author
File size: 5.2 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: 299 $
12 * $Author: duh $
13 * $Date: 2010-03-22 13:40:35 +0000 (ma, 22 mrt 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 columnindex column in the header we're talking about
75     * @return chooser object
76     * */
77    def propertyChooser = { attrs ->
78        // TODO: this should be changed to retrieving fields per entity
79        def t = Template.get(session.importtemplate_id)
80        def columnindex = attrs['columnindex']
81
82        switch (attrs['entitytype']) {
83            case 0  : createPropertySelect(attrs['name'], t.fields, columnindex)
84                      break
85            case 1  : break
86            case 2  : break
87            case 3  : break
88            default : out << createPropertySelect(attrs['name'], t.fields, columnindex)
89                     break
90        }
91    }
92
93    /**
94     * @param name name of the HTML select object
95     * @param options list of options to be used
96     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
97     * @return HTML select object
98     */
99    def createPropertySelect(String name, options, String columnIndex)
100    {
101        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
102
103        options.each { f ->
104            res += "<option value=\"${columnIndex}:${f.id}\""
105            //res += (e.type.toInteger() == selected) ? " selected" : ""
106            res += ">${f}</option>"
107        }
108
109        res += "</select>"
110        return res
111    }
112
113    /**
114    * @param selected selected TemplateFieldType
115    * @param custval custom value to be combined in the option(s) of the selector
116    * @param name name of the HTML select object
117    * @return HTML select object
118    *
119    * @see dbnp.studycapturing.TemplateFieldType
120    */
121
122     def entitySelect = { attrs ->
123        def sel = (attrs['selected']==null) ? -1 : attrs['selected']
124        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
125        def name = (attrs['name']==null) ? -1 : attrs['name']
126
127        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
128
129        standardentities.each { e ->
130            res += "<option value=\"${custval}:${e.type}\""
131            res += (e.type == sel) ? " selected" : ""
132            res += ">${e.name}</option>"
133        }
134
135        res += "</select>"
136        out << res
137    }
138
139    /**
140    * @param selected selected TemplateFieldType
141    * @param customvalue custom value to be combined in the option(s) of the selector
142    * @param name name of the HTML select object
143    * @return HTML select object
144    *
145    * @see dbnp.studycapturing.TemplateFieldType
146    */
147    def templatefieldtypeSelect = { attrs ->
148        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
149        def customvalue = (attrs['customvalue']==null) ? "" : attrs['customvalue']
150        def name = (attrs['name']==null) ? "" : attrs['name']   
151
152        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
153
154        TemplateFieldType.list().each { e ->
155            res += "<option value=\"${customvalue}:${e}\""
156            res += (e == selected) ? " selected" : ""
157            res += ">${e}</option>"
158        }
159
160        res += "</select>"
161
162        out << res
163    }
164
165    /**
166    * @param cell HSSFCell variable
167    * @return good representation of variable (instead of toString())
168    */
169    def displayCell = { attrs ->       
170        def cell = attrs['cell']
171        def df = new DataFormatter()
172
173        switch (cell.getCellType()) {
174            case HSSFCell.CELL_TYPE_STRING      :   out << cell.getStringCellValue()
175                                                    break
176            case HSSFCell.CELL_TYPE_NUMERIC     :   out << df.formatCellValue(cell)
177                                                    break
178        }
179    }
180}
Note: See TracBrowser for help on using the repository browser.