New features

109 views
Skip to first unread message

Naftoli Gugenheim

unread,
Jul 27, 2009, 3:57:04 PM7/27/09
to lif...@googlegroups.com
I committed some code last night, which can help building mapper-based view snippets, with G-d's help. It includes the following classes:
(1-2) net.liftweb.mapper.OneToMany, ManyToMany: Gives a more object-oriented approach to managing related entites. You can manage the many side of a 1-n and n-n as a mutable collection of children, and the parent of a child can be set directly, instead of via its id. And the children are not saved to the database when you add them until you call save on it or its parent, nor deleted when you remove tem until you call delete_!, which is very helpful when you need to keep track of adds/removes through multiple requests. For example, if you are displaying a list and you can click delete, but it shouldn't be permanently deleted until you click save.
There is a new package, ...mapper.view, which contains a number of utilities for mapper-based views:
(3-4) ModelView and ModelSnippet provide a number of building blocks for views that are too complex to CRUDify. Inherit ModelSnippet (which extends StatefulSnippet) and wrap your entities in ModelView (view is used in the sense of a wrapper).
(5) Util provides some more building blocks that just be imported, without needing a ModelSnippet context.
(6-7) Paginator makes it easy to create paginated, user sortable listings. You can use PaginatedSnippet instead of ModelSnippet to help.
(8-10) ItemsList lets you manage a list of entities with pending additions and deletions. It's used by TableEditor, which is a very easy to use and customizable snippet to edit tables directly. It's useful for editing short lists, e.g., a lookup table like cities. Don't forget to register the table in Boot.
(11) Then there's the experimental FormProcessor, if you need your form to be processed in one block instead of separate closures, e.g., to surround with try.
(12) Also experimental is sitemap.XmlMenu which lets you write menus in xml.
(13) I may add CaseEnum, which lets you write case classes that automatically double as an Enumeration.
Questions, comments, suggestions, and constructive criticism are more than welcome!
Thanks.

marius d.

unread,
Jul 27, 2009, 4:15:42 PM7/27/09
to Lift
Would you please add some examples on the wiki so that people can
actually visualize how these things can be used?


As far as XmlMenu goes why do we want to express menus as xml ?

Br's,
Marius

Naftoli Gugenheim

unread,
Jul 27, 2009, 4:39:18 PM7/27/09
to lif...@googlegroups.com
I will try but I may not have a chance in the next few days. I did write up scaladocs, although I haven't pushed some clarifications for TableEditor.
You probably don't want to use XmlMenu, but David said I may as well throw it in. The advantage is if you want to read it in from an xml file.


-------------------------------------

glenn

unread,
Jul 27, 2009, 6:40:15 PM7/27/09
to Lift
Sounds great. I've been using hacks such as adding code like this to
my mapper classes just to create a Many-to-Many
relationship between say, tag and content tables (using an
intermediary ContentTag table). Similarly, I've done User/Roles
relationships.

private object _dbTags extends HasManyThrough(this, Tag, ContentTag,
ContentTag.content, ContentTag.tag)

private[model] var _tags : List[Tag] = _

private val locker = new Object

def tags : List[Tag] = locker.synchronized {
if(_tags eq null){
_tags = _dbTags()
}
_tags
}

def tags(newTags:String) = locker.synchronized {
_tags = newTags.roboSplit(",").map(Tag.byName(_))
this
}

def tags(newTags:List[Tag]) = locker.synchronized {
_tags = newTags
this
}

def tagsToo:List[Tag] = ContentTag.findAll(By(ContentTag.content,
this.id)).map(_.tag.obj.open_!)

def showTags = Text(tags.map(_.name.is).mkString(", "))

But, then I have to add code like this to the meta-mapper objects:

def addTags(entry: Content) {
if(entry._tags ne null){
entry._tags.foreach(ContentTag.join(_, entry))
}
}

def delTags(entry:Content) =
ContentTag.findAll(By(ContentTag.content, entry)).foreach
(_.delete_!)

It's not very pretty. Nor easy to duplicate.

I hope your new code is considerably less messy.

Is it in the repo-snapshot repository yet.

Naftoli Gugenheim

unread,
Jul 27, 2009, 6:50:06 PM7/27/09
to gl...@exmbly.com, lif...@googlegroups.com
I committed it last night, so I think it should be there.
To use many-to-many, simply mix ManyToMany to your mapper, then create a MappedManyToMany field like any other field. See the scaladocs for specifics.
Then in your view you can use the field like a collection, e.g., remove an element with -=, and save the mapper to apply the changes.



