Commits

Anonymous committed a984dd3

Explored responsibleParty type and runtime type determination.

Comments (0)

Files changed (10)

grails-app/controllers/ImportController.groovy

 
     def index = {
 
-      importXMLService.importFromFile("/Users/rob/Dropbox/Projects/basecamp/db/xml/koyanainc-20090716141017.xml")
+                           
+       importXMLService.importFromFile("/Users/rmacgreg/Dropbox/Projects/basecamp/db/xml/koyanainc-20090716141017.xml")
+
+
+      //importXMLService.importFromFile("/Users/rob/Dropbox/Projects/basecamp/db/xml/koyanainc-20090716141017.xml")
       //importXMLService.importFromFile("/Users/rob/Dropbox/Projects/basecamp/db/xml/rapportinc-20090721001012.xml")
 
     }

grails-app/domain/com/koyanainc/bc/Account.groovy

                         clients:Client, projects:Project]
 
     Firm firm
+    
+    String toString()
+    {
+       "Plan:${plan}. Account Holder:${accountHolder}"
+    }
 
     static constraints = {
         accountHolder(nullable:false)

grails-app/domain/com/koyanainc/bc/Client.groovy

     static mapping = {
         id generator: 'assigned'
 
-    }}
+    }
+}

grails-app/domain/com/koyanainc/bc/Milestone.groovy

   Person creator
   ResponsibleParty responsibleParty
 
-  Project project
+  //Project project
 
   Boolean wantsNotification = false
 
   static hasMany = [comments:Comment]
+  
+  static belongsTo = [project:Project]
+   
+  
   static constraints = {
     title(blank:false)
     completedOn(nullable:true)

grails-app/domain/com/koyanainc/bc/ResponsibleParty.groovy

 package com.koyanainc.bc
 
 class ResponsibleParty {
-
+   
+   /**   
+   static getById(id)
+   {
+      def rp = ResponsibleParty.findById(id)
+      if(null == rp ) println "\n\n\t Responsible Party id:${id} cannot be found in db"
+      
+      println "ResponsibleParty type:${rp?.'responsiblePartyType'}"
+   }
+**/
     static mapping = {
         id generator: 'assigned'
         tablePerHierarchy false

grails-app/domain/com/koyanainc/bc/TodoList.groovy

     Integer uncompletedCount = 0
 
     String description = ""
-    //Milestone milestone
 
     String name
     Integer position = 0

grails-app/services/ImportClientService.groovy

       def clientMap = XMLUtil.xmlToGorm(client.children())
       clientMap.remove('people')    //We have to remove the 'people' key and it's associated data - not digestible by GORM
 
+      clientMap['responsiblePartyType'] = 'Company'
       def c  = new Client(clientMap)
+      if(!c.validate()) println "Cannot create Client using map $clientMap"
       c.id = clientMap.id
       c.save(flush:true)
+      //println "client errors ${c.errors}"
 
       client.people.person.each{person->
         def personMap = XMLUtil.xmlToGorm(person.children())

grails-app/services/ImportProjectService.groovy

       }
 
       def crp = ResponsibleParty.findById(project.company.id.text())
-      if(null == crp ) log.warn "\n\n\t Company id:${project.company.id.text()} cannot be found in db"
       projectMap.company = crp
 
       def p  = new Project(projectMap)
 
         milestoneMap['responsibleParty'] = rp
         milestoneMap['creator'] = cr
-        milestoneMap['project'] = p
 
         //Insert company info
         def cId = milestone.company.id
+        
+        //Add belongsTo relationship to Project
+        milestoneMap['project'] = p
 
         def m = new Milestone(milestoneMap)
         m.id = milestoneMap.id
       }
 
       //Insert project posts and comments into db
+      XMLUtil.postXmlToGorm(project)
+      /**
       project.posts.post.each{postXml->
 
         def postMap = XMLUtil.xmlToGorm(postXml.children())
 
         po.save()
       }
+      **/
 
     }
   }

grails-app/services/ImportXMLService.groovy

 
     def importFromFile(String pathAndFilename) {
 
-        def accountXML = XML.parse(new FileInputStream(pathAndFilename), "UTF-8")
+       def accountXML = XML.parse(new FileInputStream(pathAndFilename), "UTF-8")
 
-    
-        importFirmService.importXML(accountXML.firm)
-        importClientService.importXML(accountXML.clients)
-        importProjectService.importXML(accountXML.projects)
-    
-        importTodoListsService.importXML(accountXML.projects)
+       log.info "Importing Firm";      importFirmService.importXML(accountXML.firm)
+       log.info "Importing Clients";   importClientService.importXML(accountXML.clients)
+       log.info "Importing Projects";  importProjectService.importXML(accountXML.projects)
+       log.info "Importing TodoLists"; importTodoListsService.importXML(accountXML.projects)
 
     }
 }

grails-app/utils/XMLUtil.groovy

 
 			date = match
 		}
+		if(null == date)
+		{
+			println "Null date parsed from $dateTimeString. Using epoch date 2007-01-01"
+			date = "2007-01-01"
+		}
 
 		def time
 		dateTimeString.eachMatch(/\d\d:\d\d:\d\d/){match->
 
 			time = match
 		}
+		if(null == time)
+		{
+			println "Null time parsed from $dateTimeString. Using epoch time 12:00:01"
+			time = "12:00:01"
+		}
 
 		def f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
 		return f.parse("$date $time")
     return f.parse(date)
   }
 
+
+  static void postXmlToGorm(xml)
+  {
+
+     xml.posts.post.each{postXml->
+
+        def postMap = XMLUtil.xmlToGorm(postXml.children())
+        postMap.remove('comments')
+//println "\n\tpostMap = ${postMap}"
+        def rp = com.koyanainc.bc.ResponsibleParty.findById(postMap.authorId)
+
+        postMap['author'] = rp
+        def po = new com.koyanainc.bc.Post(postMap)
+
+        po.id = postMap.id
+        po.save()
+
+//println("\n\tpost erorrs:${po.errors}")
+
+        postXml.comments.comment.each{commentXml->
+
+          def commentMap = XMLUtil.xmlToGorm(commentXml.children())
+//println "\n\tComment map = >${commentMap}<"
+if(null == commentMap) println "\n\ncomment map was null!\nFor commentXml $commentXml\n postXml $postXml"
+          def commentAuth = com.koyanainc.bc.ResponsibleParty.findById(commentMap.authorId)
+			if(null == commentAuth)
+				{
+					println "\n\n** Null author for ${commentMap}"
+					println "\nGenerating Person with id:${commentMap.authorId}"
+					String name  = ""+ commentMap.authorId.toString()
+					commentAuth = new com.koyanainc.bc.Person(firstName:name)
+					commentAuth.id = commentMap.authorId.toInteger()
+					commentAuth.save()
+				}
+
+          commentMap['author'] = commentAuth
+
+          def co = new com.koyanainc.bc.Comment(commentMap)
+          co.id = commentMap.id
+          co.save()
+//println("\n\tcomment erorrs:${co.errors}")
+
+
+          po.addToComments(co)
+        }
+
+        po.save()
+      }
+  }
 }