Source

Texa / grails-app / services / ImportProjectService.groovy

Full commit
import com.koyanainc.bc.*

class ImportProjectService {

  boolean transactional = true

  def importXML(projects) {

    projects.project.each{project->

      def projectMap = XMLUtil.xmlToGorm(project.children())

      //These keys are indigestible by GORM as-is
      "todoLists,attachmentCategories,company,timeEntries,posts,postCategories,milestones,participants".split(',').each{
        projectMap.remove(it)
      }

      def crp = ResponsibleParty.findById(project.company.id.text())
      projectMap.responsibleParty = crp

      def p  = new Project(projectMap)
      p.id = projectMap.id
      p.save()

      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)

        milestoneMap['responsibleParty'] = rp
        milestoneMap['creator'] = cr

        //Add belongsTo relationship to Project
        milestoneMap['project'] = p

        def m = new Milestone(milestoneMap)
				if( !m.validate() ) { m.errors.allErrors.each{e-> println "Failed creating Milestone with map ${milestoneMap}. Error:$e"} }
        m.id = milestoneMap.id
        m.save() 

      }

      //Insert project participants into project
      project.participants.person.each{personId->
        def per = ResponsibleParty.findById(personId.text())
        p.addToParticipants(per)
      }

      //Insert project posts and comments into db
      XMLUtil.postXmlToGorm(project)

    }
  }
}