Source

Texa / grails-app / utils / XMLUtil.groovy


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @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("-")

    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
  }

  static Map convertIdToLong(Map m)
  {
    if( m.containsKey("id"))
    {
      m.id = m.id.toLong()
    }

    m
  }

  static Date convertDatetimeStringToDate(String dateTimeString)
	{
		def date
		dateTimeString.eachMatch(/\d\d\d\d-\d\d-\d\d/){match->

			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")
	}

  static Date convertDateToDateObject(String date)
  {
 		def f = new SimpleDateFormat("yyyy-MM-dd")
    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()
      }
  }
}