root/trunk/grails-app/services/dbnp/studyexport/ExportService.groovy @ 1516

Revision 1516, 3.5 KB (checked in by j.saito@…, 3 years ago)

Exporter for representing Study objects as XML and parsing them back.
Import not ready yet.

Line 
1/**
2 *  ExporterService
3 * 
4 *  @author Jahn
5 * 
6 *  This service for exporting a Study and all domain objects depending on it to XML.
7 */
8
9
10
11package dbnp.studyexport
12
13import dbnp.authentication.*
14import dbnp.studycapturing.*
15import grails.converters.XML
16import org.codehaus.groovy.grails.web.converters.configuration.ConvertersConfigurationHolder;
17import org.dbnp.bgdt.*
18import org.dbnp.gdt.*
19
20
21class ExportService
22{
23
24    static transactional = true
25    static scope = "session"
26
27    def grailsApplication
28    /**
29     *  List of classes that recursion does not go further into when building an XML 
30     *  document.
31         * 
32         *  @see #getRelatedObjects().
33     */
34        def static IgnoredClasses = [ String, long, Date, Boolean, Publication, SecUser ]
35
36    /**
37     *  List of classes that recursion does not go further into when building an XML 
38     *  document; the elements are still included.
39         * 
40         *  @see #getRelatedObjects().
41     */
42        def static TerminalClasses = [ AssayModule, Identity, Ontology, PersonAffiliation,
43                        PersonRole, Template, TemplateField,
44                        TemplateFieldListItem, TemplateFieldType, Term ]
45
46
47
48        /**
49         *  Returns a list of all Grails domain objects relevant for creating a full
50         *  XML representation of a Study.
51         * 
52         *  The actual XML is then created by the controller using Grails' XML converter.
53         * 
54         *  @param Study
55         *
56         *  @return List of all Grails domain objects
57         */
58
59        def getDependentObjects( Study study ) {
60                return getRelatedObjects( study )
61        }
62
63
64
65        /**
66         *  Returns a list of Grails domain objects. 
67         * 
68         *  Helper method for getDependentObjects().
69         * 
70         *  This method produces a list of all objects that need to be
71         *  written out in order to get an XML representation of a Study object.
72         * 
73         *  This is achieved by recursion. The recursion stops at objects whose
74         *  class is member of IgnoredClasses or TerminalClasses.
75         * 
76         *      Example call:
77         * 
78         *              def objects = getDependentObjects( Study.get(1) )
79         *              (objects*.class).unique().sort().each { println it }
80         * 
81         *  @param domainObject  A grails domain object.
82         * 
83         *  @return List of all Grails domain objects
84         */
85
86        def     getRelatedObjects( domainObject ) {
87
88                if(domainObject==null) {
89                        return []
90                }
91
92                def domainClass = domainObject.class
93                def objects = []
94
95                if( IgnoredClasses.contains(domainClass) )   {
96                        return objects
97                }
98
99                if( TerminalClasses.contains(domainClass) )  {
100                        return [domainObject]
101                }
102
103                objects = [domainObject]
104
105                                                                                                // enter recursion with regular domain fields
106                domainObject.getProperties().domainFields.each { field ->
107                        objects.addAll( getRelatedObjects(field) )
108                }
109
110                                                                                                // enter recursion with hasMany fields
111                domainObject.getProperties().hasMany.each { property, theClass ->
112
113                        boolean isTemplateField = ( domainObject instanceof TemplateEntity  &&
114                                property==~/template(.+)Fields/ )
115                        if( !isTemplateField ) {
116                                domainObject."$property".each {
117                                                objects.addAll( getRelatedObjects(it) )
118                                }
119                        }
120
121                }
122                return objects.unique()
123        }
124
125
126
127        /**
128         *  Returns Study object from List of objects created by
129         *  XML representation of a Study.
130         * 
131         *  @param List of objects
132         *
133         *  @return Study object that has to be saved
134         *
135         *  @see getDependentObjects()
136         */
137
138        def     getStudy( Collection objectList ) {
139        }
140
141
142
143        /**
144         *  Parse XML object to List of objects to be translated into
145         *  a Study by getStudy().
146         * 
147         *  @param Study
148         *
149         *  @return List of all Grails domain objects
150         *
151         *  @see getStudy()
152         */
153
154        def     parseXMLStudy( XML xml ) {
155        }
156
157}
Note: See TracBrowser for help on using the browser.