You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Lubos and Alena Pochman <po...@gmail.com> on 2007/03/15 03:52:24 UTC

Cannot update previous version of nt:file node

I create nt:file node like:

        Node root = session.getRootNode();
        document = root.addNode(instanceName, "nt:file");
        document.addMixin("mix:versionable");  // Make document referencable
and versionable
        Node content = document.addNode("jcr:content", "nt:resource");
        content.setProperty("jcr:mimeType", "application/zip");
        content.setProperty("jcr:data", new
ByteArrayInputStream(zipContents));
        content.setProperty("jcr:lastModified", Calendar.getInstance());
        this.session.save();

Then I create a new version like:

        document.checkout();
        Node content = (Node)document.getPrimaryItem();
        content.setProperty("jcr:data", new
ByteArrayInputStream(zipContents));
        content.setProperty("jcr:lastModified", Calendar.getInstance());
        this.session.save();
        document.checkin();

Then I try to retrieve current version data (that works OK):

        node = this.session.getNodeByUUID(id);
        Property prop = node.getProperty("jcr:content/jcr:data");
        InputStream is = prop.getStream();

But then I try to retrieve previous version of the node, it fails with
"PathNotFoundException: jcr:frozenNode/jcr:content/jcr:data" exception when
calling getProperty("jcr:frozenNode/jcr:content/jcr:data"):

            Node document = this.session.getNodeByUUID(id);
            Version base = document.getBaseVersion();
            Version[] predecessors = base.getPredecessors();
             for (int i = 0; i < predecessors.length; i++) {
                       System.out.println(predecessors[i].getCreated() + ":
"
                               +
predecessors[i].getProperty("jcr:frozenNode/jcr:content/jcr:data").getLength());

How do I access and read properties of previous versions of a node?

Thanks, Lubos

Re: Cannot update previous version of nt:file node

Posted by Lubos and Alena Pochman <po...@gmail.com>.
Jukka, Tobias, and all.

you guys are the best "documentation" and I very much appreciate your help
and patience!
I found about the empty frozen node sentinel (the hard way from debugging
8-).
I found out what was wrong. When I created the first version of the
node/document, I did not check it in,
because I did not know I should do it. Even the example
http://www.artima.com/lejava/articles/contentrepository3.html
misses that. But then I downloaded the example source code and voila!

Anyway I now create the new versionable document like (see the
document.checkin() at the end):

        Node root = session.getRootNode();
        document = root.addNode(instanceName, "nt:file");
        document.addMixin("mix:versionable");  // Make document referencable
and versionable
        Node content = document.addNode("jcr:content", "nt:resource");
        content.setProperty("jcr:mimeType", "application/zip");
        content.setProperty("jcr:data", new
ByteArrayInputStream(zipContents));
        content.setProperty("jcr:lastModified", Calendar.getInstance());
        this.session.save();
        document.checkin();

And then iterate over versions like:

        Node document = this.session.getNodeByUUID(documentId);
        VersionHistory history = document.getVersionHistory();
        VersionIterator ito = history.getAllVersions();
        while (ito.hasNext()) {
           Version v = ito.nextVersion();
           //The version node will have a "frozen" child node that contains
           //the corresponding version's node data
           NodeIterator it = v.getNodes("jcr:frozenNode");
           if (it.hasNext()) {
             Node no = it.nextNode();
             if (no.hasNode("jcr:content"))
                ...
           }
        }

This seems to be working well, and the versions are sorted from current to
oldest.

Thanks to Tobias, you, and Frank Sommers for the help. Lubos


On 3/15/07, Jukka Zitting <ju...@gmail.com> wrote:
>
> Hi,
>
> On 3/15/07, Lubos and Alena Pochman <po...@gmail.com> wrote:
> > thanks for the response. My question is, why is the predecessor empty?
>
> When you create a mix:versionable node, an empty "root version" get's
> created in the version storage as a sentinel that acts as the
> predecessor of the first actual version that you checkin().
>
> > Didn't I created new version with the document.checkin(),
> document.checkout()?
> > The modification worked, when I retrieve the modified data by using the
> modified
> > document node directly, the new data are there. But where is the
> previous version?
> > When I repeat the process I still get only predecessors.length == 1. So
> I
> > must be doing something wrong in trying to create new version it the
> code:
> >
> >         document.checkout();
> >         Node content = (Node)document.getPrimaryItem();
> >         content.setProperty("jcr:data", new
> > ByteArrayInputStream(zipContents));
> >         content.setProperty("jcr:lastModified", Calendar.getInstance());
> >         this.session.save();
> >         document.checkin();
> >
> > It looks like I am updating the current node without creating a new
> version?
> > And why in this case predecessors.length == 1? Shouldn't it be 0?
>
> It depends on what you did before the document.checkout() call. If you
> just created the document node, then the results you describe are
> correct. The base version you get using document.getBaseVersion() is
> the one checked in with the document.checkin() call, and the one
> predecessor is the root version that got created when the versionable
> document node was first saved.
>
> You can create new versions of the document node and increase the
> predecessors list by repeating the document.checout() ...
> document.checkin() cycle.
>
> > To be honest, even after re-reading the JSR-170 spec several times on
> > versioning, it is hard for me to get my head around it. Is there any
> tutorial
> > and or book I can use to learn more about the versioning topic?
>
> I feel your pain. Unfortunately there isn't yet very much introductory
> material on JCR.
>
> PS. I will be giving a JCR tutorial in ApacheCon EU in early May, see
> http://www.eu.apachecon.com/ for the details. The tutorial materials
> will sooner or later after the ApacheCon find their way also onto the
> Jackrabbit web site.
>
> BR,
>
> Jukka Zitting
>

Re: Cannot update previous version of nt:file node

Posted by Jukka Zitting <ju...@gmail.com>.
Hi,

On 3/15/07, Lubos and Alena Pochman <po...@gmail.com> wrote:
> thanks for the response. My question is, why is the predecessor empty?

When you create a mix:versionable node, an empty "root version" get's
created in the version storage as a sentinel that acts as the
predecessor of the first actual version that you checkin().

> Didn't I created new version with the document.checkin(), document.checkout()?
> The modification worked, when I retrieve the modified data by using the modified
> document node directly, the new data are there. But where is the previous version?
> When I repeat the process I still get only predecessors.length == 1. So I
> must be doing something wrong in trying to create new version it the code:
>
>         document.checkout();
>         Node content = (Node)document.getPrimaryItem();
>         content.setProperty("jcr:data", new
> ByteArrayInputStream(zipContents));
>         content.setProperty("jcr:lastModified", Calendar.getInstance());
>         this.session.save();
>         document.checkin();
>
> It looks like I am updating the current node without creating a new version?
> And why in this case predecessors.length == 1? Shouldn't it be 0?

It depends on what you did before the document.checkout() call. If you
just created the document node, then the results you describe are
correct. The base version you get using document.getBaseVersion() is
the one checked in with the document.checkin() call, and the one
predecessor is the root version that got created when the versionable
document node was first saved.

You can create new versions of the document node and increase the
predecessors list by repeating the document.checout() ...
document.checkin() cycle.

> To be honest, even after re-reading the JSR-170 spec several times on
> versioning, it is hard for me to get my head around it. Is there any tutorial
> and or book I can use to learn more about the versioning topic?

I feel your pain. Unfortunately there isn't yet very much introductory
material on JCR.

PS. I will be giving a JCR tutorial in ApacheCon EU in early May, see
http://www.eu.apachecon.com/ for the details. The tutorial materials
will sooner or later after the ApacheCon find their way also onto the
Jackrabbit web site.

BR,

Jukka Zitting

Re: Cannot update previous version of nt:file node

Posted by Lubos and Alena Pochman <po...@gmail.com>.
Tobias,

thanks for the response. My question is, why is the predecessor empty?
Didn't I created new version
with the document.checkin(), document.checkout()? The modification worked,
when I retrieve the modified data
by using the modified document node directly, the new data are there. But
where is the previous version?
When I repeat the process I still get only predecessors.length == 1. So I
must be doing something wrong in trying to create
new version it the code:

        document.checkout();
        Node content = (Node)document.getPrimaryItem();
        content.setProperty("jcr:data", new
ByteArrayInputStream(zipContents));
        content.setProperty("jcr:lastModified", Calendar.getInstance());
        this.session.save();
        document.checkin();

It looks like I am updating the current node without creating a new version?
And why in this case predecessors.length == 1? Shouldn't it be 0?

To be honest, even after re-reading the JSR-170 spec several times on
versioning,
it is hard for me to get my head around it. Is there any tutorial and or
book I can use
to learn more about the versioning topic?

I looked at the browser (nice tool 8-), but I still cannot see how to
dereference baseVersion
or predecessors. But that might be because I am not creating new versions in
the code above.

Thanks, Lubos

On 3/15/07, Tobias Bocanegra <to...@day.com> wrote:
>
> hi,
> please note that in you case the version only has one predecessor, the
> rootVersion. and it does contains an empty frozen node. thats probably
> where your PathNotFoundException is comming from.
>
> regards, toby
>
> ps: play around with the jcr-browser at http://jcr.day.com
> open the content explorer, click on 'view settings' and clear all
> filters. then you can browse the jcr:system/jcr:versionStorage. maybe
> that clarifies the structure of the version storage.
>
> On 3/15/07, Lubos and Alena Pochman <po...@gmail.com> wrote:
> > I create nt:file node like:
> >
> >         Node root = session.getRootNode();
> >         document = root.addNode(instanceName, "nt:file");
> >         document.addMixin("mix:versionable");  // Make document
> referencable
> > and versionable
> >         Node content = document.addNode("jcr:content", "nt:resource");
> >         content.setProperty("jcr:mimeType", "application/zip");
> >         content.setProperty("jcr:data", new
> > ByteArrayInputStream(zipContents));
> >         content.setProperty("jcr:lastModified", Calendar.getInstance());
> >         this.session.save();
> >
> > Then I create a new version like:
> >
> >         document.checkout();
> >         Node content = (Node)document.getPrimaryItem();
> >         content.setProperty("jcr:data", new
> > ByteArrayInputStream(zipContents));
> >         content.setProperty("jcr:lastModified", Calendar.getInstance());
> >         this.session.save();
> >         document.checkin();
> >
> > Then I try to retrieve current version data (that works OK):
> >
> >         node = this.session.getNodeByUUID(id);
> >         Property prop = node.getProperty("jcr:content/jcr:data");
> >         InputStream is = prop.getStream();
> >
> > But then I try to retrieve previous version of the node, it fails with
> > "PathNotFoundException: jcr:frozenNode/jcr:content/jcr:data" exception
> when
> > calling getProperty("jcr:frozenNode/jcr:content/jcr:data"):
> >
> >             Node document = this.session.getNodeByUUID(id);
> >             Version base = document.getBaseVersion();
> >             Version[] predecessors = base.getPredecessors();
> >              for (int i = 0; i < predecessors.length; i++) {
> >                        System.out.println(predecessors[i].getCreated() +
> ":
> > "
> >                                +
> >
> predecessors[i].getProperty("jcr:frozenNode/jcr:content/jcr:data").getLength());
> >
> > How do I access and read properties of previous versions of a node?
> >
> > Thanks, Lubos
> >
>
>
> --
> -----------------------------------------< tobias.bocanegra@day.com >---
> Tobias Bocanegra, Day Management AG, Barfuesserplatz 6, CH - 4001 Basel
> T +41 61 226 98 98, F +41 61 226 98 97
> -----------------------------------------------< http://www.day.com >---
>

Re: Cannot update previous version of nt:file node

Posted by Tobias Bocanegra <to...@day.com>.
hi,
please note that in you case the version only has one predecessor, the
rootVersion. and it does contains an empty frozen node. thats probably
where your PathNotFoundException is comming from.

regards, toby

ps: play around with the jcr-browser at http://jcr.day.com
open the content explorer, click on 'view settings' and clear all
filters. then you can browse the jcr:system/jcr:versionStorage. maybe
that clarifies the structure of the version storage.

On 3/15/07, Lubos and Alena Pochman <po...@gmail.com> wrote:
> I create nt:file node like:
>
>         Node root = session.getRootNode();
>         document = root.addNode(instanceName, "nt:file");
>         document.addMixin("mix:versionable");  // Make document referencable
> and versionable
>         Node content = document.addNode("jcr:content", "nt:resource");
>         content.setProperty("jcr:mimeType", "application/zip");
>         content.setProperty("jcr:data", new
> ByteArrayInputStream(zipContents));
>         content.setProperty("jcr:lastModified", Calendar.getInstance());
>         this.session.save();
>
> Then I create a new version like:
>
>         document.checkout();
>         Node content = (Node)document.getPrimaryItem();
>         content.setProperty("jcr:data", new
> ByteArrayInputStream(zipContents));
>         content.setProperty("jcr:lastModified", Calendar.getInstance());
>         this.session.save();
>         document.checkin();
>
> Then I try to retrieve current version data (that works OK):
>
>         node = this.session.getNodeByUUID(id);
>         Property prop = node.getProperty("jcr:content/jcr:data");
>         InputStream is = prop.getStream();
>
> But then I try to retrieve previous version of the node, it fails with
> "PathNotFoundException: jcr:frozenNode/jcr:content/jcr:data" exception when
> calling getProperty("jcr:frozenNode/jcr:content/jcr:data"):
>
>             Node document = this.session.getNodeByUUID(id);
>             Version base = document.getBaseVersion();
>             Version[] predecessors = base.getPredecessors();
>              for (int i = 0; i < predecessors.length; i++) {
>                        System.out.println(predecessors[i].getCreated() + ":
> "
>                                +
> predecessors[i].getProperty("jcr:frozenNode/jcr:content/jcr:data").getLength());
>
> How do I access and read properties of previous versions of a node?
>
> Thanks, Lubos
>


-- 
-----------------------------------------< tobias.bocanegra@day.com >---
Tobias Bocanegra, Day Management AG, Barfuesserplatz 6, CH - 4001 Basel
T +41 61 226 98 98, F +41 61 226 98 97
-----------------------------------------------< http://www.day.com >---