-------------------------------------

Naftoli Gugenheim

unread,
Jul 28, 2009, 3:55:27 PM7/28/09
to marius...@gmail.com, lif...@googlegroups.com
In the meantime, if there are any scaladoc comments that could be improved or clarified, please tell me!

-------------------------------------
marius d.<marius...@gmail.com> wrote:


Would you please add some examples on the wiki so that people can
actually visualize how these things can be used?


As far as XmlMenu goes why do we want to express menus as xml ?

Br's,
Marius

glenn

unread,
Jul 28, 2009, 5:56:27 PM7/28/09
to Lift
Naftoli,

The ManyToMany class is in the new lift-mapper jar, but the source is
not available in 1.1-SNAPSHOT-sources. Could you provide?
Thanks,

Glenn...

Naftoli Gugenheim

unread,
Jul 28, 2009, 6:05:30 PM7/28/09
to gl...@exmbly.com, lif...@googlegroups.com
Did you update your source jar? Try deleting it from your repository just to be sure, then mvn dependency:sources etc.
Either way you can access the source on github.

-------------------------------------

David Pollak

unread,
Jul 28, 2009, 8:18:47 PM7/28/09
to lif...@googlegroups.com
THis is all very good stuff.  Thanks for contributing this excellent stuff to Lift!
--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

glenn

unread,
Jul 31, 2009, 5:06:23 PM7/31/09
to Lift
Naftoli,

I set up my mapper to use your new ManyToMany trait, but I'm not sure
how exactly to use it in a snippet or view.

Here's what I have so far, a User, Role and UserRole for my model
classes:

class Role extends LongKeyedMapper[Role] with IdPK {

def getSingleton = Role

object name extends MappedPoliteString(this, 50) {
override def setFilter = notNull _ :: trim _ :: super.setFilter
}
}


class UserRole extends LongKeyedMapper[UserRole] with IdPK {
def getSingleton = UserRole

object role extends MappedLongForeignKey(this, Role){
override def dbIndexed_? = true
}

object user extends MappedLongForeignKey(this,User) {
override def dbIndexed_? = true
}
}

class User extends MegaProtoUser[User] with ManyToMany[UserRole, Role]
{
def getSingleton = User // what's the "meta" server

// define an additional field for a personal essay
object textArea extends MappedTextarea(this, 2048) {
override def textareaRows = 10
override def textareaCols = 50
override def displayName = "Personal Essay"
}

object role extends MappedManyToMany(UserRole, this, Role)


}

and their corresponding singleton objects.

In the past, without the ManyToMany trait, I would do this in User
object

override def editXhtml(user: User) = {
(<form method="post" action={S.uri}>
<table><tr><td colspan="2">{S.??("edit")}</td></tr>
{localForm(user, true)}
<tr><td>Roles</td><td><user:roles/></td></tr>
<tr><td>&nbsp;</td><td><user:submit/></td></tr>
<tr><td><a href={changePasswordPath.mkString("/", "/",
"")}
>{S.??("change.password")}</a></td></tr>
</table>


</form>)
}

and bind this with the following to show the roles for a particular
user as a comma delimited string.

def innerEdit = User.isa_?("admin") match {
case true => bind("user", editXhtml(theUser),
"roles" -> text(roles,
doRolesAndSubmit),
"submit" -> SHtml.submit(S.??("edit"),
testEdit _))
case false => bind("user", editXhtml(theUser),
"roles" ->
theUser.showRoles,
"submit" -> SHtml.submit(S.??("edit"),
testEdit _))
}

here roles is defined as

var roles = theUser.roles.map(_.name.is).mkString(", ")

Do you have an example of how to achieve something similar with your
new ManyToMany and MappedManyToMany code?
Appreciate any help you can give me.

Thanks,

Glenn...

Naftoli Gugenheim

unread,
Jul 31, 2009, 5:18:50 PM7/31/09
to gl...@exmbly.com, lif...@googlegroups.com
I didn't yet look at your code too closely, but the idea is that the "field" that represents the relationship implements Buffer, so you can add, remove, and iterate its elements, which are the other side of the relationship. These changes are remembered until you call save, when they are acted on.

Naftoli Gugenheim

unread,
Jul 31, 2009, 5:20:12 PM7/31/09
to gl...@exmbly.com, lif...@googlegroups.com
I should probably take Mapped out of the name, because it's not a MappedField.
Message has been deleted
Message has been deleted

Naftoli Gugenheim

