Source

Texa / grails-app / services / ImportTodoListsService.groovy

Full commit
import com.koyanainc.bc.*

class ImportTodoListsService {

  boolean transactional = true

  def importXML(projects) {

    projects.project.each{project->

      project.'todo-lists'.'todo-list'.each{todoListXml->

        def todoListMap = XMLUtil.xmlToGorm(todoListXml.children())

        todoListMap.remove('todoItems')

        def p = Project.findById(todoListXml.'project-id'.text())
        if( null == p )
        {
          log.warn "\n\tFailed to locate project with id ${todoListXml.'project-id'.text()}"
        }
        todoListMap['project'] = p

        def tl = new TodoList(todoListMap)
        if( null == tl )
        {
          log.warn "\n\tFailed to create todoList using map: ${todoListMap}.\nGot errors: ${tl.errors}"
        }
        tl.id = todoListMap.id
        tl.save()

        //Add our todoList to it's parent project'
        p.addToTodoLists(tl)

        //Add todoList items to our todoList
        todoListXml.'todo-items'.'todo-item'.each{todoItemXml->

          def todoItemMap = XMLUtil.xmlToGorm(todoItemXml.children())

          todoItemMap.remove('comments')

          def co = "Undefined Completer"
          if(todoItemMap.containsKey('completerId'))
          {
            co = ResponsibleParty.findById(todoItemMap.completerId)
            todoItemMap['completer'] = co
          }
//log.warn "\n\ntodoItemMap is ${todoItemMap}"

          def rp = "Undefined responsible party"
          if( todoItemMap.containsKey('responsiblePartyId'))
          {
            rp = ResponsibleParty.findById(todoItemMap.responsiblePartyId)
            todoItemMap['responsibleParty'] = rp
          }

          def cr = ResponsibleParty.findById(todoItemMap.creatorId)
          todoItemMap['creator'] = cr

          todoItemMap['todoList'] = tl

          def ti = new TodoItem(todoItemMap)
          if( null == ti )
          {
           log.warn "\n\tFailed to create todoItem using map: ${todoItemMap}.\nGot errors: ${ti.errors}"
          }
          ti.id = todoItemMap.id
          ti.save()

        }

      }
    }
  }
}