Permalink
Browse files

Mapper: Fold LongMappedMapper features into MappedForeignKey

Fixes #1021
  • Loading branch information...
1 parent 305b7b8 commit 2f5ca7e02f97728507bd096904caf180fc99dd30 @nafg nafg committed May 24, 2011
@@ -25,7 +25,7 @@ import http.{js, S, SHtml}
import js._
import S.?
import json._
-
+import util.FieldError
/**
@@ -77,7 +77,9 @@ object MappedForeignKey {
/**
* The Trait that defines a field that is mapped to a foreign key
*/
-trait MappedForeignKey[KeyType, MyOwner <: Mapper[MyOwner], Other <: KeyedMapper[KeyType, Other]] extends MappedField[KeyType, MyOwner] {
+trait MappedForeignKey[KeyType, MyOwner <: Mapper[MyOwner], Other <: KeyedMapper[KeyType, Other]]
+extends MappedField[KeyType, MyOwner]
+with LifecycleCallbacks {
type FieldType <: KeyType
// type ForeignType <: KeyedMapper[KeyType, Other]
@@ -162,66 +164,69 @@ trait MappedForeignKey[KeyType, MyOwner <: Mapper[MyOwner], Other <: KeyedMapper
private var _obj: Box[Other] = Empty
private var _calcedObj = false
-}
-
-
-/**
- * A subclass of MappedLongForeignKey whose value can be
- * get and set as the target parent mapper instead of as its primary key.
- * @author nafg
- */
-class LongMappedMapper[T<:Mapper[T], O<:KeyedMapper[Long,O]](theOwner: T, foreign: => KeyedMetaMapper[Long, O])
- extends MappedLongForeignKey[T,O](theOwner, foreign) with LongMappedForeignMapper[T,O]
-/**
- * A subtype of MappedLongForeignKey whose value can be
- * get and set as the target parent mapper instead of as its primary key.
- * @deprecated Use LongMappedMapper instead, to avoid mixing in MappedLongForeignKey manually. May be folded into it in the future.
- * @author nafg
- */
-@deprecated
-trait LongMappedForeignMapper[T<:Mapper[T],O<:KeyedMapper[Long,O]]
- extends MappedLongForeignKey[T,O]
- with LifecycleCallbacks {
- import net.liftweb.common.{Box, Empty, Full}
-
- override def apply(f: O) = {
- this(Full(f))
- }
- override def apply(f: Box[O]) = {
- val ret = super.apply(f)
- primeObj(f)
- ret
+ /**
+ * Set the value from a possible instance of the foreign mapper class.
+ * v will be cached in obj.
+ * If v is Empty, set the value to defaultValue (-1)
+ * @return the Mapper containing this field
+ */
+ def apply(v: Box[Other]): MyOwner = {
+ apply(v.dmap(defaultValue)(_.primaryKeyField.is))
+ primeObj(v)
+ fieldOwner
}
- override def set(v: Long) = {
- val ret = super.set(v)
- primeObj(if(defined_?) dbKeyToTable.find(i_is_!) else Empty)
- ret
+ /**
+ * Set the value from an instance of the foreign mapper class.
+ * obj will be set to Full(v)
+ * @return the Mapper containing this field
+ */
+ def apply(v: Other): MyOwner = {
+ apply(v.primaryKeyField.is)
+ primeObj(Full(v))
+ fieldOwner
}
+ /**
+ * This method, which gets called when the mapper class is going to be saved,
+ * sets the field's value from obj if it's set to the default (!defined_?).
+ * Overrides LifecycleCallbacks.beforeSave
+ */
override def beforeSave {
if(!defined_?)
for(o <- obj)
set(o.primaryKeyField.is)
super.beforeSave
}
- import net.liftweb.util.FieldError
+ /**
+ * A validation function that checks that obj is nonempty
+ */
val valHasObj = (value: Long) =>
- if (obj eq Empty) List(FieldError(this, scala.xml.Text("Required field: " + name)))
+ if (obj.isEmpty) List(FieldError(this, scala.xml.Text("Required field: " + name)))
else Nil
}
+@deprecated("Functionality folded into *MappedForeignKey; will be removed in 2.5")
+class LongMappedMapper[T<:Mapper[T], O<:KeyedMapper[Long,O]](theOwner: T, foreign: => KeyedMetaMapper[Long, O])
+ extends MappedLongForeignKey[T,O](theOwner, foreign) with LongMappedForeignMapper[T,O]
+
+
+@deprecated("Functionality folded into *MappedForeignKey; will be removed in 2.5")
+trait LongMappedForeignMapper[T<:Mapper[T],O<:KeyedMapper[Long,O]]
+ extends MappedLongForeignKey[T,O]
+
+
abstract class MappedLongForeignKey[T<:Mapper[T],O<:KeyedMapper[Long, O]](theOwner: T, _foreignMeta: => KeyedMetaMapper[Long, O])
extends MappedLong[T](theOwner) with MappedForeignKey[Long,T,O] with BaseForeignKey {
def defined_? = i_is_! > 0L
def foreignMeta = _foreignMeta
- @deprecated
+ @deprecated("Use 'box' instead")
def can: Box[Long] = if (defined_?) Full(is) else Empty
def box: Box[Long] = if (defined_?) Full(is) else Empty
@@ -263,18 +268,6 @@ extends MappedLong[T](theOwner) with MappedForeignKey[Long,T,O] with BaseForeign
override def toString = if (defined_?) super.toString else "NULL"
- def apply(v: Box[O]): T = {
- apply(v.dmap(0L)(_.primaryKeyField.is))
- primeObj(v)
- fieldOwner
- }
-
- def apply(v: O): T = {
- apply(v.primaryKeyField.is)
- primeObj(Full(v))
- fieldOwner
- }
-
def findFor(key: KeyType): List[OwnerType] = theOwner.getSingleton.findAll(By(this, key))
def findFor(key: KeyedForeignType): List[OwnerType] = theOwner.getSingleton.findAll(By(this, key))
@@ -326,8 +319,6 @@ extends MappedString[T](fieldOwner, maxLen) with MappedForeignKey[String,T,O] wi
this(toSet)
}
- def apply(v: O): T = this(v.primaryKeyField.is)
-
def findFor(key: KeyType): List[OwnerType] = fieldOwner.getSingleton.findAll(By(this, key))
def findFor(key: KeyedForeignType): List[OwnerType] = fieldOwner.getSingleton.findAll(By(this, key))
@@ -130,19 +130,11 @@ abstract class MappedString[T<:Mapper[T]](val fieldOwner: T,val maxLen: Int) ext
}
- def apply(ov: Box[String]): T = {
- ov match {
- case Full(s) => this.set(s)
- case _ => this.set(null)
- }
-
- fieldOwner
- }
+ @deprecated("Just use apply(x openOr null). Will be removed in 2.5.")
+ def apply(ov: Box[String])(implicit disambiguateFromApplyBoxedForeign: BoxedStringToken): T = apply(ov openOr null)
def asJsExp: JsExp = JE.Str(is)
- override def apply(ov: String): T = apply(Full(ov))
-
def jdbcFriendly(field : String): String = data.get
def real_convertToJDBCFriendly(value: String): Object = value
@@ -195,3 +187,7 @@ private[mapper] object IsElem {
}
}
+sealed trait BoxedStringToken
+object BoxedStringToken {
+ implicit val theBoxedStringToken: BoxedStringToken = new BoxedStringToken {}
+}

0 comments on commit 2f5ca7e

Please sign in to comment.