private void purgeHistory(Node fileNode, Session session, PentahoJcrConstants pentahoJcrConstants) throws RepositoryException { // Delete all previous versions of this node VersionManager versionManager = session.getWorkspace().getVersionManager(); if (JcrRepositoryFileUtils.isPentahoFolder(pentahoJcrConstants, fileNode)) { // go down to children NodeIterator nodes = fileNode.getNodes(); while (nodes.hasNext()) { Node next = (Node) nodes.next(); purgeHistory(next, session, pentahoJcrConstants); } } else if (JcrRepositoryFileUtils.isPentahoFile(pentahoJcrConstants, fileNode) && fileNode.isNodeType(pentahoJcrConstants.getPHO_MIX_VERSIONABLE())) { VersionHistory versionHistory = versionManager.getVersionHistory(fileNode.getPath()); VersionIterator allVersions = versionHistory.getAllVersions(); while (allVersions.hasNext()) { Version next = (Version) allVersions.next(); String name = next.getName(); // Root version cannot be deleted, the remove below will take care of that. if (!JCR_ROOT_VERSION.equals(name)) { versionHistory.removeVersion(name); } } } }
@Test /** * Name of a versioned node is available in the parent history. Translator takes care of this when * hippo:paths is present on the node, with the parent as one of its values. */ public void testVersionedNode() throws Exception { Node parent = session.getRootNode().addNode("test", "nt:unstructured"); parent.addMixin("mix:versionable"); Node child = parent.addNode("child", "nt:unstructured"); child.addMixin("mix:versionable"); child.setProperty("hippo:paths", new String[] {child.getUUID(), parent.getUUID()}); session.save(); child.checkin(); parent.checkin(); VersionHistory vh = child.getVersionHistory(); parent.checkout(); child.remove(); session.save(); parent.checkin(); VersionIterator versionIter = vh.getAllVersions(); versionIter.nextVersion(); // ignore root Version version = versionIter.nextVersion(); NodeTranslator nt = new NodeTranslator(new JcrNodeModel(version.getNode("jcr:frozenNode"))); assertEquals("child", nt.getNodeName().getObject()); assertTrue(version.getNode("jcr:frozenNode").isNodeType("mix:referenceable")); }
/** * Return the <code>List</code> that contains this <code>DocumentMetadata</code>. * * @return the <code>List</code> that contains this <code>DocumentMetadata</code>. * @throws EcmException if an error occurs. */ public List<DocumentMetadata> getVersionHistory(String filePath) throws EcmException { logger.debug("Loading document version history using path : " + filePath); List<DocumentMetadata> list = new ArrayList<DocumentMetadata>(); try { /** Validate the node path */ this.validateNodePath(filePath); /** Get the version manager */ VersionManager vm = this.session.getWorkspace().getVersionManager(); VersionHistory history = vm.getVersionHistory(filePath); VersionIterator iterator = history.getAllVersions(); iterator.skip(1); while (iterator.hasNext()) { Version version = iterator.nextVersion(); DocumentMetadata meta = this.loadFileVersionMetadata(version); list.add(meta); } } catch (UnsupportedRepositoryOperationException e) { throw new EcmException( "Fail to retrieve version history, check is not supported", e, ErrorCodes.REPOSITROY_ERR_OPERATION_NOT_SUPPORTED); } catch (RepositoryException e) { throw new EcmException( "Fail to retrieve version history", e, ErrorCodes.REPOSITROY_ERR_GENERIC); } logger.debug("Version history loaded for file :" + filePath); return list; }
@FixFor("MODE-1624") @Test public void shouldAllowRemovingVersionFromVersionHistoryByRemovingVersionNode() throws Exception { print = false; Node outerNode = session.getRootNode().addNode("outerFolder"); Node innerNode = outerNode.addNode("innerFolder"); Node fileNode = innerNode.addNode("testFile.dat"); fileNode.setProperty("jcr:mimeType", "text/plain"); fileNode.setProperty("jcr:data", "Original content"); session.save(); fileNode.addMixin("mix:versionable"); session.save(); // Make several changes ... String path = fileNode.getPath(); for (int i = 2; i != 7; ++i) { versionManager.checkout(path); fileNode.setProperty("jcr:data", "Original content " + i); session.save(); versionManager.checkin(path); } // Get the version history ... VersionHistory history = versionManager.getVersionHistory(path); if (print) System.out.println("Before: \n" + history); assertThat(history, is(notNullValue())); assertThat(history.getAllLinearVersions().getSize(), is(6L)); // Get the versions ... VersionIterator iter = history.getAllLinearVersions(); Version v1 = iter.nextVersion(); Version v2 = iter.nextVersion(); Version v3 = iter.nextVersion(); Version v4 = iter.nextVersion(); Version v5 = iter.nextVersion(); Version v6 = iter.nextVersion(); assertThat(iter.hasNext(), is(false)); assertThat(v1, is(notNullValue())); assertThat(v2, is(notNullValue())); assertThat(v3, is(notNullValue())); assertThat(v4, is(notNullValue())); assertThat(v5, is(notNullValue())); assertThat(v6, is(notNullValue())); // Remove the 3rd version (that is, i=3) ... // history.removeVersion(versionName); try { v3.remove(); fail("Should not allow removing a protected node"); } catch (ConstraintViolationException e) { // expected } }
/** * Builds from the root version node and from a language the object representation of a versioned * document and its history. * * @param rootVersionNode the root version node (master). * @param lang the aimed content language. * @return the instance of a versioned document. * @throws RepositoryException */ HistorisedDocument buildHistorisedDocument(Node rootVersionNode, String lang) throws RepositoryException { VersionManager versionManager = rootVersionNode.getSession().getWorkspace().getVersionManager(); HistorisedDocument historisedDocument = new HistorisedDocument(fillDocument(rootVersionNode, lang)); try { String path = rootVersionNode.getPath(); VersionHistory history = versionManager.getVersionHistory(path); Version root = history.getRootVersion(); String rootId = ""; if (root != null) { rootId = root.getIdentifier(); } Version base = versionManager.getBaseVersion(path); String baseId = ""; if (base != null) { baseId = base.getIdentifier(); } VersionIterator versionsIterator = history.getAllVersions(); List<SimpleDocumentVersion> documentHistory = new ArrayList<SimpleDocumentVersion>((int) versionsIterator.getSize()); int versionIndex = 0; SimpleDocumentVersion previousVersion = null; while (versionsIterator.hasNext()) { Version version = versionsIterator.nextVersion(); if (!version.getIdentifier().equals(rootId) && !version.getIdentifier().equals(baseId)) { SimpleDocumentVersion versionDocument = new SimpleDocumentVersion( fillDocument(version.getFrozenNode(), lang), historisedDocument); versionDocument.setNodeName(rootVersionNode.getName()); versionDocument.setVersionIndex(versionIndex++); versionDocument.setPreviousVersion(previousVersion); documentHistory.add(versionDocument); previousVersion = versionDocument; } } HistoryDocumentSorter.sortHistory((List) documentHistory); historisedDocument.setHistory(documentHistory); historisedDocument.setVersionIndex(versionIndex); } catch (RepositoryException ex) { if (ex.getCause() instanceof NoSuchItemStateException) { historisedDocument.setHistory(new ArrayList<SimpleDocumentVersion>(0)); } else { throw ex; } } return historisedDocument; }
/** * Load the version of file name and version no specified * * @param filePath the full file path * @param versionNo version no of the document * @return the generated Version instance * @throws RepositoryException if there is any problem during loading Version */ private Version getDocumentVersion(String filePath, String versionNo) throws RepositoryException { Version version = null; /** Get the version manager */ VersionManager vm = this.session.getWorkspace().getVersionManager(); VersionHistory history = vm.getVersionHistory(filePath); /** * Get the version iterator and find the exact version, we always need to skip the first node * because this not a real version , just like a head node. */ VersionIterator iterator = history.getAllVersions(); iterator.skip(1); while (iterator.hasNext()) { Version v = iterator.nextVersion(); if (v.getName().equals(versionNo)) { version = v; break; } } return version; }
@Test public void testGet() throws VersionException, UnsupportedRepositoryOperationException, InvalidItemStateException, LockException, RepositoryException, IOException, ServletException { SlingHttpServletRequest request = createNiceMock(SlingHttpServletRequest.class); SlingHttpServletResponse response = createNiceMock(SlingHttpServletResponse.class); Resource resource = createNiceMock(Resource.class); Node node = createNiceMock(Node.class); Version version = createNiceMock(Version.class); PropertyIterator propertyIterator = createNiceMock(PropertyIterator.class); VersionHistory versionHistory = createNiceMock(VersionHistory.class); VersionIterator versionIterator = createNiceMock(VersionIterator.class); Node versionNode = createNiceMock(Node.class); Session session = createNiceMock(Session.class); Workspace workspace = createNiceMock(Workspace.class); VersionManager versionManager = createNiceMock(VersionManager.class); EasyMock.expect(request.getResource()).andReturn(resource).anyTimes(); EasyMock.expect(resource.adaptTo(Node.class)).andReturn(node).anyTimes(); PrintWriter pw = new PrintWriter(new ByteArrayOutputStream()); EasyMock.expect(response.getWriter()).andReturn(pw).anyTimes(); EasyMock.expect(node.getSession()).andReturn(session); EasyMock.expect(session.getWorkspace()).andReturn(workspace); EasyMock.expect(workspace.getVersionManager()).andReturn(versionManager); EasyMock.expect(versionManager.getVersionHistory("/foo")).andReturn(versionHistory); EasyMock.expect(node.getPath()).andReturn("/foo").anyTimes(); EasyMock.expect(versionHistory.getAllVersions()).andReturn(versionIterator); EasyMock.expect(versionIterator.getSize()).andReturn(2L); EasyMock.expect(versionIterator.hasNext()).andReturn(true); EasyMock.expect(versionIterator.nextVersion()).andReturn(version); EasyMock.expect(versionIterator.hasNext()).andReturn(true); EasyMock.expect(versionIterator.nextVersion()).andReturn(version); EasyMock.expect(version.getName()).andReturn("NameVersioNode").anyTimes(); EasyMock.expect(version.getNode(JcrConstants.JCR_FROZENNODE)).andReturn(versionNode).anyTimes(); EasyMock.expect(version.getProperties()).andReturn(propertyIterator).anyTimes(); EasyMock.expect(propertyIterator.hasNext()).andReturn(false).anyTimes(); replay(); listVersionsServlet.doGet(request, response); verify(); }
@Override public void deleteFile(long companyId, long repositoryId, String fileName) throws PortalException, SystemException { Session session = null; // A bug in Jackrabbit requires us to create a dummy node and delete the // version tree manually to successfully delete a file // Create a dummy node try { session = JCRFactoryUtil.createSession(); Workspace workspace = session.getWorkspace(); VersionManager versionManager = workspace.getVersionManager(); Node rootNode = getRootNode(session, companyId); Node repositoryNode = getFolderNode(rootNode, repositoryId); Node fileNode = repositoryNode.getNode(fileName); Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT); versionManager.checkout(contentNode.getPath()); contentNode.setProperty(JCRConstants.JCR_MIME_TYPE, "text/plain"); contentNode.setProperty(JCRConstants.JCR_DATA, ""); contentNode.setProperty(JCRConstants.JCR_LAST_MODIFIED, Calendar.getInstance()); session.save(); Version version = versionManager.checkin(contentNode.getPath()); VersionHistory versionHistory = versionManager.getVersionHistory(contentNode.getPath()); versionHistory.addVersionLabel(version.getName(), "0.0", false); } catch (PathNotFoundException pnfe) { throw new NoSuchFileException(fileName); } catch (RepositoryException re) { throw new SystemException(re); } finally { JCRFactoryUtil.closeSession(session); } // Delete version tree try { session = JCRFactoryUtil.createSession(); Workspace workspace = session.getWorkspace(); VersionManager versionManager = workspace.getVersionManager(); Node rootNode = getRootNode(session, companyId); Node repositoryNode = getFolderNode(rootNode, repositoryId); Node fileNode = repositoryNode.getNode(fileName); Node contentNode = fileNode.getNode(JCRConstants.JCR_CONTENT); VersionHistory versionHistory = versionManager.getVersionHistory(contentNode.getPath()); VersionIterator itr = versionHistory.getAllVersions(); while (itr.hasNext()) { Version version = itr.nextVersion(); if (itr.getPosition() == itr.getSize()) { break; } else { if (!StringUtils.equals(JCRConstants.JCR_ROOT_VERSION, version.getName())) { versionHistory.removeVersion(version.getName()); } } } session.save(); } catch (PathNotFoundException pnfe) { throw new NoSuchFileException(fileName); } catch (RepositoryException re) { throw new SystemException(re); } finally { JCRFactoryUtil.closeSession(session); } // Delete file try { session = JCRFactoryUtil.createSession(); Node rootNode = getRootNode(session, companyId); Node repositoryNode = getFolderNode(rootNode, repositoryId); Node fileNode = repositoryNode.getNode(fileName); fileNode.remove(); session.save(); } catch (PathNotFoundException pnfe) { throw new NoSuchFileException(fileName); } catch (RepositoryException re) { throw new SystemException(re); } finally { JCRFactoryUtil.closeSession(session); } }
@FixFor("MODE-1624") @Test public void shouldAllowRemovingVersionFromVersionHistory() throws Exception { print = false; Node outerNode = session.getRootNode().addNode("outerFolder"); Node innerNode = outerNode.addNode("innerFolder"); Node fileNode = innerNode.addNode("testFile.dat"); fileNode.setProperty("jcr:mimeType", "text/plain"); fileNode.setProperty("jcr:data", "Original content"); session.save(); fileNode.addMixin("mix:versionable"); session.save(); // Make several changes ... String path = fileNode.getPath(); for (int i = 2; i != 7; ++i) { versionManager.checkout(path); fileNode.setProperty("jcr:data", "Original content " + i); session.save(); versionManager.checkin(path); } // Get the version history ... VersionHistory history = versionManager.getVersionHistory(path); if (print) System.out.println("Before: \n" + history); assertThat(history, is(notNullValue())); assertThat(history.getAllLinearVersions().getSize(), is(6L)); // Get the versions ... VersionIterator iter = history.getAllLinearVersions(); Version v1 = iter.nextVersion(); Version v2 = iter.nextVersion(); Version v3 = iter.nextVersion(); Version v4 = iter.nextVersion(); Version v5 = iter.nextVersion(); Version v6 = iter.nextVersion(); assertThat(iter.hasNext(), is(false)); String versionName = v3.getName(); assertThat(v1, is(notNullValue())); assertThat(v2, is(notNullValue())); assertThat(v3, is(notNullValue())); assertThat(v4, is(notNullValue())); assertThat(v5, is(notNullValue())); assertThat(v6, is(notNullValue())); // Remove the 3rd version (that is, i=3) ... history.removeVersion(versionName); if (print) System.out.println("After (same history used to remove): \n" + history); assertThat(history.getAllLinearVersions().getSize(), is(5L)); // Get the versions using the history node we already have ... iter = history.getAllLinearVersions(); Version v1a = iter.nextVersion(); Version v2a = iter.nextVersion(); Version v4a = iter.nextVersion(); Version v5a = iter.nextVersion(); Version v6a = iter.nextVersion(); assertThat(iter.hasNext(), is(false)); assertThat(v1a.getName(), is(v1.getName())); assertThat(v2a.getName(), is(v2.getName())); assertThat(v4a.getName(), is(v4.getName())); assertThat(v5a.getName(), is(v5.getName())); assertThat(v6a.getName(), is(v6.getName())); // Get the versions using a fresh history node ... VersionHistory history2 = versionManager.getVersionHistory(path); if (print) System.out.println("After (fresh history): \n" + history2); assertThat(history.getAllLinearVersions().getSize(), is(5L)); iter = history2.getAllLinearVersions(); Version v1b = iter.nextVersion(); Version v2b = iter.nextVersion(); Version v4b = iter.nextVersion(); Version v5b = iter.nextVersion(); Version v6b = iter.nextVersion(); assertThat(iter.hasNext(), is(false)); assertThat(v1b.getName(), is(v1.getName())); assertThat(v2b.getName(), is(v2.getName())); assertThat(v4b.getName(), is(v4.getName())); assertThat(v5b.getName(), is(v5.getName())); assertThat(v6b.getName(), is(v6.getName())); }