Changeset 1694


Ignore:
Timestamp:
Apr 4, 2011, 1:32:53 PM (12 years ago)
Author:
j.saito@…
Message:

Moved methods from service into controller.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/services/dbnp/studyexport/ExportService.groovy

    r1581 r1694  
    11/**
    2  *  ExporterService
     2 *  This service is for exporting studies to a XML flat list and for
     3 *  importing them back.
    34 * 
    45 *  @author Jahn
    56 * 
    6  *  This service for exporting a Study and all domain objects depending on it to XML.
     7 *  The file is used in combination with a controller.
    78 */
    8 
    99
    1010
     
    1515import grails.converters.XML
    1616import groovy.util.slurpersupport.*
     17import org.dbnp.bgdt.*
    1718import org.dbnp.gdt.*
    1819
     
    5051     *  List of domain classes related to Study.
    5152     */
    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 = [
    5654                        'Assay':dbnp.studycapturing.Assay,
     55                        'AssayModule':org.dbnp.gdt.AssayModule,
    5756                        'Event':dbnp.studycapturing.Event, 'EventGroup':dbnp.studycapturing.EventGroup,
     57                        'Identity':org.dbnp.gdt.Identity,
    5858                        'PersonAffiliation':dbnp.studycapturing.PersonAffiliation,
    5959                        'Person':dbnp.studycapturing.Person,
    6060                        'PersonRole':dbnp.studycapturing.PersonRole,
    6161                        'Publication':dbnp.studycapturing.Publication,
     62                        'RegistrationCode':dbnp.authentication.RegistrationCode,
     63                        'RelTime':org.dbnp.gdt.RelTime,
    6264                        'Sample':dbnp.studycapturing.Sample,
    6365                        'SamplingEvent':dbnp.studycapturing.SamplingEvent,
     66                        'SecRole':dbnp.authentication.SecRole, 'SecUser':dbnp.authentication.SecUser,
     67                        'SecUserSecRole':dbnp.authentication.SecUserSecRole,
     68                        'SessionAuthenticatedUser':dbnp.authentication.SessionAuthenticatedUser,
    6469                        'Study':dbnp.studycapturing.Study,
    6570                        'StudyPerson':dbnp.studycapturing.StudyPerson,
    6671                        'Subject':dbnp.studycapturing.Subject,
    67                         'AssayModule':org.dbnp.gdt.AssayModule,
    68                         'Identity':org.dbnp.gdt.Identity,
    69                         'RelTime':org.dbnp.gdt.RelTime,
    7072                        'TemplateEntity':org.dbnp.gdt.TemplateEntity,
     73                        'TemplateFieldListItem':org.dbnp.gdt.TemplateFieldListItem,
    7174                        'TemplateField':org.dbnp.gdt.TemplateField,
    72                         'TemplateFieldListItem':org.dbnp.gdt.TemplateFieldListItem,
    7375                        'TemplateFieldType':org.dbnp.gdt.TemplateFieldType,
    7476                        'Template':org.dbnp.gdt.Template ]
     77
    7578
    7679
     
    134137                if(domainObject instanceof TemplateEntity ) {
    135138                        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
    137152                }
    138153
     
    195210
    196211
    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 void
    207          */
    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 list 
    222           *     of ParseObjects based on parsed id. If the TemplateEntity instance does
    223           * not have an matching template in the list of ParseObjects, it remains empty.
    224           *
    225           * @param domainObject Some Template Entity
    226           *
    227           * @param node Node with parse information
    228           *
    229           * @param parseObjects List of ParseObjects
    230           */
    231         def addTemplateRelations( TemplateEntity domainObject, Node node, List parseObjects ) {
    232                 def id = node.children().find{it.name=='template'}?.attributes()?.id
    233                 if(id) {
    234                         def template = parseObjects.find{ it.theClass==Template && it.id==id }?.domainObject
    235                         if(template) {
    236                                 domainObject.template = template
    237                         }
    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 Entity
    247           *
    248           * @param node Node with parse information
    249           *
    250           */
    251         def addTemplateFields( TemplateEntity domainObject, Node node ) {
    252                 domainObject.metaClass.getProperties().each{ property ->
    253                         def name = property.name      // name of templateFields, e.g., templateStringFields
    254                         if( name ==~/template(.+)Fields/ ) {
    255                                 node.children().find{it.name==name}?.children()?.each{ fieldNode ->     
    256                                         def key = fieldNode.attributes()?.key
    257                                         def value = fieldNode.text()
    258                                         //domainObject.setFieldValue(key,value)  -> needs to be fixed based on class/type
    259                                 }
    260                         }
    261                 }
    262         }
    263 
    264 
    265 
    266         /**
    267          *  Populate one-to-many maps of a new domainObject 
    268          *  from list of ParseObjects.
    269          * 
    270          *  (For importing Study objects)
    271          * 
    272          *  @param domainObject   domainObject to be fielled 
    273          * 
    274          *  @param  List of parseObjects representing a Study.
    275          *
    276          *  @return the new domainObject
    277          */
    278         def populateOneToManies( domainObject, node, parseObjects ) {
    279                 if( !domainObject.class.metaClass.getProperties().find{ it.name=='hasMany' } ) {
    280                         return
    281                 }
    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.id
    288                                                 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 fielled 
    309          * 
    310          *  @param  List of parseObjects representing a Study.
    311          *
    312          *  @return the new domainObject
    313          */
    314 
    315         private class ParseObject {
    316                 String tag
    317                 String id
    318                 Class theClass
    319                 Object domainObject
    320                 Node node
    321 
    322 
    323                 public ParseObject( node ){
    324                         tag = node.name()
    325                         theClass = getClassForTag( tag )
    326                         domainObject = theClass.newInstance()
    327                         id = null
    328                         if(node.attributes && node.attributes.id) {
    329                                 id = node.attributes.id
    330                         }
    331                         this.node=node
    332 
    333                         if(theClass==Template) {
    334                                                                 // Templates are suppsed to have been imported before
    335                                                                 // importing a study. Study.template is matched to a
    336                                                                 // 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 of
    349                  *  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.name
    359                                 def field = fields.find{ it == name }
    360 
    361                                 if(field) {
    362                                         def type = property.type
    363                                         def value = node.children().find{ it.name == field }.text()
    364 
    365                                         switch(type) {
    366                                                 case String: map[field]=value; break
    367                                                 case Date: map[field] = Date.parse( 'yyyy-MM-dd', value ); break
    368                                                 //case Boolean: ???; break
    369                                         }
    370                                 }
    371                         }
    372 
    373                         def newDomainObject = domainObject.class.newInstance(map)
    374                         newDomainObject.id = 1  // neccessary?
    375                         domainObject = newDomainObject
    376                 }
    377 
    378         }
    379 
    380 
    381212}
Note: See TracChangeset for help on using the changeset viewer.