For this you can use loc.link.createPath. I use something similar in
my codebase. This has a little cruft you may not be precisely
interested in, but if you look at the link and flink methods, you
should be able to get an idea of how to use the Loc.Link:

    class Path(val elems: List[String])

    object Path {
        def apply(str: String) = new Path(str.split("/").toList)
        def apply(elems: List[String]) = new Path(elems)
    }

    case class UULink[T <: UUEntity](path: Path) extends
Loc.Link[T](path.elems, false) {
        override def pathList(entity: T) = path.elems :::
entity.getUuid.toString :: Nil
    }

    class UULoc[T <: UUEntity](override val params:
List[LocParam[T]])(implicit manifest: Manifest[T]) extends Loc[T] {
        private val log = Logging.logger("UULoc")
        private val typeName = manifest.erasure.getSimpleName
        private val path = Path(typeName.toLowerCase + "s" :: "show" :: Nil)

        override val name = typeName + "  Details"
        override val link: UULink[T] = new UULink[T](path)
        override val text = LinkText((t: T) => Text(typeName + " " + t.getUuid))
        override val defaultValue = Empty

        private def isLegalPath(root: String, uuid: String): Boolean = {
            //log.info("checking path root " + root + " against " +
typeName.toLowerCase + " and uuid " + uuid)
            root == (typeName.toLowerCase + "s") &&
EM.findByUuid[T](uuid).isDefined
        }

        def link(entity: T, linkText: Box[NodeSeq], attrs: (String,
String)*): NodeSeq = {
            <a 
href={link.createPath(entity)}>{linkText.openOr(title(entity))}</a>
//% attrs
        }

        def flink(entity: T, linkText: Box[NodeSeq], attrs: (String,
String)*)(f: () => Any): NodeSeq = {
            SHtml.link(link.createPath(entity), f,
linkText.openOr(text.text(entity)), attrs: _*)
        }

        override def rewrite = {
            Full({
                    case RewriteRequest(ParsePath(root :: "show" ::
uuid :: Nil,_,_,_),_,_)
                        if isLegalPath(root, uuid) =>
                        (RewriteResponse(path.elems,
Map(typeName.toLowerCase + "Uuid" -> uuid)),
EM.findByUuid[T](uuid).get)
                })
        }
    }

    object UULoc {
        def apply[T <: UUEntity](params: LocParam[T]*)(implicit
manifest: Manifest[T]) : UULoc[T] = new UULoc[T](params.toList)
    }

On Tue, Jan 26, 2010 at 3:29 AM, Adam Warski <a...@warski.org> wrote:
> Hello,
>
> I'm having some trouble generating a link basing on a Loc.
> My original use-case is to redirect the user to a  login page if the user is 
> not logged in. I found a wiki on this and it says there to simply redirect to 
> "/user_mgt/login", however I think it would be much nicer if I could generate 
> the link basing on looking up the right Loc.
>
> However having the Loc I can only generate a NodeSeq, not a plain String.
>
> So the solution would be to add a method into the Loc trait parallel to this 
> one:
>
> def createDefaultLink: Option[NodeSeq] = currentValue.flatMap(p => 
> link.createLink(p)).toOption
>
> which would be:
>
> def createDefaultPath: Option[String] = currentValue.flatMap(p => 
> link.createPath(p)).toOption
>
> (btw., why is it Option here, not Box?)
>
> Then generating a link to the login page would simply be:
>
> SiteMap.findLoc("Login").open_!.createDefaultPath
>
> --
> Adam Warski
> http://www.warski.org
> http://www.softwaremill.eu
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Lift" group.
> To post to this group, send email to lift...@googlegroups.com.
> To unsubscribe from this group, send email to 
> liftweb+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/liftweb?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Lift" group.
To post to this group, send email to lift...@googlegroups.com.
To unsubscribe from this group, send email to 
liftweb+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/liftweb?hl=en.

Reply via email to