unread,
Aug 2, 2009, 4:20:02 PM8/2/09
to lif...@googlegroups.com
Glad to hear. Also see mapper.view.ItemsList (anyone have a better name?), which is used by TableEditor. It's a similar idea without relationships.
Should MappedOne/ManyToMany be based on ItemsList?
Also should I take Mapped out of those names? After all, they're not MappedFields and they don't correspond to a table column.

-------------------------------------
Magnus Alvestad<magnus....@gmail.com> wrote:

This work that you've done in Mapper fits in very well with something
I've been planning to do. In a Java project some time ago we
implemented a 'change engine'. We were loading big pension agreements,
manipulating them in a web interface and finally saving them back to
the database. While the user was working on the agreement, we
generated a list of changes, containing enough information to replay
or unroll the change. We could use this to implement undo and some
semi-intelligent merging when two users were trying to commit  changes
to the same agreement. We also let the user review his changes before
saving, and we indicated changed fields visually. We were even
thinking of implementing some kind of macro or replay functionality,
but never got that far.

I've been trying to implement something similar as a lift component,
but with the old relationship handling it wasn't practical. With your
changes it should be. Thanks!

-Magnus

Naftoli Gugenheim

unread,
Aug 2, 2009, 4:21:15 PM8/2/09
to lif...@googlegroups.com

glenn

unread,
Aug 3, 2009, 6:48:20 PM8/3/09
to Lift
Naftoli,

Liked your OneToMany article, but not sure how the new
ModelView and ModelSnippet code can be applied to ManyToMany.
Can you provide a sample?

Glenn...



On Aug 2, 1:21 pm, Naftoli Gugenheim <naftoli...@gmail.com> wrote:
> I put an article on the wiki about OneToMany --http://wiki.github.com/dpp/liftweb/how-to-work-with-one-to-many-relat...
> .
>
> On Sun, Aug 2, 2009 at 11:20 AM, Naftoli Gugenheim <naftoli...@gmail.com>wrote:
>
> > Glad to hear. Also see mapper.view.ItemsList (anyone have a better name?),
> > which is used by TableEditor. It's a similar idea without relationships.
> > Should MappedOne/ManyToMany be based on ItemsList?
> > Also should I take Mapped out of those names? After all, they're not
> > MappedFields and they don't correspond to a table column.
>
> > -------------------------------------

glenn

unread,
Aug 3, 2009, 7:09:49 PM8/3/09
to Lift
Just to add to what I just wrote, I don't see how your ModelView can
be applied to
User, which already extends a class, MegaProtoUser. Maybe I'm not
comprehending this correctly.

Glenn...

Naftoli Gugenheim

unread,
Aug 3, 2009, 9:22:24 PM8/3/09
to gl...@exmbly.com, lif...@googlegroups.com
Your snippet should extend ModelSnippet (which extends StatefulSnippet).
Then write:
val view = new ModelView(new User, this) {}
Calling load on another ModelView that references the snippet will load its entity into that view val. It has a number of methods of possible interest, like save which checks validity.
Also see Util.
Are you seeing the scaladocs?

-------------------------------------

glenn

unread,
Aug 4, 2009, 12:01:30 PM8/4/09
to Lift
Naftoli,

Functional programming systems are notoriously difficult to document.
The only way to really know what's going on is to meticulously trace
through the source. But that requires
time-consuming trial and error coding. So, without clear examples
demonstrating exactly what you have in mind, I and others in the same
boat, could spend days and still not
get it right.

For example, I tried this:

var theUser:User = null
val view = new ModelView(theUser, this)

