Example #1
0
  public void testIsCheckedOut() throws Exception {
    // create versionable subnode and checkin its versionable parent
    // testRoot - versionable ancestor

    testRoot.checkout();
    Node subNode = testRoot.addNode("node1").addNode("node2").addNode("subNode");
    testRoot.save();

    subNode.addMixin("mix:versionable");
    testRoot.save();

    subNode.checkin();
    subNode.checkout();
    subNode.setProperty("property1", "property1 v1");
    subNode.save();
    subNode.checkin();
    subNode.checkout();

    // test
    testRoot.checkin(); // make subtree checked-in
    try {
      assertTrue("subNode should be checked-out as it's a mix:versionable", subNode.isCheckedOut());
    } catch (RepositoryException e) {

    }
  }
  private EcmDocument readDocumentByNode(Node node, int readType) throws EcmException {
    EcmDocument doc = null;
    try {
      if (node == null) {
        return doc;
      }
      doc = new EcmDocument();

      Node resNode = node.getNode(Property.JCR_CONTENT);
      if (resNode == null) {
        return doc;
      }
      /** Load the document meta data */
      DocumentMetadata meta = this.loadDocumentMetadata(resNode);

      meta.setFileName(node.getName());
      meta.setIdentifier(node.getIdentifier());

      String createdBy = node.getProperty(Property.JCR_CREATED_BY).getString();
      Calendar createdDate = node.getProperty(Property.JCR_CREATED).getDate();

      meta.setCreated(createdDate.getTime());
      meta.setCreatedBy(createdBy);
      meta.setCheckedOut(node.isCheckedOut());
      meta.setFullPath(node.getPath());

      doc.setMetadata(meta);
      doc.setFileName(meta.getFileName());
      doc.setParentFolder(node.getParent().getName());

      /** Load the file content */
      Property dataProperty = resNode.getProperty(Property.JCR_DATA);
      if (dataProperty != null && readType == 0) {
        doc.setInputStream(dataProperty.getBinary().getStream());
        doc.getMetadata().setSize(dataProperty.getBinary().getSize());
      } else if (dataProperty != null && readType == 1) {
        byte[] contents = convertToByteArray(dataProperty.getBinary());
        doc.setContent(contents);
        doc.getMetadata().setSize(dataProperty.getBinary().getSize());
      }

    } catch (PathNotFoundException e) {
      throw new EcmException(
          "Fail to read document from repository.", e, ErrorCodes.REPOSITROY_ERR_INVALID_PATH);

    } catch (RepositoryException e) {
      throw new EcmException(
          "Fail to read document from repository.", e, ErrorCodes.REPOSITROY_ERR_GENERIC);

    } catch (IOException e) {
      throw new EcmException(
          "Fail to read document from repository.",
          e,
          ErrorCodes.REPOSITROY_ERR_FAIL_TO_READ_CONTENT);
    }

    return doc;
  }
  private void doInitialCheckin() throws RepositoryException {

    Node rootNode = session.getRootNode();

    Node frontPage = rootNode.getNode("wiki/frontPage");

    if (frontPage.isCheckedOut()) {
      frontPage.checkin();
    }
  }
 /**
  * Update link status.
  *
  * @param session the session
  * @param queryCommand the query command
  * @throws Exception the exception
  */
 private void updateLinkStatus(Session session, String queryCommand) throws Exception {
   List<String> listBrokenLinks = new ArrayList<String>();
   ValueFactory valueFactory = session.getValueFactory();
   QueryManager queryManager = session.getWorkspace().getQueryManager();
   Query query = queryManager.createQuery(queryCommand, Query.SQL);
   QueryResult results = query.execute();
   NodeIterator iter = results.getNodes();
   for (; iter.hasNext(); ) {
     Node webContent = iter.nextNode();
     if (!webContent.isCheckedOut()
         || webContent.isLocked()
         || (webContent.isCheckedOut() && !webContent.getParent().isCheckedOut())) {
       continue;
     }
     Property links = webContent.getProperty("exo:links");
     Value[] oldValues = links.getValues();
     Value[] newValues = new Value[oldValues.length];
     for (int iValues = 0; iValues < oldValues.length; iValues++) {
       String oldLink = oldValues[iValues].getString();
       if (!oldLink.equals("")) {
         LinkBean linkBean = LinkBean.parse(oldLink);
         String oldUrl = linkBean.getUrl();
         String oldStatus = getLinkStatus(oldUrl);
         String updatedLink = new LinkBean(oldUrl, oldStatus).toString();
         if (LOG.isInfoEnabled()) {
           LOG.info(updatedLink);
         }
         newValues[iValues] = valueFactory.createValue(updatedLink);
         if (oldStatus.equals(LinkBean.STATUS_BROKEN)) {
           listBrokenLinks.add(oldUrl);
         }
       }
     }
     webContent.setProperty("exo:links", newValues);
     brokenLinksCache.put(webContent.getUUID(), listBrokenLinks);
   }
   session.save();
 }
 /**
  * Performs check out of the specified node.
  *
  * @param node the node to perform the check out
  * @see VersionManager#checkout(String) for details
  */
 public void checkout(Node node)
     throws UnsupportedRepositoryOperationException, LockException, RepositoryException {
   while (!node.isCheckedOut()) {
     if (!node.isNodeType("mix:versionable") && !node.isNodeType("mix:simpleVersionable")) {
       node = node.getParent();
     } else {
       String absPath = node.getPath();
       VersionManager versionManager = getWorkspace().getVersionManager();
       if (!versionManager.isCheckedOut(absPath)) {
         versionManager.checkout(absPath);
       }
       return;
     }
   }
 }
 public String updateVersion(Node node, String lang, boolean isPublic) throws RepositoryException {
   int majorVersion = getIntProperty(node, SLV_PROPERTY_MAJOR);
   int minorVersion = getIntProperty(node, SLV_PROPERTY_MINOR);
   if (isVersionedMaster(node) && node.isCheckedOut()) {
     releaseDocumentNode(node, lang);
     if (isPublic) {
       majorVersion = majorVersion + 1;
       minorVersion = 0;
       node.setProperty(SLV_PROPERTY_MAJOR, majorVersion);
       node.setProperty(SLV_PROPERTY_MINOR, 0);
     } else {
       minorVersion = minorVersion + 1;
       node.setProperty(SLV_PROPERTY_MINOR, minorVersion);
       if (!node.hasProperty(SLV_PROPERTY_MAJOR)) {
         node.setProperty(SLV_PROPERTY_MAJOR, 0);
       }
     }
   }
   return "Version " + majorVersion + "." + minorVersion;
 }
  @Override
  public List<DocumentMetadata> listNode(String path) throws EcmException {
    Node node = getNode(path);
    NodeIterator nodes;
    List<DocumentMetadata> childreen = new ArrayList<DocumentMetadata>();
    try {
      nodes = node.getNodes();
      while (nodes.hasNext()) {

        Node child = nodes.nextNode();

        DocumentMetadata meta = new DocumentMetadata();
        if (isFile(child)) {
          Node resNode = child.getNode(Property.JCR_CONTENT);
          DocumentMetadata contentMetaData = null;
          if (resNode != null) {
            contentMetaData = this.loadDocumentMetadata(resNode);
            /** Load the file content */
            Property dataProperty = resNode.getProperty(Property.JCR_DATA);
            if (dataProperty != null) {
              long size = dataProperty.getBinary().getSize();
              meta.setSize(size);
            }
            if (contentMetaData != null) {
              meta.setMimeType(contentMetaData.getMimeType());
              meta.setEncoding(contentMetaData.getEncoding());
              meta.setLastModified(contentMetaData.getLastModified());
              meta.setLastModifiedBy(contentMetaData.getLastModifiedBy());
            }
          }
        }

        /** Load the document meta data */
        meta.setFileName(child.getName());
        meta.setIdentifier(child.getIdentifier());
        meta.setFullPath(child.getPath());

        if (child.getProperty(Property.JCR_CREATED_BY) != null) {
          String createdBy = child.getProperty(Property.JCR_CREATED_BY).getString();
          meta.setCreatedBy(createdBy);
        }
        if (child.getProperty(Property.JCR_CREATED) != null) {
          Calendar createdDate = child.getProperty(Property.JCR_CREATED).getDate();

          meta.setCreated(createdDate.getTime());
        }

        meta.setCheckedOut(child.isCheckedOut());

        meta.setFolder(false);
        if (isFolder(child)) {
          meta.setFolder(true);
        }

        childreen.add(meta);
      }
    } catch (RepositoryException e) {
      throw new EcmException("Fail to list node content ", e, ErrorCodes.REPOSITROY_ERR_GENERIC);
    }

    return childreen;
  }