Ah ha, that's right, I forgot you will need to create a custom file node type
instead of resource. In our env, I've created custom node types for folder,
file and resource to allow any custom properties we want. I just happened to
have the code right in front of me....
----
//if not already registered you'll have to register the namespace
//
ses.getWorkspace().getNamespaceRegistry().registerNamespace("company","http:/
/company.com/jr/ns/1.0");
public void register(Session ses) throws Exception {
registerNodeType(ses,"company:folder","nt:file",false);
registerNodeType(ses,"company:file","nt:file",false);
registerNodeType(ses,"company:resource","nt:resource",true);
}
public void registerNodeType(Session ses, String name, String superType,
boolean versionable) throws Exception {
NodeTypeManager ntm = ses.getWorkspace().getNodeTypeManager();
if(ntm.hasNodeType(name)){
ntm.unregisterNodeType(name);
}
NodeTypeTemplate ntt = createNodeType(ses,name, superType,versionable);
ntm.registerNodeType(ntt,true);
ses.save();
}
public NodeTypeTemplate createNodeType(Session ses, String name, String
superType, boolean versionable) throws Exception{
NodeTypeManager ntm = ses.getWorkspace().getNodeTypeManager();
PropertyDefinitionTemplate pdts = ntm.createPropertyDefinitionTemplate();
pdts.setName("*");
pdts.setMultiple(false);
PropertyDefinitionTemplate pdtm = ntm.createPropertyDefinitionTemplate();
pdtm.setName("*");
pdtm.setMultiple(true);
NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();
ntt.setName(name);
ntt.getPropertyDefinitionTemplates().add(pdts);
ntt.getPropertyDefinitionTemplates().add(pdtm);
if(versionable){
ntt.setDeclaredSuperTypeNames(new String[] {superType,
JcrConstants.MIX_VERSIONABLE});
}else{
ntt.setDeclaredSuperTypeNames(new String[] {superType});
}
return ntt;
}
----
One other note. From your initial question it sounded like you register every
time create a new session? It should be a onetime thing, then the node types
will just be available to new sessions.
-----Original Message-----
From: Lee F [mailto:[email protected]]
Sent: Monday, January 23, 2012 5:20 PM
To: [email protected]
Subject: Re: How to add unversioned metadata to a versioned node?
Hi Mark,
Thanks for the reply. What you say makes sense, but I'm having a hard time
figuring out how to do it. I mean, I can see by looking at my code why it's
doing what it's doing. But I'm not quite able to wrap my head around some of
these node-type concepts enough to change it to what I want.
If you have a moment, could you look at this piece of code and tell me if you
have any quick pointers? Maybe some quick pseudo-code?
In addition to the code I provided previously, here's a sample of what I
currently have for creating a node for a new file....
-----------------------------------------------------------------------------
-----------------------------------------------------------
Node folderNode = session.getNode(folderPath); Node fileNode =
folderNode.addNode(fileName, "nt:file");
fileNode.addMixin("mix:versionable");
Node resNode = fileNode.addNode("jcr:content", "fileContent");
resNode.setProperty("jcr:mimeType", mimeType);
resNode.setProperty("jcr:data", stream);
resNode.setProperty("jcr:lastModified", Calendar.getInstance());
resNode.setProperty("description", description); session.save();
vman.checkin(fileNode.getPath());
-----------------------------------------------------------------------------
-------------------------------------------------------
It seems to me that I should (conceptually) change the code from above to
something like the following....
However, I can't figure out to add custom properties to that top-level node.
-----------------------------------------------------------------------------
-----------------------------------------------------------
Node folderNode = session.getNode(folderPath); Node fileNode =
folderNode.addNode(fileName, "nt:file"); fileNode.setProperty("description",
description); Node contentNode = fileNode.addNode("jcr:content",
"fileContent"); contentNode.addMixin("mix:versionable");
contentNode.setProperty("jcr:mimeType", mimeType);
contentNode.setProperty("jcr:data", stream);
contentNode.setProperty("jcr:lastModified", Calendar.getInstance());
session.save(); vman.checkin( contentNode.getPath());
-----------------------------------------------------------------------------
-------------------------------------------------------
On Mon, Jan 23, 2012 at 2:12 PM, Mark Herman <[email protected]> wrote:
> I would suggest that you don't make your "fileNode" versionable.
> Where you define your "fileContent" nodetype, add the mix:versionable
> there. Then the metadata of your fileNode can change all you want,
> but your resource has its own independent versioning.
>
> -----Original Message-----
> From: Lee F [mailto:[email protected]]
> Sent: Monday, January 23, 2012 3:42 PM
> To: [email protected]
> Subject: How to add unversioned metadata to a versioned node?
>
> Hello,
> I have an app that allows users to upload binary files to a Jackrabbit
> repository. There are also a few pieces of metadata that I am
> tracking withing each file. For example, description, document id,
> user, etc. The other requirement is that the files be versioned and
> have access to historical versions. This is all working beautifully
> except for one small problem...
>
> Whenever the user changes something like the description, the version
> number for the file goes up one. I really only want a new version to
> occur when the actual file is replaced with a new one.
> Can someone tell me how I can store unversioned metadata with my
> versioned node? Would be much appreciated. Thanks!
>
>
> Here is some sample code to illustrate how I am doing it now....
>
>
> When the session starts, I register a custom property type....
>
> ----------------------------------------------------------------------
> -------
> --------------------
> NodeTypeManager nodeTypeManager = (NodeTypeManager)
> session.getWorkspace().getNodeTypeManager();
> NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
> nodeType.setDeclaredSuperTypeNames(new String[] {"nt:resource"});
> nodeType.setName("fileContent");
>
> PropertyDefinitionTemplate property =
> nodeTypeManager.createPropertyDefinitionTemplate();
> property.setName("description");
> property.setMultiple(false);
> property.setRequiredType(type);
> template.getPropertyDefinitionTemplates().add(property);
> nodeTypeManager.registerNodeType(nodeType, true);
>
> ----------------------------------------------------------------------
> -------
> --------------------
>
>
>
> To change property value....
>
> ----------------------------------------------------------------------
> -------
> --------------------
>
> VersionManager vman = session.getWorkspace().getVersionManager();
> Node fileNode = session.getNodeByIdentifier(id); Node resNode =
> fileNode.getNode("jcr:content"); vman.checkout(fileNode.getPath());
> resNode.setProperty("description", description); session.save();
> vman.checkin(fileNode.getPath());
>
> ----------------------------------------------------------------------
> -------
> --------------------
>