src/main/scala/radview/model/Account.scala
author Tomas Zeman <tzeman@volny.cz>
Sun, 03 Apr 2011 15:55:02 +0200
changeset 2 cf829ec742b3
child 7 6e3d323f9ec5
permissions -rw-r--r--
Main project functionality

/*
 * Copyright 2011 Tomas Zeman <tzeman@volny.cz>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package radview.model

import net.liftweb.common._
import net.liftweb.mapper._
import radview.snippet._
import scala.xml.{Node, NodeSeq, Text}

trait ImsiAware {
  self: BaseMapper =>
  object imsi extends MappedPoliteStringColName(this.asInstanceOf[MapperType],
    17, "imsi", "IMSI") {
    def imsi = is takeRight 12
    override def displayName = calcImsiDisplayName
  }
  def calcImsiDisplayName = "IMSI"
}

trait Imsi12Aware {
  self: BaseMapper =>
  object imsi12 extends MappedPoliteStringColName(this.asInstanceOf[MapperType],
    14, "imsi_12", "IMSI") {
  }
}


trait UsernameAware {
  self: BaseMapper =>
  object username extends MappedPoliteStringColName(
    this.asInstanceOf[MapperType], 64, "username", "Username")
}

trait AppAware {
  self: BaseMapper =>
  object app extends MappedPoliteStringColName(this.asInstanceOf[MapperType],
    5, "appl", "Service") {
    override def asHtml = Text(tr)
    def tr = is match {
      case "BOSS" => "Mobile"
      case x => x
    }
  }
}

trait ServiceStatus { self: MappedInt[_] =>
  override def asHtml = Text(is match {
    case 0 => "service disabled (0)"
    case 1 => "service enabled (1)"
    case x => x.toString
  })
}

/*
  Table: radcheck_SUB
 */
object RadCheckSub extends RadCheckSub with LongKeyedMetaMapper[RadCheckSub] {
  override def dbTableName = "v_radcheck_SUB"
  override def dbDefaultConnectionIdentifier = RadiusConnectionIdentifier

  def getApps = findAllFields(List(app), Distinct[RadCheckSub],
    OrderBy(app, Ascending))

  def byImsi(imsiVal: String) = Box(findAll(By(imsi, imsiVal)))
}

class RadCheckSub extends LongKeyedMapper[RadCheckSub] with ImsiAware with
  UsernameAware with AppAware with IdPK with Imsi12Aware {

  def getSingleton = RadCheckSub

  object userStatus extends MappedIntColName(this, "user_status",
    "User Status") with ServiceStatus

  object msisdn extends MappedPoliteStringColName(this, 22, "msisdn",
    "Phone No.")

  override def calcImsiDisplayName = ifAdsl("Phone No.",
    super.calcImsiDisplayName)

  def fieldsForDisplay = List(msisdn) ++ ifAdsl(Empty, Full(imsi)) ++
    List(app, username, userStatus)

  def fieldsForList = List(username) ++
    ifAdsl(Empty, Full(msisdn)) :+ userStatus

  def ifAdsl[T](yes: => T, no: => T) = app.is match {
    case "ADSL" => yes
    case _ => no
  }

  def isAdsl = app.is == "ADSL"

  lazy val replySub = RadReplySub.byImsi(imsi)
  lazy val replyNai = RadReplyNai.byImsi12(imsi12)
  lazy val checkNai = replyNai flatMap { rn => RadCheckNai.byEsn(rn.esn) }
}

/*
  Table: radreply_SUB
 */
object RadReplySub extends RadReplySub with LongKeyedMetaMapper[RadReplySub] {
  override def dbTableName = "radreply_SUB"
  override def dbDefaultConnectionIdentifier = RadiusConnectionIdentifier

  def byImsiMap(imsis: Seq[String]) = Map.empty ++ 
    (findAll(ByList(imsi, imsis)) map { i => (i.imsi.is, i) })

  def fieldsForList = List(statIp)
  def byImsi(imsiVal: String) = Box(findAll(By(imsi, imsiVal)))
}

class RadReplySub extends LongKeyedMapper[RadReplySub] with ImsiAware with
  AppAware with IdPK {

  def getSingleton = RadReplySub

  object statIp extends MappedPoliteStringColName(this, 253, "value",
    "Static IP")

  def fieldsForDisplay = List(statIp)
}

trait EsnAware {
  self: BaseMapper =>
  object esn extends MappedPoliteStringColName(
    this.asInstanceOf[MapperType], 64, "username", "ESN") {
    def esn = is.takeWhile {_ != '@'}
    override def asHtml: Node = Text(esn)
  }
}

/*
  Table: radcheck_NAI
 */
object RadCheckNai extends RadCheckNai with LongKeyedMetaMapper[RadCheckNai] {
  override def dbTableName = "radcheck_NAI"
  override def dbDefaultConnectionIdentifier = RadiusConnectionIdentifier

  def byRrnMap(rrn: Seq[RadReplyNai]) = Map.empty ++
    (findAll(ByList(esn, rrn map { _.esn.is })) map { i => (i.esn.is, i) })

  def fieldsForList = List(termStatus)
  def byEsn(fullEsn: String) = Box(findAll(By(esn, fullEsn)))
}

class RadCheckNai extends LongKeyedMapper[RadCheckNai] with EsnAware
  with AppAware with IdPK {

  def getSingleton = RadCheckNai

  object termStatus extends MappedIntColName(this, "term_status",
    "Term Status") with ServiceStatus
  def fieldsForDisplay = List(termStatus)
}

/*
  Table: radreply_NAI
 */
object RadReplyNai extends RadReplyNai with LongKeyedMetaMapper[RadReplyNai] {
  override def dbTableName = "v_radreply_NAI"
  override def dbDefaultConnectionIdentifier = RadiusConnectionIdentifier

  def byImsi12map(imsis12: Seq[String]) = Map.empty ++ 
    (findAll(ByList(imsi12, imsis12)) map { i => (i.imsi12.is, i) })

  def fieldsForList = List(esn)
  def byImsi12(in: String) = Box(findAll(By(imsi12, in)))
}

class RadReplyNai extends LongKeyedMapper[RadReplyNai] with EsnAware with
  AppAware with IdPK with Imsi12Aware {

  def getSingleton = RadReplyNai

  object value extends MappedPoliteStringColName(this, 253, "value", "Value") {
    def imsi = is takeRight 12
    override def asHtml: Node = Text(imsi)
  }

  def fieldsForDisplay = List(esn)
}

// vim: set ts=2 sw=2 et: