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

Lift Framework example source code file (MappedBoolean.scala)

This example Lift Framework source code file (MappedBoolean.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

anyref, boolean, boolean, box, empty, full, full, jdbc, mappedboolean, method, reflection, sql, string, string, t, t, unit, util

The Lift Framework MappedBoolean.scala source code

/*
 * Copyright 2006-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 mapper

import java.sql.{ResultSet, Types}
import java.lang.reflect.Method
import net.liftweb.util.Helpers._
import net.liftweb.http.{S, SHtml}
import java.util.Date
import net.liftweb.util._
import net.liftweb.common._
import net.liftweb.json._
import net.liftweb.http.js._
import scala.xml._

abstract class MappedBoolean[T<:Mapper[T]](val fieldOwner: T) extends MappedField[Boolean, T] {
  private var data : Box[Boolean] = Full(defaultValue)
  private var orgData: Box[Boolean] = Full(defaultValue)

  def defaultValue: Boolean = false

  def dbFieldClass = classOf[Boolean]

  /**
   * Get the JDBC SQL Type for this field
   */
  def targetSQLType = Types.BOOLEAN

  protected def i_is_! = data openOr false
  protected def i_was_! = orgData openOr false
  protected[mapper] def doneWithSave() {orgData = data}

  protected def real_i_set_!(value : Boolean) : Boolean = {
    dirty_?(data.map(_ != value) openOr true)
    data = Full(value)
    value
  }
  override def readPermission_? = true
  override def writePermission_? = true

     def asJsonValue: Box[JsonAST.JValue] = Full(JsonAST.JBool(is))

  def real_convertToJDBCFriendly(value: Boolean): Object = new java.lang.Integer(if (value) 1 else 0)

  def jdbcFriendly(field : String) = data.map(v => new java.lang.Integer(if(v) 1 else 0)) openOr null

  def asJsExp: JsExp = if (is) JE.JsTrue else JE.JsFalse

  override def setFromAny(in: Any): Boolean = {
    in match {
      case b: Boolean => this.set(b)
      case JsonAST.JBool(v) => this.set(v)
      case (b: Boolean) :: _ => this.set(b)
      case Some(b: Boolean) => this.set(b)
      case Full(b: Boolean) => this.set(b)
      case Empty | Failure(_, _, _) | None => this.set(false)
      case (s: String) :: _ => this.set(toBoolean(s))
      case s :: _ => this.setFromAny(s)
      case null => this.set(false)
      case s: String => this.set(toBoolean(s))
      case o => this.set(toBoolean(o))
    }
  }

  protected def i_obscure_!(in : Boolean) = false

  def buildSetActualValue(accessor : Method, inst : AnyRef, columnName : String) : (T, AnyRef) => Unit = {
    inst match {
      case null => {(inst : T, v : AnyRef) => {val tv = getField(inst, accessor).asInstanceOf[MappedBoolean[T]]; tv.data = Full(false)}}
      case _ => {(inst : T, v : AnyRef) => {val tv = getField(inst, accessor).asInstanceOf[MappedBoolean[T]]; tv.data = Full(toBoolean(v))}}
    }
  }

  private def allSet(in: Box[Boolean]) {
    this.data = in
    this.orgData = in
  }

  def buildSetLongValue(accessor : Method, columnName : String): (T, Long, Boolean) => Unit =
    (inst, v, isNull) => doField(inst, accessor, {case tv: MappedBoolean[T] => tv.allSet(if (isNull) Empty else Full(v != 0L))})

  def buildSetStringValue(accessor : Method, columnName : String): (T, String) => Unit =
    (inst, v) => doField(inst, accessor, {case tv: MappedBoolean[T] => tv.allSet(if (v == null) Empty else Full(toBoolean(v)))})

  def buildSetDateValue(accessor: Method, columnName: String): (T, Date) => Unit =
    (inst, v) => doField(inst, accessor, {case tv: MappedBoolean[T] => tv.allSet(if (v == null) Empty else Full(true))})

  def buildSetBooleanValue(accessor: Method, columnName : String) : (T, Boolean, Boolean) => Unit   =
    (inst, v, isNull) => doField(inst, accessor, {case tv: MappedBoolean[T] => tv.allSet(if (isNull) Empty else Full(v))})


  /**
   * Given the driver type, return the string required to create the column in the database
   */
  def fieldCreatorString(dbType: DriverType, colName: String): String = colName + " " + dbType.booleanColumnType + notNullAppender()


  /**
   * Create an input field for the item
   */
  override def _toForm: Box[NodeSeq] = Full(SHtml.checkbox(is,this.apply _))
}

Other Lift Framework examples (source code examples)

Here is a short list of links related to this Lift Framework MappedBoolean.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.