From e0d905db47c714c00f84fb6690223b0df58948e3 Mon Sep 17 00:00:00 2001 From: Na Lee Ha Date: Fri, 24 Nov 2023 16:18:53 +0000 Subject: [PATCH] #980 adding basket trading constituent row - using menu for testing --- .../BasketTradingConstituentJoinService.scala | 75 ++++++++++++++++++- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/example/basket/src/main/scala/org/finos/vuu/core/module/basket/service/BasketTradingConstituentJoinService.scala b/example/basket/src/main/scala/org/finos/vuu/core/module/basket/service/BasketTradingConstituentJoinService.scala index 96e637d80..6b2526c4d 100644 --- a/example/basket/src/main/scala/org/finos/vuu/core/module/basket/service/BasketTradingConstituentJoinService.scala +++ b/example/basket/src/main/scala/org/finos/vuu/core/module/basket/service/BasketTradingConstituentJoinService.scala @@ -4,9 +4,10 @@ import com.typesafe.scalalogging.StrictLogging import org.finos.toolbox.time.Clock import org.finos.vuu.api.JoinTableDef import org.finos.vuu.core.module.basket.BasketConstants.Side -import org.finos.vuu.core.module.basket.BasketModule.{BasketTradingConstituentColumnNames => ColumnName} -import org.finos.vuu.core.table.{DataTable, JoinTable, RowWithData, TableContainer} -import org.finos.vuu.net.ClientSessionId +import org.finos.vuu.core.module.basket.BasketModule +import org.finos.vuu.core.module.basket.BasketModule.{BasketTradingTable, BasketTradingColumnNames => BTColumnName, BasketTradingConstituentColumnNames => ColumnName} +import org.finos.vuu.core.table.{DataTable, JoinTable, RowData, RowWithData, TableContainer} +import org.finos.vuu.net.{ClientSessionId, RequestContext} import org.finos.vuu.net.rpc.{EditRpcHandler, RpcHandler} import org.finos.vuu.viewport._ @@ -21,6 +22,7 @@ object BasketTradingConstituentJoinService {} class BasketTradingConstituentJoinService(val table: DataTable, val tableContainer: TableContainer)(implicit clock: Clock) extends BasketTradingConstituentJoinServiceIF with RpcHandler with StrictLogging { def onDeleteRow(key: String, vp: ViewPort, session: ClientSessionId): ViewPortEditAction = { + ViewPortEditSuccess() } @@ -32,6 +34,70 @@ class BasketTradingConstituentJoinService(val table: DataTable, val tableContain ViewPortEditSuccess() } + def testAddConstituent(selection: ViewPortSelection, session: ClientSessionId): ViewPortAction = { + addConstituents(Array("VOD.L", "BP.L")) + //todo handle failure + } + def addConstituents(rics: Array[String]): ViewPortEditAction = { + //def addConstituents(rics: Array[String])(ctx: RequestContext): ViewPortEditAction = { + //todo handle if no rows + if(table.size() == 0) + ViewPortEditFailure(s"Failed to add constituents to ${table.name} as adding row to empty table is currently not supported") + + val existingConstituentRow = table.pullRow(table.primaryKeys.head) + val tradeId = existingConstituentRow.get(ColumnName.InstanceId).asInstanceOf[String] + + val tradeRow = tableContainer.getTable(BasketModule.BasketTradingTable).pullRow(tradeId) + val basketId = tradeRow.get(BTColumnName.BasketId).asInstanceOf[String] + val tradeUnit = tradeRow.get(BTColumnName.Units).asInstanceOf[Int] + + rics.foreach(ric => { + + //todo should we guard against adding row for ric that already exist? + val newRow = mkTradingConstituentRow( + basketTradeInstanceId = tradeId, + sourceBasketId = basketId, + tradeUnit = tradeUnit, + ric = ric) + + updateJoinTable(table, newRow) + + logger.info(s"added constituent $ric") + }) + + ViewPortEditSuccess() + + } + + private def mkTradingConstituentRow(basketTradeInstanceId: String, sourceBasketId:String, tradeUnit:Int, ric: String): RowWithData = { + val constituentKey = s"$basketTradeInstanceId.$ric" + val weighting: Double = 0.1 + RowWithData( + constituentKey, + Map( + ColumnName.Ric -> ric, + ColumnName.BasketId -> sourceBasketId, + ColumnName.InstanceId -> basketTradeInstanceId, + ColumnName.InstanceIdRic -> constituentKey, + ColumnName.Quantity -> (weighting * 100).asInstanceOf[Long], + ColumnName.Description -> "", //todo look up description from instrument table + ColumnName.Side -> Side.Buy, + ColumnName.Weighting -> weighting, + ColumnName.PriceStrategyId -> 2, + ColumnName.Algo -> -1, + )) + } + + def updateJoinTable(table: DataTable, row: RowWithData): Unit = { + val joinTable = table.asTable.asInstanceOf[JoinTable] + val baseTableDef = joinTable.getTableDef.asInstanceOf[JoinTableDef].baseTable + joinTable.sourceTables.get(baseTableDef.name) match { + case Some(table: DataTable) => + table.processUpdate(row.key, row, clock.now()) + case None => + //log and throw? + } + } private def onEditCell(key: String, columnName: String, data: Any, vp: ViewPort, session: ClientSessionId): ViewPortEditAction = { val joinTable = vp.table.asTable.asInstanceOf[JoinTable] val baseTableDef = joinTable.getTableDef.asInstanceOf[JoinTableDef].baseTable @@ -118,6 +184,7 @@ class BasketTradingConstituentJoinService(val table: DataTable, val tableContain } override def menuItems(): ViewPortMenu = ViewPortMenu("Direction", new SelectionViewPortMenuItem("Set Sell", "", this.setSell, "SET_SELECTION_SELL"), - new SelectionViewPortMenuItem("Set Buy", "", this.setBuy, "SET_SELECTION_Buy") + new SelectionViewPortMenuItem("Set Buy", "", this.setBuy, "SET_SELECTION_Buy"), + new SelectionViewPortMenuItem("Add Row", "", this.testAddConstituent, "TEST_ADD_ROW"), ) }