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; }
/** * 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; }
@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(); }
/** * 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; }
@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); } }