/**
   * Check if the named node type is in use in any workspace in the repository
   *
   * @param nodeTypeName the name of the node type to check
   * @return true if at least one node is using that type; false otherwise
   * @throws InvalidQueryException if there is an error searching for uses of the named node type
   */
  boolean isNodeTypeInUse(Name nodeTypeName) throws InvalidQueryException {

    String nodeTypeString = nodeTypeName.getString(context.getNamespaceRegistry());
    String expression = "SELECT * from [" + nodeTypeString + "] LIMIT 1";
    TypeSystem typeSystem = context.getValueFactories().getTypeSystem();
    // Parsing must be done now ...
    QueryCommand command = queryParser.parseQuery(expression, typeSystem);
    assert command != null : "Could not parse " + expression;

    Schemata schemata = getRepositorySchemata();

    // Now query the entire repository for any nodes that use this node type ...
    RepositoryCache repoCache = repository.repositoryCache();
    RepositoryQueryManager queryManager = repository.queryManager();
    Set<String> workspaceNames = repoCache.getWorkspaceNames();
    Map<String, NodeCache> overridden = null;
    NodeTypes nodeTypes = repository.nodeTypeManager().getNodeTypes();
    RepositoryIndexes indexDefns = repository.queryManager().getIndexes();
    CancellableQuery query =
        queryManager.query(
            context,
            repoCache,
            workspaceNames,
            overridden,
            command,
            schemata,
            indexDefns,
            nodeTypes,
            null,
            null);
    try {
      QueryResults result = query.execute();
      if (result.isEmpty()) return false;
      if (result.getRowCount() < 0) {
        // Try to get the first row ...
        NodeSequence seq = result.getRows();
        Batch batch = seq.nextBatch();
        while (batch != null) {
          if (batch.hasNext()) return true;
          // It's not common for the first batch may be empty, but it's possible. So try the next
          // batch ...
          batch = seq.nextBatch();
        }
        return false;
      }
      return result.getRowCount() > 0;
    } catch (RepositoryException e) {
      logger.error(e, JcrI18n.errorCheckingNodeTypeUsage, nodeTypeName, e.getLocalizedMessage());
      return true;
    }
  }
Пример #2
0
 /**
  * Resolves a Node given one of three possible passed parameters: 1) A fully qualified path to a
  * Node (e.g. "/foo/bar/baz"), 2) a Node's UUID, or 3) the PoolId from a ContentPool.
  *
  * @param pathOrIdentifier One of three possible parameters: 1) A fully qualified path to a Node
  *     (e.g. "/foo/bar/baz"), 2) a Node's UUID, or 3) the PoolId from a ContentPool.
  * @param resourceResolver
  * @return If the Node cannot be resolved, <code>null</code> will be returned.
  * @throws IllegalArgumentException
  */
 public static Node resolveNode(
     final String pathOrIdentifier, final ResourceResolver resourceResolver) {
   if (pathOrIdentifier == null || "".equals(pathOrIdentifier)) {
     throw new IllegalArgumentException("Passed argument was null or empty");
   }
   if (resourceResolver == null) {
     throw new IllegalArgumentException("Resource resolver cannot be null");
   }
   Node node = null;
   try {
     if (pathOrIdentifier.startsWith("/")) { // it is a path specification
       Resource r = resourceResolver.resolve(pathOrIdentifier);
       if (r != null) {
         node = r.adaptTo(Node.class);
       }
     } else {
       // Not a full resource path, so try the two flavors of ID.
       Session session = resourceResolver.adaptTo(Session.class);
       // First, assume we have a UUID and try to resolve
       try {
         node = session.getNodeByIdentifier(pathOrIdentifier);
       } catch (RepositoryException e) {
         log.debug("Swallowed exception; i.e. normal operation: {}", e.getLocalizedMessage(), e);
       }
       if (node == null) {
         log.warn("Unable to Tag Content Pool at this time, tried {} ", pathOrIdentifier);
         // must not have been a UUID; resolve via poolId
         // final String poolPath = CreateContentPoolServlet.hash(pathOrIdentifier);
         // node = session.getNode(poolPath);
       }
     }
   } catch (Throwable e) {
     log.error(e.getLocalizedMessage(), e);
   }
   return node;
 }