Hi,
On Tue, Apr 22, 2008 at 8:44 AM, Gert Vanthienen <[EMAIL PROTECTED]> wrote:
> ...I would like to generate a link on a page to show the previous version
> of the page (in my case: I want to show the ServiceMix MessageExchange just
> before it got processed)....
Cool - here's an example that works for me, bearing in mind that
people who have done more JCR versioning stuff might have a better
solution.
Assuming a versionable node at /content/versioned, with
sling:resourceType=foo, here's the /apps/foo/html.esp script that
handles the /content/versioned.html request:
<html>
<%
// assume we have a versionable node
var iter = currentNode.getVersionHistory().getAllVersions();
%>
<body>
<h1>Versions of node <%= currentNode.getPath() %></h1>
<%
while(iter.hasNext()) {
var v = iter.nextVersion();
// use UUID of version node to build a link, and add a .version
// selector to have another esp script process that request
var uuid = v.getProperty("jcr:uuid");
var vPath = currentNode.getPath() + ".version." + uuid + ".html";
// Use Version creation date as the link text
var vDate = v.getCreated().getTime();
%>
<a href="<%= vPath %>"><%= vDate %></a><br/>
<%
}
%>
</body>
</html>
The links to the individual versions look like:
/content/versioned.version.313016e1.html
Where the first .version. selector causes a different esp script to be
called to display the version data, and the 313016e1 selector is the
UUID of the versioned node (real UUIDs are longer).
That request is handled by this second script,
/apps/foo/version/html.esp (name will change soon, SLING-387):
<html>
<%
// Get version node UUID, which is the second selector
var uuid = null;
var sel = request.getRequestPathInfo().getSelectors();
if(sel.length >= 2) {
uuid = sel[1];
} else {
response.sendError(400, "Version node UUID must be given as
second selector");
}
// Get version node
var v = currentNode.getSession().getNodeByUUID(uuid);
var frozen = v.getNode("jcr:frozenNode");
var title = frozen.title;
%>
<body>
<h1>Version of node <%= currentNode.getPath() %></h1>
Name: <b><%= v.getName() %></b><br/>
UUID: <b><%= uuid %></b><br/>
Path: <b><%= v.getPath() %></b><br/>
Frozen node path: <b><%= frozen.getPath() %></b><br/>
<% if(title) { %>
Frozen node title: <b><%= frozen.getProperty("title") %></b><br/>
<% } else { %>
Frozen node does not have a title property
<% } %>
</body>
</html>
Which uses the UUID selector to retrieve the versioned node.
The second trick here is that the versioned data is saved as a
"jcr:frozenNode" node under the Version node. This is explained for
example at http://www.onjava.com/lpt/a/6784 .
Let us know if that works for you!
> ...P.S. If this is OK for everyone, I will add the answer to this thread to a
> page with example ESP
> snippets (together with my previous Q&A). Would you guys prefer it to go on
> the site or rather have it
> on the wiki?...
Sure - can you put that on the public wiki for now, at
http://cwiki.apache.org/SLING/ ? And we can transfer it later to the
website.
IMHO giving you write access to the SLINGxSITE wiki that generates the
website would be ok, given that you have contributed stuff and are
willing to contributed more, and that you're already an ASF committer
at ServiceMix. What do others think?
-Bertrand