def list(ns: NodeSeq): NodeSeq = User.currentUser.map({user =>
User.findAll.flatMap({u =>
bind("user", chooseTemplate("user", "entry", ns),
"firstname" -> Text(u.firstName.is),
"lastname" -> Text(u.lastName.is),
"email" -> Text(u.email.is),
"roles" -> u.roles.map(_.name.toString).mkString(",
"),
view.editAction,
view.removeAction
)
})
}) openOr Text("You're not logged in")


and, I get a null value exception when I try to remove a user in the
list. Simply using
val view = new ModelView(new User, this), doesn't work either,
although I don't get an
exception. Now, where should I be calling load in all of this?

Glenn...

Naftoli Gugenheim

unread,
Aug 4, 2009, 4:19:47 PM8/4/09
to gl...@exmbly.com, lif...@googlegroups.com
To answer your immediate question, the listing should not refer to the snippet's view but new ModelView instances for each entity. Then editAction is shorthand for the snippet's link method with a callback to call load on the ModelView. To set the snippet's view's entity, either call load on the snippet with the other ModelView, or call load on the other ModelView (or just set its entity directly).
As far as documentation, please tell me what scaladocs need what clarification. Thanks.



-------------------------------------

Naftoli Gugenheim

unread,
Aug 5, 2009, 12:18:19 PM8/5/09
to lif...@googlegroups.com
Correction: ModelSnippet.load takes the actual Mapper instance, not the ModelView wrapper.

-------------------------------------

glenn

unread,
Aug 5, 2009, 12:48:12 PM8/5/09
to Lift
Naftoli,

I fixed my code per your comments and now I can edit and remove users
from a list, as long as I populate the list with
ModelView instances, as you said. As for the docs, this step was not
clear to me at all. I just assumed that the list was
just populated with User entities and the view in the ModelSnippet was
instantiated with the selected User on each request.

It sounds like your plate is pretty full, so I won't expect much, but
sometime soon, could you provide an example, or improved
docs, for using TableEditor and its related ItemsList trait.

Thanks for all.

Glenn...

On Aug 5, 9:18 am, Naftoli Gugenheim <naftoli...@gmail.com> wrote:
> Correction: ModelSnippet.load takes the actual Mapper instance, not the ModelView wrapper.
>
> -------------------------------------
>
> > > > > which is used byTableEditor. It's a similar idea without relationships.
> > > > > additions and deletions. It's used byTableEditor, which is a very easy to

Naftoli Gugenheim

unread,
Aug 5, 2009, 12:57:47 PM8/5/09
to gl...@exmbly.com, lif...@googlegroups.com
I'll try.
By the way, as per my correction, you can implement list the regular way without ModelView, and just use ModelSnippet's load function in your edit link or button, passing it the User instance.

glenn

unread,
Aug 5, 2009, 6:10:09 PM8/5/09
to Lift
Naftoli,

Hate to do this to you, but I'm getting the following error using
ManyToMany for Users to Roles:

Message: java.lang.RuntimeException: Broken join
scala.Predef$.error(Predef.scala:76)
net.liftweb.mapper.ManyToMany$MappedManyToMany$$anonfun$children$1$
$anonfun$apply$1.apply(ManyToMany.scala:54)
net.liftweb.mapper.ManyToMany$MappedManyToMany$$anonfun$children$1$
$anonfun$apply$1.apply(ManyToMany.scala:54)
net.liftweb.util.EmptyBox.openOr(Box.scala:372)
net.liftweb.mapper.ManyToMany$MappedManyToMany$$anonfun$children
$1.apply(ManyToMany.scala:54)
net.liftweb.mapper.ManyToMany$MappedManyToMany$$anonfun$children
$1.apply(ManyToMany.scala:54)
scala.List.map(List.scala:812)
net.liftweb.mapper.ManyToMany$MappedManyToMany.children
(ManyToMany.scala:54)
net.liftweb.mapper.ManyToMany$MappedManyToMany.elements
(ManyToMany.scala:96)
scala.Seq$class.flatMap(Seq.scala:293)
net.liftweb.mapper.ManyToMany$MappedManyToMany.flatMap
(ManyToMany.scala:44)

def edit(ns: NodeSeq): NodeSeq = {
val theUser = view.entity
val addRole = TheBindParam("insert", view.snippet.link("edit", ()
=> theUser.roles += new Role, Text(S?("Add Role"))))

bind("user", ns,
"firstname" -> text(theUser.firstName.is, theUser.firstName
(_), ("size","20")),
"lastname" -> text(theUser.lastName.is,theUser.lastName(_),
("size", "30")),
"roles" -> theUser.roles.flatMap{role =>
bind("role", ns,
"name" -> role.name.toForm,
"remove" -> SHtml.submit(S?("Remove"), ()=>
theUser.roles -= role)
)
},
addRole,
"submit" -> SHtml.submit(S?("Save"), ()=>view.save)
)
}

The offending code seems to be the line: "roles" ->
theUser.roles.flatMap{....
in the above bind method when I click on the addRole link.

Here's my User class:

class User extends MegaProtoUser[User] with ManyToMany[Long,User]{
def getSingleton = User // what's the "meta" server

object roles
extends MappedManyToMany(UserRole, UserRole.user, UserRole.role,
Role)

}

What am I doing wrong? You can see how difficult it is to slog through
this code, let alone just
trying to explain the problem so I can get help.

Naftoli Gugenheim

unread,
Aug 5, 2009, 6:33:01 PM8/5/09
to gl...@exmbly.com, lif...@googlegroups.com
First of all, let me tell you what the error means. It means that there is a relevant row in the join table that doesn't have a corresponding element in the other table. Specifically, calling joinRecord.childMappedForeignKey.obj, so to speak, returns Empty.
The question is how it got to this inconsistent state. When you add a Role to a User, MappedManyToMany creates a UserRole for it. For some reason though there is a UserRole without a Role or User.
Can you send me a self-contained project? Also is it high priority?

Naftoli Gugenheim

unread,
Aug 5, 2009, 6:39:13 PM8/5/09
to gl...@exmbly.com, lif...@googlegroups.com
Oh, I think I know what the problem is. I think I should classify it as a bug.
Since you're adding a Role that isn't saved yet, and ManyToMany tracks the children via the join table, it can't access the child. As a workaround save the Role before adding it, although the need to do so is against the idea of ManyToMany and OneToMany. I will see what I can do, G-d willing.

glenn

unread,
Aug 5, 2009, 7:10:56 PM8/5/09
to Lift
Naftoli,

While your working on this issue, there seems to be another. Here's
what's happening:

If I make a coding mistake, and code User as follows:

class User extends MegaProtoUser[User] with ManyToMany[Long,Role]{
def getSingleton = User // what's the "meta" server

object roles
extends MappedManyToMany(UserRole, UserRole.user, UserRole.role,
Role)


}

instead of

class User extends MegaProtoUser[User] with ManyToMany[LongUser]{ ...

I don't get a compiler error (I'm using Eclipse with the Scala
plugin). Instead,
I get a stack overflow error and my IDE crashes.

Glenn...
> ...
>
> read more »

Randinn

unread,
Aug 25, 2009, 7:25:25 AM8/25/09
to Lift
Anyone have code showing this in action as it were?
> ...
>
> read more »

Naftoli Gugenheim

unread,
Aug 25, 2009, 10:08:51 AM8/25/09
to ran...@gmail.com, lif...@googlegroups.com
"This" meaning the problem, or meaning ManyToMany?
It's pretty simple to use. You're mapper should extend ManyToMany, and you should have a "field":
object m2m extends MappedManyToMany(.../*the two MetaMappers and foreign keys, see the docs/source*/)
Then you can treat m2m as a Buffer, adding and deleting children. Call save on the mapper or m2m to propogate the adding/deleting to the database, and refresh to reset its contents to reflect the database.
However, as noted currently the children must be saved before they are added. This is not the case for OneToMany, and for ManyToMany it should not be a problem because usually the children exist independently of the parent--that's why they can have 0 or 1 or more "parents."


-------------------------------------

glenn

unread,
Aug 25, 2009, 12:37:56 PM8/25/09
to Lift
I have a working example of ManyToMany that adds roles to users. I
would be happy to share my work
but I have an aversion to github. I don't like it, It's a slower-than-
molasses interface. It's buggy and user-hostile
in my opinion. I'm looking for an alternative to make it public. Any
suggestions would be appreciated.

Glenn...

On Aug 25, 7:08 am, Naftoli Gugenheim <naftoli...@gmail.com> wrote:
> "This" meaning the problem, or meaning ManyToMany?
> It's pretty simple to use. You're mapper should extend ManyToMany, and you should have a "field":
> object m2m extends MappedManyToMany(.../*the two MetaMappers and foreign keys, see the docs/source*/)
> Then you can treat m2m as a Buffer, adding and deleting children. Call save on the mapper or m2m to propogate the adding/deleting to the database, and refresh to reset its contents to reflect the database.
> However, as noted currently the children must be saved before they are added. This is not the case for OneToMany, and for ManyToMany it should not be a problem because usually the children exist independently of the parent--that's why they can have 0 or 1 or more "parents."
>
> -------------------------------------
>
> ...
>
> read more »

Naftoli Gugenheim

unread,
Aug 25, 2009, 1:43:03 PM8/25/09
to lif...@googlegroups.com
Google code? Sourceforge? ProjectLocker?
There are a lot of choices...

glenn

unread,
Aug 25, 2009, 5:29:12 PM8/25/09
to Lift
Well, I managed to upload to github.
Here is the repository:

git://github.com/glennSilverman/UserMon.git

Any feedback is appreciated.

Glenn...
> ...
>
> read more »

Randinn

unread,
Aug 25, 2009, 6:01:27 PM8/25/09
to Lift
Great! Thank you both for you help.

Reply all
Reply to author
Forward
0 new messages