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

Last change on this file since 545 was 545, checked in by tabma, 11 years ago
  • fixed Advanced Import Wizard (preferredIdentifier issues)
  • Property svn:keywords set to Date Author Rev
File size: 6.0 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: 545 $
12 * $Author: tabma $
13 * $Date: 2010-06-09 10:54:12 +0000 (wo, 09 jun 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
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
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
48    def entity = { attrs ->
49        out << entities[attrs['index']].name
50    }
51
52    /**
53     * @param entities array containing selected entities
54     */
55    def properties = { attrs ->
56        def header = attrs['header']
57        def entities = attrs['entities']
58        def allfieldtypes = (attrs['allfieldtypes']==null) ? "false" : "true"
59       
60
61        out << render ( template:"common/properties",
62                        model:[selectedentities:entities,
63                        standardentities:grailsApplication.config.gscf.domain.importableEntities,
64                        header:header,
65                        allfieldtypes:allfieldtypes]
66                        )
67    }
68
69    /**
70     * Possibly this will later on return an AJAX-like autocompletion chooser for the fields?
71     *
72     * @param name name for the property chooser element
73     * @param importtemplate_id template identifier where fields are retrieved from
74     * @param MappingColumn object containing all required information
75     * @param allfieldtypes boolean true if all templatefields should be listed, otherwise only show filtered templatefields
76     * @return chooser object
77     * */
78    def propertyChooser = { attrs ->
79        // TODO: this should be changed to retrieving fields per entity instead of from one template
80        //       and session variables should not be used inside the service, migrate to controller
81
82        def t = Template.get(session.importer_template_id)
83        def mc = attrs['mappingcolumn']
84        def allfieldtypes = attrs['allfieldtypes']
85        def domainfields = mc.entity.giveDomainFields().findAll { it.type == mc.templatefieldtype }
86            domainfields = domainfields.findAll { it.preferredIdentifier != mc.identifier}
87
88        //def templatefields = (allfieldtypes=="true") ? t.fields : t.fields.findAll { it.type == mc.templatefieldtype }
89        def templatefields = (allfieldtypes=="true") ? 
90            t.fields + mc.entity.giveDomainFields() :
91            t.fields.findAll { it.type == mc.templatefieldtype } + domainfields
92
93        // map identifier to preferred column
94        def prefcolumn = mc.entity.giveDomainFields().findAll { it.preferredIdentifier == true }
95
96        (mc.identifier) ? out << createPropertySelect(attrs['name'], prefcolumn, mc.index) :
97            out << createPropertySelect(attrs['name'], templatefields, mc.index)
98    }
99
100    /**
101     * Create the property chooser select element
102     *
103     * @param name name of the HTML select object
104     * @param options list of options (fields) to be used
105     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
106     * @return HTML select object
107     */
108    def createPropertySelect(String name, options, Integer columnIndex)
109    {   
110        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${columnIndex}\">"
111
112        options.each { f ->
113            res+= "<option value=\"${f.name}\">"
114           
115            res+= (f.preferredIdentifier) ? 
116                    "${f.name} (IDENTIFIER)</option>" :
117                    "${f.name}</option>"
118        }
119
120        res += "</select>"
121        return res
122    }
123
124    /**
125    * @param selected selected TemplateFieldType
126    * @param custval custom value to be combined in the option(s) of the selector
127    * @param name name of the HTML select object
128    * @return HTML select object
129    *
130    * @see dbnp.studycapturing.TemplateFieldType
131    */
132
133     def entitySelect = { attrs ->
134        def sel = (attrs['selected']==null) ? -1 : attrs['selected']
135        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
136        def name = (attrs['name']==null) ? -1 : attrs['name']
137
138        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
139
140        grailsApplication.config.gscf.domain.importableEntities.each { e ->
141            res += "<option value=\"${e.value.name}\""
142            res += (e.value.type == sel) ? " selected" : ""
143            res += ">${e.value.name}</option>"
144        }
145
146        res += "</select>"
147        out << res
148    }
149
150    /**
151     * Create a templatefieldtype selector
152     *
153    * @param selected selected TemplateFieldType
154    * @param customvalue custom value to be combined in the option(s) of the selector
155    * @param name name of the HTML select object
156    * @return HTML select object
157    *
158    * @see dbnp.studycapturing.TemplateFieldType
159    */
160    def templatefieldtypeSelect = { attrs ->
161        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
162        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
163        def name = (attrs['name']==null) ? "" : attrs['name']   
164
165        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
166
167        TemplateFieldType.list().each { e ->
168            res += "<option value=\"${e}\""
169            res += (e == selected) ? " selected" : ""
170            res += ">${e}</option>"
171        }
172
173        res += "</select>"
174
175        out << res
176    }
177
178    /**
179    * @param cell HSSFCell variable
180    * @return good representation of variable (instead of toString())
181    */
182    def displayCell = { attrs ->       
183        def cell = attrs['cell']
184        def df = new DataFormatter()
185
186        switch (cell.getCellType()) {
187            case HSSFCell.CELL_TYPE_STRING      :   out << cell.getStringCellValue()
188                                                    break
189            case HSSFCell.CELL_TYPE_NUMERIC     :   out << df.formatCellValue(cell)
190                                                    break
191        }
192    }
193}
Note: See TracBrowser for help on using the repository browser.