Changeset 1568


Ignore:
Timestamp:
Feb 25, 2011, 4:55:05 PM (12 years ago)
Author:
j.saito@…
Message:

Added support for matching tempaltes that are present in system.
In this way, template related information can in principle be imported, if the template is known in the current application.

File:
1 edited

Legend:

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

    r1561 r1568  
    121121                def objects = []
    122122
     123
    123124                if( IgnoredClasses.contains(domainClass) )   {
    124125                        return objects
     
    131132
    132133
     134                if(domainObject instanceof TemplateEntity ) {
     135                        objects.push(domainObject.template)
     136                        domainObject.template.fields.each { objects.push(it) }
     137                }
     138
    133139
    134140                if( TerminalClasses.contains(domainClass) )  {
    135                         return [domainObject]
    136                 }
    137 
    138                 objects = [domainObject]
     141                        objects.push(domainObject)
     142                        return objects
     143                }
     144
     145                objects.push(domainObject)
    139146
    140147                                                                                                // enter recursion with regular domain fields
     
    199206         *  @return void
    200207         */
    201 
    202208        def createStudy( List parseObjects ) {
    203209                parseObjects.each{
    204                         populateFields( it.domainObject, it.node, parseObjects )
     210                        populateOneToManies( it.domainObject, it.node, parseObjects )
    205211                }
    206212                parseObjects.each{
    207                         populateOneToManies( it.domainObject, it.node, parseObjects )
    208                 }
    209                 parseObjects.each{
    210                         populateTemplateFields( it.domainObject, it.node, parseObjects )
    211                 }
    212         }
    213 
    214 
    215 
    216         /**
    217          *  Populate fields of a domainObject to be created.
    218          *  The fields of a new object are filled with simple Class objects.
    219          * 
    220          *  (For importing Study objects)
    221          * 
    222          *  @param domainObject   The Object to be fielled 
    223          * 
    224          *  @param  List of ParseObjects representing a Study.
    225          *
    226          *  @return the new domainObject
    227          */
    228 
    229         def populateFields( domainObject, node, List parseObjects ) {
    230 
    231                 def fields =
    232                         domainObject.getProperties().domainFields.collect { it.toString() }
    233 
    234                 def map = [:]
    235                 domainObject.metaClass.getProperties().each { property ->
    236                         def name = property.name
    237                         def field = fields.find{ it == name }
    238 
    239                         if(field) {
    240                                 def type = property.type
    241                                 def value = node.children().find{ it.name == field }.text()
    242 
    243                                 switch(type) {
    244                                         case String: map[field]=value; break
    245                                         case Date: map[field] = Date.parse( 'yyyy-MM-dd', value ); break
     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
    246259                                }
    247260                        }
    248261                }
    249 
    250                 def newDomainObject = domainObject.class.newInstance(map)
    251                 newDomainObject.id = 0
    252                 domainObject = newDomainObject
    253262        }
    254263
     
    267276         *  @return the new domainObject
    268277         */
    269        
    270278        def populateOneToManies( domainObject, node, parseObjects ) {
    271279                if( !domainObject.class.metaClass.getProperties().find{ it.name=='hasMany' } ) {
     
    292300
    293301
    294         def populateTemplateFields( domainObject, Node node, List parseObjects ) {
    295         }
    296 
    297 
    298 
    299         /**
    300          *  Populate one-to-many maps of a new domainObject 
    301          *  from list of ParseObjects.
     302
     303        /**
     304         *  Populate one-to-many maps of a new domainObject  from list of ParseObjects.
    302305         *
    303306         *  (For importing Study objects)
     
    315318                Class theClass
    316319                Object domainObject
    317                 groovy.util.slurpersupport.Node node
    318        
     320                Node node
     321
    319322
    320323                public ParseObject( node ){
     
    327330                        }
    328331                        this.node=node
    329                 }
    330 
    331                 private static getClassForTag( String tag ) {
    332                         def shortName = tag.replaceFirst( tag[0], tag[0].toUpperCase() )
    333                         return DomainClasses[ shortName ]
    334                 }
    335 
    336         }
     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
    337380
    338381}
Note: See TracChangeset for help on using the changeset viewer.