Java Code Examples for javax.jcr.Item#isNode()

The following examples show how to use javax.jcr.Item#isNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkOperation.java    From sling-samples with Apache License 2.0 6 votes vote down vote up
@Override
protected void doRun(SlingHttpServletRequest request,
        HtmlResponse response, List<Modification> changes)
        throws RepositoryException {

    Session session = request.getResourceResolver().adaptTo(Session.class);
    String resourcePath = request.getResource().getPath();
    if (session.itemExists(resourcePath)) {
        Node source = (Node) session.getItem(resourcePath);

        // create a symetric link
        RequestParameter linkParam = request.getRequestParameter("target");
        if (linkParam != null) {
            String linkPath = linkParam.getString();
            if (session.itemExists(linkPath)) {
                Item targetItem = session.getItem(linkPath);
                if (targetItem.isNode()) {
                    linkHelper.createSymetricLink(source,
                        (Node) targetItem, "link");
                }
            }
        }
    }

}
 
Example 2
Source File: FileFolderNodeFilter.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * Returns {@code true} if the item is a node of type nt:hierarchyNode
 * that has or defines a 'jcr:content' child node.
 */
public boolean matches(Item item) throws RepositoryException {
    if (item.isNode()) {
        Node node = (Node) item;
        if (node.isNodeType(JcrConstants.NT_HIERARCHYNODE)) {
            if (node.hasNode(JcrConstants.JCR_CONTENT)) {
                return true;
            } else {
                for (NodeDefinition pd: node.getPrimaryNodeType().getChildNodeDefinitions()) {
                    if (pd.getName().equals(JcrConstants.JCR_CONTENT)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
 
Example 3
Source File: NtFileItemFilter.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @return {@code true} if the item is a nt:file or nt:resource property
 */
public boolean matches(Item item, int depth) throws RepositoryException {
    if (item.isNode()) {
        // include nt:file node
        Node node = (Node) item;
        if (depth == 0) {
            return node.isNodeType(JcrConstants.NT_FILE);
        } else if (depth == 1) {
            // include jcr:content
            return item.getName().equals(JcrConstants.JCR_CONTENT);
        } else {
            return false;
        }
    } else {
        if (depth == 1) {
            return fileNames.contains(item.getName());
        } else if (depth == 2 && item.getParent().getName().equals(JcrConstants.JCR_CONTENT)) {
            return resNames.contains(item.getName());
        } else {
            return false;
        }
    }
}
 
Example 4
Source File: NodeTypeItemFilter.java    From jackrabbit-filevault with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * Returns {@code true} if the item is a node and if the configured
 * node type is equal to the primary type of the node. if super types are
 * respected it also returns {@code true} if the items node type
 * extends from the configured node type (Node.isNodeType() check).
 */
public boolean matches(Item item) throws RepositoryException {
    if (item.isNode()) {
        if (respectSupertype) {
            try {
                return ((Node) item).isNodeType(nodeType);
            } catch (RepositoryException e) {
                // ignore
                return false;
            }
        } else {
            return ((Node) item).getPrimaryNodeType().getName().equals(nodeType);
        }
    }
    return false;
    
}
 
Example 5
Source File: LinkProcessor.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
public void process(SlingHttpServletRequest request,
		List<Modification> changes) throws Exception {

	Session session = request.getResourceResolver().adaptTo(Session.class);

	RequestParameter linkParam = request.getRequestParameter(":link");
	if (linkParam != null){
		String linkPath = linkParam.getString();
		// check if a new node have been created
		if (changes.size() > 0 && changes.get(0).getType() == ModificationType.CREATE) {
			// hack to get the resource path
			// is it possible to add the response to the method header ?
			String resourcePath = changes.get(0).getSource();
			Node source = (Node) session.getItem(resourcePath);

			// create a symetric link
			if (session.itemExists(linkPath)) {
				Item targetItem = session.getItem(linkPath);
				if (targetItem.isNode()) {
					linkHelper.createSymetricLink(source, (Node) targetItem, "link");
				}
			}
		}
	}


}
 
Example 6
Source File: NodeImpl.java    From jackalope with Apache License 2.0 5 votes vote down vote up
@Override
public NodeIterator getNodes() throws RepositoryException {
    List<Node> children = new ArrayList<>();
    for (Item item : session.getChildren(this))
        if (item.isNode())
            children.add((Node)item);
    return new NodeIteratorImpl(children);
}
 
Example 7
Source File: NodeImpl.java    From jackalope with Apache License 2.0 5 votes vote down vote up
@Override
public PropertyIterator getProperties() throws RepositoryException {
    List<Property> children = new ArrayList<>();
    for (Item item : session.getChildren(this))
        if (!item.isNode())
            children.add((Property)item);
    return new PropertyIteratorImpl(children);
}
 
Example 8
Source File: IsMandatoryFilter.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
public boolean matches(Item item) throws RepositoryException {
    if (item.isNode()) {
        return ((Node) item).getDefinition().isMandatory() == isMandatory;
    } else {
        return ((Property) item).getDefinition().isMandatory() == isMandatory;
    }
}
 
Example 9
Source File: DeclaringTypeItemFilter.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * Matches if the declaring node type of the item is equal to the one
 * specified in this filter. If the item is a node and {@code propsOnly}
 * flag is {@code true} it returns {@code false}.
 */
public boolean matches(Item item) throws RepositoryException {
    if (item.isNode()) {
        return !propsOnly && ((Node) item).getDefinition().getDeclaringNodeType().getName().equals(nodeType);
    } else {
        return ((Property) item).getDefinition().getDeclaringNodeType().getName().equals(nodeType);
    }
}
 
Example 10
Source File: BaseFilter.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean matches(Item item, int depth) throws RepositoryException {
    if (item.isNode()) {
        PropertyIterator iter = ((Node) item).getProperties();
        while (iter.hasNext()) {
            String name = iter.nextProperty().getName();
            if (!validNames.contains(name)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
 
Example 11
Source File: ConfirmedOrdersObserver.java    From sling-samples with Apache License 2.0 4 votes vote down vote up
/** Meant to be called often (every second maybe) by the Sling scheduler */
public void run() {
    if(changedPropertyPaths.isEmpty()) {
        return;
    }
    
    final List<String> paths = new ArrayList<String>();
    final List<String> toRetry = new ArrayList<String>();
    synchronized (changedPropertyPaths) {
        paths.addAll(changedPropertyPaths);
        changedPropertyPaths.clear();
    }
    
    try {
        while(!paths.isEmpty()) {
            final String path = paths.remove(0);
            if(session.itemExists(path)) {
                final Item it = session.getItem(path);
                if(!it.isNode()) {
                    final Property p = (Property)it;
                    final Node n = p.getParent();
                    if(!n.hasProperty(SlingbucksConstants.LAST_MODIFIED_PROPERTY_NAME)) {
                        log.debug("Node {} doesn't have property {}, ignored", n.getPath(), SlingbucksConstants.LAST_MODIFIED_PROPERTY_NAME);
                    } else {
                        Calendar lastMod = n.getProperty(SlingbucksConstants.LAST_MODIFIED_PROPERTY_NAME).getDate();
                        if(System.currentTimeMillis() - lastMod.getTime().getTime() < WAIT_AFTER_LAST_CHANGE_MSEC) {
                            log.debug("Node {} modified more recently than {} msec, ignored", n.getPath(), WAIT_AFTER_LAST_CHANGE_MSEC);
                            toRetry.add(path);
                        } else {
                            final String targetPath = SlingbucksConstants.CONFIRMED_ORDERS_PATH + "/" + n.getName();
                            session.getWorkspace().move(n.getPath(), targetPath);
                            log.info("Confirmed order node {} moved to {}", n.getPath(), targetPath);
                        }
                    }
                }
            }
        }
    } catch(Exception e){
        log.error("Exception in run()", e);
    } finally {
        // Re-add any paths that we didn't process
        synchronized (changedPropertyPaths) {
            changedPropertyPaths.addAll(paths);
            changedPropertyPaths.addAll(toRetry);
        }
    }
}
 
Example 12
Source File: ResourceResolverImpl.java    From jackalope with Apache License 2.0 4 votes vote down vote up
private Resource constructResource(Item item) {
    return (item.isNode()) ? new NodeResourceImpl(this, (Node)item) : new PropertyResourceImpl(this, (Property)item);
}
 
Example 13
Source File: ItemImpl.java    From jackalope with Apache License 2.0 4 votes vote down vote up
@Override
public boolean isSame(Item otherItem) throws RepositoryException {
    return Objects.equals(path, otherItem.getPath())
        && (isNode() == otherItem.isNode())
        && (isNode() || getParent().isSame(otherItem.getParent()));
}
 
Example 14
Source File: IsNodeFilter.java    From jackrabbit-filevault with Apache License 2.0 2 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * Returns {@code true} if the item is a node and the polarity is
 * positive (true).
 */
public boolean matches(Item item) throws RepositoryException {
    return item.isNode() == isNode;
}