Changeset 1694
- Timestamp:
- Apr 4, 2011, 1:32:53 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/dbnp/studyexport/ExportService.groovy
r1581 r1694 1 1 /** 2 * ExporterService 2 * This service is for exporting studies to a XML flat list and for 3 * importing them back. 3 4 * 4 5 * @author Jahn 5 6 * 6 * Th is service for exporting a Study and all domain objects depending on it to XML.7 * The file is used in combination with a controller. 7 8 */ 8 9 9 10 10 … … 15 15 import grails.converters.XML 16 16 import groovy.util.slurpersupport.* 17 import org.dbnp.bgdt.* 17 18 import org.dbnp.gdt.* 18 19 … … 50 51 * List of domain classes related to Study. 51 52 */ 52 def static DomainClasses = [ 'RegistrationCode':dbnp.authentication.RegistrationCode, 53 'SecRole':dbnp.authentication.SecRole, 'SecUser':dbnp.authentication.SecUser, 54 'SecUserSecRole':dbnp.authentication.SecUserSecRole, 55 'SessionAuthenticatedUser':dbnp.authentication.SessionAuthenticatedUser, 53 def static DomainClasses = [ 56 54 'Assay':dbnp.studycapturing.Assay, 55 'AssayModule':org.dbnp.gdt.AssayModule, 57 56 'Event':dbnp.studycapturing.Event, 'EventGroup':dbnp.studycapturing.EventGroup, 57 'Identity':org.dbnp.gdt.Identity, 58 58 'PersonAffiliation':dbnp.studycapturing.PersonAffiliation, 59 59 'Person':dbnp.studycapturing.Person, 60 60 'PersonRole':dbnp.studycapturing.PersonRole, 61 61 'Publication':dbnp.studycapturing.Publication, 62 'RegistrationCode':dbnp.authentication.RegistrationCode, 63 'RelTime':org.dbnp.gdt.RelTime, 62 64 'Sample':dbnp.studycapturing.Sample, 63 65 'SamplingEvent':dbnp.studycapturing.SamplingEvent, 66 'SecRole':dbnp.authentication.SecRole, 'SecUser':dbnp.authentication.SecUser, 67 'SecUserSecRole':dbnp.authentication.SecUserSecRole, 68 'SessionAuthenticatedUser':dbnp.authentication.SessionAuthenticatedUser, 64 69 'Study':dbnp.studycapturing.Study, 65 70 'StudyPerson':dbnp.studycapturing.StudyPerson, 66 71 'Subject':dbnp.studycapturing.Subject, 67 'AssayModule':org.dbnp.gdt.AssayModule,68 'Identity':org.dbnp.gdt.Identity,69 'RelTime':org.dbnp.gdt.RelTime,70 72 'TemplateEntity':org.dbnp.gdt.TemplateEntity, 73 'TemplateFieldListItem':org.dbnp.gdt.TemplateFieldListItem, 71 74 'TemplateField':org.dbnp.gdt.TemplateField, 72 'TemplateFieldListItem':org.dbnp.gdt.TemplateFieldListItem,73 75 'TemplateFieldType':org.dbnp.gdt.TemplateFieldType, 74 76 'Template':org.dbnp.gdt.Template ] 77 75 78 76 79 … … 134 137 if(domainObject instanceof TemplateEntity ) { 135 138 objects.push(domainObject.template) 136 domainObject.template.fields.each { objects.push(it) } 139 domainObject.template.fields.each { 140 objects.push(it) 141 if(domainClass==Assay) 142 println "${domainObject.class} ${it}" 143 } 144 145 if(domainClass==Assay) { 146 domainObject.domainFields.each { 147 def memberClass = domainObject."$it".class 148 println "${it.class} ${it}. member class: ${memberClass}" 149 } 150 } 151 137 152 } 138 153 … … 195 210 196 211 197 198 199 /**200 * Create Study from list of objects repesenting a Study.201 *202 * (For importing Study objects)203 *204 * @param List of objects representing a Study.205 *206 * @return void207 */208 def createStudy( List parseObjects ) {209 parseObjects.each{210 populateOneToManies( it.domainObject, it.node, parseObjects )211 }212 parseObjects.each{213 if( it.domainObject instanceof TemplateEntity ) {214 addTemplateRelations( it.domainObject, it.node, parseObjects )215 addTemplateFields( it.domainObject, it.node, parseObjects )216 }217 }218 }219 220 221 /** Set a TemplateEntity's template field. Find the right Template in the list222 * of ParseObjects based on parsed id. If the TemplateEntity instance does223 * not have an matching template in the list of ParseObjects, it remains empty.224 *225 * @param domainObject Some Template Entity226 *227 * @param node Node with parse information228 *229 * @param parseObjects List of ParseObjects230 */231 def addTemplateRelations( TemplateEntity domainObject, Node node, List parseObjects ) {232 def id = node.children().find{it.name=='template'}?.attributes()?.id233 if(id) {234 def template = parseObjects.find{ it.theClass==Template && it.id==id }?.domainObject235 if(template) {236 domainObject.template = template237 }238 }239 }240 241 242 243 /** Set a TemplateEntity's template fields with values from a Node.244 * The template fields are fields such as TemplateStringField or TemplateFieldType.245 *246 * @param domainObject Some Template Entity247 *248 * @param node Node with parse information249 *250 */251 def addTemplateFields( TemplateEntity domainObject, Node node ) {252 domainObject.metaClass.getProperties().each{ property ->253 def name = property.name // name of templateFields, e.g., templateStringFields254 if( name ==~/template(.+)Fields/ ) {255 node.children().find{it.name==name}?.children()?.each{ fieldNode ->256 def key = fieldNode.attributes()?.key257 def value = fieldNode.text()258 //domainObject.setFieldValue(key,value) -> needs to be fixed based on class/type259 }260 }261 }262 }263 264 265 266 /**267 * Populate one-to-many maps of a new domainObject268 * from list of ParseObjects.269 *270 * (For importing Study objects)271 *272 * @param domainObject domainObject to be fielled273 *274 * @param List of parseObjects representing a Study.275 *276 * @return the new domainObject277 */278 def populateOneToManies( domainObject, node, parseObjects ) {279 if( !domainObject.class.metaClass.getProperties().find{ it.name=='hasMany' } ) {280 return281 }282 283 domainObject.class.hasMany.each{ name, theClass ->284 node.children().each { child ->285 if(child.name==name) {286 child.children().each { grandChild ->287 def id = grandChild.attributes.id288 if(id) {289 def ref = parseObjects.find{ it.theClass==theClass && it.id==id }290 if(ref) {291 def addTo = "addTo" + name.replaceFirst(name[0],name[0].toUpperCase())292 domainObject.invokeMethod(addTo,(Object) ref.domainObject )293 }294 }295 }296 }297 }298 }299 }300 301 302 303 /**304 * Populate one-to-many maps of a new domainObject from list of ParseObjects.305 *306 * (For importing Study objects)307 *308 * @param domainObject domainObject to be fielled309 *310 * @param List of parseObjects representing a Study.311 *312 * @return the new domainObject313 */314 315 private class ParseObject {316 String tag317 String id318 Class theClass319 Object domainObject320 Node node321 322 323 public ParseObject( node ){324 tag = node.name()325 theClass = getClassForTag( tag )326 domainObject = theClass.newInstance()327 id = null328 if(node.attributes && node.attributes.id) {329 id = node.attributes.id330 }331 this.node=node332 333 if(theClass==Template) {334 // Templates are suppsed to have been imported before335 // importing a study. Study.template is matched to a336 // Template by the template's name.337 def child = node.children.find{ "name"==it.name }338 domainObject = Template.findByName( child.text() )339 }340 else {341 setSimpleFields()342 }343 }344 345 346 347 /**348 * Populate this.domainObject's String and Date fields of349 * a domainObject from parsed XML node.350 */351 private void setSimpleFields() {352 353 def fields =354 domainObject.getProperties().domainFields.collect { it.toString() }355 356 def map = [:]357 domainObject.metaClass.getProperties().each { property ->358 def name = property.name359 def field = fields.find{ it == name }360 361 if(field) {362 def type = property.type363 def value = node.children().find{ it.name == field }.text()364 365 switch(type) {366 case String: map[field]=value; break367 case Date: map[field] = Date.parse( 'yyyy-MM-dd', value ); break368 //case Boolean: ???; break369 }370 }371 }372 373 def newDomainObject = domainObject.class.newInstance(map)374 newDomainObject.id = 1 // neccessary?375 domainObject = newDomainObject376 }377 378 }379 380 381 212 }
Note: See TracChangeset
for help on using the changeset viewer.