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

Last change on this file since 256 was 256, checked in by tabma, 9 years ago
  • Template enums now used instead of POI Celltypes
  • Property svn:keywords set to Author Rev Date
File size: 4.5 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: 256 $
12 * $Author: tabma $
13 * $Date: 2010-03-11 10:48:16 +0000 (do, 11 mrt 2010) $
14 */
15
16package dbnp.importer
17import dbnp.studycapturing.Template
18import dbnp.studycapturing.TemplateFieldType
19
20class ImporterTagLib {
21    static namespace = 'importer'
22    def standardentities = [[type:-1, name:"Don't import"], [type:0, name:"Study"], [type:1, name:"Subject"], [type:2, name:"Event"],
23                        [type:3, name:"Protocol"], [type:4, name:"Sample"]]
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    def entity = { attrs ->
39        out << entities[attrs['index']].name
40    }
41
42    /**
43     * @param entities array in the format of columnindex:entitytype format
44     */
45    def properties = { attrs ->
46        def selectedentities = []
47        def header = attrs['header']
48
49        attrs['entities'].each { se ->
50            def temp = se.split(":")
51            def entity = [type:temp[1],columnindex:temp[0]]
52            selectedentities.add(entity)
53        }
54
55        out << render (template:"common/properties", model:[selectedentities:selectedentities, standardentities:standardentities, header:header])
56    }
57
58    def createSelect(selected, String name, options, String customvalue) {
59        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
60
61        options.each { e ->
62            res += "<option value=\"${customvalue}:${e}\""
63            res += (e == selected) ? " selected" : ""
64            res += ">${e}</option>"
65        }
66
67        res += "</select>"
68        return res
69    }
70
71    /**
72     * Possibly this will later on return an AJAX-like autocompletion chooser for the fields?
73     *
74     * @param importtemplate_id template identifier where fields are retrieved from
75     * @param columnindex column in the header we're talking about
76     * @return chooser object
77     * */
78    def propertyChooser = { attrs ->
79        // TODO: this should be changed to retrieving fields per entity
80        def t = Template.get(session.importtemplate_id)
81        def columnindex = attrs['columnindex']
82
83        switch (attrs['entitytype']) {
84            case 0  : createPropertySelect(attrs['name'], t.fields, columnindex)
85                      break
86            case 1  : break
87            case 2  : break
88            case 3  : break
89            default : out << createPropertySelect(attrs['name'], t.fields, columnindex)
90                     break
91        }
92    }
93
94    /**
95     * @param name name of the HTML select object
96     * @param options list of options 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, String columnIndex)
101    {
102        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
103
104        options.each { f ->
105            res += "<option value=\"${columnIndex}:${f.id}\""
106            //res += (e.type.toInteger() == selected) ? " selected" : ""
107            res += ">${f}</option>"
108        }
109
110        res += "</select>"
111        return res
112    }   
113
114     def entitySelect = { attrs ->
115        def sel = (attrs['selected']==null) ? -1 : attrs['selected']
116        def custval = (attrs['customvalue']==null) ? "" : attrs['customvalue']
117        def name = (attrs['name']==null) ? -1 : attrs['name']
118
119        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
120
121        standardentities.each { e ->
122            res += "<option value=\"${custval}:${e.type}\""
123            res += (e.type == sel) ? " selected" : ""
124            res += ">${e.name}</option>"
125        }
126
127        res += "</select>"
128        out << res
129    }
130
131    /**
132    * @param selected selected celltype
133    * @param name name of the HTML select object
134    * @see org.apache.poi.ss.usermodel.Cell for the possible cell types
135    * @return HTML select object
136    */
137    def templatefieldtypeSelect = { attrs ->
138        def selected = (attrs['selected']==null) ? -1 : attrs['selected']
139        def customvalue = (attrs['customvalue']==null) ? "" : attrs['customvalue']
140        def name = (attrs['name']==null) ? "" : attrs['name']
141        //out << createSelect(selected, attrs['name'], standardcelltypes, customvalue)
142
143        def res = "<select style=\"font-size:10px\" name=\"${name}\">"
144
145        TemplateFieldType.list().each { e ->
146            res += "<option value=\"${customvalue}:${e}\""
147            res += (e == selected) ? " selected" : ""
148            res += ">${e}</option>"
149        }
150
151        res += "</select>"
152
153        out << res
154    }
155}
Note: See TracBrowser for help on using the repository browser.