/**
   * 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;
  }
 public SimpleDocument convertNode(Node node, String lang) throws RepositoryException {
   if (isVersionedMaster(node)) {
     return buildHistorisedDocument(node, lang);
   }
   Node parentNode = node.getParent();
   if (parentNode instanceof Version) {
     // Getting the parent node, the versionned one
     Node masterNode = getMasterNodeForVersion((Version) parentNode);
     // The historised document is built from the parent node
     HistorisedDocument document = buildHistorisedDocument(masterNode, lang);
     // Returning the version
     SimpleDocumentVersion version = document.getVersionIdentifiedBy(node.getIdentifier());
     if (version != null) {
       return new HistorisedDocumentVersion(version);
     }
     throw new PathNotFoundException(
         "Version identified by " + node.getIdentifier() + " has not been found.");
   }
   return fillDocument(node, lang);
 }