Commits

Robert MacGregor committed 0a8ea43

Added Milestone import.

Comments (0)

Files changed (14)

application.properties

 #utf-8
-#Sun Aug 23 18:29:55 EDT 2009
+#Mon Aug 24 13:45:21 EDT 2009
 app.version=0.1
 plugins.runtime-logging=0.3
 app.servlet.version=2.4

grails-app/conf/Config.groovy

     }
     
     appenders {
-        file name:'file', file:'/Users/rob/sherpa.log'
-
         console name:'stdout', layout:pattern(conversionPattern: '%c{2} %m%n')
     }
 
     root{
-        info 'stdout', 'file'
+        info 'stdout'
         additivity = true
 
     }

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/rapportinc-20090721001012.xml")
 
     }

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

 package com.koyanainc.bc
 
-class Client {
+class Client extends ResponsibleParty {
 
     String name
     String addressOne = ""

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

 package com.koyanainc.bc
 
-class Firm {
+class Firm extends ResponsibleParty {
 
     String name
     String addressOne = ""

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

 
 class Milestone {
 
-    String title
-    Integer commentsCount = 0
-    Boolean completed = false
+  String title
+  Integer commentsCount = 0
+  Boolean completed = false
 
-    Date completedOn
-    Person completer
+  Date completedOn
+  Date deadline
+  Date createdOn
 
-    Date createdOn = new Date()
-    Person creator
+  Person completer
+  Person creator
+  ResponsibleParty responsibleParty
 
-    Date deadline
-    Project project
+  Project project
 
-    Person responsibleParty
-    Boolean wantsNotification = false
+  Boolean wantsNotification = false
 
-    static hasMany = [comments:Comment]
-    static constraints = {
-        title(blank:false)
-        completedOn(nullable:true)
-        creator(nullable:false)
+  static hasMany = [comments:Comment]
+  static constraints = {
+    title(blank:false)
+    completedOn(nullable:true)
+    creator(nullable:false)
 
-        deadline(nullable:false)
-        project(nullable:false)
-        responsibleParty(nullable:true)
+    //deadline(nullable:false)
+    project(nullable:false)
+    completer(nullable:true)
+    //responsibleParty(nullable:true)
 
-    static mapping = {
-        id generator: 'assigned'
-
-    }    }
+  }
+    
+  static mapping = {
+    id generator: 'assigned'
+  }
 
 }

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

 package com.koyanainc.bc
 
-class Person {
+class Person extends ResponsibleParty{
 
     String firstName = ""
     String lastName = ""

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

+package com.koyanainc.bc
+
+class ResponsibleParty {
+
+    static mapping = {
+        id generator: 'assigned'
+        tablePerHierarchy false
+    }
+
+}

grails-app/services/ImportClientService.groovy

 
         def p = new Person(personMap)
         p.id = personMap.id
-
+log.info "Saving person under id ${p.id} using personMap id ${personMap.id}"
         p.save(flush:true)
         c.addToPeople(p)
       }

grails-app/services/ImportFirmService.groovy

       }
     }
 
-    def leafMap = XMLUtil.xmlToMap(firmLeafs)
+    def leafMap = XMLUtil.xmlToGorm(firmLeafs)
 
     def f = new Firm(leafMap)
-    f.id = leafMap.id.toInteger()
+    f.id = leafMap.id
     f.save(flush:true)
 
 
     }
 
     peopleList.each{
-      def pm = XMLUtil.xmlToMap(it.children())
+      def pm = XMLUtil.xmlToGorm(it.children())
 
       def p = new Person(pm)
-      p.id = pm.id.toInteger()
+      p.id = pm.id
       p.save(flush:true)
       f.addToPeople(p)
     }
   }
-/**
-  Map xmlToMap(xml)
-  {
-    Map m = [:]
-    xml.each{
-      m[xmlToCamel(it.name())] = it.text()
-    }
 
-    m
-  }
-
-  String xmlToCamel(String xmlString)
-  {
-    def b = xmlString.split("-")
-
-    String r = b[0]
-    b.eachWithIndex{v,i->
-      StringBuffer sb = new StringBuffer(v)
-      if(i!=0){
-        Character c = v[0].toUpperCase()
-        sb.setCharAt(0,c)
-
-        r += sb
-      }
-    }
-
-    r
-  }
-**/
-  }
+ }

