source: trunk/grails-app/controllers/dbnp/studycapturing/TemplateController.groovy @ 1257

Last change on this file since 1257 was 1257, checked in by robert@…, 13 years ago

Added an export and import functionality for templates, as described in ticket #76

  • Property svn:keywords set to Author Date Rev
File size: 4.2 KB
Line 
1/**
2 * TemplateController Controler
3 *
4 * An overview of
5 *
6 * @author  Kees van Bochove
7 * @since       20100726
8 * @package     dbnp.studycapturing
9 *
10 * Revision information:
11 * $Rev: 1257 $
12 * $Author: robert@isdat.nl $
13 * $Date: 2010-12-09 20:12:10 +0000 (do, 09 dec 2010) $
14 */
15package dbnp.studycapturing
16
17// Automatic marshalling of XML and JSON
18import grails.converters.*
19import grails.plugins.springsecurity.Secured
20import dbnp.authentication.AuthenticationService
21
22@Secured(['IS_AUTHENTICATED_REMEMBERED'])
23class TemplateController {
24        def authenticationService
25
26        /**
27         * Shows a form to pick a file to import templates
28         */
29        def importTemplate = {
30        }
31
32        /**
33         * Handles file import
34         */
35        def handleImportedFile = {
36                if( !request.getFile("file") ) {
37                        flash.message = "No file given.";
38                        redirect( action: 'importTemplate' );
39                        return;
40                }
41
42                // Parse XML
43                def xml
44               
45                try {
46                        xml = XML.parse( request.getFile("file").inputStream.text )
47                } catch( Exception e ) {
48                        // Error in parsing. Probably the file is not a XML file
49                        flash.message = "Imported file could not be read. Please specify an XML template file.";
50                        redirect( action: 'importTemplate' );
51                }
52               
53                def numTemplates = xml.@count;
54
55                if( !xml.template ) {
56                        flash.message = "No templates could be found in the imported file. Please specify an XML template file.";
57                        redirect( action: 'importTemplate' );
58                }
59
60                // Check whether the templates already exist
61                def templates = []
62                def id = 0;
63                xml.template.each { template ->
64                        try {
65                                def t = Template.parse( template, authenticationService.getLoggedInUser() );
66
67                                def templateData = [:]
68                                templateData.key = id++;
69                                templateData.template = t
70                                templateData.alternatives = []
71
72                                // If a template exists that equals this xml template , return it.
73                                for( def otherTemplate in Template.findAllByEntity( t.entity ) ) {
74                                        if( t.contentEquals( otherTemplate ) ) {
75                                                templateData.alternatives << otherTemplate;
76                                        }
77                                }
78
79                                templates << templateData
80                        } catch (Exception e) {
81                                templates << [ template: null, error: "Template " + ( template.name ?: " without name" ) + " could not be parsed"];
82                        }
83                }
84
85                // Save templates in session in order to have data available in the next (import) step
86                session.templates = templates
87               
88                [templates: templates]
89        }
90
91        /**
92         * Saves the imported templates that the user has chosen
93         */
94        def saveImportedTemplates = {
95                def ids = params.selectedTemplate
96                def templates = session.templates
97                def messages = []
98               
99                // Save all selected templates
100                ids.each { id ->
101                        def templateData = templates.find { template -> template.key == id.toLong() }
102                        def importTemplate = templateData?.template
103                        println id + ": " + templateData
104
105                        if( !importTemplate ) {
106                                messages << "Template with id " + id + " could not be found."
107                        } else {
108                                def originalName = importTemplate.name
109                                def newName = null
110
111                                // Check whether a new name has been given
112                                if( params[ 'templateNames_' + id ] && params[ 'templateNames_' + id ] != importTemplate.name ) {
113                                        importTemplate.name = params[ 'templateNames_' + id ]
114                                        newName = params[ 'templateNames_' + id ]
115                                }
116
117                                if( importTemplate.save() ) {
118                                        messages << "Template " + originalName + " saved" + ( newName ? " as " + newName : "" )
119                                } else {
120                                        messages << "Template " + originalName + " could not be saved"
121                                }
122                        }
123                }
124
125                // Remove templates from the session
126                session.templates = null
127
128                [messages: messages]
129        }
130
131        /**
132         * Shows a form to select templates for export
133         */
134        def export = {
135                // If the templates are already selected, export them
136                if( params.templates ) {
137                        if( !( params.templates instanceof String ) ) {
138                                params.templates = params.templates.join(",")
139                        }
140
141                        switch( params.type ) {
142                                case "xml":
143                                default:
144                                        xml();
145                                        return
146                        }
147                        return;
148                } else {
149                        [templates: Template.findAll()]
150                }
151        }
152
153        /**
154         * XML Export of given templates, or all templates if no templates are given
155         */
156        def xml = {
157                def templates
158                if( params.templates ) {
159                        def ids = [];
160                        params.templates.split(",").each { ids << it.toLong() }
161
162                        def c = Template.createCriteria()
163                        templates = c {
164                                'in'("id", ids)
165                        }
166                } else {
167                        templates = Template.findAll()
168                }
169               
170                response.setHeader "Content-disposition", "attachment; filename=templates.xml"
171
172                render(view: 'xml', contentType:"text/xml", model: [templates: templates])
173        }
174
175}
Note: See TracBrowser for help on using the repository browser.