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

Last change on this file since 354 was 354, checked in by keesvb, 9 years ago

removed unneccesary imports from importer ;-) which still disabled clean build

  • Property svn:keywords set to Date Rev Author
File size: 5.3 KB
RevLine 
[169]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: 354 $
12 * $Author: keesvb $
13 * $Date: 2010-04-20 09:30:56 +0000 (di, 20 apr 2010) $
14 */
15
16package dbnp.importer
[231]17import dbnp.studycapturing.Template
[255]18import dbnp.studycapturing.TemplateFieldType
[297]19import org.apache.poi.hssf.usermodel.HSSFCell
20import org.apache.poi.ss.usermodel.DataFormatter
[169]21
22class ImporterTagLib {
23    static namespace = 'importer'
[245]24    def standardentities = [[type:-1, name:"Don't import"], [type:0, name:"Study"], [type:1, name:"Subject"], [type:2, name:"Event"],
[203]25                        [type:3, name:"Protocol"], [type:4, name:"Sample"]]
[169]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
[283]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
[200]50    def entity = { attrs ->
51        out << entities[attrs['index']].name
52    }
53
54    /**
[245]55     * @param entities array in the format of columnindex:entitytype format
[200]56     */
57    def properties = { attrs ->
[203]58        def selectedentities = []
59        def header = attrs['header']
[200]60
[203]61        attrs['entities'].each { se ->
62            def temp = se.split(":")
[245]63            def entity = [type:temp[1],columnindex:temp[0]]
[203]64            selectedentities.add(entity)
[200]65        }
66
[203]67        out << render (template:"common/properties", model:[selectedentities:selectedentities, standardentities:standardentities, header:header])
[200]68    }
69
[231]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
[305]74     * @param MappingColumn object containing all required information
[231]75     * @return chooser object
76     * */
77    def propertyChooser = { attrs ->
[305]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
[200]80
[328]81        def t = Template.get(session.importer_template_id)
[305]82        def mc = attrs['mappingcolumn']
[321]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)
[231]86    }
87
[173]88    /**
[231]89     * @param name name of the HTML select object
[305]90     * @param options list of options (fields) to be used
[231]91     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
92     * @return HTML select object
93     */
[305]94    def createPropertySelect(String name, options, Integer columnIndex)
95    {   
[231]96        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
97
[305]98        options.each { f ->         
[231]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
[259]106    }
[231]107
[259]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
[256]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
[169]132    }
133
[173]134    /**
[259]135    * @param selected selected TemplateFieldType
136    * @param customvalue custom value to be combined in the option(s) of the selector
[173]137    * @param name name of the HTML select object
138    * @return HTML select object
[259]139    *
140    * @see dbnp.studycapturing.TemplateFieldType
[173]141    */
[256]142    def templatefieldtypeSelect = { attrs ->
[173]143        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
[200]144        def customvalue = (attrs['customvalue']==null) ? "" : attrs['customvalue']
[259]145        def name = (attrs['name']==null) ? "" : attrs['name']   
[256]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
[173]158    }
[297]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    }
[169]175}
Note: See TracBrowser for help on using the repository browser.