grails-app/services/ImportProjectService.groovy

-import com.koyanainc.bc.Project
+import com.koyanainc.bc.*
 
 class ImportProjectService {
 
-    boolean transactional = true
+  boolean transactional = true
 
-    def importXML(projects) {
+  def importXML(projects) {
 
     projects.project.each{project->
 
       p.id = projectMap.id
       p.save(flush:true)
 
+      project.milestones.milestone.each{milestone->
+
+        def milestoneMap = XMLUtil.xmlToGorm(milestone.children())
+
+        milestoneMap.remove('comments')
+
+        def co = "Undefined Completer"
+        if(milestoneMap.containsKey('completerId'))
+        {
+          co = ResponsibleParty.findById(milestoneMap.completerId)
+          milestoneMap['completer'] = co
+        }
+
+        def rp = ResponsibleParty.findById(milestoneMap.responsiblePartyId)
+        def cr = ResponsibleParty.findById(milestoneMap.creatorId)
+        log.info "\n\n\t Rp is $rp, completer is $co, creator is $cr"
+        milestoneMap['responsibleParty'] = rp
+        milestoneMap['creator'] = cr
+        milestoneMap['project'] = p
+
+        log.warn "\n\n\t Milestone is ${milestoneMap}"
+
+        def m = new Milestone(milestoneMap)
+        m.id = milestoneMap.id
+        m.save()
+
+        log.warn "Milestone creation errors ${m.errors}"
+      }
+
     }
   }
 }

grails-app/services/ImportXMLService.groovy

 
         def accountXML = XML.parse(new FileInputStream(pathAndFilename), "UTF-8")
 
-        //importFirmService.importXML(accountXML.firm)
-        //importClientService.importXML(accountXML.clients)
+        importFirmService.importXML(accountXML.firm)
+        importClientService.importXML(accountXML.clients)
         importProjectService.importXML(accountXML.projects)
 
     }

grails-app/utils/XMLUtil.groovy

  *
  * @author rob
  */
+
+import java.text.SimpleDateFormat
+
+
 class XMLUtil {
+
+  static Map xmlToGorm(xml)
+  {
+    Map m = [:]
+    xml.each{
+
+      if( it.attributes().type == 'datetime' )
+      {
+        m[xmlToCamel(it.name())] = convertDatetimeStringToDate(it.text())
+      }
+      else if( it.attributes().type == 'date' )
+      {
+        m[xmlToCamel(it.name())] = convertDateToDateObject(it.text())
+      }
+      else
+      {
+         m[xmlToCamel(it.name())] = it.text()
+      }
+    }
+
+    convertIdToLong(m)
+  }
+
+
   static String xmlToCamel(String xmlString)
   {
     def b = xmlString.split("-")
     r
   }
 
-  static Map xmlToGorm(xml)
-  {
-    Map m = [:]
-    xml.each{
-
-      m[XMLUtil.xmlToCamel(it.name())] = it.text()
-    }
-
-    convertIdToLong(m)
-  }
-
   static Map convertIdToLong(Map m)
   {
     if( m.containsKey("id"))
 
     m
   }
-	
+
+  static Date convertDatetimeStringToDate(String dateTimeString)
+	{
+		def date
+		dateTimeString.eachMatch(/\d\d\d\d-\d\d-\d\d/){match->
+
+			date = match
+		}
+
+		def time
+		dateTimeString.eachMatch(/\d\d:\d\d:\d\d/){match->
+
+			time = match
+		}
+
+		def f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
+		return f.parse("$date $time")
+	}
+
+  static Date convertDateToDateObject(String date)
+  {
+ 		def f = new SimpleDateFormat("yyyy-MM-dd")
+    return f.parse(date)
+  }
+
 }
 

test/unit/com/koyanainc/bc/ResponsiblePartyTests.groovy

+package com.koyanainc.bc
+
+import grails.test.*
+
+class ResponsiblePartyTests extends GrailsUnitTestCase {
+    protected void setUp() {
+        super.setUp()
+    }
+
+    protected void tearDown() {
+        super.tearDown()
+    }
+
+    void testSomething() {
+
+    }
+}