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

Last change on this file since 305 was 305, checked in by tabma, 10 years ago
  • templatefieldtypes chosen in the preview page are used as a filter for the properties page (so only matches between the columns/properties can be made if they are of the equal templatefieldtype)
  • columns in the preview which are not imported and/or entities which are not used are not shown anymore in the properties-assignment step in the wizard
  • Property svn:keywords set to Date Rev Author
File size: 5.4 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: 305 $
12 * $Author: tabma $
13 * $Date: 2010-03-23 12:57:18 +0000 (di, 23 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
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.importtemplate_id) 
87        def mc = attrs['mappingcolumn']
88       
89        out << createPropertySelect(attrs['name'], t.fields.findAll { it.type == mc.templatefieldtype }, mc.index)
90    }
91
92    /**
93     * @param name name of the HTML select object
94     * @param options list of options (fields) to be used
95     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
96     * @return HTML select object
97     */
98    def createPropertySelect(String name, options, Integer columnIndex)
99    {   
100        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
101
102        options.each { f ->         
103            res += "<option value=\"${columnIndex}:${f.id}\""
104            //res += (e.type.toInteger() == selected) ? " selected" : ""
105            res += ">${f}</option>"
106        }
107
108        res += "</select>"
109        return res
110    }
111
112    /**
113    * @param selected selected TemplateFieldType
114    * @param custval custom value to be combined in the option(s) of the selector
115    * @param name name of the HTML select object
116    * @return HTML select object
117    *
118    * @see dbnp.studycapturing.TemplateFieldType
119    */
120
121     def entitySelect = { attrs ->
122        def sel = (attrs['selected']==null) ? -1 : attrs['selected']
123        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
124        def name = (attrs['name']==null) ? -1 : attrs['name']
125
126        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
127
128        standardentities.each { e ->
129            res += "<option value=\"${custval}:${e.type}\""
130            res += (e.type == sel) ? " selected" : ""
131            res += ">${e.name}</option>"
132        }
133
134        res += "</select>"
135        out << res
136    }
137
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 customvalue = (attrs['customvalue']==null) ? "" : attrs['customvalue']
149        def name = (attrs['name']==null) ? "" : attrs['name']   
150
151        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
152
153        TemplateFieldType.list().each { e ->
154            res += "<option value=\"${customvalue}:${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.