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

Last change on this file since 328 was 328, checked in by tabma, 9 years ago
  • study can be selected, imported data fancier postview, lazy loading bug fixed (hopefully)
  • Property svn:keywords set to Date Rev Author
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: 328 $
12 * $Author: tabma $
13 * $Date: 2010-04-07 09:24:56 +0000 (wo, 07 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
21import dbnp.studycapturing.Study
22import dbnp.studycapturing.Subject
23import dbnp.studycapturing.Event
24import dbnp.studycapturing.Protocol
25import dbnp.studycapturing.Sample
26
27class ImporterTagLib {
28    static namespace = 'importer'
29    def standardentities = [[type:-1, name:"Don't import"], [type:0, name:"Study"], [type:1, name:"Subject"], [type:2, name:"Event"],
30                        [type:3, name:"Protocol"], [type:4, name:"Sample"]]
31
32    /**
33    * @param header string array containing header
34    * @param datamatrix two dimensional array containing actual data
35    * @return preview of the data with the ability to adjust the datatypes
36    */
37    def preview = { attrs ->
38       
39        def header = attrs['header']
40        def datamatrix = attrs['datamatrix']
41
42        out << render (template:"common/preview", model:[header:header, datamatrix:datamatrix])
43    }
44
45    /**
46     * @param datamatrix two dimensional array containing entities with read values
47     * @return postview of the imported data
48     */
49    def postview = { attrs ->
50        def datamatrix = attrs['datamatrix']
51
52        out << render (template:"common/postview", model:[datamatrix:datamatrix])
53    }
54
55    def entity = { attrs ->
56        out << entities[attrs['index']].name
57    }
58
59    /**
60     * @param entities array in the format of columnindex:entitytype format
61     */
62    def properties = { attrs ->
63        def selectedentities = []
64        def header = attrs['header']
65
66        attrs['entities'].each { se ->
67            def temp = se.split(":")
68            def entity = [type:temp[1],columnindex:temp[0]]
69            selectedentities.add(entity)
70        }
71
72        out << render (template:"common/properties", model:[selectedentities:selectedentities, standardentities:standardentities, header:header])
73    }
74
75    /**
76     * Possibly this will later on return an AJAX-like autocompletion chooser for the fields?
77     *
78     * @param importtemplate_id template identifier where fields are retrieved from
79     * @param MappingColumn object containing all required information
80     * @return chooser object
81     * */
82    def propertyChooser = { attrs ->
83        // TODO: this should be changed to retrieving fields per entity instead of from one template
84        //       and session variables should not be used inside the service, migrate to controller
85
86        def t = Template.get(session.importer_template_id)
87        def mc = attrs['mappingcolumn']
88
89        (mc.identifier) ? out << "<select style=\"font-size:10px\" name=\"\" disabled><option>Identifier</option></select>":
90            out << createPropertySelect(attrs['name'], t.fields.findAll { it.type == mc.templatefieldtype }, mc.index)
91    }
92
93    /**
94     * @param name name of the HTML select object
95     * @param options list of options (fields) 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, Integer 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.