Groovy web console

subscribe to the feed Subscribe
to this
site

Financial Message Transformation

Published 2 months ago by Abhijit
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def xmltext = """<paste financial message payload here>"""
 def inputXML = new XmlSlurper().parseText( xmltext )
 def writer = new StringWriter()
 def xml = new groovy.xml.MarkupBuilder(writer)
 inputXML.financialMessage.each{ finMessage->   
 def id = finMessage.@id.toString()
 def jobId = finMessage.@jobId.toString()
 def messageDate = finMessage.@messageDate.toString()
 def policyMap = [:].withDefault{[]}
 def policyMemberMap = [:].withDefault{[]}
 def pollist = []  
 finMessage.invoices.invoice.each{ invoice->
   def source = invoice.@source.toString()
   def ohiInvoiceId = invoice.@ohiInvoiceId.toString()
   def billAccount = invoice.invoiceBulkingCriteria.@counterpartyCode.toString()
   def documentId = invoice.@documentId.toString()
   def invoiceAmount = invoice.@invoiceAmount.toString()
   def counter = 0
   def memlist = []
      policyMap = [:].withDefault{[]}
   policyMemberMap = [:].withDefault{[]}
   invoice.invoiceLines.invoiceLine.each{ invoiceLine->
  pollist << invoiceLine.@attribute1.toString()
}
def polUniqueList = pollist.unique( false )
polUniqueList.each{pol ->
  def aggMemberList = []
  memlist = []
  invoice.invoiceLines.invoiceLine.each{ invoiceLine->
   invoiceLinePolicy = invoiceLine.@attribute1.toString()
   if(invoiceLinePolicy == pol){
   aggMemberList.add( ['ohiLineId': invoiceLine.@ohiLineId.toString(), 'amount': invoiceLine.@amount.toString(), 'description': invoiceLine.@description.toString(),'policyCd':invoiceLine.@attribute1.toString(), 'memberCd':invoiceLine.@attribute2.toString(), 'premiumRateList': invoiceLine.@attribute3.toString()  ])
   memlist << invoiceLine.@attribute2.toString()
   }
  }
  memUniqueList = memlist.unique( false )
  policyMemberMap.put(pol, memUniqueList)
  policyMap.put(pol, aggMemberList)
    }
  int count = 0	
xml.root()
{
  headerData(){
   sourceSystem('OHI')}
  policyMap.each{map-> 
   policyCode = map.key
   policyDetails = map.value
   premiumRateStructureMessage(){
count = count+1
    headerData(){    
     messageType('CM-PREMIUMRATEINBOUNDMESSAGE')
     messageID(id)
     messageBatchID(jobId)
     sourceSystem('OHI')
     sourceSystemTransID()
     sourceSystemSourceID(policyCode)
     bo('CMPremiumRateInboundMessage')
     status('Pending')
     createDateTime(messageDate)
     ORMBcreateDateTime()
    }
    groupedMemList = policyDetails.groupBy{mem -> mem.memberCd}
    groupedMemList.each{list-> memCode = list.key
     memDetails = list.value
     premiumRateStructureData(){
      subscriberData(){
       Id(){
        IDType('PHI')
        IDValue(memCode)
       }
      }
      memDetails.each{premiumRate->
       premiumRates(premiumRate.premiumRateList)
      }
     }  
    }
   }
  }
  trailerData(){
recordCount(count.toString())
totalAmount(invoiceAmount)
   }
}
  
 }
 }
println writer.toString()