Optional Predicate

25 views
Skip to first unread message

mee...@servicesymphony.com

unread,
May 16, 2015, 8:26:05 PM5/16/15
to scala...@googlegroups.com
Hi,

I have a search form submitted from a web page, where one or more of the search parameters are optional. What is the best way to build a series of conjunctions, against each column, only if a search parameter has been specified for a given column?


  case class OpaqueInstruction(id: String,
                               paymentType: String,
                               receiveDate : Timestamp,
                               receiverBic : String,
                               rawSource : String,
                               hostSystemId : String,
                               status : String,
                               region : String)


  class OpaqueInstructionT(tag: Tag) extends Table[OpaqueInstruction](tag, "V_OPAQUE_INSTRUCTION_UI") {
    def id = column[String]("OPAQUE_INSTRUCTION_ID", O.PrimaryKey)
    def paymentType = column[String]("PAYMENT_TYPE")
    def receiveDate = column[Timestamp]("RECEIVE_DATE")
    def receiverBic = column[String]("RECEIVER_BIC")
    def rawSource = column[String]("RAW_SOURCE")
    def hostSystemId = column[String]("HOST_SYSTEM_ID")
    def status = column[String]("STATUS")
    def region = column[String]("REGION")
    def * = (id, paymentType, receiveDate, receiverBic, rawSource, hostSystemId, status, region) <>
      (OpaqueInstruction.tupled, OpaqueInstruction.unapply)
  }


  val opaqueInstructionQ = TableQuery[OpaqueInstructionT]

case class SearchRequest(fromDate : Option[String], toDate : Option[String], hostSystem : Option[String],
                         status : Option[String], format : Option[String], receiverBic : Option[String])

  def search(sr : SearchRequest)(implicit session : Session) = {
    // TODO apply predicates
    // TODO apply paging
    // TODO apply sorting
    opaqueInstructionQ filter(_.paymentType === sr.format.get)  list
  }

I want to apply the filter against each column, only if the option resolves in a value.

Many thanks
Meeraj

mee...@servicesymphony.com

unread,
May 17, 2015, 4:09:34 PM5/17/15
to scala...@googlegroups.com
I have done the below, which is obviously not very elegant.


  def search(sr : SearchRequest)(implicit session : Session) = {
    // TODO apply paging
    // TODO apply sorting
    opaqueInstructionQ filter(predicate(sr, _))  list
  }

  private def predicate(sr : SearchRequest, o : OpaqueInstructionT) = {
    var p = o.status === sr.status
    if (sr.receiverBic.isDefined && sr.receiverBic.get != "") {
      p = p && o.receiverBic === sr.receiverBic.get
    }
    if (sr.format.isDefined && sr.format.get != "") {
      p = p && o.paymentType === sr.format.get
    }
    if (sr.hostSystem.isDefined && sr.hostSystem.get != "") {
      p = p && o.hostSystemId === sr.hostSystem.get
    }
    // TODO apply date predicates
    p
Reply all
Reply to author
Forward
0 new messages