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
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: 632 $
12 * $Author: tabma $
13 * $Date: 2010-07-01 10:31:49 +0000 (do, 01 jul 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    def datapreview = { attrs ->
53        def datamatrix = attrs['datamatrix']
54        out << render (template:"common/datapreview", model:[datamatrix:datamatrix])
55    }
56
57    /**
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    /**
66     * @param entities array containing selected entities
67     * @param header array containing mappingcolumn objects
68     * @param allfieldtypes if set, show all fields
69     * @param layout constant value: "horizontal" or "vertical"
70     */
71    def properties = { attrs ->
72        def header = attrs['header']
73        def entities = attrs['entities']
74        def allfieldtypes = (attrs['allfieldtypes']==null) ? "false" : "true"
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"
79       
80        out << render ( template:template,
81                        model:[selectedentities:entities,
82                        standardentities:grailsApplication.config.gscf.domain.importableEntities,
83                        header:header,
84                        allfieldtypes:allfieldtypes,
85                        layout:layout]
86                        )
87    }
88
89    /**
90     * Possibly this will later on return an AJAX-like autocompletion chooser for the fields?
91     *
92     * @param name name for the property chooser element
93     * @param importtemplate_id template identifier where fields are retrieved from
94     * @param MappingColumn object containing all required information
95     * @param allfieldtypes boolean true if all templatefields should be listed, otherwise only show filtered templatefields
96     * @return chooser object
97     * */
98    def propertyChooser = { attrs ->
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
101
102        def t = Template.get(session.importer_template_id)
103        def mc = attrs['mappingcolumn']
104        def allfieldtypes = attrs['allfieldtypes']
105        def domainfields = mc.entity.giveDomainFields().findAll { it.type == mc.templatefieldtype }
106            domainfields = domainfields.findAll { it.preferredIdentifier != mc.identifier}
107
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() :
111            t.fields.findAll { it.type == mc.templatefieldtype } + domainfields
112
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) :
117            out << createPropertySelect(attrs['name'], templatefields, mc.index)
118    }
119
120    /**
121     * Create the property chooser select element
122     *
123     * @param name name of the HTML select object
124     * @param options list of options (fields) to be used
125     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
126     * @return HTML select object
127     */
128    def createPropertySelect(String name, options, Integer columnIndex)
129    {   
130        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${columnIndex}\">"
131
132        options.each { f ->
133            res+= "<option value=\"${f.name}\">"
134           
135            res+= (f.preferredIdentifier) ? 
136                    "${f.name} (IDENTIFIER)</option>" :
137                    "${f.name}</option>"
138        }
139
140        res += "</select>"
141        return res
142    }
143
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
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
158        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
159
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>"
164        }
165
166        res += "</select>"
167        out << res
168    }
169
170    /**
171     * Create a templatefieldtype selector
172     *
173    * @param selected selected TemplateFieldType
174    * @param customvalue custom value to be combined in the option(s) of the selector
175    * @param name name of the HTML select object
176    * @return HTML select object
177    *
178    * @see dbnp.studycapturing.TemplateFieldType
179    */
180    def templatefieldtypeSelect = { attrs ->
181        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
182        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
183        def name = (attrs['name']==null) ? "" : attrs['name']   
184
185        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
186
187        TemplateFieldType.list().each { e ->
188            res += "<option value=\"${e}\""
189            res += (e == selected) ? " selected" : ""
190            res += ">${e}</option>"
191        }
192
193        res += "</select>"
194
195        out << res
196    }
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    }
213}
Note: See TracBrowser for help on using the repository browser.