Skip to content

Instantly share code, notes, and snippets.

@fbettag
Created November 2, 2011 11:53
Show Gist options
  • Save fbettag/1333455 to your computer and use it in GitHub Desktop.
Save fbettag/1333455 to your computer and use it in GitHub Desktop.
User class
Menu.param[Customer]("Customer", "Customer", s => CustomerCache.find(s),
customer => customer.id.is.toString) / "customer" >> Hidden >> has_?(Administrator, Accounting),
class Customer
extends LongKeyedMapper[Customer]
with IdPK {
def getSingleton = Customer
override def save = {
val ret = super.save
if (ret) CustomerCache ! this
ret
}
}
import net.liftweb.http._
import net.liftweb.actor._
import net.liftweb.common._
import net.liftweb.mapper._
import net.liftweb.util._
import net.liftweb.util.Helpers._
import scala.collection.mutable.HashMap
case class CustomerAll
object CustomerCache extends LiftActor {
private var customers = new HashMap[Long, Customer]()
protected def messageHandler = {
case a: Customer => update(a)
case a: Long => reply(customers.get(a) match {
case Some(c: Customer) => c
case _ => null
})
case CustomerAll => reply(sort(customers))
case _ =>
}
private def update(customer: Customer) {
println("saving customer: %s".format(customer))
customers.put(customer.id.is, customer)
}
lazy val init = {
Customer.findAll(OrderBy(Customer.id, Ascending)).foreach(c => customers.put(c.id.is, c))
println("CustomerCache loaded...")
true
}
private def sort(hm: HashMap[Long, Customer]) = {
hm.toList.sort((e1, e2) => (e1._1 compareTo e2._1) < 0).map(_._2)
}
def find(s: String): Box[Customer] =
try {
find(s.toLong)
} catch {
case _ => Empty
}
def find(i: Long): Box[Customer] =
this !! i match {
case Full(a: Customer) => Full(a)
case _ => Empty
}
def all: List[Customer] = {
val user = User.currentUser match {
case Full(u: User) => u
case _ => return List()
}
if (user.role_?(Role.Administrator, Role.Support)) {
return (this !! CustomerAll match {
case Full(a: List[Customer]) => a
case _ => List()
})
}
List()
}
}
class User
extends MegaProtoUser[User]
with CreatedUpdated {
def getSingleton = User
object roles extends MappedEnumList(this, Role)
def role_?(ro: Role.Value*) =
ro.exists(roles.is.contains)
object customer extends MappedLongForeignKey(this, Customer) {
override def obj = CustomerCache !! this.is match {
case Full(a: Customer) => Full(a)
case _ => Empty
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment