Skip to content

Commit

Permalink
finos#980 adding basket trading constituent row - using menu for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
naleeha committed Dec 5, 2023
1 parent 6463da7 commit e0d905d
Showing 1 changed file with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand All @@ -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()
}

Expand All @@ -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
Expand Down Expand Up @@ -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"),
)
}

0 comments on commit e0d905d

Please sign in to comment.