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

Last change on this file since 492 was 492, checked in by tabma, 11 years ago
  • properties in simple import wizard now shows all template fields
  • moved study chooser in advanced wizard to first page
  • Property svn:keywords set to Date Author Rev
File size: 5.6 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: 492 $
12 * $Author: tabma $
13 * $Date: 2010-05-28 09:08:19 +0000 (vr, 28 mei 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
86        def templatefields = (allfieldtypes=="true") ? t.fields : t.fields.findAll { it.type == mc.templatefieldtype }
87
88        (mc.identifier) ? out << "<select style=\"font-size:10px\" name=\"\" disabled><option>Identifier</option></select>":
89            out << createPropertySelect(attrs['name'], templatefields, mc.index)
90    }
91
92    /**
93     * Create the property chooser select element
94     *
95     * @param name name of the HTML select object
96     * @param options list of options (fields) to be used
97     * @param columnIndex column identifier (corresponding to position in header of the Excel sheet)
98     * @return HTML select object
99     */
100    def createPropertySelect(String name, options, Integer columnIndex)
101    {   
102        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${columnIndex}\">"
103
104        options.each { f ->         
105            res += "<option value=\"${f.id}\""
106            //res += (e.type.toInteger() == selected) ? " selected" : ""
107            res += ">${f}</option>"
108        }
109
110        res += "</select>"
111        return res
112    }
113
114    /**
115    * @param selected selected TemplateFieldType
116    * @param custval custom value to be combined in the option(s) of the selector
117    * @param name name of the HTML select object
118    * @return HTML select object
119    *
120    * @see dbnp.studycapturing.TemplateFieldType
121    */
122
123     def entitySelect = { attrs ->
124        def sel = (attrs['selected']==null) ? -1 : attrs['selected']
125        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
126        def name = (attrs['name']==null) ? -1 : attrs['name']
127
128        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
129
130        grailsApplication.config.gscf.domain.importableEntities.each { e ->
131            res += "<option value=\"${e.value.name}\""
132            res += (e.value.type == sel) ? " selected" : ""
133            res += ">${e.value.name}</option>"
134        }
135
136        res += "</select>"
137        out << res
138    }
139
140    /**
141     * Create a templatefieldtype selector
142     *
143    * @param selected selected TemplateFieldType
144    * @param customvalue custom value to be combined in the option(s) of the selector
145    * @param name name of the HTML select object
146    * @return HTML select object
147    *
148    * @see dbnp.studycapturing.TemplateFieldType
149    */
150    def templatefieldtypeSelect = { attrs ->
151        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
152        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
153        def name = (attrs['name']==null) ? "" : attrs['name']   
154
155        def res = "<select style=\"font-size:10px\" name=\"${name}.index.${custval}\">"
156
157        TemplateFieldType.list().each { e ->
158            res += "<option value=\"${e}\""
159            res += (e == selected) ? " selected" : ""
160            res += ">${e}</option>"
161        }
162
163        res += "</select>"
164
165        out << res
166    }
167
168    /**
169    * @param cell HSSFCell variable
170    * @return good representation of variable (instead of toString())
171    */
172    def displayCell = { attrs ->       
173        def cell = attrs['cell']
174        def df = new DataFormatter()
175
176        switch (cell.getCellType()) {
177            case HSSFCell.CELL_TYPE_STRING      :   out << cell.getStringCellValue()
178                                                    break
179            case HSSFCell.CELL_TYPE_NUMERIC     :   out << df.formatCellValue(cell)
180                                                    break
181        }
182    }
183}
Note: See TracBrowser for help on using the repository browser.