0
votes

I am very new in Jack Rabbit repository.! So please guide me for fetching recent version of a file from Jackrabbit repository. My code is : try{

            session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
            Node root = session.getRootNode();
            if (!root.hasNode(nodeName)) {
                root.addNode(nodeName, "nt:unstructured");
                session.save();
            }

            String extention = FilenameUtils.getExtension(originalFileName);

            InputStream stream = new BufferedInputStream(new FileInputStream(file));                
            Node roseNode = session.getRootNode().getNode(nodeName);                

            VersionManager vm = session.getWorkspace().getVersionManager();

            Node fileNode = null;
            if(roseNode.hasNode(fileName.toString()+"."+extention)){
                fileNode = roseNode.getNode(fileName.toString()+"."+extention);                 
                vm.checkout(fileNode.getPath());                

            } else {
                fileNode = roseNode.addNode(fileName.toString()+"."+extention, "nt:unstructured");
                fileNode.addMixin("mix:versionable");
                fileNode.setProperty(extention, fileName.toString());                   
                session.save();
            }


            Node content = fileNode.addNode("jcr:content", "nt:resource");
            Binary binary = session.getValueFactory().createBinary(stream);
            content.setProperty("jcr:data", binary);
            stream.close();             
            session.save();
            vm.checkin(fileNode.getPath());

I tried this.. But I am getting the older file while fetching.. Please help me on this... Thanks in advance..

1
Could you pleas explain your problem in more detail?!Cleb
the requirement is, when a document with some content is uploaded in to the jackrabbit repository, another user may download it and modifies it and he uploads the document again into repository with same name of the document, I need to maintain these two versions with only one name of the document, and when fetching is required by default it has to give recent version file and when required the old version it has to give the old version of the file. @Cleb thank you for your responseAhmed Shareef

1 Answers

0
votes

Finally I got it in this way, I tried following query as:

String expression = "select * from [nt:base] as b where name(b)= '"+fileName+"' ";
Query query = queryManager.createQuery(expression, Query.JCR_SQL2);
QueryResult result = query.execute();
NodeIterator iterator = result.getNodes();
VersionManager vm = session.getWorkspace().getVersionManager();
if(iterator.getSize() > 0){
    Node node = iterator.nextNode();
    Node recentVersionNode = null;

    NodeIterator versionIterator = node.getNodes();
 /*
 A node has multiple content nodes with ("jcr:content", "nt:resource")
 these are of different versions, so the file with name abc.pdf 
 will have multiple versions/content so that we can get 
 the recent version of content node of the same file.
 */
    while(versionIterator.hasNext()){
         recentVersionNode = versionIterator.nextNode();             
    }

finally I got the file node that is uploaded recently in to the Jackrabbit repository. And we can fetch any version of the file by specifying the index of "jcr:content"