Partway through switching to bsonrecords instead of jsonrecords, as bsonrecords allow optional fields

This commit is contained in:
Everett Sochowski 2012-03-06 15:41:43 +01:00
parent 183f2fbe33
commit 8ca319513f
4 changed files with 73 additions and 59 deletions

View File

@ -8,6 +8,9 @@ import net.liftweb.mongodb.record.MongoRecord
import net.liftweb.mongodb.record.field.BsonRecordField
import net.liftweb.mongodb.record.field.MongoJsonObjectListField
import net.liftweb.common.{Box, Empty, Full}
import net.liftweb.mongodb.record.field.BsonRecordListField
import net.liftweb.mongodb.record.{BsonRecord, BsonMetaRecord}
import net.liftweb.record.field.StringField
/**
* There should be only one of these for every real life "this" account. TODO: Enforce this
@ -18,11 +21,12 @@ import net.liftweb.common.{Box, Empty, Full}
class Account extends MongoRecord[Account] with ObjectIdPk[Account]{
def meta = Account
protected object holder extends net.liftweb.record.field.StringField(this, 255)
protected object number extends net.liftweb.record.field.StringField(this, 255)
protected object kind extends net.liftweb.record.field.StringField(this, 255)
protected object holder extends StringField(this, 255)
protected object number extends StringField(this, 255)
protected object kind extends StringField(this, 255)
protected object bank extends BsonRecordField(this, OBPBank)
object otherAccounts extends MongoJsonObjectListField[Account, OtherAccount](this, OtherAccount)
object otherAccounts extends BsonRecordListField(this, OtherAccount)
//object otherAccounts extends MongoJsonObjectListField[Account, OtherAccount](this, OtherAccount)
def getUnmediatedOtherAccountUrl(user: String, otherAccountHolder: String) : Box[String] = {
for{
@ -38,12 +42,9 @@ class Account extends MongoRecord[Account] with ObjectIdPk[Account]{
def getMediatedOtherAccountURL(user: String, otherAccountHolder: String) : Box[String] = {
val otherAccountURL = for{
o <- otherAccounts.get.find(acc=> {
acc match{
case OtherAccount(`otherAccountHolder`, _, _, _, _, _) => true
case _ => false
}
acc.holder.get.equals(otherAccountHolder)
})
} yield o.url
} yield o.url.get
user match{
case "team" => otherAccountURL
@ -69,12 +70,9 @@ class Account extends MongoRecord[Account] with ObjectIdPk[Account]{
def getMediatedOtherAccountImageURL(user: String, otherAccountHolder: String) : Box[String] = {
val otherAccountImageURL = for{
o <- otherAccounts.get.find(acc=> {
acc match{
case OtherAccount(`otherAccountHolder`, _, _, _, _, _) => true
case _ => false
}
acc.holder.get.equals(otherAccountHolder)
})
} yield o.imageUrl
} yield o.imageUrl.get
user match{
case "team" => otherAccountImageURL
@ -100,12 +98,9 @@ class Account extends MongoRecord[Account] with ObjectIdPk[Account]{
def getMediatedOtherAccountMoreInfo(user: String, otherAccountHolder: String) : Box[String] = {
val otherAccountMoreInfo = for{
o <- otherAccounts.get.find(acc=> {
acc match{
case OtherAccount(`otherAccountHolder`, _, _, _, _, _) => true
case _ => false
}
acc.holder.get.equals(otherAccountHolder)
})
} yield o.moreInfo
} yield o.moreInfo.get
user match{
case "team" => otherAccountMoreInfo
@ -121,14 +116,36 @@ class Account extends MongoRecord[Account] with ObjectIdPk[Account]{
object Account extends Account with MongoMetaRecord[Account]
case class OtherAccount(holder: String = "",
class OtherAccount private() extends BsonRecord[OtherAccount] {
def meta = OtherAccount
object holder extends StringField(this, 200)
object publicAlias extends StringField(this, 100)
object privateAlias extends StringField(this, 100)
object moreInfo extends StringField(this, 100)
object url extends StringField(this, 100)
object imageUrl extends StringField(this, 100)
object openCorporatesUrl extends StringField(this, 100){
override def optional_? = true
}
}
object OtherAccount extends OtherAccount with BsonMetaRecord[OtherAccount]
/*case class OtherAccount(holder: String = "",
publicAlias: String = "",
privateAlias: String = "",
moreInfo: String = "",
url: String = "",
imageUrl: String = "")
imageUrl: String = "",
openCorporatesUrl: Box[String])
//TODO: Probably need a rework here as openCorporatesUrl is marked optional due to existing
// OtherAccount objects in the db that don't have it set. Probably all these fields except
// holder should be made optional.
extends JsonObject[OtherAccount]{
def meta = OtherAccount
}
object OtherAccount extends JsonObjectMeta[OtherAccount]
*/

