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 | |
---|
11 | package dbnp.studyexport |
---|
12 | |
---|
13 | import dbnp.authentication.* |
---|
14 | import dbnp.studycapturing.* |
---|
15 | import grails.converters.XML |
---|
16 | import org.codehaus.groovy.grails.web.converters.configuration.ConvertersConfigurationHolder; |
---|
17 | import org.dbnp.bgdt.* |
---|
18 | import org.dbnp.gdt.* |
---|
19 | |
---|
20 | |
---|
21 | class ExportService |
---|
22 | { |
---|
23 | |
---|
24 | static transactional = true |
---|
25 | static scope = "session" |
---|
26 | |
---|
27 | |
---|
28 | def grailsApplication |
---|
29 | /** |
---|
30 | * List of classes that recursion does not go further into when building an XML |
---|
31 | * document. |
---|
32 | * |
---|
33 | * @see #getRelatedObjects(). |
---|
34 | */ |
---|
35 | def static IgnoredClasses = [ String, long, Date, Boolean, SecUser, Publication, SecUser ] |
---|
36 | |
---|
37 | |
---|
38 | /** |
---|
39 | * List of classes that recursion does not go further into when building an XML |
---|
40 | * document; the elements are still included. |
---|
41 | * |
---|
42 | * @see #getRelatedObjects(). |
---|
43 | */ |
---|
44 | def static TerminalClasses = [ AssayModule, Identity, Ontology, PersonAffiliation, |
---|
45 | PersonRole, Template, TemplateField, |
---|
46 | TemplateFieldListItem, TemplateFieldType, Term ] |
---|
47 | |
---|
48 | |
---|
49 | /** |
---|
50 | * Returns a list of all Grails domain objects relevant for creating a full |
---|
51 | * XML representation of a Study. |
---|
52 | * |
---|
53 | * The actual XML is then created by the controller using Grails' XML converter. |
---|
54 | * |
---|
55 | * @param Study |
---|
56 | * |
---|
57 | * @return List of all Grails domain objects |
---|
58 | */ |
---|
59 | |
---|
60 | def getDependentObjects( Study study ) { |
---|
61 | return getRelatedObjects( study ) |
---|
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( domainClass.toString()==~/class dbnp.authentication.SecUser.+/ || |
---|
100 | domainClass.toString()==~/class dbnp.studycapturing.Publication.+/ ) { |
---|
101 | println "${domainClass} -- ${domainObject}" |
---|
102 | return objects |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | if( TerminalClasses.contains(domainClass) ) { |
---|
108 | return [domainObject] |
---|
109 | } |
---|
110 | |
---|
111 | objects = [domainObject] |
---|
112 | |
---|
113 | // enter recursion with regular domain fields |
---|
114 | domainObject.getProperties().domainFields.each { field -> |
---|
115 | objects.addAll( getRelatedObjects(field) ) |
---|
116 | } |
---|
117 | |
---|
118 | // enter recursion with hasMany fields |
---|
119 | domainObject.getProperties().hasMany.each { property, theClass -> |
---|
120 | |
---|
121 | boolean isTemplateField = ( domainObject instanceof TemplateEntity && |
---|
122 | property==~/template(.+)Fields/ ) |
---|
123 | if( !isTemplateField ) { |
---|
124 | domainObject."$property".each { |
---|
125 | objects.addAll( getRelatedObjects(it) ) |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | } |
---|
130 | return objects.unique() |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | /** |
---|
135 | * Returns Study object from List of objects created by |
---|
136 | * XML representation of a Study. |
---|
137 | * |
---|
138 | * @param List of objects |
---|
139 | * |
---|
140 | * @return Study object that has to be saved |
---|
141 | * |
---|
142 | * @see getDependentObjects() |
---|
143 | */ |
---|
144 | |
---|
145 | def getStudy( Collection objectList ) { |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | /** |
---|
151 | * Parse XML object to List of objects to be translated into |
---|
152 | * a Study by getStudy(). |
---|
153 | * |
---|
154 | * @param Study |
---|
155 | * |
---|
156 | * @return List of all Grails domain objects |
---|
157 | * |
---|
158 | * @see getStudy() |
---|
159 | */ |
---|
160 | |
---|
161 | def parseXMLStudy( XML xml ) { |
---|
162 | } |
---|
163 | |
---|
164 | } |
---|