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

Last change on this file since 632 was 632, checked in by tabma, 11 years ago
  • extra step for Simple Import Wizard implemented for easy adding and editing of cells (which were not imported yet)
  • Property svn:keywords set to Date Author Rev
File size: 6.8 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: 632 $
12 * $Author: tabma $
13 * $Date: 2010-07-01 10:31:49 +0000 (do, 01 jul 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'
24
25    /**
26    * @param header string array containing header
27    * @param datamatrix two dimensional array containing actual data
28    * @return preview of the data with the ability to adjust the datatypes
29    */
30    def preview = { attrs ->
31       
32        def header = attrs['header']
33        def datamatrix = attrs['datamatrix']
34
35        out << render (template:"common/preview", model:[header:header, datamatrix:datamatrix])
36    }
37
[283]38    /**
39     * @param datamatrix two dimensional array containing entities with read values
40     * @return postview of the imported data
41     */
42    def postview = { attrs ->
43        def datamatrix = attrs['datamatrix']
44
45        out << render (template:"common/postview", model:[datamatrix:datamatrix])
46    }
47
[200]48    def entity = { attrs ->
49        out << entities[attrs['index']].name
50    }
51
[632]52    def datapreview = { attrs ->
53        def datamatrix = attrs['datamatrix']
54        out << render (template:"common/datapreview", model:[datamatrix:datamatrix])
55    }
56
[200]57    /**
[632]58     * Show missing properties
59     */
60    def missingProperties = { attrs ->
61        def datamatrix = attrs['datamatrix']   
62        out << render (template:"common/missingproperties", model:[datamatrix:datamatrix])
63    }
64
65    /**
[485]66     * @param entities array containing selected entities
[632]67     * @param header array containing mappingcolumn objects
68     * @param allfieldtypes if set, show all fields
69     * @param layout constant value: "horizontal" or "vertical"
[200]70     */
71    def properties = { attrs ->
[203]72        def header = attrs['header']
[485]73        def entities = attrs['entities']
[489]74        def allfieldtypes = (attrs['allfieldtypes']==null) ? "false" : "true"
[632]75        def layout = (attrs['layout']==null) ? "vertical" : attrs['layout']
76
77        //choose template for vertical layout (default) or horizontal layout
78        def template = (layout == "vertical") ? "common/properties_vertical" : "common/properties_horizontal"
[492]79       
[632]80        out << render ( template:template,
[485]81                        model:[selectedentities:entities,
82                        standardentities:grailsApplication.config.gscf.domain.importableEntities,
[489]83                        header:header,
[632]84                        allfieldtypes:allfieldtypes,
85                        layout:layout]
[489]86                        )
[200]87    }
88
[231]89    /**
90     * Possibly this will later on return an AJAX-like autocompletion chooser for the fields?
[415]91     *
92     * @param name name for the property chooser element
[231]93     * @param importtemplate_id template identifier where fields are retrieved from
[305]94     * @param MappingColumn object containing all required information
[489]95     * @param allfieldtypes boolean true if all templatefields should be listed, otherwise only show filtered templatefields
[231]96     * @return chooser object
97     * */
98    def propertyChooser = { attrs ->
[305]99        // TODO: this should be changed to retrieving fields per entity instead of from one template
100        //       and session variables should not be used inside the service, migrate to controller
[200]101
[328]102        def t = Template.get(session.importer_template_id)
[305]103        def mc = attrs['mappingcolumn']
[489]104        def allfieldtypes = attrs['allfieldtypes']
[545]105        def domainfields = mc.entity.giveDomainFields().findAll { it.type == mc.templatefieldtype }
106            domainfields = domainfields.findAll { it.preferredIdentifier != mc.identifier}
[321]107
[534]108        //def templatefields = (allfieldtypes=="true") ? t.fields : t.fields.findAll { it.type == mc.templatefieldtype }
109        def templatefields = (allfieldtypes=="true") ? 
110            t.fields + mc.entity.giveDomainFields() :
[545]111            t.fields.findAll { it.type == mc.templatefieldtype } + domainfields
[489]112
[545]113        // map identifier to preferred column
114        def prefcolumn = mc.entity.giveDomainFields().findAll { it.preferredIdentifier == true }
115
116        (mc.identifier) ? out << createPropertySelect(attrs['name'], prefcolumn, mc.index) :
[489]117            out << createPropertySelect(attrs['name'], templatefields, mc.index)
[231]118    }
119
[173]120    /**
[415]121     * Create the property chooser select element
122     *
[231]123     * @param name name of the HTML select object
[305]124     * @param options list of options (fields) to be used
[231]125     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
126     * @return HTML select object
127     */
[305]128    def createPropertySelect(String name, options, Integer columnIndex)
129    {   
[415]130        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${columnIndex}\">"
[231]131
[534]132        options.each { f ->
133            res+= "<option value=\"${f.name}\">"
134           
135            res+= (f.preferredIdentifier) ? 
136                    "${f.name} (IDENTIFIER)</option>" :
137                    "${f.name}</option>"
[231]138        }
139
140        res += "</select>"
141        return res
[259]142    }
[231]143
[259]144    /**
145    * @param selected selected TemplateFieldType
146    * @param custval custom value to be combined in the option(s) of the selector
147    * @param name name of the HTML select object
148    * @return HTML select object
149    *
150    * @see dbnp.studycapturing.TemplateFieldType
151    */
152
[256]153     def entitySelect = { attrs ->
154        def sel = (attrs['selected']==null) ? -1 : attrs['selected']
155        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
156        def name = (attrs['name']==null) ? -1 : attrs['name']
157
[415]158        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
[256]159
[485]160        grailsApplication.config.gscf.domain.importableEntities.each { e ->
161            res += "<option value=\"${e.value.name}\""
162            res += (e.value.type == sel) ? " selected" : ""
163            res += ">${e.value.name}</option>"
[256]164        }
165
166        res += "</select>"
167        out << res
[169]168    }
169
[173]170    /**
[415]171     * Create a templatefieldtype selector
172     *
[259]173    * @param selected selected TemplateFieldType
174    * @param customvalue custom value to be combined in the option(s) of the selector
[173]175    * @param name name of the HTML select object
176    * @return HTML select object
[259]177    *
178    * @see dbnp.studycapturing.TemplateFieldType
[173]179    */
[256]180    def templatefieldtypeSelect = { attrs ->
[173]181        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
[415]182        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
[259]183        def name = (attrs['name']==null) ? "" : attrs['name']   
[256]184
[415]185        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
[256]186
187        TemplateFieldType.list().each { e ->
[415]188            res += "<option value=\"${e}\""
[256]189            res += (e == selected) ? " selected" : ""
190            res += ">${e}</option>"
191        }
192
193        res += "</select>"
194
195        out << res
[173]196    }
[297]197
198    /**
199    * @param cell HSSFCell variable
200    * @return good representation of variable (instead of toString())
201    */
202    def displayCell = { attrs ->       
203        def cell = attrs['cell']
204        def df = new DataFormatter()
205
206        switch (cell.getCellType()) {
207            case HSSFCell.CELL_TYPE_STRING      :   out << cell.getStringCellValue()
208                                                    break
209            case HSSFCell.CELL_TYPE_NUMERIC     :   out << df.formatCellValue(cell)
210                                                    break
211        }
212    }
[169]213}
Note: See TracBrowser for help on using the repository browser.