/**
   * 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();
  }
示例#3
0
  @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);
    }
  }