How to use ModeShape 2.2.0 to store custom filesystem metadatas like JackRabbit
penkween Aug 26, 2010 11:38 PMMy company project require a Hierachical Datastore. Basically, the project is a Photo Album like Flickr which allow user to upload photo and store it using conventional file system in Windows (or Linux) and later will be used by the web presentation layer. The reason we choose to try Modeshape (JCR) is for storing the metadata of the photo stored inside the filesystem which allow for easy indexing/searching later. I have tried the Modeshape 2.2.0 using the following configuration following the Modeshape's UFOs example:
<mode:source jcr:name="UFOs" mode:classname="org.modeshape.connector.filesystem.FileSystemSource"
mode:workspaceRootPath="C:/test/modeshape/ufoSource"
mode:defaultWorkspaceName="workspace1"
mode:creatingWorkspacesAllowed="false"
mode:updatesAllowed="true"/>
1. Reading Repository -> is OK
#########################################################################################################
SecurityContextCredentials credential = new SecurityContextCredentials(securityContext);
session = repository.login(credential);
nodeRoot = session.getRootNode();
dump(nodeRoot); // From here, i can see my filesystem contents. dump is a function written to dump the Nodes
session.save();
session.logout();
2. Creating Nodes-> OK (using graph api) but Fail (using Node.addNode)
#########################################################################################################
Fail by using Node.addNode() ** during session.save()
==============================================
*** if I try to add it at root "/" ***
javax.jcr.RepositoryException: org.modeshape.graph.connector.RepositorySourceException: Primary type "UFOs" for path "nt:unstructured" in workspace "/" in workspace1 is not valid for the file system connector. Valid primary types are nt:file, nt:folder, nt:resource, and dna:resouce.
at org.modeshape.jcr.SessionCache.save(SessionCache.java:412)
at org.modeshape.jcr.JcrSession.save(JcrSession.java:1346)
*** if I try to add it at a folder named "FolderA" under root "/" ***
javax.jcr.nodetype.ConstraintViolationException: Unable to determine a valid node definition for the node "/{}FolderA/{}newfile.txt" in workspace "workspace1" of "JCR UFOs"
at org.modeshape.jcr.SessionCache$NodeEditor.createChild(SessionCache.java:1565)
at org.modeshape.jcr.AbstractJcrNode.addNode(AbstractJcrNode.java:1468)
at org.modeshape.jcr.AbstractJcrNode.addNode(AbstractJcrNode.java:1344)
OK by using Graph api
==============================================
By using the Modeshape graph API (learned from : http://community.jboss.org/message/528581). I manage to create and remove File and Folder using following codes:
[Create File]
String file = "/newfile.txt";
Graph graph = jcrEngine.getGraph("UFOs);
graph.create(file).with("jcr:primaryType","nt:file").and();
graph.create(file+"/jcr:content").with("jcr:primaryType","nt:resource").and("jcr:data",data).orReplace().and();
[Create Folder]
String folder = "/newfolder"
Graph graph = jcrEngine.getGraph("UFOs);
graph.create(folder).with("jcr:primaryType","nt:folder").and();
3. Creating Properties -> Fail
#########################################################################################################
My problem is how do I add properties like "Author" to newfile.txt using Node.setProperty("Author","David") and later fetch it using Node.getProperty() ? I have tried the above setProperty and getProperty and all failed with the following exceptions:
======================================================================
javax.jcr.nodetype.ConstraintViolationException: Cannot find a definition for the property named 'author' on the node at '/newfile.txt' with primary type 'nt:file' and mixin types: []
at org.modeshape.jcr.SessionCache$NodeEditor.setProperty(SessionCache.java:1046)
at org.modeshape.jcr.SessionCache$NodeEditor.setProperty(SessionCache.java:971)
at org.modeshape.jcr.AbstractJcrNode.setProperty(AbstractJcrNode.java:1667)
=======================================================================
Anybody can help out how do I create new properties to attach (as metadata) to the file (nt:file) ? I strongly believe that modeshape is a fantastic technology but only if we know how to use it properly . Thanks .
Rgds,
Danny