alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lift Framework example source code file (MongoCaseClassField.scala)

This example Lift Framework source code file (MongoCaseClassField.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lift Framework tags/keywords

box, box, dbobject, dbobject, empty, jarray, jnothing, jvalue, jvalue, mytype, ownertype, ownertype, record, string

The Lift Framework MongoCaseClassField.scala source code

/*
* Copyright 2010-2011 WorldWide Conferencing, LLC
*
* 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 net.liftweb 
package mongodb 
package record 
package field 

import net.liftweb.record._
import net.liftweb.record.RecordHelpers.jvalueToJsExp
import net.liftweb.record.field._
import net.liftweb.http.js.JE.{JsObj, Num, Str, JsNull}
import xml.{Text, NodeSeq}
import net.liftweb.mongodb.JObjectParser
import com.mongodb.{BasicDBList, DBObject}
import net.liftweb.common.{Failure, Empty, Full, Box}
import net.liftweb.util.Helpers
import net.liftweb.json._
import reflect.Manifest
import net.liftweb.http.js.JsExp


class MongoCaseClassField[OwnerType <: Record[OwnerType],CaseType](rec: OwnerType)( implicit mf: Manifest[CaseType]) extends Field[CaseType, OwnerType] with MandatoryTypedField[CaseType] with MongoFieldFlavor[CaseType] {
  
  implicit val formats = net.liftweb.json.DefaultFormats

  override type MyType = CaseType

  def owner = rec

  def asXHtml = Text(value.toString)

  def toForm: Box[NodeSeq] = Empty

  override def defaultValue = null.asInstanceOf[MyType]
  override def optional_? = true

  def asJValue = valueBox.map(v => Extraction.decompose(v)) openOr (JNothing: JValue)

  def setFromJValue(jvalue: JValue): Box[CaseType] = jvalue match {
    case JNothing|JNull => setBox(Empty)
    case s => setBox(Helpers.tryo[CaseType]{ s.extract[CaseType] })
  }

  def asDBObject: DBObject = {
    JObjectParser.parse(asJValue.asInstanceOf[JObject])
  }

  def setFromDBObject(dbo: DBObject): Box[CaseType] = {
    val jvalue = JObjectParser.serialize(dbo)
    setFromJValue(jvalue)
  }
  
  override def setFromString(in: String): Box[CaseType] = {
    Helpers.tryo{ JsonParser.parse(in).extract[CaseType] }
  }

  def setFromAny(in: Any): Box[CaseType] = in match {
    case dbo: DBObject => setFromDBObject(dbo)
    case c if mf.erasure.isInstance(c) => setBox(Full(c.asInstanceOf[CaseType]))
    case Full(c) if mf.erasure.isInstance(c) => setBox(Full(c.asInstanceOf[CaseType]))
    case null|None|Empty     => setBox(defaultValueBox)
    case (failure: Failure)  => setBox(failure)
    case _ => setBox(defaultValueBox)
  }
}

class MongoCaseClassListField[OwnerType <: Record[OwnerType],CaseType](rec: OwnerType)( implicit mf: Manifest[CaseType]) extends Field[List[CaseType], OwnerType] with MandatoryTypedField[List[CaseType]] with MongoFieldFlavor[List[CaseType]] {
  
  implicit val formats = net.liftweb.json.DefaultFormats
  
  override type MyType = List[CaseType]
  
  def owner = rec

  def asXHtml = Text(value.toString)
  
  def toForm: Box[NodeSeq] = Empty

  override def defaultValue: MyType = Nil
  override def optional_? = true
  
  def asJValue = JArray(value.map(v => Extraction.decompose(v)))
  
  def setFromJValue(jvalue: JValue): Box[MyType] = jvalue match {
    case JArray(contents) => setBox(Full(contents.flatMap(s => Helpers.tryo[CaseType]{ s.extract[CaseType] })))
    case _ => setBox(Empty)
  }

  def asDBObject: DBObject = {
    val dbl = new BasicDBList
    
    asJValue match {
      case JArray(list) => 
        list.foreach(v => dbl.add(JObjectParser.parse(v.asInstanceOf[JObject])))
    }
    
    dbl
  }

  def setFromDBObject(dbo: DBObject): Box[MyType] = {
    val jvalue = JObjectParser.serialize(dbo)
    setFromJValue(jvalue)
  }

  def setFromAny(in: Any): Box[MyType] = in match {
    case dbo: DBObject => setFromDBObject(dbo)
    case list@c::xs if mf.erasure.isInstance(c) =>  setBox(Full(list.asInstanceOf[MyType]))
    case _ => setBox(Empty)
  }

  override def setFromString(in: String): Box[MyType] = {
    setFromJValue(JsonParser.parse(in))
  }
}

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework MongoCaseClassField.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.