View File

@ -248,10 +248,7 @@ class OBPAccount private() extends BsonRecord[OBPAccount]{
case Full(a) =>{
val otherAccs = a.otherAccounts.get
val aliasInQuestion = otherAccs.find(o =>
o match{
case OtherAccount(`realValue`, _, _, _, _, _) => true
case _ => false
})
o.holder.get.equals(realValue))
aliasInQuestion.isDefined
}
case _ => false
@ -264,10 +261,7 @@ class OBPAccount private() extends BsonRecord[OBPAccount]{
case Full(a) =>{
val otherAccs = a.otherAccounts.get
val aliasInQuestion = otherAccs.find(o =>
o match{
case OtherAccount(`realValue`, _, _, _, _, _) => true
case _ => false
})
o.holder.get.equals(realValue))
aliasInQuestion.isDefined
}
case _ => false
@ -287,12 +281,12 @@ class OBPAccount private() extends BsonRecord[OBPAccount]{
val updatedAccount = otherAccount match{
case Some(o) =>{
//update the "otherAccount"
val newOtherAcc= o.copy(publicAlias = randomAliasName)
val newOtherAcc= o.publicAlias(randomAliasName)
a.otherAccounts(a.otherAccounts.get -- List(o) ++ List(newOtherAcc))
}
case _ => {
//create a new "otherAccount"
a.otherAccounts(a.otherAccounts.get ++ List(OtherAccount(holder.get, randomAliasName, "", "", "", "")))
a.otherAccounts(a.otherAccounts.get ++ List(OtherAccount.createRecord.holder(holder.get)))
}
}
@ -310,12 +304,12 @@ class OBPAccount private() extends BsonRecord[OBPAccount]{
val updatedAccount = otherAccount match{
case Some(o) =>{
//update the "otherAccount"
val newOtherAcc= o.copy(publicAlias = "")
val newOtherAcc= o.publicAlias("")
a.otherAccounts(a.otherAccounts.get -- List(o) ++ List(newOtherAcc))
}
case _ => {
//create a new "otherAccount"
a.otherAccounts(a.otherAccounts.get ++ List(OtherAccount(holder.get, "", "", "", "", "")))
a.otherAccounts(a.otherAccounts.get ++ List(OtherAccount.createRecord.holder(holder.get)))
}
}
//val updatedAccount = a.publicAliases(a.publicAliases.get ++ List(Alias(holder.get, "")))
@ -333,12 +327,12 @@ class OBPAccount private() extends BsonRecord[OBPAccount]{
val updatedAccount = otherAccount match{
case Some(o) =>{
//update the "otherAccount"
val newOtherAcc= o.copy(privateAlias = "")
val newOtherAcc= o.privateAlias("")
a.otherAccounts(a.otherAccounts.get -- List(o) ++ List(newOtherAcc))
}
case _ => {
//create a new "otherAccount"
a.otherAccounts(a.otherAccounts.get ++ List(OtherAccount(holder.get, "", "", "", "", "")))
a.otherAccounts(a.otherAccounts.get ++ List(OtherAccount.createRecord.holder(holder.get)))
}
}
//val updatedAccount = a.privateAliases(a.privateAliases.get ++ List(Alias(holder.get, "")))
@ -356,12 +350,10 @@ class OBPAccount private() extends BsonRecord[OBPAccount]{
def usePrivateAliasIfExists() : (Box[String], Box[OBPAccount.AnAlias])= {
val privateAlias = for{
account <- theAccount
otheracc <- account.otherAccounts.get.find(o => o match{
case OtherAccount(`theHolder`, _, "", _, _, _) => false
case OtherAccount(`theHolder`, _, _, _, _, _) => true
case _ => false
})
} yield otheracc.privateAlias
otheracc <- account.otherAccounts.get.find(o =>
o.holder.get.equals(theHolder)
)
} yield otheracc.privateAlias.get
privateAlias match{
case Full(a) => (Full(a), Full(OBPAccount.APrivateAlias))
@ -372,12 +364,10 @@ class OBPAccount private() extends BsonRecord[OBPAccount]{
def usePublicAlias() : (Box[String], Box[OBPAccount.AnAlias])= {
val publicAlias = for{
account <- theAccount
otheracc <- account.otherAccounts.get.find(o => o match{
case OtherAccount(`theHolder`, "", _, _, _, _) => false
case OtherAccount(`theHolder`, _, _, _, _, _) => true
case _ => false
})
} yield otheracc.publicAlias
otheracc <- account.otherAccounts.get.find(o =>
o.holder.get.equals(theHolder)
)
} yield otheracc.publicAlias.get
publicAlias match{
case Full(a) => (Full(a), Full(OBPAccount.APublicAlias))

View File

@ -29,7 +29,9 @@ class Management {
val currentAccount = Account.find(accJObj) getOrElse Account.createRecord
def getMostUpToDateOtherAccount(holder: String) = {
currentAccount.otherAccounts.get.find(o => o.holder.equals(holder))
currentAccount.otherAccounts.get.find(o => {
o.holder.get.equals(holder)
})
}
def editable(initialValue: String, holder: String, alterOtherAccount: (OtherAccount, String) => OtherAccount) = {
@ -52,37 +54,37 @@ class Management {
}
def editablePublicAlias(initialValue : String, holder: String) = {
def alterPublicAlias = (oAccount: OtherAccount, newValue: String) => oAccount.copy(publicAlias = newValue)
def alterPublicAlias = (oAccount: OtherAccount, newValue: String) => oAccount.publicAlias(newValue)
editable(initialValue, holder, alterPublicAlias)
}
def editablePrivateAlias(initialValue : String, holder: String) = {
def alterPrivateAlias = (oAccount: OtherAccount, newValue: String) => oAccount.copy(privateAlias = newValue)
def alterPrivateAlias = (oAccount: OtherAccount, newValue: String) => oAccount.privateAlias(newValue)
editable(initialValue, holder, alterPrivateAlias)
}
def editableImageUrl(initialValue : String, holder: String) = {
def alterImageUrl = (oAccount: OtherAccount, newValue: String) => oAccount.copy(imageUrl = newValue)
def alterImageUrl = (oAccount: OtherAccount, newValue: String) => oAccount.imageUrl(newValue)
editable(initialValue, holder, alterImageUrl)
}
def editableUrl(initialValue : String, holder: String) = {
def alterUrl = (oAccount: OtherAccount, newValue: String) => oAccount.copy(url = newValue)
def alterUrl = (oAccount: OtherAccount, newValue: String) => oAccount.url(newValue)
editable(initialValue, holder, alterUrl)
}
def editableMoreInfo(initialValue : String, holder: String) = {
def moreInfo = (oAccount: OtherAccount, newValue: String) => oAccount.copy(moreInfo = newValue)
def moreInfo = (oAccount: OtherAccount, newValue: String) => oAccount.moreInfo(newValue)
editable(initialValue, holder, moreInfo)
}
currentAccount.otherAccounts.get.flatMap(other => {
(".image *" #> editableImageUrl(other.imageUrl, other.holder) &
".real_name *" #> Text(other.holder) &
".public_alias_name *" #> editablePublicAlias(other.publicAlias, other.holder) &
".private_alias_name *" #> editablePrivateAlias(other.privateAlias, other.holder) &
".more_info *" #> editableMoreInfo(other.moreInfo, other.holder) &
".website_url *" #> editableUrl(other.url, other.holder) ).apply(xhtml)
(".image *" #> editableImageUrl(other.imageUrl.get, other.holder.get) &
".real_name *" #> Text(other.holder.get) &
".public_alias_name *" #> editablePublicAlias(other.publicAlias.get, other.holder.get) &
".private_alias_name *" #> editablePrivateAlias(other.privateAlias.get, other.holder.get) &
".more_info *" #> editableMoreInfo(other.moreInfo.get, other.holder.get) &
".website_url *" #> editableUrl(other.url.get, other.holder.get) ).apply(xhtml)
})
}

View File

@ -169,7 +169,12 @@ class OBPTransactionSnippet extends StatefulSnippet with PaginatorSnippet[OBPEnv
url getOrElse ""
}
val openCorporatesUrl = for{
a <- theAccount
oacc <- a.otherAccounts.get.find(o => o.holder.equals(otherUnmediatedHolder))
} yield oacc.openCorporatesUrl.get
println("OPEN CORPORATES: " + openCorporatesUrl.getOrElse("UNDEFINED"))
(
".amount *" #> transactionValue.mediated_amount(consumer).getOrElse(FORBIDDEN) &
".other_account_holder_name *" #> otherMediatedHolder._1.getOrElse(FORBIDDEN) &