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

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