You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jackrabbit.apache.org by Fabián Mandelbaum <fm...@gmail.com> on 2010/07/06 19:23:19 UTC
Changing the content/properties of mix:versionable nodes without
creating a new version
Hello there,
I have an nt:file node with the mix:versionable mixin. There are times
when I'd like to NOT create a new revision/version of the file when
storing the file. That is, the jcr:content node contents of the
nt:file change, but I don't want a new revision in the revision
history for that nt:file node.
The code I'm using now is something this:
public String store(File file, String folder, boolean newVersion)
throws RepositoryException, FileNotFoundException {
folder = folderName(folder);
String nname = file.getName();
String path = String.format("%s%s/%s", PATH, folder, nname); // NOI18N
String fpath = path;
Node rnode = null;
if (session.itemExists(path)) { // Update
Node fnode = ((Node)session.getItem(path));
rnode = fnode.getNode("jcr:content"); // NOI18N
if (newVersion) { // Create new version
rnode.checkout();
}
} else { // Add
path = String.format("%s%s", PATH, folder);
Node fnode = ((Node)session.getItem(path)).addNode(nname,
"nt:file"); // NOI18N
rnode = fnode.addNode("jcr:content", "nt:resource"); // NOI18N
rnode.addMixin("mix:versionable"); // NOI18N
fnode.addMixin("mix:lockable"); // NOI18N
}
rnode.setProperty("jcr:mimeType",
MimeUtils.getInstance().getMimeType(file)); // NOI18N
Calendar lastModified = Calendar.getInstance();
lastModified.setTimeInMillis(file.lastModified());
rnode.setProperty("jcr:lastModified", lastModified); // NOI18N
rnode.setProperty("jcr:data", new AutoCloseInputStream(new
FileInputStream(file))); // NOI18N
path = String.format("%s%s/%s", PPATH, folder, nname); // NOI18N
Node pnode = null;
if (session.itemExists(path)) {
pnode = (Node)session.getItem(path);
} else {
path = String.format("%s%s", PPATH, folder); // NOI18N
pnode = ((Node)session.getItem(path)).addNode(nname);
pnode.addMixin("mix:lockable"); // NOI18N
}
pnode.setProperty("cco_size", file.length()); // NOI18N
session.save();
if (newVersion) {
rnode.checkin();
}
return fpath;
}
PATH and PPATH are constants with base path for content and custom
properties, respectively, folderName is a name-normalizing function to
handle spaces and other 'strange' chars. The rest is fairly standard
or self-explained
If I first call this function with newVersion = true, a revision 1.0
is created, so far, so good. When I call it again, for the same file,
with newVersion = false (because now I just want to update the file's
content node without creating a new version), I get the following
exception:
javax.jcr.version.VersionException: Unable to perform operation. Node
is checked-in.
at org.apache.jackrabbit.core.ItemValidator.checkCondition(ItemValidator.java:308)
at org.apache.jackrabbit.core.ItemValidator.checkModify(ItemValidator.java:274)
at org.apache.jackrabbit.core.NodeImpl.checkSetProperty(NodeImpl.java:1381)
at org.apache.jackrabbit.core.NodeImpl.setProperty(NodeImpl.java:2410)
at com.calenco.repository.ContentDAO.store(ContentDAO.java:189)
....
So. Can I modify the code above in any way to be able to create
versions of files as I wish, or do mix:versionable nodes must always
have a new version created when one of its properties change?
Thanks in advance for your answers.
--
Fabián Mandelbaum
IS Engineer
Re: Changing the content/properties of mix:versionable nodes without
creating a new version
Posted by Fabián Mandelbaum <fm...@gmail.com>.
Hello Jukka,
thanks for your quick, and accurate answer. I've just removed the 1st
if, calling checkout() always, and left the last if calling checkin()
only if newVersion is true. It works fine.
On Tue, Jul 6, 2010 at 2:35 PM, Jukka Zitting <ju...@gmail.com> wrote:
> Hi,
>
> 2010/7/6 Fabián Mandelbaum <fm...@gmail.com>:
>> So. Can I modify the code above in any way to be able to create
>> versions of files as I wish, or do mix:versionable nodes must always
>> have a new version created when one of its properties change?
>
> See the checkout() method. When you checkin() a node, a new version is
> created and the node enters the "checked in" state in which it can not
> be modified. Callin checkout() moves the node to a "checked out" state
> where it can be modified. Only when you call checkin() again is a new
> version created based on the current contents of the checked out node.
>
> BR,
>
> Jukka Zitting
>
--
Fabián Mandelbaum
IS Engineer
Re: Changing the content/properties of mix:versionable nodes without
creating a new version
Posted by Jukka Zitting <ju...@gmail.com>.
Hi,
2010/7/6 Fabián Mandelbaum <fm...@gmail.com>:
> So. Can I modify the code above in any way to be able to create
> versions of files as I wish, or do mix:versionable nodes must always
> have a new version created when one of its properties change?
See the checkout() method. When you checkin() a node, a new version is
created and the node enters the "checked in" state in which it can not
be modified. Callin checkout() moves the node to a "checked out" state
where it can be modified. Only when you call checkin() again is a new
version created based on the current contents of the checked out node.
BR,
Jukka Zitting