3 Replies Latest reply on Feb 15, 2017 3:05 AM by hchiorean

    Unable to retrieve versioned data of a node

    rohithreddy41

      I have created a node that holds MS office word document.( i have posted the exact question on stack overflow: jcr - Unable to retrieve versioned data of a node - Stack Overflow

       

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

                              File wordFile = new File("DMS.docx");

                               Node documentNode = rootNode.addNode("Documents");

                               Node fileNode = documentNode.addNode(wordFile.getName(),"nt:file");

                               fileNode.addMixin("mix:versionable");

             

                               // Upload the file to that node ...

                               Node contentNode = fileNode.addNode("jcr:content", "nt:resource");

                               Binary binary = getFileBinary(wordFile);

                               contentNode.setProperty("jcr:data", binary);

                               session.save();

                               Version v1 =   versionManager.checkin(fileNode.getPath());

       

      now i checkout the file through versionManager and update contents and  then check back in like below.

       

       

            fileNode = session.getRootNode().getNode("Documents").getNode(wordFile.getName());

             versionManager.checkout(fileNode.getPath());

              java.nio.file.Path path = Paths.get("DMSUpdated.docx");

                                   if (!Files.exists(path)) {

                                       Files.createFile(path);

                                   }

                                   Files.write(path, "Text to be added".getBytes(), StandardOpenOption.APPEND);

                                   Binary updatedBinary = getFileBinary(new File(filePath));

                                   contentNode.setProperty("jcr:data", updatedBinary);

             

                                   session.save();

          //check - in

           Version updatedVersion =   versionManager.checkin(fileNode.getPath());

       

      Now i am trying to restore the node to updated version like below:

       

          Node finalFileNode =   session.getNode("/Documents").getNode(wordFile.getName());

                    versionManager.restore(finalFileNode.getPath(),updatedVersion,false);

              Node finalContentNode = finalFileNode.getNode("jcr:content");

                           InputStream initialStream = finalContentNode.getProperty("jcr:data").getBinary().getStream();

                              byte[] buffer = new byte[initialStream.available()];

                              initialStream.read(buffer);

                                File updatedFile = new File("//TestFiles//FinalDMS.docx");

                              OutputStream outStream = new FileOutputStream(updatedFile);

                              outStream.write(buffer);

       

       

      This gives me contents of version v1 and not updated version, not sure if i am doing something wrong?