Commit 82523709 by Amelin Konstantin

Add automatic template choice

parent 11e60d11
......@@ -5,11 +5,11 @@ import templates._
object Main extends App {
//val p = new PrettyPrinter(120, 2)
val sourceXml = XML.loadFile("src/main/resources/nsd/Repo NSD entity.xml")
val template = new RepoTemplate(sourceXml)
//val outputXml = XML.loadString(p.format(template.render))
val outputXml = template.template
val p = new PrettyPrinter(140, 2)
val sourceXml = XML.loadFile("src/main/resources/nsd/Repo NSD bond.xml")
val template = Template(sourceXml)
val outputXml = XML.loadString(p.format(template.template))
//val outputXml = template.template
template.log()
......
......@@ -2,11 +2,16 @@ package templates
import scala.util.Try
import scala.xml.Node
import templates.Template._
final class RepoTemplate(source: Node) extends TradeTemplate(source) {
override protected[this] val msgNotFound = "No elem found"
protected[this] def templateTrade: Node = {
val productType = (source \ "trade" \ "repo" \ "productType").head.text.mkString match {
case "Equity:Repo:EquityRepo" => Equity
case "InterestRate:Repo:BondRepo" => Bond
}
<fpml:trade>
<fpml:tradeHeader>
{ for (s <- source \ "trade" \ "tradeHeader" \ "partyTradeIdentifier") yield {
......@@ -117,18 +122,26 @@ final class RepoTemplate(source: Node) extends TradeTemplate(source) {
<fpmlext:repo xsi:type="rtsrep:Repo">
<fpml:productId>{ get(Try { (source \ "trade" \ "repo" \ "productType" ).head }) }</fpml:productId>
<fpml:productId productIdScheme={ get(Try { (source \ "trade" \ "repo" \ "productId" ).head }, "productIdScheme") }>{ get(Try { (source \ "trade" \ "repo" \ "productId" ).head }) }</fpml:productId>
{ val iv = get(Try { (source \ "trade" \ "repo" \ "fixedRateSchedule" \ "initialValue").head })
if (iv.mkString != msgNotFound)
{ if (productType == Equity)
<fpmlext:fixedRateSchedule>
<fpml:initialValue>{ iv }</fpml:initialValue>
<fpml:initialValue>{ get(Try { (source \ "trade" \ "repo" \ "fixedRateSchedule" \ "initialValue").head }) }</fpml:initialValue>
<step>
<stepDate>{ get(Try { (source \ "trade" \ "repo" \ "fixedRateSchedule" \\ "stepDate").head }) }</stepDate>
<stepValue>{ get(Try { (source \ "trade" \ "repo" \ "fixedRateSchedule" \\ "stepValue").head }) }</stepValue>
</step>
</fpmlext:fixedRateSchedule>
else
/* Add different type to template */
else if (productType == Bond)
<fpmlext:floatingRateCalculation>
<fpml:floatingRateIndex>{ get(Try { (source \ "trade" \ "repo" \ "floatingRateCalculation" \ "floatingRateIndex" ).head }) }</fpml:floatingRateIndex>
<indexTenor>
<periodMultiplier>{ get(Try { (source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "periodMultiplier" ).head }) }</periodMultiplier>
<period>{ get(Try { (source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "period" ).head }) }</period>
</indexTenor>
<spreadSchedule>
<initialValue>{ get(Try { (source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "initialValue" ).head }) }</initialValue>
</spreadSchedule>
<initialRate>{ get(Try { (source \ "trade" \ "repo" \ "floatingRateCalculation" \\ "initialRate" ).head }) }</initialRate>
</fpmlext:floatingRateCalculation>
/*---------------------------------*/
}
<fpmlext:dayCountFraction>{ get(Try { (source \ "trade" \ "repo" \ "dayCountFraction" ).head }) }</fpmlext:dayCountFraction>
<fpmlext:spotLeg xsi:type="rtsrep:RepoTransactionLeg">
......@@ -173,10 +186,15 @@ final class RepoTemplate(source: Node) extends TradeTemplate(source) {
</fpml:adjustableDate>
</rtsrep:deliveryDate>
</fpmlext:forwardLeg>
{/*Its necessary to add instrument choice */}
{ if (productType == Equity)
<fpml:equity id={ get(Try { (source \ "trade" \ "repo" \ "equity").head }, "id") }>
<fpml:instrumentId instrumentIdScheme="">{ get(Try { (source \ "trade" \ "repo" \ "equity" \ "instrumentId").head }) }</fpml:instrumentId>
</fpml:equity>
else if (productType == Bond)
<fpml:bond id={ get(Try { (source \ "trade" \ "repo" \ "bond").head }, "id") }>
<fpml:instrumentId instrumentIdScheme="">{ get(Try { (source \ "trade" \ "repo" \ "bond" \ "instrumentId").head }) }</fpml:instrumentId>
</fpml:bond>
}
</fpmlext:repo>
<fpml:documentation>
<fpml:masterAgreement>
......
......@@ -3,7 +3,7 @@ package templates
import scala.collection.mutable
import scala.util.{Success, Try}
import scala.xml.transform.{RewriteRule, RuleTransformer}
import scala.xml.{Node, NodeSeq, Text}
import scala.xml._
abstract class Template(protected[this] val source: Node) {
protected[this] val msgNotFound = "Not found"
......@@ -79,3 +79,21 @@ abstract class Template(protected[this] val source: Node) {
def log(): Unit = data.foreach(e => println(e._2))
}
object Template {
sealed trait ProductType
case object Equity extends ProductType
case object Bond extends ProductType
def apply(source: Node): Template = (source \ "trade").head.child
.filter {
case v: Elem => true
case _ => false
}(1).label match {
case "repo" => new RepoTemplate(source)
case v => throw new Exception("Undefined trade type")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment