Skip to content

Commit

Permalink
#980 removed the add row menu that was for testing, clean up and erro…
Browse files Browse the repository at this point in the history
…r handling around adding row
  • Loading branch information
naleeha committed Dec 5, 2023
1 parent e0d905d commit 9d23e74
Showing 1 changed file with 78 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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
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.core.module.basket.BasketModule.{BasketTradingColumnNames => BTColumnName, BasketTradingConstituentColumnNames => ColumnName}
import org.finos.vuu.core.table.{DataTable, JoinTable, 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,26 +21,32 @@ 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 = {
override def menuItems(): ViewPortMenu = ViewPortMenu("Direction",
new SelectionViewPortMenuItem("Set Sell", "", this.setSell, "SET_SELECTION_SELL"),
new SelectionViewPortMenuItem("Set Buy", "", this.setBuy, "SET_SELECTION_Buy"),
)
override def deleteRowAction(): ViewPortDeleteRowAction = ViewPortDeleteRowAction("", this.onDeleteRow)

ViewPortEditSuccess()
}
override def deleteCellAction(): ViewPortDeleteCellAction = ViewPortDeleteCellAction("", this.onDeleteCell)

def onDeleteCell(key: String, column: String, vp: ViewPort, session: ClientSessionId): ViewPortEditAction = {
ViewPortEditSuccess()
}
override def addRowAction(): ViewPortAddRowAction = ViewPortAddRowAction("", this.onAddRow)

def onAddRow(key: String, data: Map[String, Any], vp: ViewPort, session: ClientSessionId): ViewPortEditAction = {
ViewPortEditSuccess()
override def editCellAction(): ViewPortEditCellAction = ViewPortEditCellAction("", this.onEditCell)

override def editRowAction(): ViewPortEditRowAction = ViewPortEditRowAction("", this.onEditRow)

override def onFormSubmit(): ViewPortFormSubmitAction = ViewPortFormSubmitAction("", this.onFormSubmit)

override def onFormClose(): ViewPortFormCloseAction = ViewPortFormCloseAction("", this.onFormClose)

def setSell(selection: ViewPortSelection, session: ClientSessionId): ViewPortAction = {
updateSelected(selection, Map(ColumnName.Side -> Side.Sell))
}

def testAddConstituent(selection: ViewPortSelection, session: ClientSessionId): ViewPortAction = {
addConstituents(Array("VOD.L", "BP.L"))
//todo handle failure
def setBuy(selection: ViewPortSelection, session: ClientSessionId): ViewPortAction = {
updateSelected(selection, Map(ColumnName.Side -> Side.Buy))
}
def addConstituents(rics: Array[String]): ViewPortEditAction = {
//def addConstituents(rics: Array[String])(ctx: RequestContext): ViewPortEditAction = {
//todo handle if no rows
def addConstituents(rics: Array[String])(ctx: RequestContext): ViewPortEditAction = {
if(table.size() == 0)
ViewPortEditFailure(s"Failed to add constituents to ${table.name} as adding row to empty table is currently not supported")

Expand All @@ -51,59 +57,21 @@ class BasketTradingConstituentJoinService(val table: DataTable, val tableContain
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(
val newRows = rics.map(ric =>
mkTradingConstituentRow(
basketTradeInstanceId = tradeId,
sourceBasketId = basketId,
tradeUnit = tradeUnit,
ric = ric)

updateJoinTable(table, newRow)

logger.info(s"added constituent $ric")
})

ViewPortEditSuccess()
ric = ric))

//todo should we guard against adding row for ric that already exist?
updateJoinTable(newRows)
}

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
joinTable.sourceTables.get(baseTableDef.name) match {
case Some(table: DataTable) =>
table.processUpdate(key, RowWithData(key, Map(ColumnName.InstanceIdRic -> key, columnName -> data)), clock.now())
getBaseTable() match {
case Some(baseTable: DataTable) =>
baseTable.processUpdate(key, RowWithData(key, Map(ColumnName.InstanceIdRic -> key, columnName -> data)), clock.now())
ViewPortEditSuccess()
case None =>
ViewPortEditFailure("Could not find base table")
Expand All @@ -116,6 +84,19 @@ class BasketTradingConstituentJoinService(val table: DataTable, val tableContain
ViewPortEditSuccess()
}

private def onDeleteRow(key: String, vp: ViewPort, session: ClientSessionId): ViewPortEditAction = {

ViewPortEditSuccess()
}

private def onDeleteCell(key: String, column: String, vp: ViewPort, session: ClientSessionId): ViewPortEditAction = {
ViewPortEditSuccess()
}

private def onAddRow(key: String, data: Map[String, Any], vp: ViewPort, session: ClientSessionId): ViewPortEditAction = {
ViewPortEditSuccess()
}

private def onFormSubmit(vp: ViewPort, session: ClientSessionId): ViewPortAction = {
// val table = vp.table.asTable
// val primaryKeys = table.primaryKeys
Expand All @@ -136,55 +117,51 @@ class BasketTradingConstituentJoinService(val table: DataTable, val tableContain
CloseDialogViewPortAction(vp.id)
}

override def deleteRowAction(): ViewPortDeleteRowAction = ViewPortDeleteRowAction("", this.onDeleteRow)

override def deleteCellAction(): ViewPortDeleteCellAction = ViewPortDeleteCellAction("", this.onDeleteCell)

override def addRowAction(): ViewPortAddRowAction = ViewPortAddRowAction("", this.onAddRow)

override def editCellAction(): ViewPortEditCellAction = ViewPortEditCellAction("", this.onEditCell)

override def editRowAction(): ViewPortEditRowAction = ViewPortEditRowAction("", this.onEditRow)

override def onFormSubmit(): ViewPortFormSubmitAction = ViewPortFormSubmitAction("", this.onFormSubmit)

override def onFormClose(): ViewPortFormCloseAction = ViewPortFormCloseAction("", this.onFormClose)

def setSell(selection: ViewPortSelection, session: ClientSessionId): ViewPortAction = {
updateSelected(selection, Map(ColumnName.Side -> Side.Sell))
private def getBaseTable(): Option[DataTable] = {
val joinTable = table.asTable.asInstanceOf[JoinTable]
val baseTableDef = joinTable.getTableDef.asInstanceOf[JoinTableDef].baseTable
joinTable.sourceTables.get(baseTableDef.name)
}

def setBuy(selection: ViewPortSelection, session: ClientSessionId): ViewPortAction = {
updateSelected(selection, Map(ColumnName.Side -> Side.Buy))
private def updateJoinTable(rows: Array[RowWithData]): ViewPortEditAction = {
getBaseTable() match {
case Some(baseTable: DataTable) =>
rows.foreach(row =>
baseTable.processUpdate(row.key, row, clock.now())
)
ViewPortEditSuccess()
case None =>
ViewPortEditFailure(s"Could not find base table for ${table.name}")
}
}

private def updateSelected(selection: ViewPortSelection, updates: Map[String, Any]): ViewPortAction = {
val selectedKeys = selection.rowKeyIndex.map({ case (key, _) => key }).toList
selectedKeys.foreach(key => {
val updateRows = selectedKeys.map(key => {
//require source table primary key for join table updates
val sourceTableKey = Map(ColumnName.InstanceIdRic -> key)
update(key, sourceTableKey ++ updates)
RowWithData(key, sourceTableKey ++ updates)
})
//todo handle error
//todo care about checking current value? simpler not to
ViewPortEditSuccess()
updateJoinTable(updateRows.toArray)
}

//todo can update multiple rows in one go?
private def update(key: String, keyValuesToUpdate: Map[String, Any]): 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(key, RowWithData(key, keyValuesToUpdate), clock.now())
ViewPortEditSuccess()
case None =>
ViewPortEditFailure("Could not find base table")
}
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,
))
}
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("Add Row", "", this.testAddConstituent, "TEST_ADD_ROW"),
)

}

0 comments on commit 9d23e74

Please sign in to comment.