0

I have created a node that holds MS office word document.

  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?

rohith
  • 733
  • 4
  • 10
  • 24
  • I'd recommend to create a simple JUnit test case that demonstrates the issue and report it in the Apache Jackrabbit JIRA. – Julian Reschke Feb 14 '17 at 06:43
  • First of all, your `contentNode` is still assigned to the first node, not the updated one. I would move your code segments into separate methods, with local variables, to avoid confusion. – TedTrippin Mar 16 '17 at 23:56

0 Answers0