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);
        }
      }
    }
  }
  @Override
  public void deleteFile(long companyId, long repositoryId, String fileName, String versionLabel)
      throws PortalException, SystemException {

    Session session = null;

    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());

      if (!versionHistory.hasVersionLabel(versionLabel)) {
        throw new NoSuchFileException(
            "{fileName=" + fileName + ", versionLabel=" + versionLabel + "}");
      }

      Version version = versionHistory.getVersionByLabel(versionLabel);

      versionManager.restore(version.getPredecessors()[0], true);

      versionHistory.removeVersion(version.getName());

      session.save();
    } catch (PathNotFoundException pnfe) {
      throw new NoSuchFileException(
          "{fileName=" + fileName + ", versionLabel=" + versionLabel + "}");
    } catch (RepositoryException re) {
      throw new SystemException(re);
    } finally {
      JCRFactoryUtil.closeSession(session);
    }
  }
  @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()));
  }